Search in sources :

Example 1 with PatchsetHook

use of com.gitblit.extensions.PatchsetHook 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)

Example 2 with PatchsetHook

use of com.gitblit.extensions.PatchsetHook in project gitblit by gitblit.

the class PatchsetReceivePack method processPatchset.

/**
	 * Creates or updates an ticket with the specified patchset.
	 *
	 * @param cmd
	 * @return a ticket if the creation or update was successful
	 */
private TicketModel processPatchset(PatchsetCommand cmd) {
    Change change = cmd.getChange();
    if (cmd.isNewTicket()) {
        // create the ticket object
        TicketModel ticket = ticketService.createTicket(repository, cmd.getTicketId(), change);
        if (ticket != null) {
            sendInfo("");
            sendHeader("#{0,number,0}: {1}", ticket.number, StringUtils.trimString(ticket.title, Constants.LEN_SHORTLOG));
            sendInfo("created proposal ticket from patchset");
            sendInfo(ticketService.getTicketUrl(ticket));
            sendInfo("");
            // log the new patch ref
            RefLogUtils.updateRefLog(user, getRepository(), Arrays.asList(new ReceiveCommand(cmd.getOldId(), cmd.getNewId(), cmd.getRefName())));
            // call any patchset hooks
            for (PatchsetHook hook : gitblit.getExtensions(PatchsetHook.class)) {
                try {
                    hook.onNewPatchset(ticket);
                } catch (Exception e) {
                    LOGGER.error("Failed to execute extension", e);
                }
            }
            return ticket;
        } else {
            sendError("FAILED to create ticket");
        }
    } else {
        // update an existing ticket
        TicketModel ticket = ticketService.updateTicket(repository, cmd.getTicketId(), change);
        if (ticket != null) {
            sendInfo("");
            sendHeader("#{0,number,0}: {1}", ticket.number, StringUtils.trimString(ticket.title, Constants.LEN_SHORTLOG));
            if (change.patchset.rev == 1) {
                // new patchset
                sendInfo("uploaded patchset {0} ({1})", change.patchset.number, change.patchset.type.toString());
            } else {
                // updated patchset
                sendInfo("added {0} {1} to patchset {2}", change.patchset.added, change.patchset.added == 1 ? "commit" : "commits", change.patchset.number);
            }
            sendInfo(ticketService.getTicketUrl(ticket));
            sendInfo("");
            // log the new patchset ref
            RefLogUtils.updateRefLog(user, getRepository(), Arrays.asList(new ReceiveCommand(cmd.getOldId(), cmd.getNewId(), cmd.getRefName())));
            // call any patchset hooks
            final boolean isNewPatchset = change.patchset.rev == 1;
            for (PatchsetHook hook : gitblit.getExtensions(PatchsetHook.class)) {
                try {
                    if (isNewPatchset) {
                        hook.onNewPatchset(ticket);
                    } else {
                        hook.onUpdatePatchset(ticket);
                    }
                } catch (Exception e) {
                    LOGGER.error("Failed to execute extension", e);
                }
            }
            // return the updated ticket
            return ticket;
        } else {
            sendError("FAILED to upload {0} for ticket {1,number,0}", change.patchset, cmd.getTicketId());
        }
    }
    return null;
}
Also used : ReceiveCommand(org.eclipse.jgit.transport.ReceiveCommand) PatchsetHook(com.gitblit.extensions.PatchsetHook) TicketModel(com.gitblit.models.TicketModel) Change(com.gitblit.models.TicketModel.Change) IOException(java.io.IOException)

Aggregations

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