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