use of com.google.gerrit.entities.SubmitRequirementResult in project gerrit by GerritCodeReview.
the class SubmitRequirementsUtil method mergeLegacyAndNonLegacyRequirements.
/**
* Merge legacy and non-legacy submit requirement results. If both input maps have submit
* requirements with the same name and fulfillment status (according to {@link
* SubmitRequirementResult#fulfilled()}), we eliminate the entry from the {@code
* legacyRequirements} input map and only include the one from the {@code
* projectConfigRequirements} in the result.
*
* @param projectConfigRequirements map of {@link SubmitRequirement} to {@link
* SubmitRequirementResult} containing results for submit requirements stored in the
* project.config.
* @param legacyRequirements map of {@link SubmitRequirement} to {@link SubmitRequirementResult}
* containing the results of converting legacy submit records to submit requirements.
* @return a map that is the result of merging both input maps, while eliminating requirements
* with the same name and status.
*/
public ImmutableMap<SubmitRequirement, SubmitRequirementResult> mergeLegacyAndNonLegacyRequirements(Map<SubmitRequirement, SubmitRequirementResult> projectConfigRequirements, Map<SubmitRequirement, SubmitRequirementResult> legacyRequirements, ChangeData cd) {
// Cannot use ImmutableMap.Builder here since entries in the map may be overridden.
Map<SubmitRequirement, SubmitRequirementResult> result = new HashMap<>();
result.putAll(projectConfigRequirements);
Map<String, SubmitRequirementResult> requirementsByName = projectConfigRequirements.entrySet().stream().collect(Collectors.toMap(sr -> sr.getKey().name().toLowerCase(), sr -> sr.getValue()));
for (Map.Entry<SubmitRequirement, SubmitRequirementResult> legacy : legacyRequirements.entrySet()) {
String srName = legacy.getKey().name().toLowerCase();
SubmitRequirementResult projectConfigResult = requirementsByName.get(srName);
SubmitRequirementResult legacyResult = legacy.getValue();
// then add the legacy SR to the result. There is no mismatch in results in this case.
if (projectConfigResult == null) {
result.put(legacy.getKey(), legacy.getValue());
if (shouldReportMetric(cd)) {
metrics.legacyNotInSrs.increment(cd.project().get(), srName);
}
continue;
}
if (matchByStatus(projectConfigResult, legacyResult)) {
// config SR is already there.
if (shouldReportMetric(cd)) {
metrics.submitRequirementsMatchingWithLegacy.increment(cd.project().get(), srName);
}
continue;
}
// matching in their result. Increment the mismatch count and add the legacy SR to the result.
if (shouldReportMetric(cd)) {
metrics.submitRequirementsMismatchingWithLegacy.increment(cd.project().get(), srName);
}
result.put(legacy.getKey(), legacy.getValue());
}
Set<String> legacyNames = legacyRequirements.keySet().stream().map(SubmitRequirement::name).collect(Collectors.toSet());
for (String projectConfigSrName : requirementsByName.keySet()) {
if (!legacyNames.contains(projectConfigSrName) && shouldReportMetric(cd)) {
metrics.srsNotInLegacy.increment(cd.project().get(), projectConfigSrName);
}
}
return ImmutableMap.copyOf(result);
}
use of com.google.gerrit.entities.SubmitRequirementResult in project gerrit by GerritCodeReview.
the class SubmitRequirementsEvaluatorImpl method getRequirements.
/**
* Evaluate and return all {@link SubmitRequirement}s.
*
* <p>This includes all globally bound {@link SubmitRequirement}s, as well as requirements stored
* in this project's config and its parents.
*
* <p>The behaviour in case of the name match is controlled by {@link
* SubmitRequirement#allowOverrideInChildProjects} of global {@link SubmitRequirement}.
*/
private ImmutableMap<SubmitRequirement, SubmitRequirementResult> getRequirements(ChangeData cd) {
Map<String, SubmitRequirement> globalRequirements = getGlobalRequirements();
ProjectState state = projectCache.get(cd.project()).orElseThrow(illegalState(cd.project()));
Map<String, SubmitRequirement> projectConfigRequirements = state.getSubmitRequirements();
ImmutableMap<String, SubmitRequirement> requirements = Stream.concat(globalRequirements.entrySet().stream(), projectConfigRequirements.entrySet().stream()).collect(toImmutableMap(Map.Entry::getKey, Map.Entry::getValue, (globalSubmitRequirement, projectConfigRequirement) -> globalSubmitRequirement.allowOverrideInChildProjects() ? projectConfigRequirement : globalSubmitRequirement));
ImmutableMap.Builder<SubmitRequirement, SubmitRequirementResult> results = ImmutableMap.builder();
for (SubmitRequirement requirement : requirements.values()) {
results.put(requirement, evaluateRequirement(requirement, cd));
}
return results.build();
}
use of com.google.gerrit.entities.SubmitRequirementResult in project gerrit by GerritCodeReview.
the class SubmitRequirementIT method globalSubmitRequirement_storedForClosedChanges.
@Test
public void globalSubmitRequirement_storedForClosedChanges() throws Exception {
SubmitRequirement globalSubmitRequirement = SubmitRequirement.builder().setName("global-submit-requirement").setSubmittabilityExpression(SubmitRequirementExpression.create("topic:test")).setAllowOverrideInChildProjects(false).build();
try (Registration registration = extensionRegistry.newRegistration().add(globalSubmitRequirement)) {
PushOneCommit.Result r = createChange();
String changeId = r.getChangeId();
ChangeInfo change = gApi.changes().id(changeId).get();
assertThat(change.submitRequirements).hasSize(2);
assertSubmitRequirementStatus(change.submitRequirements, "global-submit-requirement", Status.UNSATISFIED, /* isLegacy= */
false);
assertSubmitRequirementStatus(change.submitRequirements, "Code-Review", Status.UNSATISFIED, /* isLegacy= */
true);
voteLabel(changeId, "Code-Review", 2);
gApi.changes().id(changeId).topic("test");
change = gApi.changes().id(changeId).get();
assertThat(change.submitRequirements).hasSize(2);
assertSubmitRequirementStatus(change.submitRequirements, "global-submit-requirement", Status.SATISFIED, /* isLegacy= */
false);
assertSubmitRequirementStatus(change.submitRequirements, "Code-Review", Status.SATISFIED, /* isLegacy= */
true);
gApi.changes().id(changeId).current().submit();
ChangeNotes notes = notesFactory.create(project, r.getChange().getId());
SubmitRequirementResult result = notes.getSubmitRequirementsResult().stream().collect(MoreCollectors.onlyElement());
assertThat(result.status()).isEqualTo(SubmitRequirementResult.Status.SATISFIED);
assertThat(result.submittabilityExpressionResult().get().status()).isEqualTo(SubmitRequirementExpressionResult.Status.PASS);
assertThat(result.submittabilityExpressionResult().get().expression().expressionString()).isEqualTo("topic:test");
}
}
use of com.google.gerrit.entities.SubmitRequirementResult in project gerrit by GerritCodeReview.
the class SubmitRequirementsEvaluatorIT method submittabilityAndOverrideNotEvaluated_whenApplicabilityIsFalse.
@Test
public void submittabilityAndOverrideNotEvaluated_whenApplicabilityIsFalse() throws Exception {
SubmitRequirement sr = createSubmitRequirement(/* applicabilityExpr= */
"project:non-existent-project", /* submittabilityExpr= */
"message:\"Fix bug\"", /* overrideExpr= */
"");
SubmitRequirementResult result = evaluator.evaluateRequirement(sr, changeData);
assertThat(result.status()).isEqualTo(SubmitRequirementResult.Status.NOT_APPLICABLE);
assertThat(result.applicabilityExpressionResult().get().status()).isEqualTo(Status.FAIL);
assertThat(result.submittabilityExpressionResult().isPresent()).isFalse();
assertThat(result.overrideExpressionResult().isPresent()).isFalse();
}
use of com.google.gerrit.entities.SubmitRequirementResult in project gerrit by GerritCodeReview.
the class SubmitRequirementsEvaluatorIT method submitRequirementIsError_whenSubmittabilityExpressionHasInvalidSyntax.
@Test
public void submitRequirementIsError_whenSubmittabilityExpressionHasInvalidSyntax() throws Exception {
addLabel("build-cop-override");
SubmitRequirement sr = createSubmitRequirement(/* applicabilityExpr= */
"project:" + project.get(), /* submittabilityExpr= */
"invalid_field:invalid_value", /* overrideExpr= */
"label:\"build-cop-override=+1\"");
SubmitRequirementResult result = evaluator.evaluateRequirement(sr, changeData);
assertThat(result.status()).isEqualTo(SubmitRequirementResult.Status.ERROR);
assertThat(result.submittabilityExpressionResult().get().errorMessage().get()).isEqualTo("Unsupported operator invalid_field:invalid_value");
}
Aggregations