Search in sources :

Example 41 with BatchUpdate

use of com.google.gerrit.server.update.BatchUpdate in project gerrit by GerritCodeReview.

the class PutAssignee method applyImpl.

@Override
protected AccountInfo applyImpl(BatchUpdate.Factory updateFactory, ChangeResource rsrc, AssigneeInput input) throws RestApiException, UpdateException, OrmException, IOException, PermissionBackendException {
    rsrc.permissions().check(ChangePermission.EDIT_ASSIGNEE);
    input.assignee = Strings.nullToEmpty(input.assignee).trim();
    if (input.assignee.isEmpty()) {
        throw new BadRequestException("missing assignee field");
    }
    IdentifiedUser assignee = accounts.parse(input.assignee);
    if (!assignee.getAccount().isActive()) {
        throw new UnprocessableEntityException(input.assignee + " is not active");
    }
    try {
        rsrc.permissions().database(db).user(assignee).check(ChangePermission.READ);
    } catch (AuthException e) {
        throw new AuthException("read not permitted for " + input.assignee);
    }
    try (BatchUpdate bu = updateFactory.create(db.get(), rsrc.getChange().getProject(), rsrc.getControl().getUser(), TimeUtil.nowTs())) {
        SetAssigneeOp op = assigneeFactory.create(assignee);
        bu.addOp(rsrc.getId(), op);
        PostReviewers.Addition reviewersAddition = addAssigneeAsCC(rsrc, input.assignee);
        bu.addOp(rsrc.getId(), reviewersAddition.op);
        bu.execute();
        return accountLoaderFactory.create(true).fillOne(assignee.getAccountId());
    }
}
Also used : Addition(com.google.gerrit.server.change.PostReviewers.Addition) UnprocessableEntityException(com.google.gerrit.extensions.restapi.UnprocessableEntityException) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) AuthException(com.google.gerrit.extensions.restapi.AuthException) IdentifiedUser(com.google.gerrit.server.IdentifiedUser) BatchUpdate(com.google.gerrit.server.update.BatchUpdate)

Example 42 with BatchUpdate

use of com.google.gerrit.server.update.BatchUpdate in project gerrit by GerritCodeReview.

the class PrimaryStorageMigrator method releaseReadOnlyLeaseInNoteDb.

private void releaseReadOnlyLeaseInNoteDb(Project.NameKey project, Change.Id id) throws OrmException {
    // (In practice retrying won't happen, since we aren't using fused updates at this point.)
    try {
        retryHelper.execute(updateFactory -> {
            try (BatchUpdate bu = updateFactory.create(db.get(), project, internalUserFactory.create(), TimeUtil.nowTs())) {
                bu.addOp(id, new BatchUpdateOp() {

                    @Override
                    public boolean updateChange(ChangeContext ctx) {
                        ctx.getUpdate(ctx.getChange().currentPatchSetId()).setReadOnlyUntil(new Timestamp(0));
                        return true;
                    }
                });
                bu.execute();
                return null;
            }
        });
    } catch (RestApiException | UpdateException e) {
        throw new OrmException(e);
    }
}
Also used : ChangeContext(com.google.gerrit.server.update.ChangeContext) OrmException(com.google.gwtorm.server.OrmException) UpdateException(com.google.gerrit.server.update.UpdateException) RestApiException(com.google.gerrit.extensions.restapi.RestApiException) Timestamp(java.sql.Timestamp) BatchUpdate(com.google.gerrit.server.update.BatchUpdate) BatchUpdateOp(com.google.gerrit.server.update.BatchUpdateOp)

Example 43 with BatchUpdate

use of com.google.gerrit.server.update.BatchUpdate in project gerrit by GerritCodeReview.

the class ChangeEditUtil method publish.

/**
   * Promote change edit to patch set, by squashing the edit into its parent.
   *
   * @param updateFactory factory for creating updates.
   * @param ctl the {@code ChangeControl} of the change to which the change edit belongs
   * @param edit change edit to publish
   * @param notify Notify handling that defines to whom email notifications should be sent after the
   *     change edit is published.
   * @param accountsToNotify Accounts that should be notified after the change edit is published.
   * @throws IOException
   * @throws OrmException
   * @throws UpdateException
   * @throws RestApiException
   */
public void publish(BatchUpdate.Factory updateFactory, ChangeControl ctl, final ChangeEdit edit, NotifyHandling notify, ListMultimap<RecipientType, Account.Id> accountsToNotify) throws IOException, OrmException, RestApiException, UpdateException {
    Change change = edit.getChange();
    try (Repository repo = gitManager.openRepository(change.getProject());
        ObjectInserter oi = repo.newObjectInserter();
        ObjectReader reader = oi.newReader();
        RevWalk rw = new RevWalk(reader)) {
        PatchSet basePatchSet = edit.getBasePatchSet();
        if (!basePatchSet.getId().equals(change.currentPatchSetId())) {
            throw new ResourceConflictException("only edit for current patch set can be published");
        }
        RevCommit squashed = squashEdit(rw, oi, edit.getEditCommit(), basePatchSet);
        PatchSet.Id psId = ChangeUtil.nextPatchSetId(repo, change.currentPatchSetId());
        PatchSetInserter inserter = patchSetInserterFactory.create(ctl, psId, squashed).setNotify(notify).setAccountsToNotify(accountsToNotify);
        StringBuilder message = new StringBuilder("Patch Set ").append(inserter.getPatchSetId().get()).append(": ");
        // Previously checked that the base patch set is the current patch set.
        ObjectId prior = ObjectId.fromString(basePatchSet.getRevision().get());
        ChangeKind kind = changeKindCache.getChangeKind(change.getProject(), rw, repo.getConfig(), prior, squashed);
        if (kind == ChangeKind.NO_CODE_CHANGE) {
            message.append("Commit message was updated.");
            inserter.setDescription("Edit commit message");
        } else {
            message.append("Published edit on patch set ").append(basePatchSet.getPatchSetId()).append(".");
        }
        try (BatchUpdate bu = updateFactory.create(db.get(), change.getProject(), ctl.getUser(), TimeUtil.nowTs())) {
            bu.setRepository(repo, rw, oi);
            bu.addOp(change.getId(), inserter.setDraft(change.getStatus() == Status.DRAFT || basePatchSet.isDraft()).setMessage(message.toString()));
            bu.addOp(change.getId(), new BatchUpdateOp() {

                @Override
                public void updateRepo(RepoContext ctx) throws Exception {
                    ctx.addRefUpdate(edit.getEditCommit().copy(), ObjectId.zeroId(), edit.getRefName());
                }
            });
            bu.execute();
        }
        indexer.index(db.get(), inserter.getChange());
    }
}
Also used : RepoContext(com.google.gerrit.server.update.RepoContext) ObjectId(org.eclipse.jgit.lib.ObjectId) PatchSet(com.google.gerrit.reviewdb.client.PatchSet) Change(com.google.gerrit.reviewdb.client.Change) RevWalk(org.eclipse.jgit.revwalk.RevWalk) BatchUpdate(com.google.gerrit.server.update.BatchUpdate) OrmException(com.google.gwtorm.server.OrmException) UpdateException(com.google.gerrit.server.update.UpdateException) AuthException(com.google.gerrit.extensions.restapi.AuthException) NoSuchChangeException(com.google.gerrit.server.project.NoSuchChangeException) RestApiException(com.google.gerrit.extensions.restapi.RestApiException) IOException(java.io.IOException) ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) Repository(org.eclipse.jgit.lib.Repository) ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) ObjectInserter(org.eclipse.jgit.lib.ObjectInserter) PatchSetInserter(com.google.gerrit.server.change.PatchSetInserter) ObjectReader(org.eclipse.jgit.lib.ObjectReader) RevCommit(org.eclipse.jgit.revwalk.RevCommit) ChangeKind(com.google.gerrit.extensions.client.ChangeKind) BatchUpdateOp(com.google.gerrit.server.update.BatchUpdateOp)

Example 44 with BatchUpdate

use of com.google.gerrit.server.update.BatchUpdate in project gerrit by GerritCodeReview.

the class ConsistencyChecker method insertMergedPatchSet.

private void insertMergedPatchSet(final RevCommit commit, @Nullable final PatchSet.Id psIdToDelete, boolean reuseOldPsId) {
    ProblemInfo notFound = problem("No patch set found for merged commit " + commit.name());
    if (!user.get().isIdentifiedUser()) {
        notFound.status = Status.FIX_FAILED;
        notFound.outcome = "Must be called by an identified user to insert new patch set";
        return;
    }
    ProblemInfo insertPatchSetProblem;
    ProblemInfo deleteOldPatchSetProblem;
    if (psIdToDelete == null) {
        insertPatchSetProblem = problem(String.format("Expected merged commit %s has no associated patch set", commit.name()));
        deleteOldPatchSetProblem = null;
    } else {
        String msg = String.format("Expected merge commit %s corresponds to patch set %s," + " not the current patch set %s", commit.name(), psIdToDelete.get(), change().currentPatchSetId().get());
        // Maybe an identical problem, but different fix.
        deleteOldPatchSetProblem = reuseOldPsId ? null : problem(msg);
        insertPatchSetProblem = problem(msg);
    }
    List<ProblemInfo> currProblems = new ArrayList<>(3);
    currProblems.add(notFound);
    if (deleteOldPatchSetProblem != null) {
        currProblems.add(insertPatchSetProblem);
    }
    currProblems.add(insertPatchSetProblem);
    try {
        PatchSet.Id psId = (psIdToDelete != null && reuseOldPsId) ? psIdToDelete : ChangeUtil.nextPatchSetId(repo, change().currentPatchSetId());
        PatchSetInserter inserter = patchSetInserterFactory.create(ctl, psId, commit);
        try (BatchUpdate bu = newBatchUpdate()) {
            bu.setRepository(repo, rw, oi);
            if (psIdToDelete != null) {
                // Delete the given patch set ref. If reuseOldPsId is true,
                // PatchSetInserter will reinsert the same ref, making it a no-op.
                bu.addOp(ctl.getId(), new BatchUpdateOp() {

                    @Override
                    public void updateRepo(RepoContext ctx) throws IOException {
                        ctx.addRefUpdate(commit, ObjectId.zeroId(), psIdToDelete.toRefName());
                    }
                });
                if (!reuseOldPsId) {
                    bu.addOp(ctl.getId(), new DeletePatchSetFromDbOp(checkNotNull(deleteOldPatchSetProblem), psIdToDelete));
                }
            }
            bu.addOp(ctl.getId(), inserter.setValidate(false).setFireRevisionCreated(false).setNotify(NotifyHandling.NONE).setAllowClosed(true).setMessage("Patch set for merged commit inserted by consistency checker"));
            bu.addOp(ctl.getId(), new FixMergedOp(notFound));
            bu.execute();
        }
        ctl = changeControlFactory.controlFor(db.get(), inserter.getChange(), ctl.getUser());
        insertPatchSetProblem.status = Status.FIXED;
        insertPatchSetProblem.outcome = "Inserted as patch set " + psId.get();
    } catch (OrmException | IOException | UpdateException | RestApiException e) {
        warn(e);
        for (ProblemInfo pi : currProblems) {
            pi.status = Status.FIX_FAILED;
            pi.outcome = "Error inserting merged patch set";
        }
        return;
    }
}
Also used : RepoContext(com.google.gerrit.server.update.RepoContext) ProblemInfo(com.google.gerrit.extensions.common.ProblemInfo) ArrayList(java.util.ArrayList) PatchSet(com.google.gerrit.reviewdb.client.PatchSet) IOException(java.io.IOException) BatchUpdate(com.google.gerrit.server.update.BatchUpdate) OrmException(com.google.gwtorm.server.OrmException) UpdateException(com.google.gerrit.server.update.UpdateException) RestApiException(com.google.gerrit.extensions.restapi.RestApiException) BatchUpdateOp(com.google.gerrit.server.update.BatchUpdateOp)

Example 45 with BatchUpdate

use of com.google.gerrit.server.update.BatchUpdate in project gerrit by GerritCodeReview.

the class ConsistencyChecker method fixMerged.

private void fixMerged(ProblemInfo p) {
    try (BatchUpdate bu = newBatchUpdate()) {
        bu.setRepository(repo, rw, oi);
        bu.addOp(ctl.getId(), new FixMergedOp(p));
        bu.execute();
    } catch (UpdateException | RestApiException e) {
        log.warn("Error marking " + ctl.getId() + "as merged", e);
        p.status = Status.FIX_FAILED;
        p.outcome = "Error updating status to merged";
    }
}
Also used : UpdateException(com.google.gerrit.server.update.UpdateException) RestApiException(com.google.gerrit.extensions.restapi.RestApiException) BatchUpdate(com.google.gerrit.server.update.BatchUpdate)

Aggregations

BatchUpdate (com.google.gerrit.server.update.BatchUpdate)54 BatchUpdateOp (com.google.gerrit.server.update.BatchUpdateOp)26 Change (com.google.gerrit.reviewdb.client.Change)22 ObjectId (org.eclipse.jgit.lib.ObjectId)14 OrmException (com.google.gwtorm.server.OrmException)13 ResourceConflictException (com.google.gerrit.extensions.restapi.ResourceConflictException)12 RestApiException (com.google.gerrit.extensions.restapi.RestApiException)12 UpdateException (com.google.gerrit.server.update.UpdateException)12 PatchSet (com.google.gerrit.reviewdb.client.PatchSet)11 ChangeControl (com.google.gerrit.server.project.ChangeControl)11 ChangeContext (com.google.gerrit.server.update.ChangeContext)11 AuthException (com.google.gerrit.extensions.restapi.AuthException)10 ObjectInserter (org.eclipse.jgit.lib.ObjectInserter)10 ObjectReader (org.eclipse.jgit.lib.ObjectReader)10 Repository (org.eclipse.jgit.lib.Repository)9 RevWalk (org.eclipse.jgit.revwalk.RevWalk)9 BadRequestException (com.google.gerrit.extensions.restapi.BadRequestException)8 IOException (java.io.IOException)8 RevCommit (org.eclipse.jgit.revwalk.RevCommit)8 IdentifiedUser (com.google.gerrit.server.IdentifiedUser)7