Search in sources :

Example 36 with PatchSetApproval

use of com.google.gerrit.entities.PatchSetApproval in project gerrit by GerritCodeReview.

the class CommitRewriter method collectAccounts.

/**
 * Retrieves accounts, that are associated with a change (e.g. reviewers, commenters, etc.). These
 * accounts are used to verify that commits do not contain user data. See {@link #verifyCommit}
 *
 * @param changeNotes {@link ChangeNotes} of the change to retrieve associated accounts from.
 * @return {@link AccountState} of accounts, that are associated with the change.
 */
private ImmutableSet<AccountState> collectAccounts(ChangeNotes changeNotes) {
    Set<Account.Id> accounts = new HashSet<>();
    accounts.add(changeNotes.getChange().getOwner());
    for (PatchSetApproval patchSetApproval : changeNotes.getApprovals().values()) {
        if (patchSetApproval.accountId() != null) {
            accounts.add(patchSetApproval.accountId());
        }
        if (patchSetApproval.realAccountId() != null) {
            accounts.add(patchSetApproval.realAccountId());
        }
    }
    accounts.addAll(changeNotes.getAllPastReviewers());
    accounts.addAll(changeNotes.getPastAssignees());
    changeNotes.getAttentionSetUpdates().forEach(attentionSetUpdate -> accounts.add(attentionSetUpdate.account()));
    for (SubmitRecord submitRecord : changeNotes.getSubmitRecords()) {
        if (submitRecord.labels != null) {
            accounts.addAll(submitRecord.labels.stream().map(label -> label.appliedBy).filter(Objects::nonNull).collect(Collectors.toSet()));
        }
    }
    for (HumanComment comment : changeNotes.getHumanComments().values()) {
        if (comment.author != null) {
            accounts.add(comment.author.getId());
        }
        if (comment.getRealAuthor() != null) {
            accounts.add(comment.getRealAuthor().getId());
        }
    }
    return ImmutableSet.copyOf(accountCache.get(accounts).values());
}
Also used : Arrays(java.util.Arrays) ObjectInserter(org.eclipse.jgit.lib.ObjectInserter) FOOTER_SUBMITTED_WITH(com.google.gerrit.server.notedb.ChangeNoteUtil.FOOTER_SUBMITTED_WITH) Inject(com.google.inject.Inject) RawTextComparator(org.eclipse.jgit.diff.RawTextComparator) ReceiveCommand(org.eclipse.jgit.transport.ReceiveCommand) HumanComment(com.google.gerrit.entities.HumanComment) StringUtils(org.apache.commons.lang3.StringUtils) RevWalk(org.eclipse.jgit.revwalk.RevWalk) FOOTER_ASSIGNEE(com.google.gerrit.server.notedb.ChangeNoteUtil.FOOTER_ASSIGNEE) PatchSetApproval(com.google.gerrit.entities.PatchSetApproval) OutputFormat(com.google.gerrit.json.OutputFormat) Matcher(java.util.regex.Matcher) FooterLine(org.eclipse.jgit.revwalk.FooterLine) Gson(com.google.gson.Gson) Map(java.util.Map) RefNames(com.google.gerrit.entities.RefNames) RawText(org.eclipse.jgit.diff.RawText) CommitMessageRange(com.google.gerrit.server.notedb.ChangeNoteUtil.CommitMessageRange) ACCOUNT_TEMPLATE_REGEX(com.google.gerrit.server.util.AccountTemplateUtil.ACCOUNT_TEMPLATE_REGEX) RefUpdateUtil(com.google.gerrit.git.RefUpdateUtil) Splitter(com.google.common.base.Splitter) FOOTER_ATTENTION(com.google.gerrit.server.notedb.ChangeNoteUtil.FOOTER_ATTENTION) ImmutableSet(com.google.common.collect.ImmutableSet) EditList(org.eclipse.jgit.diff.EditList) ImmutableMap(com.google.common.collect.ImmutableMap) Collection(java.util.Collection) RawParseUtils(org.eclipse.jgit.util.RawParseUtils) Account(com.google.gerrit.entities.Account) Set(java.util.Set) RevSort(org.eclipse.jgit.revwalk.RevSort) Constants(org.eclipse.jgit.lib.Constants) SubmitRecord(com.google.gerrit.entities.SubmitRecord) Collectors(java.util.stream.Collectors) Preconditions.checkState(com.google.common.base.Preconditions.checkState) Serializable(java.io.Serializable) Objects(java.util.Objects) PersonIdent(org.eclipse.jgit.lib.PersonIdent) List(java.util.List) Nullable(com.google.gerrit.common.Nullable) Stream(java.util.stream.Stream) BatchRefUpdate(org.eclipse.jgit.lib.BatchRefUpdate) Ref(org.eclipse.jgit.lib.Ref) AutoValue(com.google.auto.value.AutoValue) Optional(java.util.Optional) MoreObjects.firstNonNull(com.google.common.base.MoreObjects.firstNonNull) FOOTER_LABEL(com.google.gerrit.server.notedb.ChangeNoteUtil.FOOTER_LABEL) Pattern(java.util.regex.Pattern) FluentLogger(com.google.common.flogger.FluentLogger) ChangeMessagesUtil(com.google.gerrit.server.ChangeMessagesUtil) Singleton(com.google.inject.Singleton) AccountCache(com.google.gerrit.server.account.AccountCache) HistogramDiff(org.eclipse.jgit.diff.HistogramDiff) Iterables(com.google.common.collect.Iterables) ConfigInvalidException(org.eclipse.jgit.errors.ConfigInvalidException) RevCommit(org.eclipse.jgit.revwalk.RevCommit) ByteArrayOutputStream(java.io.ByteArrayOutputStream) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) FOOTER_TAG(com.google.gerrit.server.notedb.ChangeNoteUtil.FOOTER_TAG) HashSet(java.util.HashSet) Strings(com.google.common.base.Strings) FOOTER_REAL_USER(com.google.gerrit.server.notedb.ChangeNoteUtil.FOOTER_REAL_USER) Charset(java.nio.charset.Charset) CommitBuilder(org.eclipse.jgit.lib.CommitBuilder) UsedAt(com.google.gerrit.common.UsedAt) Change(com.google.gerrit.entities.Change) AttentionStatusInNoteDb(com.google.gerrit.server.notedb.ChangeNoteUtil.AttentionStatusInNoteDb) FileRepository(org.eclipse.jgit.internal.storage.file.FileRepository) IOException(java.io.IOException) DiffAlgorithm(org.eclipse.jgit.diff.DiffAlgorithm) ObjectId(org.eclipse.jgit.lib.ObjectId) DiffFormatter(org.eclipse.jgit.diff.DiffFormatter) AccountTemplateUtil(com.google.gerrit.server.util.AccountTemplateUtil) PackInserter(org.eclipse.jgit.internal.storage.file.PackInserter) Project(com.google.gerrit.entities.Project) ExternalId(com.google.gerrit.server.account.externalids.ExternalId) AccountState(com.google.gerrit.server.account.AccountState) ACCOUNT_TEMPLATE_PATTERN(com.google.gerrit.server.util.AccountTemplateUtil.ACCOUNT_TEMPLATE_PATTERN) Repository(org.eclipse.jgit.lib.Repository) SubmitRecord(com.google.gerrit.entities.SubmitRecord) Objects(java.util.Objects) ObjectId(org.eclipse.jgit.lib.ObjectId) ExternalId(com.google.gerrit.server.account.externalids.ExternalId) PatchSetApproval(com.google.gerrit.entities.PatchSetApproval) HumanComment(com.google.gerrit.entities.HumanComment) HashSet(java.util.HashSet)

Example 37 with PatchSetApproval

use of com.google.gerrit.entities.PatchSetApproval in project gerrit by GerritCodeReview.

the class ImpersonationIT method voteOnBehalfOf.

@Test
public void voteOnBehalfOf() throws Exception {
    allowCodeReviewOnBehalfOf();
    PushOneCommit.Result r = createChange();
    RevisionApi revision = gApi.changes().id(r.getChangeId()).current();
    ReviewInput in = ReviewInput.recommend();
    in.onBehalfOf = user.id().toString();
    in.message = "Message on behalf of";
    revision.review(in);
    PatchSetApproval psa = Iterables.getOnlyElement(r.getChange().approvals().values());
    assertThat(psa.patchSetId().get()).isEqualTo(1);
    assertThat(psa.label()).isEqualTo("Code-Review");
    assertThat(psa.accountId()).isEqualTo(user.id());
    assertThat(psa.value()).isEqualTo(1);
    assertThat(psa.realAccountId()).isEqualTo(admin.id());
    ChangeData cd = r.getChange();
    ChangeMessage m = Iterables.getLast(cmUtil.byChange(cd.notes()));
    assertThat(m.getMessage()).endsWith(in.message);
    assertThat(m.getAuthor()).isEqualTo(user.id());
    assertThat(m.getRealAuthor()).isEqualTo(admin.id());
}
Also used : RevisionApi(com.google.gerrit.extensions.api.changes.RevisionApi) ChangeMessage(com.google.gerrit.entities.ChangeMessage) ReviewInput(com.google.gerrit.extensions.api.changes.ReviewInput) PatchSetApproval(com.google.gerrit.entities.PatchSetApproval) ChangeData(com.google.gerrit.server.query.change.ChangeData) PushOneCommit(com.google.gerrit.acceptance.PushOneCommit) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest) Test(org.junit.Test)

Example 38 with PatchSetApproval

use of com.google.gerrit.entities.PatchSetApproval in project gerrit by GerritCodeReview.

the class ImpersonationIT method testVoteOnBehalfOfWithComment.

private void testVoteOnBehalfOfWithComment() throws Exception {
    allowCodeReviewOnBehalfOf();
    PushOneCommit.Result r = createChange();
    ReviewInput in = new ReviewInput();
    in.onBehalfOf = user.id().toString();
    in.label("Code-Review", 1);
    CommentInput ci = new CommentInput();
    ci.path = Patch.COMMIT_MSG;
    ci.side = Side.REVISION;
    ci.line = 1;
    ci.message = "message";
    in.comments = ImmutableMap.of(ci.path, ImmutableList.of(ci));
    gApi.changes().id(r.getChangeId()).current().review(in);
    PatchSetApproval psa = Iterables.getOnlyElement(r.getChange().approvals().values());
    assertThat(psa.patchSetId().get()).isEqualTo(1);
    assertThat(psa.label()).isEqualTo("Code-Review");
    assertThat(psa.accountId()).isEqualTo(user.id());
    assertThat(psa.value()).isEqualTo(1);
    assertThat(psa.realAccountId()).isEqualTo(admin.id());
    ChangeData cd = r.getChange();
    HumanComment c = Iterables.getOnlyElement(commentsUtil.publishedHumanCommentsByChange(cd.notes()));
    assertThat(c.message).isEqualTo(ci.message);
    assertThat(c.author.getId()).isEqualTo(user.id());
    assertThat(c.getRealAuthor().getId()).isEqualTo(admin.id());
}
Also used : CommentInput(com.google.gerrit.extensions.api.changes.ReviewInput.CommentInput) RobotCommentInput(com.google.gerrit.extensions.api.changes.ReviewInput.RobotCommentInput) ReviewInput(com.google.gerrit.extensions.api.changes.ReviewInput) PatchSetApproval(com.google.gerrit.entities.PatchSetApproval) ChangeData(com.google.gerrit.server.query.change.ChangeData) HumanComment(com.google.gerrit.entities.HumanComment) PushOneCommit(com.google.gerrit.acceptance.PushOneCommit)

Example 39 with PatchSetApproval

use of com.google.gerrit.entities.PatchSetApproval in project gerrit by GerritCodeReview.

the class DefaultSubmitRule method evaluate.

@Override
public Optional<SubmitRecord> evaluate(ChangeData cd) {
    SubmitRecord submitRecord = new SubmitRecord();
    submitRecord.status = SubmitRecord.Status.OK;
    List<LabelType> labelTypes = cd.getLabelTypes().getLabelTypes();
    List<PatchSetApproval> approvals = cd.currentApprovals();
    submitRecord.labels = new ArrayList<>(labelTypes.size());
    for (LabelType t : labelTypes) {
        LabelFunction labelFunction = t.getFunction();
        checkState(labelFunction != null, "Unable to find the LabelFunction for label %s, change %s", t.getName(), cd.getId());
        Collection<PatchSetApproval> approvalsForLabel = getApprovalsForLabel(approvals, t);
        SubmitRecord.Label label = labelFunction.check(t, approvalsForLabel);
        submitRecord.labels.add(label);
        switch(label.status) {
            case OK:
            case MAY:
                break;
            case NEED:
            case REJECT:
            case IMPOSSIBLE:
                submitRecord.status = SubmitRecord.Status.NOT_READY;
                break;
        }
    }
    return Optional.of(submitRecord);
}
Also used : SubmitRecord(com.google.gerrit.entities.SubmitRecord) LabelType(com.google.gerrit.entities.LabelType) LabelFunction(com.google.gerrit.entities.LabelFunction) PatchSetApproval(com.google.gerrit.entities.PatchSetApproval)

Example 40 with PatchSetApproval

use of com.google.gerrit.entities.PatchSetApproval in project gerrit by GerritCodeReview.

the class IgnoreSelfApprovalRule method evaluate.

@Override
public Optional<SubmitRecord> evaluate(ChangeData cd) {
    List<LabelType> labelTypes = cd.getLabelTypes().getLabelTypes();
    List<PatchSetApproval> approvals = cd.currentApprovals();
    boolean shouldIgnoreSelfApproval = labelTypes.stream().anyMatch(LabelType::isIgnoreSelfApproval);
    if (!shouldIgnoreSelfApproval) {
        // Shortcut to avoid further processing if no label should ignore uploader approvals
        return Optional.empty();
    }
    Account.Id uploader = cd.currentPatchSet().uploader();
    SubmitRecord submitRecord = new SubmitRecord();
    submitRecord.status = SubmitRecord.Status.OK;
    submitRecord.labels = new ArrayList<>(labelTypes.size());
    submitRecord.requirements = new ArrayList<>();
    for (LabelType t : labelTypes) {
        if (!t.isIgnoreSelfApproval()) {
            // The default rules are enough in this case.
            continue;
        }
        LabelFunction labelFunction = t.getFunction();
        if (labelFunction == null) {
            continue;
        }
        Collection<PatchSetApproval> allApprovalsForLabel = filterApprovalsByLabel(approvals, t);
        SubmitRecord.Label allApprovalsCheckResult = labelFunction.check(t, allApprovalsForLabel);
        SubmitRecord.Label ignoreSelfApprovalCheckResult = labelFunction.check(t, filterOutPositiveApprovalsOfUser(allApprovalsForLabel, uploader));
        if (labelCheckPassed(allApprovalsCheckResult) && !labelCheckPassed(ignoreSelfApprovalCheckResult)) {
            // The label has a valid approval from the uploader and no other valid approval. Set the
            // label
            // to NOT_READY and indicate the need for non-uploader approval as requirement.
            submitRecord.labels.add(ignoreSelfApprovalCheckResult);
            submitRecord.status = SubmitRecord.Status.NOT_READY;
            // Add an additional requirement to be more descriptive on why the label counts as not
            // approved.
            submitRecord.requirements.add(LegacySubmitRequirement.builder().setFallbackText("Approval from non-uploader required").setType("non_uploader_approval").build());
        }
    }
    if (submitRecord.labels.isEmpty()) {
        return Optional.empty();
    }
    return Optional.of(submitRecord);
}
Also used : Account(com.google.gerrit.entities.Account) SubmitRecord(com.google.gerrit.entities.SubmitRecord) LabelType(com.google.gerrit.entities.LabelType) LabelFunction(com.google.gerrit.entities.LabelFunction) PatchSetApproval(com.google.gerrit.entities.PatchSetApproval)

Aggregations

PatchSetApproval (com.google.gerrit.entities.PatchSetApproval)93 Test (org.junit.Test)57 Change (com.google.gerrit.entities.Change)41 LabelType (com.google.gerrit.entities.LabelType)22 Account (com.google.gerrit.entities.Account)20 PushOneCommit (com.google.gerrit.acceptance.PushOneCommit)14 Map (java.util.Map)14 ObjectId (org.eclipse.jgit.lib.ObjectId)14 LabelId (com.google.gerrit.entities.LabelId)13 PatchSet (com.google.gerrit.entities.PatchSet)12 SubmitRecord (com.google.gerrit.entities.SubmitRecord)12 AbstractDaemonTest (com.google.gerrit.acceptance.AbstractDaemonTest)11 ReviewInput (com.google.gerrit.extensions.api.changes.ReviewInput)10 SubmissionId (com.google.gerrit.entities.SubmissionId)9 ChangeData (com.google.gerrit.server.query.change.ChangeData)9 Inject (com.google.inject.Inject)9 Instant (java.time.Instant)9 HashMap (java.util.HashMap)9 List (java.util.List)9 ChangeMessage (com.google.gerrit.entities.ChangeMessage)8