Example usage for org.eclipse.jgit.revwalk RevCommit getFooterLines

List of usage examples for org.eclipse.jgit.revwalk RevCommit getFooterLines

Introduction

In this page you can find the example usage for org.eclipse.jgit.revwalk RevCommit getFooterLines.

Prototype

public final List<FooterLine> getFooterLines() 

Source Link

Document

Parse the footer lines (e.g.

Usage

From source file:com.google.gerrit.server.changedetail.PublishDraft.java

License:Apache License

private void sendNotifications(final boolean newChange, final IdentifiedUser currentUser,
        final Change updatedChange, final PatchSet updatedPatchSet, final LabelTypes labelTypes)
        throws OrmException, IOException, PatchSetInfoNotAvailableException {
    final Repository git = repoManager.openRepository(updatedChange.getProject());
    try {//from  w  ww  .  ja  va 2 s  .co  m
        final RevWalk revWalk = new RevWalk(git);
        final RevCommit commit;
        try {
            commit = revWalk.parseCommit(ObjectId.fromString(updatedPatchSet.getRevision().get()));
        } finally {
            revWalk.release();
        }
        final PatchSetInfo info = patchSetInfoFactory.get(commit, updatedPatchSet.getId());
        final List<FooterLine> footerLines = commit.getFooterLines();
        final Account.Id me = currentUser.getAccountId();
        final MailRecipients recipients = getRecipientsFromFooters(accountResolver, updatedPatchSet,
                footerLines);
        recipients.remove(me);

        if (newChange) {
            approvalsUtil.addReviewers(db, labelTypes, updatedChange, updatedPatchSet, info,
                    recipients.getReviewers(), Collections.<Account.Id>emptySet());
            try {
                CreateChangeSender cm = createChangeSenderFactory.create(updatedChange);
                cm.setFrom(me);
                cm.setPatchSet(updatedPatchSet, info);
                cm.addReviewers(recipients.getReviewers());
                cm.addExtraCC(recipients.getCcOnly());
                cm.send();
            } catch (Exception e) {
                log.error("Cannot send email for new change " + updatedChange.getId(), e);
            }
        } else {
            final List<PatchSetApproval> patchSetApprovals = db.patchSetApprovals()
                    .byChange(updatedChange.getId()).toList();
            final MailRecipients oldRecipients = getRecipientsFromApprovals(patchSetApprovals);
            approvalsUtil.addReviewers(db, labelTypes, updatedChange, updatedPatchSet, info,
                    recipients.getReviewers(), oldRecipients.getAll());
            final ChangeMessage msg = new ChangeMessage(
                    new ChangeMessage.Key(updatedChange.getId(), ChangeUtil.messageUUID(db)), me,
                    updatedPatchSet.getCreatedOn(), updatedPatchSet.getId());
            msg.setMessage("Uploaded patch set " + updatedPatchSet.getPatchSetId() + ".");
            try {
                ReplacePatchSetSender cm = replacePatchSetFactory.create(updatedChange);
                cm.setFrom(me);
                cm.setPatchSet(updatedPatchSet, info);
                cm.setChangeMessage(msg);
                cm.addReviewers(recipients.getReviewers());
                cm.addExtraCC(recipients.getCcOnly());
                cm.send();
            } catch (Exception e) {
                log.error("Cannot send email for new patch set " + updatedPatchSet.getId(), e);
            }
        }
    } finally {
        git.close();
    }
}

From source file:com.google.gerrit.server.git.MergeUtil.java

License:Apache License

public String createCherryPickCommitMessage(RevCommit n, ChangeControl ctl, PatchSet.Id psId) {
    Change c = ctl.getChange();/*  w w  w  . j  av  a2s  .  c o m*/
    final List<FooterLine> footers = n.getFooterLines();
    final StringBuilder msgbuf = new StringBuilder();
    msgbuf.append(n.getFullMessage());

    if (msgbuf.length() == 0) {
        // WTF, an empty commit message?
        msgbuf.append("<no commit message provided>");
    }
    if (msgbuf.charAt(msgbuf.length() - 1) != '\n') {
        // Missing a trailing LF? Correct it (perhaps the editor was broken).
        msgbuf.append('\n');
    }
    if (footers.isEmpty()) {
        // Doesn't end in a "Signed-off-by: ..." style line? Add another line
        // break to start a new paragraph for the reviewed-by tag lines.
        //
        msgbuf.append('\n');
    }

    if (!contains(footers, FooterConstants.CHANGE_ID, c.getKey().get())) {
        msgbuf.append(FooterConstants.CHANGE_ID.getName());
        msgbuf.append(": ");
        msgbuf.append(c.getKey().get());
        msgbuf.append('\n');
    }

    final String siteUrl = urlProvider.get();
    if (siteUrl != null) {
        final String url = siteUrl + c.getId().get();
        if (!contains(footers, FooterConstants.REVIEWED_ON, url)) {
            msgbuf.append(FooterConstants.REVIEWED_ON.getName());
            msgbuf.append(": ");
            msgbuf.append(url);
            msgbuf.append('\n');
        }
    }

    PatchSetApproval submitAudit = null;

    for (final PatchSetApproval a : safeGetApprovals(ctl, psId)) {
        if (a.getValue() <= 0) {
            // Negative votes aren't counted.
            continue;
        }

        if (a.isSubmit()) {
            // Submit is treated specially, below (becomes committer)
            //
            if (submitAudit == null || a.getGranted().compareTo(submitAudit.getGranted()) > 0) {
                submitAudit = a;
            }
            continue;
        }

        final Account acc = identifiedUserFactory.create(a.getAccountId()).getAccount();
        final StringBuilder identbuf = new StringBuilder();
        if (acc.getFullName() != null && acc.getFullName().length() > 0) {
            if (identbuf.length() > 0) {
                identbuf.append(' ');
            }
            identbuf.append(acc.getFullName());
        }
        if (acc.getPreferredEmail() != null && acc.getPreferredEmail().length() > 0) {
            if (isSignedOffBy(footers, acc.getPreferredEmail())) {
                continue;
            }
            if (identbuf.length() > 0) {
                identbuf.append(' ');
            }
            identbuf.append('<');
            identbuf.append(acc.getPreferredEmail());
            identbuf.append('>');
        }
        if (identbuf.length() == 0) {
            // Nothing reasonable to describe them by? Ignore them.
            continue;
        }

        final String tag;
        if (isCodeReview(a.getLabelId())) {
            tag = "Reviewed-by";
        } else if (isVerified(a.getLabelId())) {
            tag = "Tested-by";
        } else {
            final LabelType lt = project.getLabelTypes().byLabel(a.getLabelId());
            if (lt == null) {
                continue;
            }
            tag = lt.getName();
        }

        if (!contains(footers, new FooterKey(tag), identbuf.toString())) {
            msgbuf.append(tag);
            msgbuf.append(": ");
            msgbuf.append(identbuf);
            msgbuf.append('\n');
        }
    }

    return msgbuf.toString();
}

From source file:com.google.gerrit.server.mail.PatchSetNotificationSender.java

License:Apache License

public void send(final ChangeNotes notes, final ChangeUpdate update, final boolean newChange,
        final IdentifiedUser currentUser, final Change updatedChange, final PatchSet updatedPatchSet,
        final LabelTypes labelTypes) throws OrmException, IOException {
    try (Repository git = repoManager.openRepository(updatedChange.getProject())) {
        final RevCommit commit;
        try (RevWalk revWalk = new RevWalk(git)) {
            commit = revWalk.parseCommit(ObjectId.fromString(updatedPatchSet.getRevision().get()));
        }//from w w  w  .j  a v  a 2  s .com
        final PatchSetInfo info = patchSetInfoFactory.get(commit, updatedPatchSet.getId());
        final List<FooterLine> footerLines = commit.getFooterLines();
        final Account.Id me = currentUser.getAccountId();
        final MailRecipients recipients = getRecipientsFromFooters(accountResolver, updatedPatchSet,
                footerLines);
        recipients.remove(me);

        if (newChange) {
            approvalsUtil.addReviewers(db.get(), update, labelTypes, updatedChange, updatedPatchSet, info,
                    recipients.getReviewers(), Collections.<Account.Id>emptySet());
            try {
                CreateChangeSender cm = createChangeSenderFactory.create(updatedChange.getId());
                cm.setFrom(me);
                cm.setPatchSet(updatedPatchSet, info);
                cm.addReviewers(recipients.getReviewers());
                cm.addExtraCC(recipients.getCcOnly());
                cm.send();
            } catch (Exception e) {
                log.error("Cannot send email for new change " + updatedChange.getId(), e);
            }
        } else {
            approvalsUtil.addReviewers(db.get(), update, labelTypes, updatedChange, updatedPatchSet, info,
                    recipients.getReviewers(), approvalsUtil.getReviewers(db.get(), notes).values());
            final ChangeMessage msg = new ChangeMessage(
                    new ChangeMessage.Key(updatedChange.getId(), ChangeUtil.messageUUID(db.get())), me,
                    updatedPatchSet.getCreatedOn(), updatedPatchSet.getId());
            msg.setMessage("Uploaded patch set " + updatedPatchSet.getPatchSetId() + ".");
            try {
                ReplacePatchSetSender cm = replacePatchSetFactory.create(updatedChange.getId());
                cm.setFrom(me);
                cm.setPatchSet(updatedPatchSet, info);
                cm.setChangeMessage(msg);
                cm.addReviewers(recipients.getReviewers());
                cm.addExtraCC(recipients.getCcOnly());
                cm.send();
            } catch (Exception e) {
                log.error("Cannot send email for new patch set " + updatedPatchSet.getId(), e);
            }
        }
    }
}

From source file:com.google.gerrit.server.query.change.ChangeData.java

License:Apache License

private boolean loadCommitData() throws OrmException, RepositoryNotFoundException, IOException,
        MissingObjectException, IncorrectObjectTypeException {
    PatchSet ps = currentPatchSet();//from   w  ww .  jav a2s  . c  om
    if (ps == null) {
        return false;
    }
    String sha1 = ps.getRevision().get();
    try (Repository repo = repoManager.openRepository(change().getProject());
            RevWalk walk = new RevWalk(repo)) {
        RevCommit c = walk.parseCommit(ObjectId.fromString(sha1));
        commitMessage = c.getFullMessage();
        commitFooters = c.getFooterLines();
        author = c.getAuthorIdent();
        committer = c.getCommitterIdent();
    }
    return true;
}

From source file:org.eclipse.foundation.gerrit.validation.EclipseCommitValidationListener.java

License:Open Source License

/**
 * Answer <code>true</code> if the identified user has signed off on the commit,
 * or <code>false</code> otherwise. The user may use any of their identities to
 * sign off on the commit (i.e. they can use any email address that is registered
 * with Gerrit.//w w w. j ava 2 s .  co  m
 * 
 * @param author The Gerrit identity of the author of the commit.
 * @param commit The commit.
 * @return <code>true</code> if the author has signed off; <code>false</code> otherwise.
 */
private boolean hasSignedOff(IdentifiedUser author, RevCommit commit) {
    for (final FooterLine footer : commit.getFooterLines()) {
        if (footer.matches(FooterKey.SIGNED_OFF_BY)) {
            final String email = footer.getEmailAddress();
            if (author.getEmailAddresses().contains(email))
                return true;
        }
    }
    return false;
}