Search in sources :

Example 1 with MergeResult

use of com.gitblit.utils.JGitUtils.MergeResult in project gitblit by gitblit.

the class PatchsetReceivePack method merge.

/**
 * Merge the specified patchset to the integration branch.
 *
 * @param ticket
 * @param patchset
 * @return true, if successful
 */
public MergeStatus merge(TicketModel ticket) {
    PersonIdent committer = new PersonIdent(user.getDisplayName(), StringUtils.isEmpty(user.emailAddress) ? (user.username + "@gitblit") : user.emailAddress);
    Patchset patchset = ticket.getCurrentPatchset();
    String message = MessageFormat.format("Merged #{0,number,0} \"{1}\"", ticket.number, ticket.title);
    Ref oldRef = null;
    try {
        oldRef = getRepository().getRef(ticket.mergeTo);
    } catch (IOException e) {
        LOGGER.error("failed to get ref for " + ticket.mergeTo, e);
    }
    MergeResult mergeResult = JGitUtils.merge(getRepository(), patchset.tip, ticket.mergeTo, getRepositoryModel().mergeType, committer, message);
    if (StringUtils.isEmpty(mergeResult.sha)) {
        LOGGER.error("FAILED to merge {} to {} ({})", new Object[] { patchset, ticket.mergeTo, mergeResult.status.name() });
        return mergeResult.status;
    }
    Change change = new Change(user.username);
    change.setField(Field.status, Status.Merged);
    change.setField(Field.mergeSha, mergeResult.sha);
    change.setField(Field.mergeTo, ticket.mergeTo);
    if (StringUtils.isEmpty(ticket.responsible)) {
        // unassigned tickets are assigned to the closer
        change.setField(Field.responsible, user.username);
    }
    long ticketId = ticket.number;
    ticket = ticketService.updateTicket(repository, ticket.number, change);
    if (ticket != null) {
        ticketNotifier.queueMailing(ticket);
        if (oldRef != null) {
            ReceiveCommand cmd = new ReceiveCommand(oldRef.getObjectId(), ObjectId.fromString(mergeResult.sha), oldRef.getName());
            cmd.setResult(Result.OK);
            List<ReceiveCommand> commands = Arrays.asList(cmd);
            logRefChange(commands);
            updateIncrementalPushTags(commands);
            updateGitblitRefLog(commands);
        }
        // call patchset hooks
        for (PatchsetHook hook : gitblit.getExtensions(PatchsetHook.class)) {
            try {
                hook.onMergePatchset(ticket);
            } catch (Exception e) {
                LOGGER.error("Failed to execute extension", e);
            }
        }
        return mergeResult.status;
    } else {
        LOGGER.error("FAILED to resolve ticket {} by merge from web ui", ticketId);
    }
    return mergeResult.status;
}
Also used : ReceiveCommand(org.eclipse.jgit.transport.ReceiveCommand) Ref(org.eclipse.jgit.lib.Ref) PatchsetHook(com.gitblit.extensions.PatchsetHook) PersonIdent(org.eclipse.jgit.lib.PersonIdent) MergeResult(com.gitblit.utils.JGitUtils.MergeResult) Patchset(com.gitblit.models.TicketModel.Patchset) IOException(java.io.IOException) Change(com.gitblit.models.TicketModel.Change) IOException(java.io.IOException)

Aggregations

PatchsetHook (com.gitblit.extensions.PatchsetHook)1 Change (com.gitblit.models.TicketModel.Change)1 Patchset (com.gitblit.models.TicketModel.Patchset)1 MergeResult (com.gitblit.utils.JGitUtils.MergeResult)1 IOException (java.io.IOException)1 PersonIdent (org.eclipse.jgit.lib.PersonIdent)1 Ref (org.eclipse.jgit.lib.Ref)1 ReceiveCommand (org.eclipse.jgit.transport.ReceiveCommand)1