Search in sources :

Example 21 with SubmitRequirement

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());
}
Also used : SubmitRequirement(com.google.gerrit.entities.SubmitRequirement) RevCommit(org.eclipse.jgit.revwalk.RevCommit) Test(org.junit.Test)

Example 22 with SubmitRequirement

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

Example 23 with SubmitRequirement

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);
}
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 24 with SubmitRequirement

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();
}
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 25 with SubmitRequirement

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");
    }
}
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)

Aggregations

SubmitRequirement (com.google.gerrit.entities.SubmitRequirement)39 SubmitRequirementResult (com.google.gerrit.entities.SubmitRequirementResult)27 Test (org.junit.Test)24 AbstractDaemonTest (com.google.gerrit.acceptance.AbstractDaemonTest)20 Registration (com.google.gerrit.acceptance.ExtensionRegistry.Registration)8 PushOneCommit (com.google.gerrit.acceptance.PushOneCommit)6 ChangeInfo (com.google.gerrit.extensions.common.ChangeInfo)6 LegacySubmitRequirement (com.google.gerrit.entities.LegacySubmitRequirement)5 ChangeData (com.google.gerrit.server.query.change.ChangeData)5 Map (java.util.Map)5 RevCommit (org.eclipse.jgit.revwalk.RevCommit)5 ImmutableList (com.google.common.collect.ImmutableList)4 ImmutableMap (com.google.common.collect.ImmutableMap)4 HashMap (java.util.HashMap)4 Label (com.google.gerrit.entities.SubmitRecord.Label)3 LinkedHashMap (java.util.LinkedHashMap)3 Optional (java.util.Optional)3 FluentLogger (com.google.common.flogger.FluentLogger)2 LabelType (com.google.gerrit.entities.LabelType)2 PatchSet (com.google.gerrit.entities.PatchSet)2