use of com.google.gerrit.entities.SubmitRecord in project gerrit by GerritCodeReview.
the class ChangeFieldTest method record.
private static SubmitRecord record(SubmitRecord.Status status, SubmitRecord.Label... labels) {
SubmitRecord r = new SubmitRecord();
r.status = status;
if (labels.length > 0) {
r.labels = ImmutableList.copyOf(labels);
}
return r;
}
use of com.google.gerrit.entities.SubmitRecord 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.SubmitRecord 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.SubmitRecord 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);
}
use of com.google.gerrit.entities.SubmitRecord in project gerrit by GerritCodeReview.
the class PrologRuleEvaluator method evaluate.
/**
* Evaluate the submit rules.
*
* @return {@link SubmitRecord} returned from the evaluated rules. Can include errors.
*/
public SubmitRecord evaluate() {
Change change;
try {
change = cd.change();
if (change == null) {
throw new StorageException("No change found");
}
if (projectState == null) {
throw new NoSuchProjectException(cd.project());
}
} catch (StorageException | NoSuchProjectException e) {
return ruleError("Error looking up change " + cd.getId(), e);
}
logger.atFine().log("input approvals: %s", cd.approvals());
List<Term> results;
try {
results = evaluateImpl("locate_submit_rule", "can_submit", "locate_submit_filter", "filter_submit_results");
} catch (RuleEvalException e) {
return ruleError(e.getMessage(), e);
}
if (results.isEmpty()) {
// whether or not that is actually possible given the permissions.
return ruleError(String.format("Submit rule '%s' for change %s of %s has no solution.", getSubmitRuleName(), cd.getId(), projectState.getName()));
}
SubmitRecord submitRecord = resultsToSubmitRecord(getSubmitRule(), results);
logger.atFine().log("submit record: %s", submitRecord);
return submitRecord;
}
Aggregations