Search in sources :

Example 56 with LabelType

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

the class EqualsLabelPredicate method match.

@Override
public boolean match(ChangeData object) {
    Change c = object.change();
    if (c == null) {
        // 
        return false;
    }
    if (Integer.valueOf(0).equals(count)) {
        // is computationally expensive.
        return false;
    }
    Optional<ProjectState> project = projectCache.get(c.getDest().project());
    if (!project.isPresent()) {
        // 
        return false;
    }
    LabelType labelType = type(project.get().getLabelTypes(), label);
    if (labelType == null) {
        // Label is not defined by this project.
        return false;
    }
    boolean hasVote = false;
    int matchingVotes = 0;
    StorageConstraint currentStorageConstraint = object.getStorageConstraint();
    object.setStorageConstraint(ChangeData.StorageConstraint.INDEX_PRIMARY_NOTEDB_SECONDARY);
    for (PatchSetApproval p : object.currentApprovals()) {
        if (labelType.matches(p)) {
            hasVote = true;
            if (match(object, p.value(), p.accountId())) {
                matchingVotes += 1;
            }
        }
    }
    object.setStorageConstraint(currentStorageConstraint);
    if (!hasVote && expVal == 0) {
        return true;
    }
    return count == null ? matchingVotes >= 1 : matchingVotes == count;
}
Also used : LabelType(com.google.gerrit.entities.LabelType) ProjectState(com.google.gerrit.server.project.ProjectState) Change(com.google.gerrit.entities.Change) StorageConstraint(com.google.gerrit.server.query.change.ChangeData.StorageConstraint) PatchSetApproval(com.google.gerrit.entities.PatchSetApproval) StorageConstraint(com.google.gerrit.server.query.change.ChangeData.StorageConstraint)

Example 57 with LabelType

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

the class PostReview method checkLabels.

private void checkLabels(RevisionResource rsrc, LabelTypes labelTypes, Map<String, Short> labels) throws BadRequestException, AuthException, PermissionBackendException {
    logger.atFine().log("checking label input: %s", labels);
    PermissionBackend.ForChange perm = rsrc.permissions();
    Iterator<Map.Entry<String, Short>> itr = labels.entrySet().iterator();
    while (itr.hasNext()) {
        Map.Entry<String, Short> ent = itr.next();
        Optional<LabelType> lt = labelTypes.byLabel(ent.getKey());
        if (!lt.isPresent()) {
            logger.atFine().log("label %s not found", ent.getKey());
            if (strictLabels) {
                throw new BadRequestException(String.format("label \"%s\" is not a configured label", ent.getKey()));
            }
            logger.atFine().log("ignoring input for unknown label %s", ent.getKey());
            itr.remove();
            continue;
        }
        if (ent.getValue() == null || ent.getValue() == 0) {
            // Later null/0 will be deleted and revoke the label.
            continue;
        }
        if (lt.get().getValue(ent.getValue()) == null) {
            logger.atFine().log("label value %s not found", ent.getValue());
            if (strictLabels) {
                throw new BadRequestException(String.format("label \"%s\": %d is not a valid value", ent.getKey(), ent.getValue()));
            }
            logger.atFine().log("ignoring input for label %s because label value is unknown", ent.getKey());
            itr.remove();
            continue;
        }
        short val = ent.getValue();
        try {
            perm.check(new LabelPermission.WithValue(lt.get(), val));
        } catch (AuthException e) {
            throw new AuthException(String.format("Applying label \"%s\": %d is restricted", lt.get().getName(), val), e);
        }
    }
}
Also used : PermissionBackend(com.google.gerrit.server.permissions.PermissionBackend) AuthException(com.google.gerrit.extensions.restapi.AuthException) LabelType(com.google.gerrit.entities.LabelType) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) Map(java.util.Map) HashMap(java.util.HashMap) LabelPermission(com.google.gerrit.server.permissions.LabelPermission)

Example 58 with LabelType

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

the class ChangeIT method nonVotingReviewerStaysAfterSubmit.

@Test
public void nonVotingReviewerStaysAfterSubmit() throws Exception {
    LabelType verified = label(LabelId.VERIFIED, value(1, "Passes"), value(0, "No score"), value(-1, "Failed"));
    String heads = "refs/heads/*";
    try (ProjectConfigUpdate u = updateProject(project)) {
        u.getConfig().upsertLabelType(verified);
        u.save();
    }
    projectOperations.project(project).forUpdate().add(allowLabel(verified.getName()).ref(heads).group(CHANGE_OWNER).range(-1, 1)).add(allowLabel(LabelId.CODE_REVIEW).ref(heads).group(REGISTERED_USERS).range(-2, +2)).update();
    // Set Code-Review+2 and Verified+1 as admin (change owner)
    PushOneCommit.Result r = createChange();
    String changeId = r.getChangeId();
    String commit = r.getCommit().name();
    ReviewInput input = ReviewInput.approve();
    input.label(verified.getName(), 1);
    gApi.changes().id(changeId).revision(commit).review(input);
    // Reviewers should only be "admin"
    ChangeInfo c = gApi.changes().id(changeId).get();
    assertThat(getReviewers(c.reviewers.get(REVIEWER))).containsExactlyElementsIn(ImmutableSet.of(admin.id()));
    assertThat(c.reviewers.get(CC)).isNull();
    // Add the user as reviewer
    ReviewerInput in = new ReviewerInput();
    in.reviewer = user.email();
    gApi.changes().id(changeId).addReviewer(in);
    c = gApi.changes().id(changeId).get();
    assertThat(getReviewers(c.reviewers.get(REVIEWER))).containsExactlyElementsIn(ImmutableSet.of(admin.id(), user.id()));
    // Approve the change as user, then remove the approval
    // (only to confirm that the user does have Code-Review+2 permission)
    requestScopeOperations.setApiUser(user.id());
    gApi.changes().id(changeId).revision(commit).review(ReviewInput.approve());
    gApi.changes().id(changeId).revision(commit).review(ReviewInput.noScore());
    // Submit the change
    requestScopeOperations.setApiUser(admin.id());
    gApi.changes().id(changeId).revision(commit).submit();
    // User should still be on the change
    c = gApi.changes().id(changeId).get();
    assertThat(getReviewers(c.reviewers.get(REVIEWER))).containsExactlyElementsIn(ImmutableSet.of(admin.id(), user.id()));
}
Also used : ChangeInfo(com.google.gerrit.extensions.common.ChangeInfo) LabelType(com.google.gerrit.entities.LabelType) ReviewInput(com.google.gerrit.extensions.api.changes.ReviewInput) PushOneCommit(com.google.gerrit.acceptance.PushOneCommit) DeleteReviewerInput(com.google.gerrit.extensions.api.changes.DeleteReviewerInput) ReviewerInput(com.google.gerrit.extensions.api.changes.ReviewerInput) Test(org.junit.Test) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest)

Example 59 with LabelType

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

the class ChangeIT method checkLabelsForMergedChange.

@Test
public void checkLabelsForMergedChange() throws Exception {
    PushOneCommit.Result r = createChange();
    gApi.changes().id(r.getChangeId()).revision(r.getCommit().name()).review(ReviewInput.approve());
    gApi.changes().id(r.getChangeId()).revision(r.getCommit().name()).submit();
    ChangeInfo change = gApi.changes().id(r.getChangeId()).get();
    assertThat(change.status).isEqualTo(MERGED);
    assertThat(change.submissionId).isNotNull();
    assertThat(change.labels.keySet()).containsExactly(LabelId.CODE_REVIEW);
    assertThat(change.permittedLabels.keySet()).containsExactly(LabelId.CODE_REVIEW);
    assertPermitted(change, LabelId.CODE_REVIEW, 2);
    LabelType verified = TestLabels.verified();
    AccountGroup.UUID registeredUsers = systemGroupBackend.getGroup(REGISTERED_USERS).getUUID();
    String heads = RefNames.REFS_HEADS + "*";
    // add new label and assert that it's returned for existing changes
    try (ProjectConfigUpdate u = updateProject(project)) {
        u.getConfig().upsertLabelType(verified);
        u.save();
    }
    projectOperations.project(project).forUpdate().add(allowLabel(verified.getName()).ref(heads).group(registeredUsers).range(-1, 1)).update();
    change = gApi.changes().id(r.getChangeId()).get();
    assertThat(change.labels.keySet()).containsExactly(LabelId.CODE_REVIEW, LabelId.VERIFIED);
    assertThat(change.permittedLabels.keySet()).containsExactly(LabelId.CODE_REVIEW, LabelId.VERIFIED);
    assertPermitted(change, LabelId.CODE_REVIEW, 2);
    assertPermitted(change, LabelId.VERIFIED, 0, 1);
    assertLabelDescription(change, LabelId.VERIFIED, TestLabels.VERIFIED_LABEL_DESCRIPTION);
    // Ignore the new label by Prolog submit rule. Permitted ranges are still going to be
    // returned for the label.
    GitUtil.fetch(testRepo, RefNames.REFS_CONFIG + ":config");
    testRepo.reset("config");
    PushOneCommit push2 = pushFactory.create(admin.newIdent(), testRepo, "Ignore Verified", "rules.pl", "submit_rule(submit(CR)) :-\n  gerrit:max_with_block(-2, 2, 'Code-Review', CR).");
    push2.to(RefNames.REFS_CONFIG);
    change = gApi.changes().id(r.getChangeId()).get();
    assertPermitted(change, LabelId.CODE_REVIEW, 2);
    assertPermitted(change, LabelId.VERIFIED, 0, 1);
    // add an approval on the new label. The label can still be voted +1 although it is ignored
    // in Prolog. 0 is not permitted because votes cannot be decreased.
    gApi.changes().id(r.getChangeId()).revision(r.getCommit().name()).review(new ReviewInput().label(verified.getName(), verified.getMax().getValue()));
    change = gApi.changes().id(r.getChangeId()).get();
    assertThat(change.labels.keySet()).containsExactly(LabelId.CODE_REVIEW, LabelId.VERIFIED);
    assertPermitted(change, LabelId.CODE_REVIEW, 2);
    assertPermitted(change, LabelId.VERIFIED, 1);
    // changes, even if there is an approval for it
    try (ProjectConfigUpdate u = updateProject(project)) {
        u.getConfig().getLabelSections().remove(verified.getName());
        u.save();
    }
    projectOperations.project(project).forUpdate().remove(permissionKey(verified.getName()).ref(heads).group(registeredUsers)).update();
    change = gApi.changes().id(r.getChangeId()).get();
    assertThat(change.labels.keySet()).containsExactly(LabelId.CODE_REVIEW);
    assertThat(change.permittedLabels.keySet()).containsExactly(LabelId.CODE_REVIEW);
    assertPermitted(change, LabelId.CODE_REVIEW, 2);
}
Also used : ChangeInfo(com.google.gerrit.extensions.common.ChangeInfo) AccountGroup(com.google.gerrit.entities.AccountGroup) LabelType(com.google.gerrit.entities.LabelType) ReviewInput(com.google.gerrit.extensions.api.changes.ReviewInput) PushOneCommit(com.google.gerrit.acceptance.PushOneCommit) Test(org.junit.Test) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest)

Example 60 with LabelType

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

the class ChangeIT method checkLabelsForUnsubmittedChange.

@Test
public void checkLabelsForUnsubmittedChange() throws Exception {
    PushOneCommit.Result r = createChange();
    ChangeInfo change = gApi.changes().id(r.getChangeId()).get();
    assertThat(change.status).isEqualTo(ChangeStatus.NEW);
    assertThat(change.labels.keySet()).containsExactly(LabelId.CODE_REVIEW);
    assertThat(change.permittedLabels.keySet()).containsExactly(LabelId.CODE_REVIEW);
    // add new label and assert that it's returned for existing changes
    AccountGroup.UUID registeredUsers = systemGroupBackend.getGroup(REGISTERED_USERS).getUUID();
    LabelType verified = TestLabels.verified();
    String heads = RefNames.REFS_HEADS + "*";
    try (ProjectConfigUpdate u = updateProject(project)) {
        u.getConfig().upsertLabelType(verified);
        u.save();
    }
    projectOperations.project(project).forUpdate().add(allowLabel(verified.getName()).ref(heads).group(registeredUsers).range(-1, 1)).update();
    change = gApi.changes().id(r.getChangeId()).get();
    assertThat(change.labels.keySet()).containsExactly(LabelId.CODE_REVIEW, LabelId.VERIFIED);
    assertThat(change.permittedLabels.keySet()).containsExactly(LabelId.CODE_REVIEW, LabelId.VERIFIED);
    assertPermitted(change, LabelId.CODE_REVIEW, -2, -1, 0, 1, 2);
    assertPermitted(change, LabelId.VERIFIED, -1, 0, 1);
    assertLabelDescription(change, LabelId.VERIFIED, TestLabels.VERIFIED_LABEL_DESCRIPTION);
    // add an approval on the new label
    gApi.changes().id(r.getChangeId()).revision(r.getCommit().name()).review(new ReviewInput().label(verified.getName(), verified.getMax().getValue()));
    try (ProjectConfigUpdate u = updateProject(project)) {
        // remove label and assert that it's no longer returned for existing
        // changes, even if there is an approval for it
        u.getConfig().getLabelSections().remove(verified.getName());
        u.save();
    }
    projectOperations.project(project).forUpdate().remove(permissionKey(Permission.forLabel(verified.getName())).ref(heads).group(registeredUsers)).update();
    change = gApi.changes().id(r.getChangeId()).get();
    assertThat(change.labels.keySet()).containsExactly(LabelId.CODE_REVIEW);
    assertThat(change.permittedLabels.keySet()).containsExactly(LabelId.CODE_REVIEW);
    // abandon the change and see that the returned labels stay the same
    // while all permitted labels disappear.
    gApi.changes().id(r.getChangeId()).abandon();
    change = gApi.changes().id(r.getChangeId()).get();
    assertThat(change.status).isEqualTo(ChangeStatus.ABANDONED);
    assertThat(change.labels.keySet()).containsExactly(LabelId.CODE_REVIEW);
    assertThat(change.permittedLabels).isEmpty();
}
Also used : ChangeInfo(com.google.gerrit.extensions.common.ChangeInfo) AccountGroup(com.google.gerrit.entities.AccountGroup) LabelType(com.google.gerrit.entities.LabelType) ReviewInput(com.google.gerrit.extensions.api.changes.ReviewInput) PushOneCommit(com.google.gerrit.acceptance.PushOneCommit) Test(org.junit.Test) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest)

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