in owners-autoassign/src/main/java/com/googlesource/gerrit/owners/common/ReviewerManager.java [83:141]
public void addReviewers(
NameKey projectNameKey, ChangeApi cApi, Collection<Account.Id> accountsIds)
throws ReviewerManagerException, NoSuchProjectException {
try {
ChangeInfo changeInfo = cApi.get();
ReviewerState reviewerState = cfg.autoassignedReviewerState(projectNameKey);
try (ManualRequestContext ctx =
requestContext.openAs(Account.id(changeInfo.owner._accountId))) {
// TODO(davido): Switch back to using changes API again,
// when it supports batch mode for adding reviewers
ReviewInput in = new ReviewInput();
in.reviewers = new ArrayList<>(accountsIds.size());
Collection<Account.Id> validOwnersForAttentionSet = new ArrayList<>(accountsIds.size());
for (Account.Id account : accountsIds) {
if (isVisibleTo(changeInfo, account)) {
ReviewerInput addReviewerInput = new ReviewerInput();
addReviewerInput.reviewer = account.toString();
addReviewerInput.state = reviewerState;
in.reviewers.add(addReviewerInput);
if (reviewerState == ReviewerState.REVIEWER) {
validOwnersForAttentionSet.add(account);
}
} else {
log.warn(
"Not adding account {} as reviewer to change {} because the associated ref is not visible",
account,
changeInfo._number);
}
}
Collection<Account.Id> reviewersAccounts;
if (validOwnersForAttentionSet.isEmpty()) {
reviewersAccounts = Collections.emptyList();
} else {
reviewersAccounts =
Optional.ofNullable(ownersForAttentionSet)
.map(DynamicItem::get)
.filter(Objects::nonNull)
.map(owners -> owners.addToAttentionSet(changeInfo, validOwnersForAttentionSet))
.orElse(validOwnersForAttentionSet);
}
in.ignoreAutomaticAttentionSetRules = true;
in.addToAttentionSet =
ownersForAttentionSet.get().addToAttentionSet(changeInfo, reviewers).stream()
.map(
(reviewer) ->
new AttentionSetInput(
reviewer.toString(), "Selected as member of the OWNERS file"))
.collect(Collectors.toList());
gApi.changes().id(changeInfo.id).current().review(in);
}
} catch (RestApiException e) {
log.error("Couldn't add reviewers to the change", e);
throw new ReviewerManagerException(e);
}
}