use of com.google.gerrit.entities.PatchSetApproval in project gerrit by GerritCodeReview.
the class RevisionApiImpl method votes.
@Override
public ListMultimap<String, ApprovalInfo> votes() throws RestApiException {
ListMultimap<String, ApprovalInfo> result = ListMultimapBuilder.treeKeys().arrayListValues().build();
try {
Iterable<PatchSetApproval> approvals = approvalsUtil.byPatchSet(revision.getNotes(), revision.getPatchSet().id());
AccountLoader accountLoader = accountLoaderFactory.create(EnumSet.of(FillOptions.ID, FillOptions.NAME, FillOptions.EMAIL, FillOptions.USERNAME));
for (PatchSetApproval approval : approvals) {
String label = approval.label();
ApprovalInfo info = new ApprovalInfo(approval.accountId().get(), Integer.valueOf(approval.value()), null, approval.tag().orElse(null), approval.granted());
accountLoader.put(info);
result.get(label).add(info);
}
accountLoader.fill();
} catch (Exception e) {
throw asRestApiException("Cannot get votes", e);
}
return result;
}
use of com.google.gerrit.entities.PatchSetApproval in project gerrit by GerritCodeReview.
the class ReviewerSet method fromApprovals.
public static ReviewerSet fromApprovals(Iterable<PatchSetApproval> approvals) {
PatchSetApproval first = null;
Table<ReviewerStateInternal, Account.Id, Instant> reviewers = HashBasedTable.create();
for (PatchSetApproval psa : approvals) {
if (first == null) {
first = psa;
} else {
checkArgument(first.key().patchSetId().changeId().equals(psa.key().patchSetId().changeId()), "multiple change IDs: %s, %s", first.key(), psa.key());
}
Account.Id id = psa.accountId();
reviewers.put(REVIEWER, id, psa.granted());
if (psa.value() != 0) {
reviewers.remove(CC, id);
}
}
return new ReviewerSet(reviewers);
}
use of com.google.gerrit.entities.PatchSetApproval in project gerrit by GerritCodeReview.
the class PatchSetUtil method isPatchSetLocked.
/**
* Is the current patch set locked against state changes?
*/
public boolean isPatchSetLocked(ChangeNotes notes) {
Change change = notes.getChange();
if (change.isMerged()) {
return false;
}
ProjectState projectState = projectCache.get(notes.getProjectName()).orElseThrow(illegalState(notes.getProjectName()));
ApprovalsUtil approvalsUtil = approvalsUtilProvider.get();
for (PatchSetApproval ap : approvalsUtil.byPatchSet(notes, change.currentPatchSetId())) {
Optional<LabelType> type = projectState.getLabelTypes(notes).byLabel(ap.label());
if (type.isPresent() && ap.value() == 1 && type.get().getFunction() == LabelFunction.PATCH_SET_LOCK) {
return true;
}
}
return false;
}
use of com.google.gerrit.entities.PatchSetApproval in project gerrit by GerritCodeReview.
the class ApprovalsUtil method addApprovalsForNewPatchSet.
/**
* Adds approvals to ChangeUpdate for a new patch set, and writes to NoteDb.
*
* @param update change update.
* @param labelTypes label types for the containing project.
* @param ps patch set being approved.
* @param user user adding approvals.
* @param approvals approvals to add.
*/
public Iterable<PatchSetApproval> addApprovalsForNewPatchSet(ChangeUpdate update, LabelTypes labelTypes, PatchSet ps, CurrentUser user, Map<String, Short> approvals) throws RestApiException, PermissionBackendException {
Account.Id accountId = user.getAccountId();
checkArgument(accountId.equals(ps.uploader()), "expected user %s to match patch set uploader %s", accountId, ps.uploader());
if (approvals.isEmpty()) {
return ImmutableList.of();
}
checkApprovals(approvals, permissionBackend.user(user).change(update.getNotes()));
List<PatchSetApproval> cells = new ArrayList<>(approvals.size());
Instant ts = update.getWhen();
for (Map.Entry<String, Short> vote : approvals.entrySet()) {
Optional<LabelType> lt = labelTypes.byLabel(vote.getKey());
if (!lt.isPresent()) {
throw new BadRequestException(String.format("label \"%s\" is not a configured label", vote.getKey()));
}
cells.add(newApproval(ps.id(), user, lt.get().getLabelId(), vote.getValue(), ts).build());
}
for (PatchSetApproval psa : cells) {
update.putApproval(psa.label(), psa.value());
}
return cells;
}
use of com.google.gerrit.entities.PatchSetApproval in project gerrit by GerritCodeReview.
the class ChangeNotesTest method reissuedApproval_otherPatchSet_differentUUID.
@Test
public void reissuedApproval_otherPatchSet_differentUUID() throws Exception {
Change c = newChange();
ChangeUpdate update = newUpdate(c, changeOwner);
update.putApproval(LabelId.CODE_REVIEW, (short) -1);
update.commit();
ChangeNotes notes = newNotes(c);
assertThat(notes.getApprovals().keySet()).containsExactly(c.currentPatchSetId());
PatchSetApproval originalPsa = Iterables.getOnlyElement(notes.getApprovals().get(c.currentPatchSetId()));
assertThat(originalPsa.patchSetId()).isEqualTo(c.currentPatchSetId());
assertThat(originalPsa.accountId().get()).isEqualTo(1);
assertThat(originalPsa.label()).isEqualTo(LabelId.CODE_REVIEW);
assertThat(originalPsa.value()).isEqualTo((short) -1);
assertParsedUuid(originalPsa);
// Create new PatchSet and re-issue vote
incrementPatchSet(c);
update = newUpdate(c, changeOwner);
update.putApproval(LabelId.CODE_REVIEW, (short) -1);
update.commit();
notes = newNotes(c);
assertThat(notes.getApprovals().keySet()).hasSize(2);
PatchSetApproval postUpdateOriginalPsa = Iterables.getOnlyElement(notes.getApprovals().get(originalPsa.patchSetId()));
PatchSetApproval reAddedPsa = Iterables.getOnlyElement(notes.getApprovals().get(c.currentPatchSetId()));
// Same patch set approval for the original patch set is returned after the vote was re-issued
// on the next patch set
assertThat(postUpdateOriginalPsa).isEqualTo(originalPsa);
assertThat(reAddedPsa.accountId()).isEqualTo(originalPsa.accountId());
assertThat(reAddedPsa.label()).isEqualTo(originalPsa.label());
assertThat(reAddedPsa.value()).isEqualTo(originalPsa.value());
// The re-added approval has a different UUID
assertParsedUuid(reAddedPsa);
assertThat(reAddedPsa.uuid().get()).isNotEqualTo(originalPsa.uuid().get());
}
Aggregations