Search in sources :

Example 1 with Patchset

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

the class TicketIndexer method ticketToDoc.

/**
 * Creates a Lucene document from a ticket.
 *
 * @param ticket
 * @return a Lucene document
 */
private Document ticketToDoc(TicketModel ticket) {
    Document doc = new Document();
    // repository and document ids for Lucene querying
    toDocField(doc, Lucene.rid, StringUtils.getSHA1(ticket.repository));
    toDocField(doc, Lucene.did, StringUtils.getSHA1(ticket.repository + ticket.number));
    toDocField(doc, Lucene.project, ticket.project);
    toDocField(doc, Lucene.repository, ticket.repository);
    toDocField(doc, Lucene.number, ticket.number);
    toDocField(doc, Lucene.title, ticket.title);
    toDocField(doc, Lucene.body, ticket.body);
    toDocField(doc, Lucene.created, ticket.created);
    toDocField(doc, Lucene.createdby, ticket.createdBy);
    toDocField(doc, Lucene.updated, ticket.updated);
    toDocField(doc, Lucene.updatedby, ticket.updatedBy);
    toDocField(doc, Lucene.responsible, ticket.responsible);
    toDocField(doc, Lucene.milestone, ticket.milestone);
    toDocField(doc, Lucene.topic, ticket.topic);
    toDocField(doc, Lucene.status, ticket.status.name());
    toDocField(doc, Lucene.comments, ticket.getComments().size());
    toDocField(doc, Lucene.type, ticket.type == null ? null : ticket.type.name());
    toDocField(doc, Lucene.mergesha, ticket.mergeSha);
    toDocField(doc, Lucene.mergeto, ticket.mergeTo);
    toDocField(doc, Lucene.labels, StringUtils.flattenStrings(ticket.getLabels(), ";").toLowerCase());
    toDocField(doc, Lucene.participants, StringUtils.flattenStrings(ticket.getParticipants(), ";").toLowerCase());
    toDocField(doc, Lucene.watchedby, StringUtils.flattenStrings(ticket.getWatchers(), ";").toLowerCase());
    toDocField(doc, Lucene.mentions, StringUtils.flattenStrings(ticket.getMentions(), ";").toLowerCase());
    toDocField(doc, Lucene.votes, ticket.getVoters().size());
    toDocField(doc, Lucene.priority, ticket.priority.getValue());
    toDocField(doc, Lucene.severity, ticket.severity.getValue());
    List<String> attachments = new ArrayList<String>();
    for (Attachment attachment : ticket.getAttachments()) {
        attachments.add(attachment.name.toLowerCase());
    }
    toDocField(doc, Lucene.attachments, StringUtils.flattenStrings(attachments, ";"));
    List<Patchset> patches = ticket.getPatchsets();
    if (!patches.isEmpty()) {
        toDocField(doc, Lucene.patchsets, patches.size());
        Patchset patchset = patches.get(patches.size() - 1);
        String flat = patchset.number + ":" + patchset.rev + ":" + patchset.tip + ":" + patchset.base + ":" + patchset.commits;
        doc.add(new org.apache.lucene.document.Field(Lucene.patchset.name(), flat, TextField.TYPE_STORED));
    }
    doc.add(new TextField(Lucene.content.name(), ticket.toIndexableString(), Store.NO));
    return doc;
}
Also used : ArrayList(java.util.ArrayList) TextField(org.apache.lucene.document.TextField) Attachment(com.gitblit.models.TicketModel.Attachment) Patchset(com.gitblit.models.TicketModel.Patchset) Document(org.apache.lucene.document.Document)

Example 2 with Patchset

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

the class TicketNotifier method formatLastChange.

protected String formatLastChange(TicketModel ticket) {
    Change lastChange = ticket.changes.get(ticket.changes.size() - 1);
    UserModel user = getUserModel(lastChange.author);
    // define the fields we do NOT want to see in an email notification
    Set<TicketModel.Field> fieldExclusions = new HashSet<TicketModel.Field>();
    fieldExclusions.addAll(Arrays.asList(Field.watchers, Field.voters));
    StringBuilder sb = new StringBuilder();
    boolean newTicket = lastChange.isStatusChange() && Status.New == lastChange.getStatus();
    boolean isFastForward = true;
    List<RevCommit> commits = null;
    DiffStat diffstat = null;
    String pattern;
    if (lastChange.hasPatchset()) {
        // patchset uploaded
        Patchset patchset = lastChange.patchset;
        String base = "";
        // determine the changed paths
        Repository repo = null;
        try {
            repo = repositoryManager.getRepository(ticket.repository);
            if (patchset.isFF() && (patchset.rev > 1)) {
                // fast-forward update, just show the new data
                isFastForward = true;
                Patchset prev = ticket.getPatchset(patchset.number, patchset.rev - 1);
                base = prev.tip;
            } else {
                // proposal OR non-fast-forward update
                isFastForward = false;
                base = patchset.base;
            }
            diffstat = DiffUtils.getDiffStat(repo, base, patchset.tip);
            commits = JGitUtils.getRevLog(repo, base, patchset.tip);
        } catch (Exception e) {
            Logger.getLogger(getClass()).error("failed to get changed paths", e);
        } finally {
            if (repo != null) {
                repo.close();
            }
        }
        String compareUrl = ticketService.getCompareUrl(ticket, base, patchset.tip);
        if (newTicket) {
            // new proposal
            pattern = "**{0}** is proposing a change.";
            sb.append(MessageFormat.format(pattern, user.getDisplayName()));
            fieldExclusions.add(Field.status);
            fieldExclusions.add(Field.title);
            fieldExclusions.add(Field.body);
        } else {
            // describe the patchset
            if (patchset.isFF()) {
                pattern = "**{0}** added {1} {2} to patchset {3}.";
                sb.append(MessageFormat.format(pattern, user.getDisplayName(), patchset.added, patchset.added == 1 ? "commit" : "commits", patchset.number));
            } else {
                pattern = "**{0}** uploaded patchset {1}. *({2})*";
                sb.append(MessageFormat.format(pattern, user.getDisplayName(), patchset.number, patchset.type.toString().toUpperCase()));
            }
        }
        sb.append(HARD_BRK);
        sb.append(MessageFormat.format("{0} {1}, {2} {3}, <span style=\"color:darkgreen;\">+{4} insertions</span>, <span style=\"color:darkred;\">-{5} deletions</span> from {6}. [compare]({7})", commits.size(), commits.size() == 1 ? "commit" : "commits", diffstat.paths.size(), diffstat.paths.size() == 1 ? "file" : "files", diffstat.getInsertions(), diffstat.getDeletions(), isFastForward ? "previous revision" : "merge base", compareUrl));
        // note commit additions on a rebase,if any
        switch(lastChange.patchset.type) {
            case Rebase:
                if (lastChange.patchset.added > 0) {
                    sb.append(SOFT_BRK);
                    sb.append(MessageFormat.format("{0} {1} added.", lastChange.patchset.added, lastChange.patchset.added == 1 ? "commit" : "commits"));
                }
                break;
            default:
                break;
        }
        sb.append(HARD_BRK);
    } else if (lastChange.isStatusChange()) {
        if (newTicket) {
            fieldExclusions.add(Field.status);
            fieldExclusions.add(Field.title);
            fieldExclusions.add(Field.body);
            pattern = "**{0}** created this ticket.";
            sb.append(MessageFormat.format(pattern, user.getDisplayName()));
        } else if (lastChange.hasField(Field.mergeSha)) {
            // closed by merged
            pattern = "**{0}** closed this ticket by merging {1} to {2}.";
            // identify patchset that closed the ticket
            String merged = ticket.mergeSha;
            for (Patchset patchset : ticket.getPatchsets()) {
                if (patchset.tip.equals(ticket.mergeSha)) {
                    merged = patchset.toString();
                    break;
                }
            }
            sb.append(MessageFormat.format(pattern, user.getDisplayName(), merged, ticket.mergeTo));
        } else {
            // workflow status change by user
            pattern = "**{0}** changed the status of this ticket to **{1}**.";
            sb.append(MessageFormat.format(pattern, user.getDisplayName(), lastChange.getStatus().toString().toUpperCase()));
        }
        sb.append(HARD_BRK);
    } else if (lastChange.hasReview()) {
        // review
        Review review = lastChange.review;
        pattern = "**{0}** has reviewed patchset {1,number,0} revision {2,number,0}.";
        sb.append(MessageFormat.format(pattern, user.getDisplayName(), review.patchset, review.rev));
        sb.append(HARD_BRK);
        String d = settings.getString(Keys.web.datestampShortFormat, "yyyy-MM-dd");
        String t = settings.getString(Keys.web.timeFormat, "HH:mm");
        DateFormat df = new SimpleDateFormat(d + " " + t);
        List<Change> reviews = ticket.getReviews(ticket.getPatchset(review.patchset, review.rev));
        sb.append("| Date | Reviewer      | Score | Description  |\n");
        sb.append("| :--- | :------------ | :---: | :----------- |\n");
        for (Change change : reviews) {
            String name = change.author;
            UserModel u = userManager.getUserModel(change.author);
            if (u != null) {
                name = u.getDisplayName();
            }
            String score;
            switch(change.review.score) {
                case approved:
                    score = MessageFormat.format(addPattern, change.review.score.getValue());
                    break;
                case vetoed:
                    score = MessageFormat.format(delPattern, Math.abs(change.review.score.getValue()));
                    break;
                default:
                    score = "" + change.review.score.getValue();
            }
            String date = df.format(change.date);
            sb.append(String.format("| %1$s | %2$s | %3$s | %4$s |\n", date, name, score, change.review.score.toString()));
        }
        sb.append(HARD_BRK);
    } else if (lastChange.hasComment()) {
        // comment update
        sb.append(MessageFormat.format("**{0}** commented on this ticket.", user.getDisplayName()));
        sb.append(HARD_BRK);
    } else if (lastChange.hasReference()) {
        // reference update
        String type = "?";
        switch(lastChange.reference.getSourceType()) {
            case Commit:
                {
                    type = "commit";
                }
                break;
            case Ticket:
                {
                    type = "ticket";
                }
                break;
            default:
                {
                }
                break;
        }
        sb.append(MessageFormat.format("**{0}** referenced this ticket in {1} {2}", type, lastChange.toString()));
        sb.append(HARD_BRK);
    } else {
        // general update
        pattern = "**{0}** has updated this ticket.";
        sb.append(MessageFormat.format(pattern, user.getDisplayName()));
        sb.append(HARD_BRK);
    }
    // ticket link
    sb.append(MessageFormat.format("[view ticket {0,number,0}]({1})", ticket.number, ticketService.getTicketUrl(ticket)));
    sb.append(HARD_BRK);
    if (newTicket) {
        // ticket title
        sb.append(MessageFormat.format("### {0}", ticket.title));
        sb.append(HARD_BRK);
        // ticket description, on state change
        if (StringUtils.isEmpty(ticket.body)) {
            sb.append("<span style=\"color: #888;\">no description entered</span>");
        } else {
            sb.append(ticket.body);
        }
        sb.append(HARD_BRK);
        sb.append(HR);
    }
    // field changes
    if (lastChange.hasFieldChanges()) {
        Map<Field, String> filtered = new HashMap<Field, String>();
        for (Map.Entry<Field, String> fc : lastChange.fields.entrySet()) {
            if (!fieldExclusions.contains(fc.getKey())) {
                // field is included
                filtered.put(fc.getKey(), fc.getValue());
            }
        }
        // sort by field ordinal
        List<Field> fields = new ArrayList<Field>(filtered.keySet());
        Collections.sort(fields);
        if (filtered.size() > 0) {
            sb.append(HARD_BRK);
            sb.append("| Field Changes               ||\n");
            sb.append("| ------------: | :----------- |\n");
            for (Field field : fields) {
                String value;
                if (filtered.get(field) == null) {
                    value = "";
                } else {
                    value = filtered.get(field).replace("\r\n", "<br/>").replace("\n", "<br/>").replace("|", "&#124;");
                }
                sb.append(String.format("| **%1$s:** | %2$s |\n", field.name(), value));
            }
            sb.append(HARD_BRK);
        }
    }
    // new comment
    if (lastChange.hasComment()) {
        sb.append(HR);
        sb.append(lastChange.comment.text);
        sb.append(HARD_BRK);
    }
    // insert the patchset details and review instructions
    if (lastChange.hasPatchset() && ticket.isOpen()) {
        if (commits != null && commits.size() > 0) {
            // append the commit list
            String title = isFastForward ? "Commits added to previous patchset revision" : "All commits in patchset";
            sb.append(MessageFormat.format("| {0} |||\n", title));
            sb.append("| SHA | Author | Title |\n");
            sb.append("| :-- | :----- | :---- |\n");
            for (RevCommit commit : commits) {
                sb.append(MessageFormat.format("| {0} | {1} | {2} |\n", commit.getName(), commit.getAuthorIdent().getName(), StringUtils.trimString(commit.getShortMessage(), Constants.LEN_SHORTLOG).replace("|", "&#124;")));
            }
            sb.append(HARD_BRK);
        }
        if (diffstat != null) {
            // append the changed path list
            String title = isFastForward ? "Files changed since previous patchset revision" : "All files changed in patchset";
            sb.append(MessageFormat.format("| {0} |||\n", title));
            sb.append("| :-- | :----------- | :-: |\n");
            for (PathChangeModel path : diffstat.paths) {
                String add = MessageFormat.format(addPattern, path.insertions);
                String del = MessageFormat.format(delPattern, path.deletions);
                String diff = null;
                switch(path.changeType) {
                    case ADD:
                        diff = add;
                        break;
                    case DELETE:
                        diff = del;
                        break;
                    case MODIFY:
                        if (path.insertions > 0 && path.deletions > 0) {
                            // insertions & deletions
                            diff = add + "/" + del;
                        } else if (path.insertions > 0) {
                            // just insertions
                            diff = add;
                        } else {
                            // just deletions
                            diff = del;
                        }
                        break;
                    default:
                        diff = path.changeType.name();
                        break;
                }
                sb.append(MessageFormat.format("| {0} | {1} | {2} |\n", getChangeType(path.changeType), path.name, diff));
            }
            sb.append(HARD_BRK);
        }
        sb.append(formatPatchsetInstructions(ticket, lastChange.patchset));
    }
    return sb.toString();
}
Also used : HashMap(java.util.HashMap) DiffStat(com.gitblit.utils.DiffUtils.DiffStat) ArrayList(java.util.ArrayList) Patchset(com.gitblit.models.TicketModel.Patchset) Review(com.gitblit.models.TicketModel.Review) UserModel(com.gitblit.models.UserModel) Field(com.gitblit.models.TicketModel.Field) HashSet(java.util.HashSet) RevCommit(org.eclipse.jgit.revwalk.RevCommit) PathChangeModel(com.gitblit.models.PathModel.PathChangeModel) TicketModel(com.gitblit.models.TicketModel) Change(com.gitblit.models.TicketModel.Change) IOException(java.io.IOException) Repository(org.eclipse.jgit.lib.Repository) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) SimpleDateFormat(java.text.SimpleDateFormat) HashMap(java.util.HashMap) Map(java.util.Map) TreeMap(java.util.TreeMap)

Example 3 with Patchset

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

the class TicketPage method createMergePanel.

/**
 * Adds a merge panel for the patchset to the markup container.  The panel
 * may just a message if the patchset can not be merged.
 *
 * @param c
 * @param user
 * @param repository
 */
protected Component createMergePanel(UserModel user, RepositoryModel repository) {
    Patchset patchset = ticket.getCurrentPatchset();
    if (patchset == null) {
        // no patchset to merge
        return new Label("mergePanel");
    }
    boolean allowMerge;
    if (repository.requireApproval) {
        // repository requires approval
        allowMerge = ticket.isOpen() && ticket.isApproved(patchset);
    } else {
        // vetoes are binding
        allowMerge = ticket.isOpen() && !ticket.isVetoed(patchset);
    }
    MergeStatus mergeStatus = JGitUtils.canMerge(getRepository(), patchset.tip, ticket.mergeTo, repository.mergeType);
    if (allowMerge) {
        if (MergeStatus.MERGEABLE == mergeStatus) {
            // patchset can be cleanly merged to integration branch OR has already been merged
            Fragment mergePanel = new Fragment("mergePanel", "mergeableFragment", this);
            mergePanel.add(new Label("mergeTitle", MessageFormat.format(getString("gb.patchsetMergeable"), ticket.mergeTo)));
            if (user.canPush(repository)) {
                // user can merge locally
                SimpleAjaxLink<String> mergeButton = new SimpleAjaxLink<String>("mergeButton", Model.of(getString("gb.merge"))) {

                    private static final long serialVersionUID = 1L;

                    @Override
                    public void onClick(AjaxRequestTarget target) {
                        // ensure the patchset is still current AND not vetoed
                        Patchset patchset = ticket.getCurrentPatchset();
                        final TicketModel refreshedTicket = app().tickets().getTicket(getRepositoryModel(), ticket.number);
                        if (patchset.equals(refreshedTicket.getCurrentPatchset())) {
                            // patchset is current, check for recent veto
                            if (!refreshedTicket.isVetoed(patchset)) {
                                // patchset is not vetoed
                                // execute the merge using the ticket service
                                app().tickets().exec(new Runnable() {

                                    @Override
                                    public void run() {
                                        PatchsetReceivePack rp = new PatchsetReceivePack(app().gitblit(), getRepository(), getRepositoryModel(), GitBlitWebSession.get().getUser());
                                        MergeStatus result = rp.merge(refreshedTicket);
                                        if (MergeStatus.MERGED == result) {
                                            // notify participants and watchers
                                            rp.sendAll();
                                        } else {
                                            // merge failure
                                            String msg = MessageFormat.format("Failed to merge ticket {0,number,0}: {1}", ticket.number, result.name());
                                            logger().error(msg);
                                            GitBlitWebSession.get().cacheErrorMessage(msg);
                                        }
                                    }
                                });
                            } else {
                                // vetoed patchset
                                String msg = MessageFormat.format("Can not merge ticket {0,number,0}, patchset {1,number,0} has been vetoed!", ticket.number, patchset.number);
                                GitBlitWebSession.get().cacheErrorMessage(msg);
                                logger().error(msg);
                            }
                        } else {
                            // not current patchset
                            String msg = MessageFormat.format("Can not merge ticket {0,number,0}, the patchset has been updated!", ticket.number);
                            GitBlitWebSession.get().cacheErrorMessage(msg);
                            logger().error(msg);
                        }
                        redirectTo(TicketsPage.class, getPageParameters());
                    }
                };
                mergePanel.add(mergeButton);
                Component instructions = getMergeInstructions(user, repository, "mergeMore", "gb.patchsetMergeableMore");
                mergePanel.add(instructions);
            } else {
                mergePanel.add(new Label("mergeButton").setVisible(false));
                mergePanel.add(new Label("mergeMore").setVisible(false));
            }
            return mergePanel;
        } else if (MergeStatus.ALREADY_MERGED == mergeStatus) {
            // patchset already merged
            Fragment mergePanel = new Fragment("mergePanel", "alreadyMergedFragment", this);
            mergePanel.add(new Label("mergeTitle", MessageFormat.format(getString("gb.patchsetAlreadyMerged"), ticket.mergeTo)));
            return mergePanel;
        } else if (MergeStatus.MISSING_INTEGRATION_BRANCH == mergeStatus) {
            // target/integration branch is missing
            Fragment mergePanel = new Fragment("mergePanel", "notMergeableFragment", this);
            mergePanel.add(new Label("mergeTitle", MessageFormat.format(getString("gb.patchsetNotMergeable"), ticket.mergeTo)));
            mergePanel.add(new Label("mergeMore", MessageFormat.format(getString("gb.missingIntegrationBranchMore"), ticket.mergeTo)));
            return mergePanel;
        } else {
            // patchset can not be cleanly merged
            Fragment mergePanel = new Fragment("mergePanel", "notMergeableFragment", this);
            mergePanel.add(new Label("mergeTitle", MessageFormat.format(getString("gb.patchsetNotMergeable"), ticket.mergeTo)));
            if (user.canPush(repository)) {
                // user can merge locally
                Component instructions = getMergeInstructions(user, repository, "mergeMore", "gb.patchsetNotMergeableMore");
                mergePanel.add(instructions);
            } else {
                mergePanel.add(new Label("mergeMore").setVisible(false));
            }
            return mergePanel;
        }
    } else {
        // merge not allowed
        if (MergeStatus.ALREADY_MERGED == mergeStatus) {
            // patchset already merged
            Fragment mergePanel = new Fragment("mergePanel", "alreadyMergedFragment", this);
            mergePanel.add(new Label("mergeTitle", MessageFormat.format(getString("gb.patchsetAlreadyMerged"), ticket.mergeTo)));
            return mergePanel;
        } else if (ticket.isVetoed(patchset)) {
            // patchset has been vetoed
            Fragment mergePanel = new Fragment("mergePanel", "vetoedFragment", this);
            mergePanel.add(new Label("mergeTitle", MessageFormat.format(getString("gb.patchsetNotMergeable"), ticket.mergeTo)));
            return mergePanel;
        } else if (repository.requireApproval) {
            // patchset has been not been approved for merge
            Fragment mergePanel = new Fragment("mergePanel", "notApprovedFragment", this);
            mergePanel.add(new Label("mergeTitle", MessageFormat.format(getString("gb.patchsetNotApproved"), ticket.mergeTo)));
            mergePanel.add(new Label("mergeMore", MessageFormat.format(getString("gb.patchsetNotApprovedMore"), ticket.mergeTo)));
            return mergePanel;
        } else {
            // other case
            return new Label("mergePanel");
        }
    }
}
Also used : PatchsetReceivePack(com.gitblit.git.PatchsetReceivePack) Label(org.apache.wicket.markup.html.basic.Label) TicketLabel(com.gitblit.tickets.TicketLabel) Patchset(com.gitblit.models.TicketModel.Patchset) TicketModel(com.gitblit.models.TicketModel) Fragment(org.apache.wicket.markup.html.panel.Fragment) AjaxRequestTarget(org.apache.wicket.ajax.AjaxRequestTarget) SimpleAjaxLink(com.gitblit.wicket.panels.SimpleAjaxLink) MergeStatus(com.gitblit.utils.JGitUtils.MergeStatus) ShockWaveComponent(com.gitblit.wicket.panels.ShockWaveComponent) Component(org.apache.wicket.Component)

Example 4 with Patchset

use of com.gitblit.models.TicketModel.Patchset 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 5 with Patchset

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

the class PatchsetReceivePack method processReferencedTickets.

/**
 * Automatically closes open tickets that have been merged to their integration
 * branch by a client and adds references to tickets if made in the commit message.
 *
 * @param cmd
 */
private Collection<TicketModel> processReferencedTickets(ReceiveCommand cmd) {
    Map<Long, TicketModel> mergedTickets = new LinkedHashMap<Long, TicketModel>();
    final RevWalk rw = getRevWalk();
    try {
        rw.reset();
        rw.markStart(rw.parseCommit(cmd.getNewId()));
        if (!ObjectId.zeroId().equals(cmd.getOldId())) {
            rw.markUninteresting(rw.parseCommit(cmd.getOldId()));
        }
        RevCommit c;
        while ((c = rw.next()) != null) {
            rw.parseBody(c);
            List<TicketLink> ticketLinks = JGitUtils.identifyTicketsFromCommitMessage(getRepository(), settings, c);
            if (ticketLinks == null) {
                continue;
            }
            for (TicketLink link : ticketLinks) {
                if (mergedTickets.containsKey(link.targetTicketId)) {
                    continue;
                }
                TicketModel ticket = ticketService.getTicket(repository, link.targetTicketId);
                if (ticket == null) {
                    continue;
                }
                String integrationBranch;
                if (StringUtils.isEmpty(ticket.mergeTo)) {
                    // unspecified integration branch
                    integrationBranch = null;
                } else {
                    // specified integration branch
                    integrationBranch = Constants.R_HEADS + ticket.mergeTo;
                }
                Change change;
                Patchset patchset = null;
                String mergeSha = c.getName();
                String mergeTo = Repository.shortenRefName(cmd.getRefName());
                if (link.action == TicketAction.Commit) {
                    // A commit can reference a ticket in any branch even if the ticket is closed.
                    // This allows developers to identify and communicate related issues
                    change = new Change(user.username);
                    change.referenceCommit(mergeSha);
                } else {
                    // ticket must be open and, if specified, the ref must match the integration branch
                    if (ticket.isClosed() || (integrationBranch != null && !integrationBranch.equals(cmd.getRefName()))) {
                        continue;
                    }
                    String baseRef = PatchsetCommand.getBasePatchsetBranch(ticket.number);
                    boolean knownPatchset = false;
                    Set<Ref> refs = getRepository().getAllRefsByPeeledObjectId().get(c.getId());
                    if (refs != null) {
                        for (Ref ref : refs) {
                            if (ref.getName().startsWith(baseRef)) {
                                knownPatchset = true;
                                break;
                            }
                        }
                    }
                    if (knownPatchset) {
                        // identify merged patchset by the patchset tip
                        for (Patchset ps : ticket.getPatchsets()) {
                            if (ps.tip.equals(mergeSha)) {
                                patchset = ps;
                                break;
                            }
                        }
                        if (patchset == null) {
                            // should not happen - unless ticket has been hacked
                            sendError("Failed to find the patchset for {0} in ticket {1,number,0}?!", mergeSha, ticket.number);
                            continue;
                        }
                        // create a new change
                        change = new Change(user.username);
                    } else {
                        // new patchset pushed by user
                        String base = cmd.getOldId().getName();
                        patchset = newPatchset(ticket, base, mergeSha);
                        PatchsetCommand psCmd = new PatchsetCommand(user.username, patchset);
                        psCmd.updateTicket(c, mergeTo, ticket, null);
                        // create a ticket patchset ref
                        updateRef(psCmd.getPatchsetBranch(), c.getId(), patchset.type);
                        RefUpdate ru = updateRef(psCmd.getTicketBranch(), c.getId(), patchset.type);
                        updateReflog(ru);
                        // create a change from the patchset command
                        change = psCmd.getChange();
                    }
                    // set the common change data about the merge
                    change.setField(Field.status, Status.Merged);
                    change.setField(Field.mergeSha, mergeSha);
                    change.setField(Field.mergeTo, mergeTo);
                    if (StringUtils.isEmpty(ticket.responsible)) {
                        // unassigned tickets are assigned to the closer
                        change.setField(Field.responsible, user.username);
                    }
                }
                ticket = ticketService.updateTicket(repository, ticket.number, change);
                if (ticket != null) {
                    sendInfo("");
                    sendHeader("#{0,number,0}: {1}", ticket.number, StringUtils.trimString(ticket.title, Constants.LEN_SHORTLOG));
                    switch(link.action) {
                        case Commit:
                            {
                                sendInfo("referenced by push of {0} to {1}", c.getName(), mergeTo);
                            }
                            break;
                        case Close:
                            {
                                sendInfo("closed by push of {0} to {1}", patchset, mergeTo);
                                mergedTickets.put(ticket.number, ticket);
                            }
                            break;
                        default:
                            {
                            }
                    }
                    sendInfo(ticketService.getTicketUrl(ticket));
                    sendInfo("");
                } else {
                    String shortid = mergeSha.substring(0, settings.getInteger(Keys.web.shortCommitIdLength, 6));
                    switch(link.action) {
                        case Commit:
                            {
                                sendError("FAILED to reference ticket {0,number,0} by push of {1}", link.targetTicketId, shortid);
                            }
                            break;
                        case Close:
                            {
                                sendError("FAILED to close ticket {0,number,0} by push of {1}", link.targetTicketId, shortid);
                            }
                            break;
                        default:
                            {
                            }
                    }
                }
            }
        }
    } catch (IOException e) {
        LOGGER.error("Can't scan for changes to reference or close", e);
    } finally {
        rw.reset();
    }
    return mergedTickets.values();
}
Also used : TicketModel(com.gitblit.models.TicketModel) Patchset(com.gitblit.models.TicketModel.Patchset) Change(com.gitblit.models.TicketModel.Change) IOException(java.io.IOException) RevWalk(org.eclipse.jgit.revwalk.RevWalk) LinkedHashMap(java.util.LinkedHashMap) Ref(org.eclipse.jgit.lib.Ref) TicketLink(com.gitblit.models.TicketModel.TicketLink) RevCommit(org.eclipse.jgit.revwalk.RevCommit) RefUpdate(org.eclipse.jgit.lib.RefUpdate) BatchRefUpdate(org.eclipse.jgit.lib.BatchRefUpdate)

Aggregations

Patchset (com.gitblit.models.TicketModel.Patchset)13 TicketModel (com.gitblit.models.TicketModel)8 Change (com.gitblit.models.TicketModel.Change)8 IOException (java.io.IOException)4 RevCommit (org.eclipse.jgit.revwalk.RevCommit)4 TicketLink (com.gitblit.models.TicketModel.TicketLink)3 UserModel (com.gitblit.models.UserModel)3 DiffStat (com.gitblit.utils.DiffUtils.DiffStat)3 ArrayList (java.util.ArrayList)3 Ref (org.eclipse.jgit.lib.Ref)3 PathChangeModel (com.gitblit.models.PathModel.PathChangeModel)2 Review (com.gitblit.models.TicketModel.Review)2 TicketLabel (com.gitblit.tickets.TicketLabel)2 MergeStatus (com.gitblit.utils.JGitUtils.MergeStatus)2 Label (org.apache.wicket.markup.html.basic.Label)2 Fragment (org.apache.wicket.markup.html.panel.Fragment)2 Repository (org.eclipse.jgit.lib.Repository)2 RevWalk (org.eclipse.jgit.revwalk.RevWalk)2 PatchsetHook (com.gitblit.extensions.PatchsetHook)1 PatchsetReceivePack (com.gitblit.git.PatchsetReceivePack)1