Search in sources :

Example 46 with PatchSetApproval

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

the class DeleteReviewerOp method updateChange.

@Override
public boolean updateChange(ChangeContext ctx) throws AuthException, ResourceNotFoundException, OrmException {
    Account.Id reviewerId = reviewer.getId();
    if (!approvalsUtil.getReviewers(ctx.getDb(), ctx.getNotes()).all().contains(reviewerId)) {
        throw new ResourceNotFoundException();
    }
    currChange = ctx.getChange();
    currPs = psUtil.current(ctx.getDb(), ctx.getNotes());
    LabelTypes labelTypes = ctx.getControl().getLabelTypes();
    // removing a reviewer will remove all her votes
    for (LabelType lt : labelTypes.getLabelTypes()) {
        newApprovals.put(lt.getName(), (short) 0);
    }
    StringBuilder msg = new StringBuilder();
    msg.append("Removed reviewer " + reviewer.getFullName());
    StringBuilder removedVotesMsg = new StringBuilder();
    removedVotesMsg.append(" with the following votes:\n\n");
    List<PatchSetApproval> del = new ArrayList<>();
    boolean votesRemoved = false;
    for (PatchSetApproval a : approvals(ctx, reviewerId)) {
        if (ctx.getControl().canRemoveReviewer(a)) {
            del.add(a);
            if (a.getPatchSetId().equals(currPs.getId()) && a.getValue() != 0) {
                oldApprovals.put(a.getLabel(), a.getValue());
                removedVotesMsg.append("* ").append(a.getLabel()).append(formatLabelValue(a.getValue())).append(" by ").append(userFactory.create(a.getAccountId()).getNameEmail()).append("\n");
                votesRemoved = true;
            }
        } else {
            throw new AuthException("delete reviewer not permitted");
        }
    }
    if (votesRemoved) {
        msg.append(removedVotesMsg);
    } else {
        msg.append(".");
    }
    ctx.getDb().patchSetApprovals().delete(del);
    ChangeUpdate update = ctx.getUpdate(currPs.getId());
    update.removeReviewer(reviewerId);
    changeMessage = ChangeMessagesUtil.newMessage(ctx, msg.toString(), ChangeMessagesUtil.TAG_DELETE_REVIEWER);
    cmUtil.addChangeMessage(ctx.getDb(), update, changeMessage);
    return true;
}
Also used : Account(com.google.gerrit.reviewdb.client.Account) LabelTypes(com.google.gerrit.common.data.LabelTypes) LabelType(com.google.gerrit.common.data.LabelType) ArrayList(java.util.ArrayList) AuthException(com.google.gerrit.extensions.restapi.AuthException) ResourceNotFoundException(com.google.gerrit.extensions.restapi.ResourceNotFoundException) PatchSetApproval(com.google.gerrit.reviewdb.client.PatchSetApproval) ChangeUpdate(com.google.gerrit.server.notedb.ChangeUpdate)

Example 47 with PatchSetApproval

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

the class DeleteReviewerOp method approvals.

private Iterable<PatchSetApproval> approvals(ChangeContext ctx, Account.Id accountId) throws OrmException {
    Change.Id changeId = ctx.getNotes().getChangeId();
    Iterable<PatchSetApproval> approvals;
    PrimaryStorage r = PrimaryStorage.of(ctx.getChange());
    if (migration.readChanges() && r == PrimaryStorage.REVIEW_DB) {
        // Because NoteDb and ReviewDb have different semantics for zero-value
        // approvals, we must fall back to ReviewDb as the source of truth here.
        ReviewDb db = ctx.getDb();
        if (db instanceof BatchUpdateReviewDb) {
            db = ((BatchUpdateReviewDb) db).unsafeGetDelegate();
        }
        db = ReviewDbUtil.unwrapDb(db);
        approvals = db.patchSetApprovals().byChange(changeId);
    } else {
        approvals = approvalsUtil.byChange(ctx.getDb(), ctx.getNotes()).values();
    }
    return Iterables.filter(approvals, psa -> accountId.equals(psa.getAccountId()));
}
Also used : PrimaryStorage(com.google.gerrit.server.notedb.NoteDbChangeState.PrimaryStorage) BatchUpdateReviewDb(com.google.gerrit.server.update.BatchUpdateReviewDb) Change(com.google.gerrit.reviewdb.client.Change) PatchSetApproval(com.google.gerrit.reviewdb.client.PatchSetApproval) ReviewDb(com.google.gerrit.reviewdb.server.ReviewDb) BatchUpdateReviewDb(com.google.gerrit.server.update.BatchUpdateReviewDb)

Example 48 with PatchSetApproval

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

the class MergedSender method format.

private String format(String type, Table<Account.Id, String, PatchSetApproval> approvals) {
    StringBuilder txt = new StringBuilder();
    if (approvals.isEmpty()) {
        return "";
    }
    txt.append(type).append(":\n");
    for (Account.Id id : approvals.rowKeySet()) {
        txt.append("  ");
        txt.append(getNameFor(id));
        txt.append(": ");
        boolean first = true;
        for (LabelType lt : labelTypes.getLabelTypes()) {
            PatchSetApproval ca = approvals.get(id, lt.getName());
            if (ca == null) {
                continue;
            }
            if (first) {
                first = false;
            } else {
                txt.append("; ");
            }
            LabelValue v = lt.getValue(ca);
            if (v != null) {
                txt.append(v.getText());
            } else {
                txt.append(lt.getName());
                txt.append('=');
                txt.append(LabelValue.formatValue(ca.getValue()));
            }
        }
        txt.append('\n');
    }
    txt.append('\n');
    return txt.toString();
}
Also used : Account(com.google.gerrit.reviewdb.client.Account) LabelValue(com.google.gerrit.common.data.LabelValue) LabelType(com.google.gerrit.common.data.LabelType) PatchSetApproval(com.google.gerrit.reviewdb.client.PatchSetApproval)

Example 49 with PatchSetApproval

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

the class MergedSender method getApprovals.

public String getApprovals() {
    try {
        Table<Account.Id, String, PatchSetApproval> pos = HashBasedTable.create();
        Table<Account.Id, String, PatchSetApproval> neg = HashBasedTable.create();
        for (PatchSetApproval ca : args.approvalsUtil.byPatchSet(args.db.get(), changeData.changeControl(), patchSet.getId())) {
            LabelType lt = labelTypes.byLabel(ca.getLabelId());
            if (lt == null) {
                continue;
            }
            if (ca.getValue() > 0) {
                pos.put(ca.getAccountId(), lt.getName(), ca);
            } else if (ca.getValue() < 0) {
                neg.put(ca.getAccountId(), lt.getName(), ca);
            }
        }
        return format("Approvals", pos) + format("Objections", neg);
    } catch (OrmException err) {
    // Don't list the approvals
    }
    return "";
}
Also used : OrmException(com.google.gwtorm.server.OrmException) LabelType(com.google.gerrit.common.data.LabelType) PatchSetApproval(com.google.gerrit.reviewdb.client.PatchSetApproval)

Example 50 with PatchSetApproval

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

the class ChangeNotes method getApprovals.

public ImmutableListMultimap<PatchSet.Id, PatchSetApproval> getApprovals() {
    if (approvals == null) {
        ImmutableListMultimap.Builder<PatchSet.Id, PatchSetApproval> b = ImmutableListMultimap.builder();
        for (Map.Entry<PatchSet.Id, PatchSetApproval> e : state.approvals()) {
            b.put(e.getKey(), new PatchSetApproval(e.getValue()));
        }
        approvals = b.build();
    }
    return approvals;
}
Also used : ImmutableListMultimap(com.google.common.collect.ImmutableListMultimap) ObjectId(org.eclipse.jgit.lib.ObjectId) RevId(com.google.gerrit.reviewdb.client.RevId) PatchSetApproval(com.google.gerrit.reviewdb.client.PatchSetApproval) Map(java.util.Map) ImmutableSortedMap(com.google.common.collect.ImmutableSortedMap)

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