Search in sources :

Example 1 with PatchSetApproval

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

the class RevisionApiImpl method votes.

@Override
public ListMultimap<String, ApprovalInfo> votes() throws RestApiException {
    ListMultimap<String, ApprovalInfo> result = ListMultimapBuilder.treeKeys().arrayListValues().build();
    try {
        Iterable<PatchSetApproval> approvals = approvalsUtil.byPatchSet(revision.getNotes(), revision.getPatchSet().id());
        AccountLoader accountLoader = accountLoaderFactory.create(EnumSet.of(FillOptions.ID, FillOptions.NAME, FillOptions.EMAIL, FillOptions.USERNAME));
        for (PatchSetApproval approval : approvals) {
            String label = approval.label();
            ApprovalInfo info = new ApprovalInfo(approval.accountId().get(), Integer.valueOf(approval.value()), null, approval.tag().orElse(null), approval.granted());
            accountLoader.put(info);
            result.get(label).add(info);
        }
        accountLoader.fill();
    } catch (Exception e) {
        throw asRestApiException("Cannot get votes", e);
    }
    return result;
}
Also used : ApprovalInfo(com.google.gerrit.extensions.common.ApprovalInfo) AccountLoader(com.google.gerrit.server.account.AccountLoader) IdString(com.google.gerrit.extensions.restapi.IdString) PatchSetApproval(com.google.gerrit.entities.PatchSetApproval) ApiUtil.asRestApiException(com.google.gerrit.server.api.ApiUtil.asRestApiException) RestApiException(com.google.gerrit.extensions.restapi.RestApiException)

Example 2 with PatchSetApproval

use of com.google.gerrit.entities.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, Instant> reviewers = HashBasedTable.create();
    for (PatchSetApproval psa : approvals) {
        if (first == null) {
            first = psa;
        } else {
            checkArgument(first.key().patchSetId().changeId().equals(psa.key().patchSetId().changeId()), "multiple change IDs: %s, %s", first.key(), psa.key());
        }
        Account.Id id = psa.accountId();
        reviewers.put(REVIEWER, id, psa.granted());
        if (psa.value() != 0) {
            reviewers.remove(CC, id);
        }
    }
    return new ReviewerSet(reviewers);
}
Also used : Account(com.google.gerrit.entities.Account) Instant(java.time.Instant) ReviewerStateInternal(com.google.gerrit.server.notedb.ReviewerStateInternal) PatchSetApproval(com.google.gerrit.entities.PatchSetApproval)

Example 3 with PatchSetApproval

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

the class PatchSetUtil method isPatchSetLocked.

/**
 * Is the current patch set locked against state changes?
 */
public boolean isPatchSetLocked(ChangeNotes notes) {
    Change change = notes.getChange();
    if (change.isMerged()) {
        return false;
    }
    ProjectState projectState = projectCache.get(notes.getProjectName()).orElseThrow(illegalState(notes.getProjectName()));
    ApprovalsUtil approvalsUtil = approvalsUtilProvider.get();
    for (PatchSetApproval ap : approvalsUtil.byPatchSet(notes, change.currentPatchSetId())) {
        Optional<LabelType> type = projectState.getLabelTypes(notes).byLabel(ap.label());
        if (type.isPresent() && ap.value() == 1 && type.get().getFunction() == LabelFunction.PATCH_SET_LOCK) {
            return true;
        }
    }
    return false;
}
Also used : LabelType(com.google.gerrit.entities.LabelType) ApprovalsUtil(com.google.gerrit.server.approval.ApprovalsUtil) ProjectState(com.google.gerrit.server.project.ProjectState) Change(com.google.gerrit.entities.Change) PatchSetApproval(com.google.gerrit.entities.PatchSetApproval)

Example 4 with PatchSetApproval

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

the class ApprovalsUtil method addApprovalsForNewPatchSet.

/**
 * Adds approvals to ChangeUpdate for a new patch set, and writes to NoteDb.
 *
 * @param update change update.
 * @param labelTypes label types for the containing project.
 * @param ps patch set being approved.
 * @param user user adding approvals.
 * @param approvals approvals to add.
 */
public Iterable<PatchSetApproval> addApprovalsForNewPatchSet(ChangeUpdate update, LabelTypes labelTypes, PatchSet ps, CurrentUser user, Map<String, Short> approvals) throws RestApiException, PermissionBackendException {
    Account.Id accountId = user.getAccountId();
    checkArgument(accountId.equals(ps.uploader()), "expected user %s to match patch set uploader %s", accountId, ps.uploader());
    if (approvals.isEmpty()) {
        return ImmutableList.of();
    }
    checkApprovals(approvals, permissionBackend.user(user).change(update.getNotes()));
    List<PatchSetApproval> cells = new ArrayList<>(approvals.size());
    Instant ts = update.getWhen();
    for (Map.Entry<String, Short> vote : approvals.entrySet()) {
        Optional<LabelType> lt = labelTypes.byLabel(vote.getKey());
        if (!lt.isPresent()) {
            throw new BadRequestException(String.format("label \"%s\" is not a configured label", vote.getKey()));
        }
        cells.add(newApproval(ps.id(), user, lt.get().getLabelId(), vote.getValue(), ts).build());
    }
    for (PatchSetApproval psa : cells) {
        update.putApproval(psa.label(), psa.value());
    }
    return cells;
}
Also used : Account(com.google.gerrit.entities.Account) Instant(java.time.Instant) ArrayList(java.util.ArrayList) PatchSetApproval(com.google.gerrit.entities.PatchSetApproval) LabelType(com.google.gerrit.entities.LabelType) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) Map(java.util.Map)

Example 5 with PatchSetApproval

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

the class ChangeNotesTest method reissuedApproval_otherPatchSet_differentUUID.

@Test
public void reissuedApproval_otherPatchSet_differentUUID() throws Exception {
    Change c = newChange();
    ChangeUpdate update = newUpdate(c, changeOwner);
    update.putApproval(LabelId.CODE_REVIEW, (short) -1);
    update.commit();
    ChangeNotes notes = newNotes(c);
    assertThat(notes.getApprovals().keySet()).containsExactly(c.currentPatchSetId());
    PatchSetApproval originalPsa = Iterables.getOnlyElement(notes.getApprovals().get(c.currentPatchSetId()));
    assertThat(originalPsa.patchSetId()).isEqualTo(c.currentPatchSetId());
    assertThat(originalPsa.accountId().get()).isEqualTo(1);
    assertThat(originalPsa.label()).isEqualTo(LabelId.CODE_REVIEW);
    assertThat(originalPsa.value()).isEqualTo((short) -1);
    assertParsedUuid(originalPsa);
    // Create new PatchSet and re-issue vote
    incrementPatchSet(c);
    update = newUpdate(c, changeOwner);
    update.putApproval(LabelId.CODE_REVIEW, (short) -1);
    update.commit();
    notes = newNotes(c);
    assertThat(notes.getApprovals().keySet()).hasSize(2);
    PatchSetApproval postUpdateOriginalPsa = Iterables.getOnlyElement(notes.getApprovals().get(originalPsa.patchSetId()));
    PatchSetApproval reAddedPsa = Iterables.getOnlyElement(notes.getApprovals().get(c.currentPatchSetId()));
    // Same patch set approval for the original patch set is returned after the vote was re-issued
    // on the next patch set
    assertThat(postUpdateOriginalPsa).isEqualTo(originalPsa);
    assertThat(reAddedPsa.accountId()).isEqualTo(originalPsa.accountId());
    assertThat(reAddedPsa.label()).isEqualTo(originalPsa.label());
    assertThat(reAddedPsa.value()).isEqualTo(originalPsa.value());
    // The re-added approval has a different UUID
    assertParsedUuid(reAddedPsa);
    assertThat(reAddedPsa.uuid().get()).isNotEqualTo(originalPsa.uuid().get());
}
Also used : Change(com.google.gerrit.entities.Change) PatchSetApproval(com.google.gerrit.entities.PatchSetApproval) Test(org.junit.Test)

Aggregations

PatchSetApproval (com.google.gerrit.entities.PatchSetApproval)93 Test (org.junit.Test)57 Change (com.google.gerrit.entities.Change)41 LabelType (com.google.gerrit.entities.LabelType)22 Account (com.google.gerrit.entities.Account)20 PushOneCommit (com.google.gerrit.acceptance.PushOneCommit)14 Map (java.util.Map)14 ObjectId (org.eclipse.jgit.lib.ObjectId)14 LabelId (com.google.gerrit.entities.LabelId)13 PatchSet (com.google.gerrit.entities.PatchSet)12 SubmitRecord (com.google.gerrit.entities.SubmitRecord)12 AbstractDaemonTest (com.google.gerrit.acceptance.AbstractDaemonTest)11 ReviewInput (com.google.gerrit.extensions.api.changes.ReviewInput)10 SubmissionId (com.google.gerrit.entities.SubmissionId)9 ChangeData (com.google.gerrit.server.query.change.ChangeData)9 Inject (com.google.inject.Inject)9 Instant (java.time.Instant)9 HashMap (java.util.HashMap)9 List (java.util.List)9 ChangeMessage (com.google.gerrit.entities.ChangeMessage)8