Search in sources :

Example 6 with ChangeUpdate

use of com.google.gerrit.server.notedb.ChangeUpdate 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 7 with ChangeUpdate

use of com.google.gerrit.server.notedb.ChangeUpdate in project gerrit by GerritCodeReview.

the class PatchSetInserter method updateChange.

@Override
public boolean updateChange(ChangeContext ctx) throws ResourceConflictException, OrmException, IOException {
    ReviewDb db = ctx.getDb();
    ChangeControl ctl = ctx.getControl();
    change = ctx.getChange();
    ChangeUpdate update = ctx.getUpdate(psId);
    update.setSubjectForCommit("Create patch set " + psId.get());
    if (!change.getStatus().isOpen() && !allowClosed) {
        throw new ResourceConflictException(String.format("Cannot create new patch set of change %s because it is %s", change.getId(), ChangeUtil.status(change)));
    }
    List<String> newGroups = groups;
    if (newGroups.isEmpty()) {
        PatchSet prevPs = psUtil.current(db, ctx.getNotes());
        if (prevPs != null) {
            newGroups = prevPs.getGroups();
        }
    }
    patchSet = psUtil.insert(db, ctx.getRevWalk(), ctx.getUpdate(psId), psId, commitId, draft, newGroups, null, description);
    if (notify != NotifyHandling.NONE) {
        oldReviewers = approvalsUtil.getReviewers(db, ctl.getNotes());
    }
    if (message != null) {
        changeMessage = ChangeMessagesUtil.newMessage(patchSet.getId(), ctx.getUser(), ctx.getWhen(), message, ChangeMessagesUtil.TAG_UPLOADED_PATCH_SET);
        changeMessage.setMessage(message);
    }
    patchSetInfo = patchSetInfoFactory.get(ctx.getRevWalk(), ctx.getRevWalk().parseCommit(commitId), psId);
    if (change.getStatus() != Change.Status.DRAFT && !allowClosed) {
        change.setStatus(Change.Status.NEW);
    }
    change.setCurrentPatchSet(patchSetInfo);
    if (copyApprovals) {
        approvalCopier.copy(db, ctl, patchSet);
    }
    if (changeMessage != null) {
        cmUtil.addChangeMessage(db, update, changeMessage);
    }
    return true;
}
Also used : ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) ChangeControl(com.google.gerrit.server.project.ChangeControl) PatchSet(com.google.gerrit.reviewdb.client.PatchSet) ReviewDb(com.google.gerrit.reviewdb.server.ReviewDb) ChangeUpdate(com.google.gerrit.server.notedb.ChangeUpdate)

Example 8 with ChangeUpdate

use of com.google.gerrit.server.notedb.ChangeUpdate in project gerrit by GerritCodeReview.

the class ChangeInserter method updateChange.

@Override
public boolean updateChange(ChangeContext ctx) throws RestApiException, OrmException, IOException {
    // Use defensive copy created by ChangeControl.
    change = ctx.getChange();
    ReviewDb db = ctx.getDb();
    ChangeControl ctl = ctx.getControl();
    patchSetInfo = patchSetInfoFactory.get(ctx.getRevWalk(), ctx.getRevWalk().parseCommit(commitId), psId);
    ctx.getChange().setCurrentPatchSet(patchSetInfo);
    ChangeUpdate update = ctx.getUpdate(psId);
    update.setChangeId(change.getKey().get());
    update.setSubjectForCommit("Create change");
    update.setBranch(change.getDest().get());
    update.setTopic(change.getTopic());
    update.setPsDescription(patchSetDescription);
    update.setPrivate(isPrivate);
    update.setWorkInProgress(workInProgress);
    boolean draft = status == Change.Status.DRAFT;
    List<String> newGroups = groups;
    if (newGroups.isEmpty()) {
        newGroups = GroupCollector.getDefaultGroups(commitId);
    }
    patchSet = psUtil.insert(ctx.getDb(), ctx.getRevWalk(), update, psId, commitId, draft, newGroups, pushCert, patchSetDescription);
    /* TODO: fixStatus is used here because the tests
     * (byStatusClosed() in AbstractQueryChangesTest)
     * insert changes that are already merged,
     * and setStatus may not be used to set the Status to merged
     *
     * is it possible to make the tests use the merge code path,
     * instead of setting the status directly?
     */
    update.fixStatus(change.getStatus());
    LabelTypes labelTypes = ctl.getProjectControl().getLabelTypes();
    approvalsUtil.addReviewers(db, update, labelTypes, change, patchSet, patchSetInfo, filterOnChangeVisibility(db, ctx.getNotes(), reviewers), Collections.<Account.Id>emptySet());
    approvalsUtil.addApprovalsForNewPatchSet(db, update, labelTypes, patchSet, ctx.getControl(), approvals);
    // reviewer which is needed in several other code paths.
    if (!approvals.isEmpty()) {
        update.putReviewer(ctx.getAccountId(), REVIEWER);
    }
    if (message != null) {
        changeMessage = ChangeMessagesUtil.newMessage(patchSet.getId(), ctx.getUser(), patchSet.getCreatedOn(), message, ChangeMessagesUtil.uploadedPatchSetTag(workInProgress));
        cmUtil.addChangeMessage(db, update, changeMessage);
    }
    return true;
}
Also used : Account(com.google.gerrit.reviewdb.client.Account) LabelTypes(com.google.gerrit.common.data.LabelTypes) ChangeControl(com.google.gerrit.server.project.ChangeControl) ReviewDb(com.google.gerrit.reviewdb.server.ReviewDb) ChangeUpdate(com.google.gerrit.server.notedb.ChangeUpdate)

Example 9 with ChangeUpdate

use of com.google.gerrit.server.notedb.ChangeUpdate in project gerrit by GerritCodeReview.

the class SubmitStrategyOp method setApproval.

private void setApproval(ChangeContext ctx, IdentifiedUser user) throws OrmException {
    Change.Id id = ctx.getChange().getId();
    List<SubmitRecord> records = args.commitStatus.getSubmitRecords(id);
    PatchSet.Id oldPsId = toMerge.getPatchsetId();
    PatchSet.Id newPsId = ctx.getChange().currentPatchSetId();
    logDebug("Add approval for " + id);
    ChangeUpdate origPsUpdate = ctx.getUpdate(oldPsId);
    origPsUpdate.putReviewer(user.getAccountId(), REVIEWER);
    LabelNormalizer.Result normalized = approve(ctx, origPsUpdate);
    ChangeUpdate newPsUpdate = ctx.getUpdate(newPsId);
    newPsUpdate.merge(args.submissionId, records);
    // approvals as well.
    if (!newPsId.equals(oldPsId)) {
        saveApprovals(normalized, ctx, newPsUpdate, true);
        submitter = convertPatchSet(newPsId).apply(submitter);
    }
}
Also used : SubmitRecord(com.google.gerrit.common.data.SubmitRecord) LabelNormalizer(com.google.gerrit.server.git.LabelNormalizer) PatchSet(com.google.gerrit.reviewdb.client.PatchSet) Change(com.google.gerrit.reviewdb.client.Change) ChangeUpdate(com.google.gerrit.server.notedb.ChangeUpdate)

Example 10 with ChangeUpdate

use of com.google.gerrit.server.notedb.ChangeUpdate in project gerrit by GerritCodeReview.

the class FusedNoteDbBatchUpdate method executeChangeOps.

private ChangesHandle executeChangeOps(boolean dryrun) throws Exception {
    logDebug("Executing change ops");
    initRepository();
    Repository repo = repoView.getRepository();
    checkState(repo.getRefDatabase().performsAtomicTransactions(), "cannot use noteDb.changes.fuseUpdates=true with a repository that does not support atomic" + " batch ref updates: %s", repo);
    ChangesHandle handle = new ChangesHandle(updateManagerFactory.create(project).setChangeRepo(repo, repoView.getRevWalk(), repoView.getInserter(), repoView.getCommands()), dryrun);
    if (user.isIdentifiedUser()) {
        handle.manager.setRefLogIdent(user.asIdentifiedUser().newRefLogIdent(when, tz));
    }
    handle.manager.setRefLogMessage(refLogMessage);
    handle.manager.setPushCertificate(pushCert);
    for (Map.Entry<Change.Id, Collection<BatchUpdateOp>> e : ops.asMap().entrySet()) {
        Change.Id id = e.getKey();
        ChangeContextImpl ctx = newChangeContext(id);
        boolean dirty = false;
        logDebug("Applying {} ops for change {}", e.getValue().size(), id);
        for (BatchUpdateOp op : e.getValue()) {
            dirty |= op.updateChange(ctx);
        }
        if (!dirty) {
            logDebug("No ops reported dirty, short-circuiting");
            handle.setResult(id, ChangeResult.SKIPPED);
            continue;
        }
        for (ChangeUpdate u : ctx.updates.values()) {
            handle.manager.add(u);
        }
        if (ctx.deleted) {
            logDebug("Change {} was deleted", id);
            handle.manager.deleteChange(id);
            handle.setResult(id, ChangeResult.DELETED);
        } else {
            handle.setResult(id, ChangeResult.UPSERTED);
        }
    }
    return handle;
}
Also used : Repository(org.eclipse.jgit.lib.Repository) Collection(java.util.Collection) RequestId(com.google.gerrit.server.util.RequestId) Change(com.google.gerrit.reviewdb.client.Change) HashMap(java.util.HashMap) Map(java.util.Map) TreeMap(java.util.TreeMap) ChangeUpdate(com.google.gerrit.server.notedb.ChangeUpdate)

Aggregations

ChangeUpdate (com.google.gerrit.server.notedb.ChangeUpdate)15 Change (com.google.gerrit.reviewdb.client.Change)7 PatchSet (com.google.gerrit.reviewdb.client.PatchSet)4 ResourceConflictException (com.google.gerrit.extensions.restapi.ResourceConflictException)3 PatchSetApproval (com.google.gerrit.reviewdb.client.PatchSetApproval)3 Repository (org.eclipse.jgit.lib.Repository)3 LabelTypes (com.google.gerrit.common.data.LabelTypes)2 Account (com.google.gerrit.reviewdb.client.Account)2 ReviewDb (com.google.gerrit.reviewdb.server.ReviewDb)2 ChangeNotes (com.google.gerrit.server.notedb.ChangeNotes)2 ChangeControl (com.google.gerrit.server.project.ChangeControl)2 RequestId (com.google.gerrit.server.util.RequestId)2 ValidationException (com.google.gerrit.server.validators.ValidationException)2 Collection (java.util.Collection)2 Map (java.util.Map)2 TreeMap (java.util.TreeMap)2 LabelType (com.google.gerrit.common.data.LabelType)1 SubmitRecord (com.google.gerrit.common.data.SubmitRecord)1 FactoryModule (com.google.gerrit.extensions.config.FactoryModule)1 AuthException (com.google.gerrit.extensions.restapi.AuthException)1