Search in sources :

Example 1 with PatchSetApproval

use of com.google.gerrit.reviewdb.client.PatchSetApproval in project gerrit by GerritCodeReview.

the class ChangeRebuilderIT method rebuildEntitiesCreatedByImpersonation.

@Test
public void rebuildEntitiesCreatedByImpersonation() throws Exception {
    PushOneCommit.Result r = createChange();
    Change.Id id = r.getPatchSetId().getParentKey();
    PatchSet.Id psId = new PatchSet.Id(id, 1);
    String prefix = "/changes/" + id + "/revisions/current/";
    // For each of the entities that have a real user field, create one entity
    // without impersonation and one with.
    CommentInput ci = new CommentInput();
    ci.path = Patch.COMMIT_MSG;
    ci.side = Side.REVISION;
    ci.line = 1;
    ci.message = "comment without impersonation";
    ReviewInput ri = new ReviewInput();
    ri.label("Code-Review", -1);
    ri.message = "message without impersonation";
    ri.drafts = DraftHandling.KEEP;
    ri.comments = ImmutableMap.of(ci.path, ImmutableList.of(ci));
    userRestSession.post(prefix + "review", ri).assertOK();
    DraftInput di = new DraftInput();
    di.path = Patch.COMMIT_MSG;
    di.side = Side.REVISION;
    di.line = 1;
    di.message = "draft without impersonation";
    userRestSession.put(prefix + "drafts", di).assertCreated();
    allowRunAs();
    try {
        Header runAs = new BasicHeader("X-Gerrit-RunAs", user.id.toString());
        ci.message = "comment with impersonation";
        ri.message = "message with impersonation";
        ri.label("Code-Review", 1);
        adminRestSession.postWithHeader(prefix + "review", ri, runAs).assertOK();
        di.message = "draft with impersonation";
        adminRestSession.putWithHeader(prefix + "drafts", runAs, di).assertCreated();
    } finally {
        removeRunAs();
    }
    List<ChangeMessage> msgs = Ordering.natural().onResultOf(ChangeMessage::getWrittenOn).sortedCopy(db.changeMessages().byChange(id));
    assertThat(msgs).hasSize(3);
    assertThat(msgs.get(1).getMessage()).endsWith("message without impersonation");
    assertThat(msgs.get(1).getAuthor()).isEqualTo(user.id);
    assertThat(msgs.get(1).getRealAuthor()).isEqualTo(user.id);
    assertThat(msgs.get(2).getMessage()).endsWith("message with impersonation");
    assertThat(msgs.get(2).getAuthor()).isEqualTo(user.id);
    assertThat(msgs.get(2).getRealAuthor()).isEqualTo(admin.id);
    List<PatchSetApproval> psas = db.patchSetApprovals().byChange(id).toList();
    assertThat(psas).hasSize(1);
    assertThat(psas.get(0).getLabel()).isEqualTo("Code-Review");
    assertThat(psas.get(0).getValue()).isEqualTo(1);
    assertThat(psas.get(0).getAccountId()).isEqualTo(user.id);
    assertThat(psas.get(0).getRealAccountId()).isEqualTo(admin.id);
    Ordering<PatchLineComment> commentOrder = Ordering.natural().onResultOf(PatchLineComment::getWrittenOn);
    List<PatchLineComment> drafts = commentOrder.sortedCopy(db.patchComments().draftByPatchSetAuthor(psId, user.id));
    assertThat(drafts).hasSize(2);
    assertThat(drafts.get(0).getMessage()).isEqualTo("draft without impersonation");
    assertThat(drafts.get(0).getAuthor()).isEqualTo(user.id);
    assertThat(drafts.get(0).getRealAuthor()).isEqualTo(user.id);
    assertThat(drafts.get(1).getMessage()).isEqualTo("draft with impersonation");
    assertThat(drafts.get(1).getAuthor()).isEqualTo(user.id);
    assertThat(drafts.get(1).getRealAuthor()).isEqualTo(admin.id);
    List<PatchLineComment> pub = commentOrder.sortedCopy(db.patchComments().publishedByPatchSet(psId));
    assertThat(pub).hasSize(2);
    assertThat(pub.get(0).getMessage()).isEqualTo("comment without impersonation");
    assertThat(pub.get(0).getAuthor()).isEqualTo(user.id);
    assertThat(pub.get(0).getRealAuthor()).isEqualTo(user.id);
    assertThat(pub.get(1).getMessage()).isEqualTo("comment with impersonation");
    assertThat(pub.get(1).getAuthor()).isEqualTo(user.id);
    assertThat(pub.get(1).getRealAuthor()).isEqualTo(admin.id);
}
Also used : PatchSet(com.google.gerrit.reviewdb.client.PatchSet) Change(com.google.gerrit.reviewdb.client.Change) ReviewInput(com.google.gerrit.extensions.api.changes.ReviewInput) PatchSetApproval(com.google.gerrit.reviewdb.client.PatchSetApproval) PushOneCommit(com.google.gerrit.acceptance.PushOneCommit) PatchLineComment(com.google.gerrit.reviewdb.client.PatchLineComment) Header(org.apache.http.Header) BasicHeader(org.apache.http.message.BasicHeader) CommentInput(com.google.gerrit.extensions.api.changes.ReviewInput.CommentInput) ChangeMessage(com.google.gerrit.reviewdb.client.ChangeMessage) DraftInput(com.google.gerrit.extensions.api.changes.DraftInput) ObjectId(org.eclipse.jgit.lib.ObjectId) BasicHeader(org.apache.http.message.BasicHeader) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest) Test(org.junit.Test)

Example 2 with PatchSetApproval

use of com.google.gerrit.reviewdb.client.PatchSetApproval in project gerrit by GerritCodeReview.

the class ReviewerRecommender method baseRankingForEmptyQuery.

private Map<Account.Id, MutableDouble> baseRankingForEmptyQuery(double baseWeight) throws OrmException {
    // Get the user's last 25 changes, check approvals
    try {
        List<ChangeData> result = internalChangeQuery.setLimit(25).setRequestedFields(ImmutableSet.of(ChangeField.REVIEWER.getName())).query(changeQueryBuilder.owner("self"));
        Map<Account.Id, MutableDouble> suggestions = new HashMap<>();
        for (ChangeData cd : result) {
            for (PatchSetApproval approval : cd.currentApprovals()) {
                Account.Id id = approval.getAccountId();
                if (suggestions.containsKey(id)) {
                    suggestions.get(id).add(baseWeight);
                } else {
                    suggestions.put(id, new MutableDouble(baseWeight));
                }
            }
        }
        return suggestions;
    } catch (QueryParseException e) {
        // Unhandled, because owner:self will never provoke a QueryParseException
        log.error("Exception while suggesting reviewers", e);
        return ImmutableMap.of();
    }
}
Also used : Account(com.google.gerrit.reviewdb.client.Account) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) MutableDouble(org.apache.commons.lang.mutable.MutableDouble) ChangeData(com.google.gerrit.server.query.change.ChangeData) PatchSetApproval(com.google.gerrit.reviewdb.client.PatchSetApproval) QueryParseException(com.google.gerrit.server.query.QueryParseException)

Example 3 with PatchSetApproval

use of com.google.gerrit.reviewdb.client.PatchSetApproval in project gerrit by GerritCodeReview.

the class ReviewerSet method fromApprovals.

public static ReviewerSet fromApprovals(Iterable<PatchSetApproval> approvals) {
    PatchSetApproval first = null;
    Table<ReviewerStateInternal, Account.Id, Timestamp> reviewers = HashBasedTable.create();
    for (PatchSetApproval psa : approvals) {
        if (first == null) {
            first = psa;
        } else {
            checkArgument(first.getKey().getParentKey().getParentKey().equals(psa.getKey().getParentKey().getParentKey()), "multiple change IDs: %s, %s", first.getKey(), psa.getKey());
        }
        Account.Id id = psa.getAccountId();
        reviewers.put(REVIEWER, id, psa.getGranted());
        if (psa.getValue() != 0) {
            reviewers.remove(CC, id);
        }
    }
    return new ReviewerSet(reviewers);
}
Also used : Account(com.google.gerrit.reviewdb.client.Account) ReviewerStateInternal(com.google.gerrit.server.notedb.ReviewerStateInternal) PatchSetApproval(com.google.gerrit.reviewdb.client.PatchSetApproval) Timestamp(java.sql.Timestamp)

Example 4 with PatchSetApproval

use of com.google.gerrit.reviewdb.client.PatchSetApproval in project gerrit by GerritCodeReview.

the class ApprovalsUtil method newApproval.

public static PatchSetApproval newApproval(PatchSet.Id psId, CurrentUser user, LabelId labelId, int value, Date when) {
    PatchSetApproval psa = new PatchSetApproval(new PatchSetApproval.Key(psId, user.getAccountId(), labelId), Shorts.checkedCast(value), when);
    user.updateRealAccountId(psa::setRealAccountId);
    return psa;
}
Also used : PatchSetApproval(com.google.gerrit.reviewdb.client.PatchSetApproval)

Example 5 with PatchSetApproval

use of com.google.gerrit.reviewdb.client.PatchSetApproval in project gerrit by GerritCodeReview.

the class ApprovalsUtil method addApprovalsForNewPatchSet.

/**
   * Adds approvals to ChangeUpdate for a new patch set, and writes to ReviewDb.
   *
   * @param db review database.
   * @param update change update.
   * @param labelTypes label types for the containing project.
   * @param ps patch set being approved.
   * @param changeCtl change control for user adding approvals.
   * @param approvals approvals to add.
   * @throws RestApiException
   * @throws OrmException
   */
public Iterable<PatchSetApproval> addApprovalsForNewPatchSet(ReviewDb db, ChangeUpdate update, LabelTypes labelTypes, PatchSet ps, ChangeControl changeCtl, Map<String, Short> approvals) throws RestApiException, OrmException {
    Account.Id accountId = changeCtl.getUser().getAccountId();
    checkArgument(accountId.equals(ps.getUploader()), "expected user %s to match patch set uploader %s", accountId, ps.getUploader());
    if (approvals.isEmpty()) {
        return ImmutableList.of();
    }
    checkApprovals(approvals, changeCtl);
    List<PatchSetApproval> cells = new ArrayList<>(approvals.size());
    Date ts = update.getWhen();
    for (Map.Entry<String, Short> vote : approvals.entrySet()) {
        LabelType lt = labelTypes.byLabel(vote.getKey());
        cells.add(newApproval(ps.getId(), changeCtl.getUser(), lt.getLabelId(), vote.getValue(), ts));
    }
    for (PatchSetApproval psa : cells) {
        update.putApproval(psa.getLabel(), psa.getValue());
    }
    db.patchSetApprovals().insert(cells);
    return cells;
}
Also used : Account(com.google.gerrit.reviewdb.client.Account) LabelType(com.google.gerrit.common.data.LabelType) ArrayList(java.util.ArrayList) PatchSetApproval(com.google.gerrit.reviewdb.client.PatchSetApproval) Map(java.util.Map) Date(java.util.Date)

Aggregations

PatchSetApproval (com.google.gerrit.reviewdb.client.PatchSetApproval)71 Change (com.google.gerrit.reviewdb.client.Change)37 Test (org.junit.Test)32 Account (com.google.gerrit.reviewdb.client.Account)17 PatchSet (com.google.gerrit.reviewdb.client.PatchSet)17 LabelType (com.google.gerrit.common.data.LabelType)14 LabelId (com.google.gerrit.reviewdb.client.LabelId)13 ChangeMessage (com.google.gerrit.reviewdb.client.ChangeMessage)10 ChangeData (com.google.gerrit.server.query.change.ChangeData)10 ObjectId (org.eclipse.jgit.lib.ObjectId)10 RevId (com.google.gerrit.reviewdb.client.RevId)8 Timestamp (java.sql.Timestamp)8 PushOneCommit (com.google.gerrit.acceptance.PushOneCommit)7 HashMap (java.util.HashMap)7 AbstractDaemonTest (com.google.gerrit.acceptance.AbstractDaemonTest)6 LabelTypes (com.google.gerrit.common.data.LabelTypes)6 ArrayList (java.util.ArrayList)6 LinkedHashMap (java.util.LinkedHashMap)6 Map (java.util.Map)6 RequestId (com.google.gerrit.server.util.RequestId)5