Search in sources :

Example 6 with SubmitRequirement

use of com.google.gerrit.entities.SubmitRequirement in project gerrit by GerritCodeReview.

the class ProjectConfig method loadSubmitRequirementSections.

private void loadSubmitRequirementSections(Config rc) {
    checkForUnsupportedSubmitRequirementParams(rc);
    Map<String, String> lowerNames = new HashMap<>();
    submitRequirementSections = new LinkedHashMap<>();
    for (String name : rc.getSubsections(SUBMIT_REQUIREMENT)) {
        String lower = name.toLowerCase();
        if (lowerNames.containsKey(lower)) {
            error(String.format("Submit requirement '%s' conflicts with '%s'.", name, lowerNames.get(lower)));
            continue;
        }
        lowerNames.put(lower, name);
        String description = rc.getString(SUBMIT_REQUIREMENT, name, KEY_SR_DESCRIPTION);
        String applicabilityExpr = rc.getString(SUBMIT_REQUIREMENT, name, KEY_SR_APPLICABILITY_EXPRESSION);
        String submittabilityExpr = rc.getString(SUBMIT_REQUIREMENT, name, KEY_SR_SUBMITTABILITY_EXPRESSION);
        String overrideExpr = rc.getString(SUBMIT_REQUIREMENT, name, KEY_SR_OVERRIDE_EXPRESSION);
        boolean canInherit;
        try {
            canInherit = rc.getBoolean(SUBMIT_REQUIREMENT, name, KEY_SR_OVERRIDE_IN_CHILD_PROJECTS, false);
        } catch (IllegalArgumentException e) {
            String canInheritValue = rc.getString(SUBMIT_REQUIREMENT, name, KEY_SR_OVERRIDE_IN_CHILD_PROJECTS);
            error(String.format("Invalid value %s.%s.%s for submit requirement '%s': %s", SUBMIT_REQUIREMENT, name, KEY_SR_OVERRIDE_IN_CHILD_PROJECTS, name, canInheritValue));
            continue;
        }
        if (submittabilityExpr == null) {
            error(String.format("Setting a submittability expression for submit requirement '%s' is required:" + " Missing %s.%s.%s", name, SUBMIT_REQUIREMENT, name, KEY_SR_SUBMITTABILITY_EXPRESSION));
            continue;
        }
        // The expressions are validated in SubmitRequirementExpressionsValidator.
        SubmitRequirement submitRequirement = SubmitRequirement.builder().setName(name).setDescription(Optional.ofNullable(description)).setApplicabilityExpression(SubmitRequirementExpression.of(applicabilityExpr)).setSubmittabilityExpression(SubmitRequirementExpression.create(submittabilityExpr)).setOverrideExpression(SubmitRequirementExpression.of(overrideExpr)).setAllowOverrideInChildProjects(canInherit).build();
        submitRequirementSections.put(name, submitRequirement);
    }
}
Also used : SubmitRequirement(com.google.gerrit.entities.SubmitRequirement) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap)

Example 7 with SubmitRequirement

use of com.google.gerrit.entities.SubmitRequirement in project gerrit by GerritCodeReview.

the class ProjectConfig method saveSubmitRequirementSections.

private void saveSubmitRequirementSections(Config rc) {
    unsetSection(rc, SUBMIT_REQUIREMENT);
    if (submitRequirementSections != null) {
        for (Map.Entry<String, SubmitRequirement> entry : submitRequirementSections.entrySet()) {
            String name = entry.getKey();
            SubmitRequirement sr = entry.getValue();
            if (sr.description().isPresent()) {
                rc.setString(SUBMIT_REQUIREMENT, name, KEY_SR_DESCRIPTION, sr.description().get());
            }
            if (sr.applicabilityExpression().isPresent()) {
                rc.setString(SUBMIT_REQUIREMENT, name, KEY_SR_APPLICABILITY_EXPRESSION, sr.applicabilityExpression().get().expressionString());
            }
            rc.setString(SUBMIT_REQUIREMENT, name, KEY_SR_SUBMITTABILITY_EXPRESSION, sr.submittabilityExpression().expressionString());
            if (sr.overrideExpression().isPresent()) {
                rc.setString(SUBMIT_REQUIREMENT, name, KEY_SR_OVERRIDE_EXPRESSION, sr.overrideExpression().get().expressionString());
            }
            rc.setBoolean(SUBMIT_REQUIREMENT, name, KEY_SR_OVERRIDE_IN_CHILD_PROJECTS, sr.allowOverrideInChildProjects());
        }
    }
}
Also used : SubmitRequirement(com.google.gerrit.entities.SubmitRequirement) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap)

Example 8 with SubmitRequirement

use of com.google.gerrit.entities.SubmitRequirement in project gerrit by GerritCodeReview.

the class ProjectState method getSubmitRequirements.

/**
 * Get all submit requirements for a project, including those from parent projects.
 */
public Map<String, SubmitRequirement> getSubmitRequirements() {
    Map<String, SubmitRequirement> requirements = new LinkedHashMap<>();
    for (ProjectState s : treeInOrder()) {
        for (SubmitRequirement requirement : s.getConfig().getSubmitRequirementSections().values()) {
            String lowerName = requirement.name().toLowerCase();
            SubmitRequirement old = requirements.get(lowerName);
            if (old == null || old.allowOverrideInChildProjects()) {
                requirements.put(lowerName, requirement);
            }
        }
    }
    return ImmutableMap.copyOf(requirements);
}
Also used : SubmitRequirement(com.google.gerrit.entities.SubmitRequirement) LinkedHashMap(java.util.LinkedHashMap)

Example 9 with SubmitRequirement

use of com.google.gerrit.entities.SubmitRequirement in project gerrit by GerritCodeReview.

the class SubmitRequirementExpressionsValidator method validateSubmitRequirementExpressions.

private ImmutableList<CommitValidationMessage> validateSubmitRequirementExpressions(Collection<SubmitRequirement> submitRequirements) {
    List<CommitValidationMessage> validationMessages = new ArrayList<>();
    for (SubmitRequirement submitRequirement : submitRequirements) {
        validateSubmitRequirementExpression(validationMessages, submitRequirement, submitRequirement.submittabilityExpression(), ProjectConfig.KEY_SR_SUBMITTABILITY_EXPRESSION);
        submitRequirement.applicabilityExpression().ifPresent(expression -> validateSubmitRequirementExpression(validationMessages, submitRequirement, expression, ProjectConfig.KEY_SR_APPLICABILITY_EXPRESSION));
        submitRequirement.overrideExpression().ifPresent(expression -> validateSubmitRequirementExpression(validationMessages, submitRequirement, expression, ProjectConfig.KEY_SR_OVERRIDE_EXPRESSION));
    }
    return ImmutableList.copyOf(validationMessages);
}
Also used : SubmitRequirement(com.google.gerrit.entities.SubmitRequirement) ArrayList(java.util.ArrayList) CommitValidationMessage(com.google.gerrit.server.git.validators.CommitValidationMessage)

Example 10 with SubmitRequirement

use of com.google.gerrit.entities.SubmitRequirement in project gerrit by GerritCodeReview.

the class SubmitRequirementIT method projectSubmitRequirementDuplicatesGlobal_overrideNotAllowed_globalEvaluated.

@Test
public void projectSubmitRequirementDuplicatesGlobal_overrideNotAllowed_globalEvaluated() throws Exception {
    SubmitRequirement globalSubmitRequirement = SubmitRequirement.builder().setName("CoDe-reView").setSubmittabilityExpression(SubmitRequirementExpression.create("topic:test")).setAllowOverrideInChildProjects(false).build();
    try (Registration registration = extensionRegistry.newRegistration().add(globalSubmitRequirement)) {
        configSubmitRequirement(project, SubmitRequirement.builder().setName("Code-Review").setSubmittabilityExpression(SubmitRequirementExpression.maxCodeReview()).setAllowOverrideInChildProjects(false).build());
        PushOneCommit.Result r = createChange();
        String changeId = r.getChangeId();
        // Vote does not satisfy submit requirement, because the global definition is evaluated.
        voteLabel(changeId, "CoDe-reView", 2);
        ChangeInfo change = gApi.changes().id(changeId).get();
        assertThat(change.submitRequirements).hasSize(2);
        assertSubmitRequirementStatus(change.submitRequirements, "CoDe-reView", Status.UNSATISFIED, /* isLegacy= */
        false);
        // In addition, the legacy submit requirement is emitted, since the status mismatch
        assertSubmitRequirementStatus(change.submitRequirements, "Code-Review", Status.SATISFIED, /* isLegacy= */
        true);
        // Setting the topic satisfies the global definition.
        gApi.changes().id(changeId).topic("test");
        change = gApi.changes().id(changeId).get();
        assertThat(change.submitRequirements).hasSize(1);
        assertSubmitRequirementStatus(change.submitRequirements, "CoDe-reView", Status.SATISFIED, /* isLegacy= */
        false);
    }
}
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) 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