Search in sources :

Example 51 with PatchSetApproval

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

the class ReviewerJson method format.

public ReviewerInfo format(ReviewerInfo out, Account.Id reviewerAccountId, ChangeData cd, Iterable<PatchSetApproval> approvals) throws PermissionBackendException {
    LabelTypes labelTypes = cd.getLabelTypes();
    out.approvals = new TreeMap<>(labelTypes.nameComparator());
    for (PatchSetApproval ca : approvals) {
        Optional<LabelType> at = labelTypes.byLabel(ca.labelId());
        at.ifPresent(lt -> out.approvals.put(lt.getName(), formatValue(ca.value())));
    }
    // Add dummy approvals for all permitted labels for the user even if they
    // do not exist in the DB.
    PatchSet ps = cd.currentPatchSet();
    if (ps != null) {
        PermissionBackend.ForChange perm = permissionBackend.absentUser(reviewerAccountId).change(cd);
        for (SubmitRecord rec : cd.submitRecords(SubmitRuleOptions.defaults())) {
            if (rec.labels == null) {
                continue;
            }
            for (SubmitRecord.Label label : rec.labels) {
                String name = label.label;
                Optional<LabelType> type = labelTypes.byLabel(name);
                if (out.approvals.containsKey(name) || !type.isPresent()) {
                    continue;
                }
                if (perm.test(new LabelPermission(type.get()))) {
                    out.approvals.put(name, formatValue((short) 0));
                }
            }
        }
    }
    if (out.approvals.isEmpty()) {
        out.approvals = null;
    }
    return out;
}
Also used : SubmitRecord(com.google.gerrit.entities.SubmitRecord) LabelTypes(com.google.gerrit.entities.LabelTypes) PermissionBackend(com.google.gerrit.server.permissions.PermissionBackend) LabelType(com.google.gerrit.entities.LabelType) PatchSet(com.google.gerrit.entities.PatchSet) PatchSetApproval(com.google.gerrit.entities.PatchSetApproval) LabelPermission(com.google.gerrit.server.permissions.LabelPermission)

Example 52 with PatchSetApproval

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

the class LabelNormalizer method applyTypeFloor.

private PatchSetApproval applyTypeFloor(LabelType lt, PatchSetApproval a) {
    PatchSetApproval.Builder b = a.toBuilder();
    LabelValue atMin = lt.getMin();
    if (atMin != null && a.value() < atMin.getValue()) {
        b.value(atMin.getValue());
    }
    LabelValue atMax = lt.getMax();
    if (atMax != null && a.value() > atMax.getValue()) {
        b.value(atMax.getValue());
    }
    return b.build();
}
Also used : LabelValue(com.google.gerrit.entities.LabelValue) PatchSetApproval(com.google.gerrit.entities.PatchSetApproval)

Example 53 with PatchSetApproval

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

the class LabelNormalizer method normalize.

/**
 * Returns copies of approvals normalized to the defined ranges for the label type. Approvals for
 * unknown labels are not included in the output
 *
 * @param notes change notes containing the given approvals.
 * @param approvals list of approvals.
 */
public Result normalize(ChangeNotes notes, Collection<PatchSetApproval> approvals) {
    List<PatchSetApproval> unchanged = Lists.newArrayListWithCapacity(approvals.size());
    List<PatchSetApproval> updated = Lists.newArrayListWithCapacity(approvals.size());
    List<PatchSetApproval> deleted = Lists.newArrayListWithCapacity(approvals.size());
    LabelTypes labelTypes = projectCache.get(notes.getProjectName()).orElseThrow(illegalState(notes.getProjectName())).getLabelTypes(notes);
    for (PatchSetApproval psa : approvals) {
        Change.Id changeId = psa.key().patchSetId().changeId();
        checkArgument(changeId.equals(notes.getChangeId()), "Approval %s does not match change %s", psa.key(), notes.getChange().getKey());
        if (psa.isLegacySubmit()) {
            unchanged.add(psa);
            continue;
        }
        Optional<LabelType> label = labelTypes.byLabel(psa.labelId());
        if (!label.isPresent()) {
            deleted.add(psa);
            continue;
        }
        PatchSetApproval copy = applyTypeFloor(label.get(), psa);
        if (copy.value() != psa.value()) {
            updated.add(copy);
        } else {
            unchanged.add(psa);
        }
    }
    return Result.create(unchanged, updated, deleted);
}
Also used : LabelTypes(com.google.gerrit.entities.LabelTypes) LabelType(com.google.gerrit.entities.LabelType) Change(com.google.gerrit.entities.Change) PatchSetApproval(com.google.gerrit.entities.PatchSetApproval)

Example 54 with PatchSetApproval

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

the class ChangeField method getLabels.

private static Iterable<String> getLabels(ChangeData cd) {
    Set<String> allApprovals = new HashSet<>();
    Set<String> distinctApprovals = new HashSet<>();
    Table<String, Short, Integer> voteCounts = HashBasedTable.create();
    for (PatchSetApproval a : cd.currentApprovals()) {
        if (a.value() != 0 && !a.isLegacySubmit()) {
            increment(voteCounts, a.label(), a.value());
            Optional<LabelType> labelType = cd.getLabelTypes().byLabel(a.labelId());
            allApprovals.add(formatLabel(a.label(), a.value(), a.accountId()));
            allApprovals.addAll(getMagicLabelFormats(a.label(), a.value(), labelType, a.accountId()));
            allApprovals.addAll(getLabelOwnerFormats(a, cd, labelType));
            allApprovals.addAll(getLabelNonUploaderFormats(a, cd, labelType));
            distinctApprovals.add(formatLabel(a.label(), a.value()));
            distinctApprovals.addAll(getMagicLabelFormats(a.label(), a.value(), labelType, /* accountId= */
            null));
        }
    }
    allApprovals.addAll(distinctApprovals);
    allApprovals.addAll(getCountLabelFormats(voteCounts, cd));
    return allApprovals;
}
Also used : LabelType(com.google.gerrit.entities.LabelType) PatchSetApproval(com.google.gerrit.entities.PatchSetApproval) HashSet(java.util.HashSet)

Example 55 with PatchSetApproval

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

the class StickyApprovalsIT method stickyVoteStoredOnUpload.

@Test
public void stickyVoteStoredOnUpload() throws Exception {
    // Code-Review will be sticky.
    String label = LabelId.CODE_REVIEW;
    try (ProjectConfigUpdate u = updateProject(project)) {
        u.getConfig().updateLabelType(label, b -> b.setCopyAnyScore(true));
        u.save();
    }
    PushOneCommit.Result r = createChange();
    // Add a new vote.
    ReviewInput input = new ReviewInput().label(LabelId.CODE_REVIEW, 2);
    input.tag = "tag";
    gApi.changes().id(r.getChangeId()).current().review(input);
    // Make new patchsets, keeping the Code-Review +2 vote.
    for (int i = 0; i < 9; i++) {
        amendChange(r.getChangeId());
    }
    List<PatchSetApproval> patchSetApprovals = r.getChange().notes().getApprovalsWithCopied().values().stream().sorted(comparing(a -> a.patchSetId().get())).collect(toImmutableList());
    for (int i = 0; i < 10; i++) {
        int patchSet = i + 1;
        assertThat(patchSetApprovals.get(i).patchSetId().get()).isEqualTo(patchSet);
        assertThat(patchSetApprovals.get(i).accountId().get()).isEqualTo(admin.id().get());
        assertThat(patchSetApprovals.get(i).realAccountId().get()).isEqualTo(admin.id().get());
        assertThat(patchSetApprovals.get(i).label()).isEqualTo(LabelId.CODE_REVIEW);
        assertThat(patchSetApprovals.get(i).value()).isEqualTo((short) 2);
        assertThat(patchSetApprovals.get(i).tag().get()).isEqualTo("tag");
        if (patchSet == 1) {
            assertThat(patchSetApprovals.get(i).copied()).isFalse();
        } else {
            assertThat(patchSetApprovals.get(i).copied()).isTrue();
        }
    }
}
Also used : ReviewInput(com.google.gerrit.extensions.api.changes.ReviewInput) PatchSetApproval(com.google.gerrit.entities.PatchSetApproval) PushOneCommit(com.google.gerrit.acceptance.PushOneCommit) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest) Test(org.junit.Test)

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