Search in sources :

Example 11 with ApprovalInfo

use of com.google.gerrit.extensions.common.ApprovalInfo in project gerrit by GerritCodeReview.

the class ChangeReviewersIT method reviewerReplyWithoutVote.

@Test
public void reviewerReplyWithoutVote() throws Exception {
    // Create change owned by admin.
    PushOneCommit.Result r = createChange();
    // Verify reviewer state.
    ChangeInfo c = gApi.changes().id(r.getChangeId()).get();
    assertReviewers(c, REVIEWER);
    assertReviewers(c, CC);
    LabelInfo label = c.labels.get("Code-Review");
    assertThat(label).isNotNull();
    assertThat(label.all).isNull();
    // Add user as REVIEWER.
    ReviewInput input = new ReviewInput().reviewer(user.username);
    ReviewResult result = review(r.getChangeId(), r.getCommit().name(), input);
    assertThat(result.labels).isNull();
    assertThat(result.reviewers).isNotNull();
    assertThat(result.reviewers).hasSize(1);
    // Verify reviewer state. Both admin and user should be REVIEWERs now,
    // because admin gets forced into REVIEWER state by virtue of being owner.
    c = gApi.changes().id(r.getChangeId()).get();
    assertReviewers(c, REVIEWER, admin, user);
    assertReviewers(c, CC);
    label = c.labels.get("Code-Review");
    assertThat(label).isNotNull();
    assertThat(label.all).isNotNull();
    assertThat(label.all).hasSize(2);
    Map<Integer, Integer> approvals = new HashMap<>();
    for (ApprovalInfo approval : label.all) {
        approvals.put(approval._accountId, approval.value);
    }
    assertThat(approvals).containsEntry(admin.getId().get(), 0);
    assertThat(approvals).containsEntry(user.getId().get(), 0);
    // Comment as user without voting. This should delete the approval and
    // then replace it with the default value.
    input = new ReviewInput().message("hello");
    RestResponse resp = userRestSession.post("/changes/" + r.getChangeId() + "/revisions/" + r.getCommit().getName() + "/review", input);
    result = readContentFromJson(resp, 200, ReviewResult.class);
    assertThat(result.labels).isNull();
    // Verify reviewer state.
    c = gApi.changes().id(r.getChangeId()).get();
    assertReviewers(c, REVIEWER, admin, user);
    assertReviewers(c, CC);
    label = c.labels.get("Code-Review");
    assertThat(label).isNotNull();
    assertThat(label.all).isNotNull();
    assertThat(label.all).hasSize(2);
    approvals.clear();
    for (ApprovalInfo approval : label.all) {
        approvals.put(approval._accountId, approval.value);
    }
    assertThat(approvals).containsEntry(admin.getId().get(), 0);
    assertThat(approvals).containsEntry(user.getId().get(), 0);
}
Also used : LabelInfo(com.google.gerrit.extensions.common.LabelInfo) ApprovalInfo(com.google.gerrit.extensions.common.ApprovalInfo) ChangeInfo(com.google.gerrit.extensions.common.ChangeInfo) HashMap(java.util.HashMap) RestResponse(com.google.gerrit.acceptance.RestResponse) ReviewResult(com.google.gerrit.extensions.api.changes.ReviewResult) 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 12 with ApprovalInfo

use of com.google.gerrit.extensions.common.ApprovalInfo in project gerrit by GerritCodeReview.

the class ChangeReviewersIT method addSelfAsReviewer.

@Test
public void addSelfAsReviewer() throws Exception {
    // Create change owned by admin.
    PushOneCommit.Result r = createChange();
    // user adds self as REVIEWER.
    ReviewInput input = new ReviewInput().reviewer(user.username);
    RestResponse resp = userRestSession.post("/changes/" + r.getChangeId() + "/revisions/" + r.getCommit().getName() + "/review", input);
    ReviewResult result = readContentFromJson(resp, 200, ReviewResult.class);
    assertThat(result.labels).isNull();
    assertThat(result.reviewers).isNotNull();
    assertThat(result.reviewers).hasSize(1);
    // Verify reviewer state.
    ChangeInfo c = gApi.changes().id(r.getChangeId()).get();
    assertReviewers(c, REVIEWER, user);
    assertReviewers(c, CC);
    LabelInfo label = c.labels.get("Code-Review");
    assertThat(label).isNotNull();
    assertThat(label.all).isNotNull();
    assertThat(label.all).hasSize(1);
    ApprovalInfo approval = label.all.get(0);
    assertThat(approval._accountId).isEqualTo(user.getId().get());
}
Also used : LabelInfo(com.google.gerrit.extensions.common.LabelInfo) ApprovalInfo(com.google.gerrit.extensions.common.ApprovalInfo) ChangeInfo(com.google.gerrit.extensions.common.ChangeInfo) RestResponse(com.google.gerrit.acceptance.RestResponse) ReviewResult(com.google.gerrit.extensions.api.changes.ReviewResult) 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 13 with ApprovalInfo

use of com.google.gerrit.extensions.common.ApprovalInfo in project gerrit by GerritCodeReview.

the class CommentAddedEventIT method getApprovalValues.

/* Need to lookup info for the label under test since there can be multiple
   * labels defined.  By default Gerrit already has a Code-Review label.
   */
private ApprovalValues getApprovalValues(LabelType label) {
    ApprovalValues res = new ApprovalValues();
    ApprovalInfo info = lastCommentAddedEvent.getApprovals().get(label.getName());
    if (info != null) {
        res.value = info.value;
    }
    info = lastCommentAddedEvent.getOldApprovals().get(label.getName());
    if (info != null) {
        res.oldValue = info.value;
    }
    return res;
}
Also used : ApprovalInfo(com.google.gerrit.extensions.common.ApprovalInfo)

Example 14 with ApprovalInfo

use of com.google.gerrit.extensions.common.ApprovalInfo in project gerrit by GerritCodeReview.

the class NoteDbPrimaryIT method updateChange.

@Test
public void updateChange() throws Exception {
    PushOneCommit.Result r = createChange();
    Change.Id id = r.getChange().getId();
    setNoteDbPrimary(id);
    gApi.changes().id(id.get()).current().review(ReviewInput.approve());
    gApi.changes().id(id.get()).current().submit();
    ChangeInfo info = gApi.changes().id(id.get()).get();
    assertThat(info.status).isEqualTo(ChangeStatus.MERGED);
    ApprovalInfo approval = Iterables.getOnlyElement(info.labels.get("Code-Review").all);
    assertThat(approval._accountId).isEqualTo(admin.id.get());
    assertThat(approval.value).isEqualTo(2);
    assertThat(info.messages).hasSize(3);
    assertThat(Iterables.getLast(info.messages).message).isEqualTo("Change has been successfully merged by " + admin.fullName);
    ChangeNotes notes = notesFactory.create(db, project, id);
    assertThat(notes.getChange().getStatus()).isEqualTo(Change.Status.MERGED);
    assertThat(notes.getChange().getNoteDbState()).isEqualTo(NoteDbChangeState.NOTE_DB_PRIMARY_STATE);
    // Writes weren't reflected in ReviewDb.
    assertThat(db.changes().get(id).getStatus()).isEqualTo(Change.Status.NEW);
    assertThat(db.patchSetApprovals().byChange(id)).isEmpty();
    assertThat(db.changeMessages().byChange(id)).hasSize(1);
}
Also used : ApprovalInfo(com.google.gerrit.extensions.common.ApprovalInfo) ChangeInfo(com.google.gerrit.extensions.common.ChangeInfo) Change(com.google.gerrit.reviewdb.client.Change) ChangeNotes(com.google.gerrit.server.notedb.ChangeNotes) PushOneCommit(com.google.gerrit.acceptance.PushOneCommit) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest) Test(org.junit.Test)

Example 15 with ApprovalInfo

use of com.google.gerrit.extensions.common.ApprovalInfo in project gerrit by GerritCodeReview.

the class ChangeJson method removableReviewers.

private Collection<AccountInfo> removableReviewers(ChangeControl ctl, ChangeInfo out) {
    // Although this is called removableReviewers, this method also determines
    // which CCs are removable.
    //
    // For reviewers, we need to look at each approval, because the reviewer
    // should only be considered removable if *all* of their approvals can be
    // removed. First, add all reviewers with *any* removable approval to the
    // "removable" set. Along the way, if we encounter a non-removable approval,
    // add the reviewer to the "fixed" set. Before we return, remove all members
    // of "fixed" from "removable", because not all of their approvals can be
    // removed.
    Collection<LabelInfo> labels = out.labels.values();
    Set<Account.Id> fixed = Sets.newHashSetWithExpectedSize(labels.size());
    Set<Account.Id> removable = Sets.newHashSetWithExpectedSize(labels.size());
    for (LabelInfo label : labels) {
        if (label.all == null) {
            continue;
        }
        for (ApprovalInfo ai : label.all) {
            Account.Id id = new Account.Id(ai._accountId);
            if (ctl.canRemoveReviewer(id, MoreObjects.firstNonNull(ai.value, 0))) {
                removable.add(id);
            } else {
                fixed.add(id);
            }
        }
    }
    // CCs are simpler than reviewers. They are removable if the ChangeControl
    // would permit a non-negative approval by that account to be removed, in
    // which case add them to removable. We don't need to add unremovable CCs to
    // "fixed" because we only visit each CC once here.
    Collection<AccountInfo> ccs = out.reviewers.get(ReviewerState.CC);
    if (ccs != null) {
        for (AccountInfo ai : ccs) {
            if (ai._accountId != null) {
                Account.Id id = new Account.Id(ai._accountId);
                if (ctl.canRemoveReviewer(id, 0)) {
                    removable.add(id);
                }
            }
        }
    }
    // Subtract any reviewers with non-removable approvals from the "removable"
    // set. This also subtracts any CCs that for some reason also hold
    // unremovable approvals.
    removable.removeAll(fixed);
    List<AccountInfo> result = Lists.newArrayListWithCapacity(removable.size());
    for (Account.Id id : removable) {
        result.add(accountLoader.get(id));
    }
    // Reviewers added by email are always removable
    for (Collection<AccountInfo> infos : out.reviewers.values()) {
        for (AccountInfo info : infos) {
            if (info._accountId == null) {
                result.add(info);
            }
        }
    }
    return result;
}
Also used : LabelInfo(com.google.gerrit.extensions.common.LabelInfo) ApprovalInfo(com.google.gerrit.extensions.common.ApprovalInfo) Account(com.google.gerrit.reviewdb.client.Account) ObjectId(org.eclipse.jgit.lib.ObjectId) AccountInfo(com.google.gerrit.extensions.common.AccountInfo)

Aggregations

ApprovalInfo (com.google.gerrit.extensions.common.ApprovalInfo)16 AbstractDaemonTest (com.google.gerrit.acceptance.AbstractDaemonTest)10 ChangeInfo (com.google.gerrit.extensions.common.ChangeInfo)10 Test (org.junit.Test)10 PushOneCommit (com.google.gerrit.acceptance.PushOneCommit)9 LabelInfo (com.google.gerrit.extensions.common.LabelInfo)9 ReviewInput (com.google.gerrit.extensions.api.changes.ReviewInput)5 RestResponse (com.google.gerrit.acceptance.RestResponse)4 ReviewResult (com.google.gerrit.extensions.api.changes.ReviewResult)3 AccountInfo (com.google.gerrit.extensions.common.AccountInfo)3 Account (com.google.gerrit.reviewdb.client.Account)3 Change (com.google.gerrit.reviewdb.client.Change)3 ProjectConfig (com.google.gerrit.server.git.ProjectConfig)3 HashMap (java.util.HashMap)3 ObjectId (org.eclipse.jgit.lib.ObjectId)3 Joiner (com.google.common.base.Joiner)2 ImmutableMap (com.google.common.collect.ImmutableMap)2 ImmutableSet (com.google.common.collect.ImmutableSet)2 Iterables (com.google.common.collect.Iterables)2 ListChangesOption (com.google.gerrit.extensions.client.ListChangesOption)2