Search in sources :

Example 21 with Change

use of com.gitblit.models.TicketModel.Change in project gitblit by gitblit.

the class FileTicketService method commitChangeImpl.

/**
	 * Commit a ticket change to the repository.
	 *
	 * @param repository
	 * @param ticketId
	 * @param change
	 * @return true, if the change was committed
	 */
@Override
protected synchronized boolean commitChangeImpl(RepositoryModel repository, long ticketId, Change change) {
    boolean success = false;
    Repository db = repositoryManager.getRepository(repository.name);
    try {
        List<Change> changes = getJournal(db, ticketId);
        changes.add(change);
        String journal = TicketSerializer.serializeJournal(changes).trim();
        String journalPath = toTicketPath(ticketId) + "/" + JOURNAL;
        File file = new File(db.getDirectory(), journalPath);
        file.getParentFile().mkdirs();
        FileUtils.writeContent(file, journal);
        success = true;
    } catch (Throwable t) {
        log.error(MessageFormat.format("Failed to commit ticket {0,number,0} to {1}", ticketId, db.getDirectory()), t);
    } finally {
        db.close();
    }
    return success;
}
Also used : Repository(org.eclipse.jgit.lib.Repository) Change(com.gitblit.models.TicketModel.Change) File(java.io.File)

Example 22 with Change

use of com.gitblit.models.TicketModel.Change in project gitblit by gitblit.

the class ITicketService method renameMilestone.

/**
	 * Renames a milestone.
	 *
	 * @param repository
	 * @param oldName
	 * @param newName
	 * @param createdBy
	 * @param notifyOpenTickets
	 * @return true if successful
	 * @since 1.6.0
	 */
public synchronized boolean renameMilestone(RepositoryModel repository, String oldName, String newName, String createdBy, boolean notifyOpenTickets) {
    if (StringUtils.isEmpty(newName)) {
        throw new IllegalArgumentException("new milestone can not be empty!");
    }
    Repository db = null;
    try {
        db = repositoryManager.getRepository(repository.name);
        TicketMilestone tm = getMilestone(repository, oldName);
        if (tm == null) {
            return false;
        }
        StoredConfig config = db.getConfig();
        config.unsetSection(MILESTONE, oldName);
        config.setString(MILESTONE, newName, STATUS, tm.status.name());
        config.setString(MILESTONE, newName, COLOR, tm.color);
        if (tm.due != null) {
            config.setString(MILESTONE, newName, DUE, new SimpleDateFormat(DUE_DATE_PATTERN).format(tm.due));
        }
        config.save();
        milestonesCache.remove(repository.name);
        TicketNotifier notifier = createNotifier();
        for (QueryResult qr : tm.tickets) {
            Change change = new Change(createdBy);
            change.setField(Field.milestone, newName);
            TicketModel ticket = updateTicket(repository, qr.number, change);
            if (notifyOpenTickets && ticket.isOpen()) {
                notifier.queueMailing(ticket);
            }
        }
        if (notifyOpenTickets) {
            notifier.sendAll();
        }
        return true;
    } catch (IOException e) {
        log.error("failed to rename milestone " + oldName + " in " + repository, e);
    } finally {
        if (db != null) {
            db.close();
        }
    }
    return false;
}
Also used : StoredConfig(org.eclipse.jgit.lib.StoredConfig) Repository(org.eclipse.jgit.lib.Repository) TicketModel(com.gitblit.models.TicketModel) Change(com.gitblit.models.TicketModel.Change) IOException(java.io.IOException) SimpleDateFormat(java.text.SimpleDateFormat)

Example 23 with Change

use of com.gitblit.models.TicketModel.Change in project gitblit by gitblit.

the class ITicketService method updateTicket.

/**
	 * Updates a ticket and promotes pending links into references.
	 *
	 * @param repository
	 * @param ticketId, or 0 to action pending links in general
	 * @param change
	 * @return the ticket model if successful, null if failure or using 0 ticketId
	 * @since 1.4.0
	 */
public final TicketModel updateTicket(RepositoryModel repository, long ticketId, Change change) {
    if (change == null) {
        throw new RuntimeException("change can not be null!");
    }
    if (StringUtils.isEmpty(change.author)) {
        throw new RuntimeException("must specify a change author!");
    }
    boolean success = true;
    TicketModel ticket = null;
    if (ticketId > 0) {
        TicketKey key = new TicketKey(repository, ticketId);
        ticketsCache.invalidate(key);
        success = commitChangeImpl(repository, ticketId, change);
        if (success) {
            ticket = getTicket(repository, ticketId);
            ticketsCache.put(key, ticket);
            indexer.index(ticket);
            // call the ticket hooks
            if (pluginManager != null) {
                for (TicketHook hook : pluginManager.getExtensions(TicketHook.class)) {
                    try {
                        hook.onUpdateTicket(ticket, change);
                    } catch (Exception e) {
                        log.error("Failed to execute extension", e);
                    }
                }
            }
        }
    }
    if (success) {
        //Now that the ticket has been successfully persisted add references to this ticket from linked tickets
        if (change.hasPendingLinks()) {
            for (TicketLink link : change.pendingLinks) {
                TicketModel linkedTicket = getTicket(repository, link.targetTicketId);
                Change dstChange = null;
                //Ignore if not available or self reference 
                if (linkedTicket != null && link.targetTicketId != ticketId) {
                    dstChange = new Change(change.author, change.date);
                    switch(link.action) {
                        case Comment:
                            {
                                if (ticketId == 0) {
                                    throw new RuntimeException("must specify a ticket when linking a comment!");
                                }
                                dstChange.referenceTicket(ticketId, change.comment.id);
                            }
                            break;
                        case Commit:
                            {
                                dstChange.referenceCommit(link.hash);
                            }
                            break;
                        default:
                            {
                                throw new RuntimeException(String.format("must add persist logic for link of type %s", link.action));
                            }
                    }
                }
                if (dstChange != null) {
                    //If not deleted then remain null in journal
                    if (link.isDelete) {
                        dstChange.reference.deleted = true;
                    }
                    if (updateTicket(repository, link.targetTicketId, dstChange) != null) {
                        link.success = true;
                    }
                }
            }
        }
    }
    return ticket;
}
Also used : TicketHook(com.gitblit.extensions.TicketHook) TicketModel(com.gitblit.models.TicketModel) Change(com.gitblit.models.TicketModel.Change) TicketLink(com.gitblit.models.TicketModel.TicketLink) ParseException(java.text.ParseException) IOException(java.io.IOException)

Example 24 with Change

use of com.gitblit.models.TicketModel.Change in project gitblit by gitblit.

the class ITicketService method renameLabel.

/**
	 * Renames a label.
	 *
	 * @param repository
	 * @param oldName
	 * @param newName
	 * @param createdBy
	 * @return true if the rename was successful
	 * @since 1.4.0
	 */
public synchronized boolean renameLabel(RepositoryModel repository, String oldName, String newName, String createdBy) {
    if (StringUtils.isEmpty(newName)) {
        throw new IllegalArgumentException("new label can not be empty!");
    }
    Repository db = null;
    try {
        db = repositoryManager.getRepository(repository.name);
        TicketLabel label = getLabel(repository, oldName);
        StoredConfig config = db.getConfig();
        config.unsetSection(LABEL, oldName);
        config.setString(LABEL, newName, COLOR, label.color);
        config.save();
        for (QueryResult qr : label.tickets) {
            Change change = new Change(createdBy);
            change.unlabel(oldName);
            change.label(newName);
            updateTicket(repository, qr.number, change);
        }
        return true;
    } catch (IOException e) {
        log.error("failed to rename label " + oldName + " in " + repository, e);
    } finally {
        if (db != null) {
            db.close();
        }
    }
    return false;
}
Also used : StoredConfig(org.eclipse.jgit.lib.StoredConfig) Repository(org.eclipse.jgit.lib.Repository) Change(com.gitblit.models.TicketModel.Change) IOException(java.io.IOException)

Example 25 with Change

use of com.gitblit.models.TicketModel.Change in project gitblit by gitblit.

the class ITicketService method deleteComment.

/**
	 * Deletes a comment from a ticket.
	 *
	 * @param ticket
	 * @param commentId
	 *            the id of the comment to delete
	 * @param deletedBy
	 * 			the user deleting the comment
	 * @return the revised ticket if the deletion was successful
	 * @since 1.4.0
	 */
public final TicketModel deleteComment(TicketModel ticket, String commentId, String deletedBy) {
    Change deletion = new Change(deletedBy);
    deletion.comment("");
    deletion.comment.id = commentId;
    deletion.comment.deleted = true;
    RepositoryModel repository = repositoryManager.getRepositoryModel(ticket.repository);
    TicketModel revisedTicket = updateTicket(repository, ticket.number, deletion);
    return revisedTicket;
}
Also used : TicketModel(com.gitblit.models.TicketModel) Change(com.gitblit.models.TicketModel.Change) RepositoryModel(com.gitblit.models.RepositoryModel)

Aggregations

Change (com.gitblit.models.TicketModel.Change)45 TicketModel (com.gitblit.models.TicketModel)32 IOException (java.io.IOException)15 Repository (org.eclipse.jgit.lib.Repository)9 Test (org.junit.Test)9 Patchset (com.gitblit.models.TicketModel.Patchset)8 TicketLink (com.gitblit.models.TicketModel.TicketLink)7 ArrayList (java.util.ArrayList)7 RepositoryModel (com.gitblit.models.RepositoryModel)6 UserModel (com.gitblit.models.UserModel)5 RevCommit (org.eclipse.jgit.revwalk.RevCommit)4 Reference (com.gitblit.models.TicketModel.Reference)3 Ref (org.eclipse.jgit.lib.Ref)3 RevWalk (org.eclipse.jgit.revwalk.RevWalk)3 ReceiveCommand (org.eclipse.jgit.transport.ReceiveCommand)3 PatchsetHook (com.gitblit.extensions.PatchsetHook)2 PathChangeModel (com.gitblit.models.PathModel.PathChangeModel)2 RefModel (com.gitblit.models.RefModel)2 Attachment (com.gitblit.models.TicketModel.Attachment)2 Review (com.gitblit.models.TicketModel.Review)2