use of com.google.gerrit.entities.SubmitRequirementResult in project gerrit by GerritCodeReview.
the class SubmitRequirementsAdapter method getLegacyRequirements.
/**
* Retrieve legacy submit records (created by label functions and other {@link
* com.google.gerrit.server.rules.SubmitRule}s) and convert them to submit requirement results.
*/
public static Map<SubmitRequirement, SubmitRequirementResult> getLegacyRequirements(ChangeData cd) {
// We use SubmitRuleOptions.defaults() which does not recompute submit rules for closed changes.
// This doesn't have an effect since we never call this class (i.e. to evaluate submit
// requirements) for closed changes.
List<SubmitRecord> records = cd.submitRecords(SubmitRuleOptions.defaults());
boolean areForced = records.stream().anyMatch(record -> SubmitRecord.Status.FORCED.equals(record.status));
List<LabelType> labelTypes = cd.getLabelTypes().getLabelTypes();
ObjectId commitId = cd.currentPatchSet().commitId();
Map<String, List<SubmitRequirementResult>> srsByName = records.stream().filter(r -> !SubmitRecord.Status.FORCED.equals(r.status)).map(r -> createResult(r, labelTypes, commitId, areForced)).flatMap(List::stream).collect(Collectors.groupingBy(sr -> sr.submitRequirement().name()));
// We convert submit records to submit requirements by generating a separate
// submit requirement result for each available label in each submit record.
// The SR status is derived from the label status of the submit record.
// This conversion might result in duplicate entries.
// One such example can be a prolog rule emitting the same label name twice.
// Another case might happen if two different submit rules emit the same label
// name. In such cases, we need to merge these entries and return a single submit
// requirement result. If both entries agree in their status, return any of them.
// Otherwise, favour the entry that is blocking submission.
ImmutableMap.Builder<SubmitRequirement, SubmitRequirementResult> result = ImmutableMap.builder();
for (Map.Entry<String, List<SubmitRequirementResult>> entry : srsByName.entrySet()) {
if (entry.getValue().size() == 1) {
SubmitRequirementResult srResult = entry.getValue().iterator().next();
result.put(srResult.submitRequirement(), srResult);
continue;
}
// If all submit requirements with the same name match in status, return the first one.
List<SubmitRequirementResult> resultsSameName = entry.getValue();
boolean allNonBlocking = resultsSameName.stream().allMatch(sr -> sr.fulfilled());
if (allNonBlocking) {
result.put(resultsSameName.get(0).submitRequirement(), resultsSameName.get(0));
} else {
// Otherwise, return the first submit requirement result that is blocking submission.
Optional<SubmitRequirementResult> nonFulfilled = resultsSameName.stream().filter(sr -> !sr.fulfilled()).findFirst();
if (nonFulfilled.isPresent()) {
result.put(nonFulfilled.get().submitRequirement(), nonFulfilled.get());
}
}
}
return result.build();
}
use of com.google.gerrit.entities.SubmitRequirementResult in project gerrit by GerritCodeReview.
the class SubmitRequirementsAdapter method createFromDefaultSubmitRecord.
private static List<SubmitRequirementResult> createFromDefaultSubmitRecord(List<Label> labels, List<LabelType> labelTypes, ObjectId psCommitId, boolean isForced) {
ImmutableList.Builder<SubmitRequirementResult> result = ImmutableList.builder();
for (Label label : labels) {
if (skipSubmitRequirementFor(label)) {
continue;
}
Optional<LabelType> maybeLabelType = getLabelType(labelTypes, label.label);
if (!maybeLabelType.isPresent()) {
// if it was blocking or not, hence we skip the label.
continue;
}
LabelType labelType = maybeLabelType.get();
if (!isBlocking(labelType)) {
continue;
}
ImmutableList<String> atoms = toExpressionAtomList(labelType);
SubmitRequirement.Builder req = SubmitRequirement.builder().setName(label.label).setSubmittabilityExpression(toExpression(atoms)).setAllowOverrideInChildProjects(labelType.isCanOverride());
result.add(SubmitRequirementResult.builder().legacy(Optional.of(true)).submitRequirement(req.build()).submittabilityExpressionResult(createExpressionResult(toExpression(atoms), mapStatus(label), atoms)).patchSetCommitId(psCommitId).forced(Optional.of(isForced)).build());
}
return result.build();
}
use of com.google.gerrit.entities.SubmitRequirementResult in project gerrit by GerritCodeReview.
the class SubmitRequirementsAdapter method createFromCustomSubmitRecord.
private static List<SubmitRequirementResult> createFromCustomSubmitRecord(SubmitRecord record, ObjectId psCommitId, boolean isForced) {
String ruleName = record.ruleName != null ? record.ruleName : "Custom-Rule";
if (record.labels == null || record.labels.isEmpty()) {
SubmitRequirement sr = SubmitRequirement.builder().setName(ruleName).setSubmittabilityExpression(SubmitRequirementExpression.create(String.format("rule:%s", ruleName))).setAllowOverrideInChildProjects(false).build();
return ImmutableList.of(SubmitRequirementResult.builder().legacy(Optional.of(true)).submitRequirement(sr).submittabilityExpressionResult(createExpressionResult(sr.submittabilityExpression(), mapStatus(record), ImmutableList.of(ruleName), record.errorMessage)).patchSetCommitId(psCommitId).forced(Optional.of(isForced)).build());
}
ImmutableList.Builder<SubmitRequirementResult> result = ImmutableList.builder();
for (Label label : record.labels) {
if (skipSubmitRequirementFor(label)) {
continue;
}
String expressionString = String.format("label:%s=%s", label.label, ruleName);
SubmitRequirement sr = SubmitRequirement.builder().setName(label.label).setSubmittabilityExpression(SubmitRequirementExpression.create(expressionString)).setAllowOverrideInChildProjects(false).build();
result.add(SubmitRequirementResult.builder().legacy(Optional.of(true)).submitRequirement(sr).submittabilityExpressionResult(createExpressionResult(sr.submittabilityExpression(), mapStatus(label), ImmutableList.of(expressionString))).patchSetCommitId(psCommitId).build());
}
return result.build();
}
use of com.google.gerrit.entities.SubmitRequirementResult in project gerrit by GerritCodeReview.
the class ChangeUpdate method storeRevisionNotes.
/**
* Returns the tree id for the updated tree
*/
private ObjectId storeRevisionNotes(RevWalk rw, ObjectInserter inserter, ObjectId curr) throws ConfigInvalidException, IOException {
if (submitRequirementResults == null && comments.isEmpty() && pushCert == null) {
return null;
}
RevisionNoteMap<ChangeRevisionNote> rnm = getRevisionNoteMap(rw, curr);
RevisionNoteBuilder.Cache cache = new RevisionNoteBuilder.Cache(rnm);
for (HumanComment c : comments) {
c.tag = tag;
cache.get(c.getCommitId()).putComment(c);
}
if (submitRequirementResults != null) {
if (submitRequirementResults.isEmpty()) {
ObjectId latestPsCommitId = Iterables.getLast(getNotes().getPatchSets().values()).commitId();
cache.get(latestPsCommitId).createEmptySubmitRequirementResults();
} else {
for (SubmitRequirementResult sr : submitRequirementResults) {
cache.get(sr.patchSetCommitId()).putSubmitRequirementResult(sr);
}
}
}
if (pushCert != null) {
checkState(commit != null);
cache.get(ObjectId.fromString(commit)).setPushCertificate(pushCert);
}
Map<ObjectId, RevisionNoteBuilder> builders = cache.getBuilders();
checkComments(rnm.revisionNotes, builders);
for (Map.Entry<ObjectId, RevisionNoteBuilder> e : builders.entrySet()) {
ObjectId data = inserter.insert(OBJ_BLOB, e.getValue().build(noteUtil.getChangeNoteJson()));
rnm.noteMap.set(e.getKey(), data);
}
return rnm.noteMap.writeTree(inserter);
}
use of com.google.gerrit.entities.SubmitRequirementResult in project gerrit by GerritCodeReview.
the class MergeOp method bypassSubmitRulesAndRequirements.
private void bypassSubmitRulesAndRequirements(ChangeSet cs) {
checkArgument(!cs.furtherHiddenChanges(), "cannot bypass submit rules for topic with hidden change");
for (ChangeData cd : cs.changes()) {
Change change = cd.change();
if (change == null) {
throw new StorageException("Change not found");
}
if (change.isClosed()) {
// No need to check submit rules if the change is closed.
continue;
}
List<SubmitRecord> records = new ArrayList<>(getSubmitRecords(cd));
SubmitRecord forced = new SubmitRecord();
forced.status = SubmitRecord.Status.FORCED;
records.add(forced);
cd.setSubmitRecords(submitRuleOptions(/* allowClosed= */
false), records);
// Also bypass submit requirements. Mark them as forced.
Map<SubmitRequirement, SubmitRequirementResult> forcedSRs = cd.submitRequirementsIncludingLegacy().entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, entry -> entry.getValue().toBuilder().forced(Optional.of(true)).build()));
cd.setSubmitRequirements(forcedSRs);
}
}
Aggregations