Search in sources :

Example 21 with LabelType

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

the class SubmitRequirementIT method submitRequirement_returnsEmpty_forAbandonedChangeWithPreviouslyStoredSRs.

@Test
public void submitRequirement_returnsEmpty_forAbandonedChangeWithPreviouslyStoredSRs() throws Exception {
    for (SubmitType submitType : SubmitType.values()) {
        Project.NameKey project = createProjectForPush(submitType);
        TestRepository<InMemoryRepository> repo = cloneProject(project);
        configSubmitRequirement(project, SubmitRequirement.builder().setName("Code-Review").setSubmittabilityExpression(SubmitRequirementExpression.maxCodeReview()).setAllowOverrideInChildProjects(false).build());
        PushOneCommit.Result r = createChange(repo, "master", "Add a file", "foo", "content", "topic");
        String changeId = r.getChangeId();
        // Abandon change. Submit requirements get stored in the revision note of patch-set 1.
        gApi.changes().id(changeId).abandon();
        ChangeInfo change = gApi.changes().id(changeId).get();
        assertThat(change.submitRequirements).hasSize(1);
        assertSubmitRequirementStatus(change.submitRequirements, "Code-Review", Status.UNSATISFIED, /* isLegacy= */
        false);
        // Clear SRs for the project and update code-review label to be non-blocking.
        clearSubmitRequirements(project);
        LabelType cr = TestLabels.codeReview().toBuilder().setFunction(LabelFunction.NO_BLOCK).build();
        try (ProjectConfigUpdate u = updateProject(project)) {
            u.getConfig().upsertLabelType(cr);
            u.save();
        }
        // Restore the change. No SRs apply.
        gApi.changes().id(changeId).restore();
        change = gApi.changes().id(changeId).get();
        assertThat(change.submitRequirements).isEmpty();
        // Abandon the change. Still, no SRs apply.
        gApi.changes().id(changeId).abandon();
        change = gApi.changes().id(changeId).get();
        assertThat(change.submitRequirements).isEmpty();
    }
}
Also used : Project(com.google.gerrit.entities.Project) InMemoryRepository(org.eclipse.jgit.internal.storage.dfs.InMemoryRepository) ChangeInfo(com.google.gerrit.extensions.common.ChangeInfo) LabelType(com.google.gerrit.entities.LabelType) SubmitType(com.google.gerrit.extensions.client.SubmitType) PushOneCommit(com.google.gerrit.acceptance.PushOneCommit) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest) Test(org.junit.Test)

Example 22 with LabelType

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

the class SubmitRequirementIT method submitRequirements_submittedTogetherWithoutLegacySubmitRequirements.

@Test
public void submitRequirements_submittedTogetherWithoutLegacySubmitRequirements() throws Exception {
    // Add a code review submit requirement and mark the 'Code-Review' label function to be
    // non-blocking.
    configSubmitRequirement(allProjects, SubmitRequirement.builder().setName("Code-Review").setSubmittabilityExpression(SubmitRequirementExpression.maxCodeReview()).setAllowOverrideInChildProjects(true).build());
    LabelType cr = TestLabels.codeReview().toBuilder().setFunction(LabelFunction.NO_BLOCK).build();
    try (ProjectConfigUpdate u = updateProject(project)) {
        u.getConfig().upsertLabelType(cr);
        u.save();
    }
    // Create two changes in a chain.
    createChange();
    PushOneCommit.Result r2 = createChange();
    // Make sure the CR requirement is unsatisfied.
    String changeId = r2.getChangeId();
    ChangeInfo change = gApi.changes().id(changeId).get();
    assertThat(change.submitRequirements).hasSize(1);
    assertSubmitRequirementStatus(change.submitRequirements, "Code-Review", Status.UNSATISFIED, /* isLegacy= */
    false);
    List<ChangeInfo> changeInfos = gApi.changes().id(changeId).submittedTogether();
    assertThat(changeInfos).hasSize(2);
    assertThat(changeInfos.stream().map(c -> c.submittable).distinct().collect(MoreCollectors.onlyElement())).isFalse();
}
Also used : ChangeInfo(com.google.gerrit.extensions.common.ChangeInfo) LabelType(com.google.gerrit.entities.LabelType) PushOneCommit(com.google.gerrit.acceptance.PushOneCommit) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest) Test(org.junit.Test)

Example 23 with LabelType

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

the class ProjectConfigTest method readConfigLabelScores.

@Test
public void readConfigLabelScores() throws Exception {
    RevCommit rev = tr.commit().add("groups", group(developers)).add("project.config", "[label \"CustomLabel\"]\n" + LABEL_SCORES_CONFIG).create();
    ProjectConfig cfg = read(rev);
    Map<String, LabelType> labels = cfg.getLabelSections();
    LabelType type = labels.entrySet().iterator().next().getValue();
    assertThat(type.isCopyAnyScore()).isNotEqualTo(LabelType.DEF_COPY_ANY_SCORE);
    assertThat(type.isCopyMinScore()).isNotEqualTo(LabelType.DEF_COPY_MIN_SCORE);
    assertThat(type.isCopyMaxScore()).isNotEqualTo(LabelType.DEF_COPY_MAX_SCORE);
    assertThat(type.isCopyAllScoresIfListOfFilesDidNotChange()).isNotEqualTo(LabelType.DEF_COPY_ALL_SCORES_IF_LIST_OF_FILES_DID_NOT_CHANGE);
    assertThat(type.isCopyAllScoresOnMergeFirstParentUpdate()).isNotEqualTo(LabelType.DEF_COPY_ALL_SCORES_ON_MERGE_FIRST_PARENT_UPDATE);
    assertThat(type.isCopyAllScoresOnTrivialRebase()).isNotEqualTo(LabelType.DEF_COPY_ALL_SCORES_ON_TRIVIAL_REBASE);
    assertThat(type.isCopyAllScoresIfNoCodeChange()).isNotEqualTo(LabelType.DEF_COPY_ALL_SCORES_IF_NO_CODE_CHANGE);
    assertThat(type.isCopyAllScoresIfNoChange()).isNotEqualTo(LabelType.DEF_COPY_ALL_SCORES_IF_NO_CHANGE);
}
Also used : LabelType(com.google.gerrit.entities.LabelType) RevCommit(org.eclipse.jgit.revwalk.RevCommit) Test(org.junit.Test)

Example 24 with LabelType

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

the class SubmitRequirementsAdapterTest method setup.

@Before
public void setup() {
    LabelType codeReview = LabelType.builder("Code-Review", ImmutableList.of(LabelValue.create((short) 1, "Looks good to me"), LabelValue.create((short) 0, "No score"), LabelValue.create((short) -1, "I would prefer this is not merged as is"))).setFunction(LabelFunction.MAX_WITH_BLOCK).build();
    LabelType verified = LabelType.builder("Verified", ImmutableList.of(LabelValue.create((short) 1, "Looks good to me"), LabelValue.create((short) 0, "No score"), LabelValue.create((short) -1, "I would prefer this is not merged as is"))).setFunction(LabelFunction.MAX_NO_BLOCK).build();
    LabelType codeStyle = LabelType.builder("Code-Style", ImmutableList.of(LabelValue.create((short) 1, "Looks good to me"), LabelValue.create((short) 0, "No score"), LabelValue.create((short) -1, "I would prefer this is not merged as is"))).setFunction(LabelFunction.ANY_WITH_BLOCK).build();
    LabelType ignoreSelfApprovalLabel = LabelType.builder("ISA-Label", ImmutableList.of(LabelValue.create((short) 1, "Looks good to me"), LabelValue.create((short) 0, "No score"), LabelValue.create((short) -1, "I would prefer this is not merged as is"))).setFunction(LabelFunction.MAX_WITH_BLOCK).setIgnoreSelfApproval(true).build();
    labelTypes = Arrays.asList(codeReview, verified, codeStyle, ignoreSelfApprovalLabel);
}
Also used : LabelType(com.google.gerrit.entities.LabelType) Before(org.junit.Before)

Example 25 with LabelType

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

the class LabelsJson method permittedLabels.

/**
 * Returns A map of all label names and the values that the provided user has permission to vote
 * on.
 *
 * @param filterApprovalsBy a Gerrit user ID.
 * @param cd {@link ChangeData} corresponding to a specific gerrit change.
 * @return A Map where the key contain a label name, and the value is a list of the permissible
 *     vote values that the user can vote on.
 */
Map<String, Collection<String>> permittedLabels(Account.Id filterApprovalsBy, ChangeData cd) throws PermissionBackendException {
    SetMultimap<String, String> permitted = LinkedHashMultimap.create();
    boolean isMerged = cd.change().isMerged();
    Map<String, Short> currentUserVotes = currentLabels(filterApprovalsBy, cd);
    for (LabelType labelType : cd.getLabelTypes().getLabelTypes()) {
        if (isMerged && !labelType.isAllowPostSubmit()) {
            continue;
        }
        Set<LabelPermission.WithValue> can = permissionBackend.absentUser(filterApprovalsBy).change(cd).test(labelType);
        for (LabelValue v : labelType.getValues()) {
            boolean ok = can.contains(new LabelPermission.WithValue(labelType, v));
            if (isMerged) {
                // Votes cannot be decreased if the change is merged. Only accept the label value if it's
                // greater or equal than the user's latest vote.
                short prev = currentUserVotes.getOrDefault(labelType.getName(), (short) 0);
                ok &= v.getValue() >= prev;
            }
            if (ok) {
                permitted.put(labelType.getName(), v.formatValue());
            }
        }
    }
    clearOnlyZerosEntries(permitted);
    return permitted.asMap();
}
Also used : LabelValue(com.google.gerrit.entities.LabelValue) LabelType(com.google.gerrit.entities.LabelType) LabelPermission(com.google.gerrit.server.permissions.LabelPermission)

Aggregations

LabelType (com.google.gerrit.entities.LabelType)71 Test (org.junit.Test)26 PatchSetApproval (com.google.gerrit.entities.PatchSetApproval)20 AbstractDaemonTest (com.google.gerrit.acceptance.AbstractDaemonTest)16 PushOneCommit (com.google.gerrit.acceptance.PushOneCommit)14 ChangeInfo (com.google.gerrit.extensions.common.ChangeInfo)13 Map (java.util.Map)12 LabelTypes (com.google.gerrit.entities.LabelTypes)10 ReviewInput (com.google.gerrit.extensions.api.changes.ReviewInput)9 Account (com.google.gerrit.entities.Account)8 LabelValue (com.google.gerrit.entities.LabelValue)8 AuthException (com.google.gerrit.extensions.restapi.AuthException)8 BadRequestException (com.google.gerrit.extensions.restapi.BadRequestException)8 HashMap (java.util.HashMap)8 ProjectConfig (com.google.gerrit.server.project.ProjectConfig)7 ArrayList (java.util.ArrayList)7 LabelPermission (com.google.gerrit.server.permissions.LabelPermission)6 ProjectState (com.google.gerrit.server.project.ProjectState)6 Change (com.google.gerrit.entities.Change)5 SubmitRecord (com.google.gerrit.entities.SubmitRecord)5