Search in sources :

Example 56 with PatchSetApproval

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

the class RevisionIT method approvalCopiedDuringSubmitIsNotPostSubmit.

@TestProjectInput(submitType = SubmitType.CHERRY_PICK)
@Test
public void approvalCopiedDuringSubmitIsNotPostSubmit() throws Exception {
    PushOneCommit.Result r = createChange();
    Change.Id id = r.getChange().getId();
    gApi.changes().id(id.get()).current().review(ReviewInput.approve());
    gApi.changes().id(id.get()).current().submit();
    ChangeData cd = r.getChange();
    assertThat(cd.patchSets()).hasSize(2);
    PatchSetApproval psa = Iterators.getOnlyElement(cd.currentApprovals().stream().filter(a -> !a.isLegacySubmit()).iterator());
    assertThat(psa.patchSetId().get()).isEqualTo(2);
    assertThat(psa.label()).isEqualTo(LabelId.CODE_REVIEW);
    assertThat(psa.value()).isEqualTo(2);
    assertThat(psa.postSubmit()).isFalse();
}
Also used : Change(com.google.gerrit.entities.Change) ChangeData(com.google.gerrit.server.query.change.ChangeData) PatchSetApproval(com.google.gerrit.entities.PatchSetApproval) PushOneCommit(com.google.gerrit.acceptance.PushOneCommit) TestProjectInput(com.google.gerrit.acceptance.TestProjectInput) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest) Test(org.junit.Test)

Example 57 with PatchSetApproval

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

the class ApprovalInference method forPatchSet.

/**
 * Returns all approvals that apply to the given patch set. Honors copied approvals from previous
 * patch-set.
 */
Iterable<PatchSetApproval> forPatchSet(ChangeNotes notes, PatchSet ps, RevWalk rw, Config repoConfig) {
    ProjectState project;
    try (TraceTimer traceTimer = TraceContext.newTimer("Computing labels for patch set", Metadata.builder().changeId(notes.load().getChangeId().get()).patchSetId(ps.id().get()).build())) {
        project = projectCache.get(notes.getProjectName()).orElseThrow(illegalState(notes.getProjectName()));
        Collection<PatchSetApproval> approvals = getForPatchSetWithoutNormalization(notes, project, ps, rw, repoConfig);
        return labelNormalizer.normalize(notes, approvals).getNormalized();
    }
}
Also used : TraceTimer(com.google.gerrit.server.logging.TraceContext.TraceTimer) ProjectState(com.google.gerrit.server.project.ProjectState) PatchSetApproval(com.google.gerrit.entities.PatchSetApproval)

Example 58 with PatchSetApproval

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

the class ApprovalInference method getForPatchSetWithoutNormalization.

private Collection<PatchSetApproval> getForPatchSetWithoutNormalization(ChangeNotes notes, ProjectState project, PatchSet patchSet, RevWalk rw, Config repoConfig) {
    checkState(project.getNameKey().equals(notes.getProjectName()), "project must match %s, %s", project.getNameKey(), notes.getProjectName());
    PatchSet.Id psId = patchSet.id();
    // Add approvals on the given patch set to the result
    Table<String, Account.Id, PatchSetApproval> resultByUser = HashBasedTable.create();
    ImmutableList<PatchSetApproval> nonCopiedApprovalsForGivenPatchSet = notes.load().getApprovals().get(patchSet.id());
    nonCopiedApprovalsForGivenPatchSet.forEach(psa -> resultByUser.put(psa.label(), psa.accountId(), psa));
    // given patch set.
    if (psId.get() == 1) {
        return resultByUser.values();
    }
    Map.Entry<PatchSet.Id, PatchSet> priorPatchSet = notes.load().getPatchSets().lowerEntry(psId);
    if (priorPatchSet == null) {
        return resultByUser.values();
    }
    ImmutableList<PatchSetApproval> priorApprovalsIncludingCopied = notes.load().getApprovalsWithCopied().get(priorPatchSet.getKey());
    // Add labels from the previous patch set to the result in case the label isn't already there
    // and settings as well as change kind allow copying.
    ChangeKind changeKind = changeKindCache.getChangeKind(project.getNameKey(), rw, repoConfig, priorPatchSet.getValue().commitId(), patchSet.commitId());
    logger.atFine().log("change kind for patch set %d of change %d against prior patch set %s is %s", patchSet.id().get(), patchSet.id().changeId().get(), priorPatchSet.getValue().id().changeId(), changeKind);
    Map<String, ModifiedFile> baseVsCurrent = null;
    Map<String, ModifiedFile> baseVsPrior = null;
    Map<String, ModifiedFile> priorVsCurrent = null;
    LabelTypes labelTypes = project.getLabelTypes();
    for (PatchSetApproval psa : priorApprovalsIncludingCopied) {
        if (resultByUser.contains(psa.label(), psa.accountId())) {
            continue;
        }
        Optional<LabelType> type = labelTypes.byLabel(psa.labelId());
        // Only compute modified files if there is a relevant label, since this is expensive.
        if (baseVsCurrent == null && type.isPresent() && type.get().isCopyAllScoresIfListOfFilesDidNotChange()) {
            baseVsCurrent = listModifiedFiles(project, patchSet, rw, repoConfig);
            baseVsPrior = listModifiedFiles(project, priorPatchSet.getValue(), rw, repoConfig);
            priorVsCurrent = listModifiedFiles(project, priorPatchSet.getValue().commitId(), patchSet.commitId(), rw, repoConfig);
        }
        if (!type.isPresent()) {
            logger.atFine().log("approval %d on label %s of patch set %d of change %d cannot be copied" + " to patch set %d because the label no longer exists on project %s", psa.value(), psa.label(), psa.key().patchSetId().get(), psa.key().patchSetId().changeId().get(), psId.get(), project.getName());
            continue;
        }
        if (!canCopyBasedOnBooleanLabelConfigs(project, psa, patchSet.id(), changeKind, type.get(), baseVsCurrent, baseVsPrior, priorVsCurrent) && !canCopyBasedOnCopyCondition(notes, psa, patchSet, type.get(), changeKind, rw, repoConfig)) {
            continue;
        }
        resultByUser.put(psa.label(), psa.accountId(), psa.copyWithPatchSet(patchSet.id()));
    }
    return resultByUser.values();
}
Also used : LabelTypes(com.google.gerrit.entities.LabelTypes) PatchSet(com.google.gerrit.entities.PatchSet) PatchSetApproval(com.google.gerrit.entities.PatchSetApproval) LabelType(com.google.gerrit.entities.LabelType) ModifiedFile(com.google.gerrit.server.patch.gitdiff.ModifiedFile) ObjectId(org.eclipse.jgit.lib.ObjectId) Map(java.util.Map) ChangeKind(com.google.gerrit.extensions.client.ChangeKind)

Example 59 with PatchSetApproval

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

the class ApprovalsUtil method addReviewers.

private List<PatchSetApproval> addReviewers(ChangeUpdate update, LabelTypes labelTypes, Change change, PatchSet.Id psId, Account.Id authorId, Account.Id committerId, Iterable<Account.Id> wantReviewers, Collection<Account.Id> existingReviewers) {
    List<LabelType> allTypes = labelTypes.getLabelTypes();
    if (allTypes.isEmpty()) {
        return ImmutableList.of();
    }
    Set<Account.Id> need = Sets.newLinkedHashSet(wantReviewers);
    if (authorId != null && canSee(update.getNotes(), authorId)) {
        need.add(authorId);
    }
    if (committerId != null && canSee(update.getNotes(), committerId)) {
        need.add(committerId);
    }
    need.remove(change.getOwner());
    need.removeAll(existingReviewers);
    if (need.isEmpty()) {
        return ImmutableList.of();
    }
    List<PatchSetApproval> cells = Lists.newArrayListWithCapacity(need.size());
    LabelId labelId = Iterables.getLast(allTypes).getLabelId();
    for (Account.Id account : need) {
        cells.add(PatchSetApproval.builder().key(PatchSetApproval.key(psId, account, labelId)).value(0).granted(update.getWhen()).build());
        update.putReviewer(account, REVIEWER);
    }
    return Collections.unmodifiableList(cells);
}
Also used : Account(com.google.gerrit.entities.Account) LabelType(com.google.gerrit.entities.LabelType) LabelId(com.google.gerrit.entities.LabelId) LabelId(com.google.gerrit.entities.LabelId) PatchSetApproval(com.google.gerrit.entities.PatchSetApproval)

Example 60 with PatchSetApproval

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

the class MergedSender method getApprovals.

public String getApprovals() {
    try {
        Table<Account.Id, String, PatchSetApproval> pos = HashBasedTable.create();
        Table<Account.Id, String, PatchSetApproval> neg = HashBasedTable.create();
        for (PatchSetApproval ca : args.approvalsUtil.byPatchSet(changeData.notes(), patchSet.id())) {
            Optional<LabelType> lt = labelTypes.byLabel(ca.labelId());
            if (!lt.isPresent()) {
                continue;
            }
            if (ca.value() > 0) {
                pos.put(ca.accountId(), lt.get().getName(), ca);
            } else if (ca.value() < 0) {
                neg.put(ca.accountId(), lt.get().getName(), ca);
            }
        }
        return format("Approvals", pos) + format("Objections", neg);
    } catch (StorageException err) {
    // Don't list the approvals
    }
    return "";
}
Also used : LabelType(com.google.gerrit.entities.LabelType) PatchSetApproval(com.google.gerrit.entities.PatchSetApproval) StorageException(com.google.gerrit.exceptions.StorageException)

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