use of com.google.gerrit.entities.SubmitRequirement in project gerrit by GerritCodeReview.
the class ProjectConfigTest method readSubmitRequirements.
@Test
public void readSubmitRequirements() throws Exception {
RevCommit rev = tr.commit().add("groups", group(developers)).add("project.config", "[submit-requirement \"Code-review\"]\n" + " description = At least one Code Review +2\n" + " applicableIf =branch(refs/heads/master)\n" + " submittableIf = label(Code-Review, +2)\n" + "[submit-requirement \"api-review\"]\n" + " description = Additional review required for API modifications\n" + " applicableIf =commit_filepath_contains(\\\"/api/.*\\\")\n" + " submittableIf = label(api-review, +2)\n" + " overrideIf = label(build-cop-override, +1)\n" + " canOverrideInChildProjects = true\n").create();
ProjectConfig cfg = read(rev);
Map<String, SubmitRequirement> submitRequirements = cfg.getSubmitRequirementSections();
assertThat(submitRequirements).containsExactly(// Capitalization preserved
"Code-review", SubmitRequirement.builder().setName(// Capitalization preserved
"Code-review").setDescription(Optional.of("At least one Code Review +2")).setApplicabilityExpression(SubmitRequirementExpression.of("branch(refs/heads/master)")).setSubmittabilityExpression(SubmitRequirementExpression.create("label(Code-Review, +2)")).setOverrideExpression(Optional.empty()).setAllowOverrideInChildProjects(false).build(), "api-review", SubmitRequirement.builder().setName("api-review").setDescription(Optional.of("Additional review required for API modifications")).setApplicabilityExpression(SubmitRequirementExpression.of("commit_filepath_contains(\"/api/.*\")")).setSubmittabilityExpression(SubmitRequirementExpression.create("label(api-review, +2)")).setOverrideExpression(SubmitRequirementExpression.of("label(build-cop-override, +1)")).setAllowOverrideInChildProjects(true).build());
}
use of com.google.gerrit.entities.SubmitRequirement in project gerrit by GerritCodeReview.
the class CheckSubmitRequirement method apply.
@Override
public Response<SubmitRequirementResultInfo> apply(ChangeResource resource, SubmitRequirementInput input) throws BadRequestException {
SubmitRequirement requirement = createSubmitRequirement(input);
SubmitRequirementResult res = evaluator.evaluateRequirement(requirement, resource.getChangeData());
return Response.ok(SubmitRequirementsJson.toInfo(requirement, res));
}
use of com.google.gerrit.entities.SubmitRequirement 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.SubmitRequirement 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.SubmitRequirement 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");
}
}
Aggregations