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;
}
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();
}
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);
}
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;
}
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();
}
}
}
Aggregations