Search in sources :

Example 21 with SubmitRequirementResult

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);
}
Also used : ImmutableMap(com.google.common.collect.ImmutableMap) Inject(com.google.inject.Inject) Set(java.util.Set) StorageConstraint(com.google.gerrit.server.query.change.ChangeData.StorageConstraint) HashMap(java.util.HashMap) Collectors(java.util.stream.Collectors) SubmitRequirementResult(com.google.gerrit.entities.SubmitRequirementResult) ChangeData(com.google.gerrit.server.query.change.ChangeData) Description(com.google.gerrit.metrics.Description) Map(java.util.Map) Counter2(com.google.gerrit.metrics.Counter2) Field(com.google.gerrit.metrics.Field) MetricMaker(com.google.gerrit.metrics.MetricMaker) SubmitRequirement(com.google.gerrit.entities.SubmitRequirement) Metadata(com.google.gerrit.server.logging.Metadata) Singleton(com.google.inject.Singleton) SubmitRequirement(com.google.gerrit.entities.SubmitRequirement) HashMap(java.util.HashMap) SubmitRequirementResult(com.google.gerrit.entities.SubmitRequirementResult) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap) Map(java.util.Map)

Example 22 with SubmitRequirementResult

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();
}
Also used : Module(com.google.inject.Module) ProjectCache.illegalState(com.google.gerrit.server.project.ProjectCache.illegalState) ImmutableMap(com.google.common.collect.ImmutableMap) SubmitRequirementChangeQueryBuilder(com.google.gerrit.server.query.change.SubmitRequirementChangeQueryBuilder) Inject(com.google.inject.Inject) SubmitRequirementExpression(com.google.gerrit.entities.SubmitRequirementExpression) Scopes(com.google.inject.Scopes) Function(java.util.function.Function) SubmitRequirementExpressionResult(com.google.gerrit.entities.SubmitRequirementExpressionResult) SubmitRequirementResult(com.google.gerrit.entities.SubmitRequirementResult) ManualRequestContext(com.google.gerrit.server.util.ManualRequestContext) ImmutableMap.toImmutableMap(com.google.common.collect.ImmutableMap.toImmutableMap) ChangeData(com.google.gerrit.server.query.change.ChangeData) Provider(com.google.inject.Provider) Stream(java.util.stream.Stream) QueryParseException(com.google.gerrit.index.query.QueryParseException) Map(java.util.Map) PredicateResult(com.google.gerrit.entities.SubmitRequirementExpressionResult.PredicateResult) PluginSetContext(com.google.gerrit.server.plugincontext.PluginSetContext) Optional(java.util.Optional) OneOffRequestContext(com.google.gerrit.server.util.OneOffRequestContext) Predicate(com.google.gerrit.index.query.Predicate) SubmitRequirement(com.google.gerrit.entities.SubmitRequirement) AbstractModule(com.google.inject.AbstractModule) SubmitRequirement(com.google.gerrit.entities.SubmitRequirement) SubmitRequirementResult(com.google.gerrit.entities.SubmitRequirementResult) ImmutableMap(com.google.common.collect.ImmutableMap) ImmutableMap.toImmutableMap(com.google.common.collect.ImmutableMap.toImmutableMap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) ImmutableMap.toImmutableMap(com.google.common.collect.ImmutableMap.toImmutableMap)

Example 23 with SubmitRequirementResult

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");
    }
}
Also used : LegacySubmitRequirement(com.google.gerrit.entities.LegacySubmitRequirement) SubmitRequirement(com.google.gerrit.entities.SubmitRequirement) ChangeInfo(com.google.gerrit.extensions.common.ChangeInfo) Registration(com.google.gerrit.acceptance.ExtensionRegistry.Registration) SubmitRequirementResult(com.google.gerrit.entities.SubmitRequirementResult) VerifyNoPiiInChangeNotes(com.google.gerrit.acceptance.VerifyNoPiiInChangeNotes) ChangeNotes(com.google.gerrit.server.notedb.ChangeNotes) PushOneCommit(com.google.gerrit.acceptance.PushOneCommit) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest) Test(org.junit.Test)

Example 24 with SubmitRequirementResult

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();
}
Also used : SubmitRequirement(com.google.gerrit.entities.SubmitRequirement) SubmitRequirementResult(com.google.gerrit.entities.SubmitRequirementResult) Test(org.junit.Test) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest)

Example 25 with SubmitRequirementResult

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");
}
Also used : SubmitRequirement(com.google.gerrit.entities.SubmitRequirement) SubmitRequirementResult(com.google.gerrit.entities.SubmitRequirementResult) Test(org.junit.Test) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest)

Aggregations

SubmitRequirementResult (com.google.gerrit.entities.SubmitRequirementResult)47 Test (org.junit.Test)36 SubmitRequirement (com.google.gerrit.entities.SubmitRequirement)27 AbstractDaemonTest (com.google.gerrit.acceptance.AbstractDaemonTest)21 SubmitRecord (com.google.gerrit.entities.SubmitRecord)14 PushOneCommit (com.google.gerrit.acceptance.PushOneCommit)5 ChangeInfo (com.google.gerrit.extensions.common.ChangeInfo)5 ChangeNotes (com.google.gerrit.server.notedb.ChangeNotes)5 ChangeData (com.google.gerrit.server.query.change.ChangeData)5 Map (java.util.Map)5 ImmutableList (com.google.common.collect.ImmutableList)4 ImmutableMap (com.google.common.collect.ImmutableMap)4 Registration (com.google.gerrit.acceptance.ExtensionRegistry.Registration)3 VerifyNoPiiInChangeNotes (com.google.gerrit.acceptance.VerifyNoPiiInChangeNotes)3 Project (com.google.gerrit.entities.Project)3 Label (com.google.gerrit.entities.SubmitRecord.Label)3 SubmitType (com.google.gerrit.extensions.client.SubmitType)3 HashMap (java.util.HashMap)3 Optional (java.util.Optional)3 ObjectId (org.eclipse.jgit.lib.ObjectId)3