use of com.google.gerrit.server.update.BatchUpdate in project gerrit by GerritCodeReview.
the class CreateMergePatchSet method applyImpl.
@Override
protected Response<ChangeInfo> applyImpl(BatchUpdate.Factory updateFactory, ChangeResource rsrc, MergePatchSetInput in) throws OrmException, IOException, InvalidChangeOperationException, RestApiException, UpdateException, PermissionBackendException {
rsrc.permissions().database(db).check(ChangePermission.ADD_PATCH_SET);
MergeInput merge = in.merge;
if (merge == null || Strings.isNullOrEmpty(merge.source)) {
throw new BadRequestException("merge.source must be non-empty");
}
ChangeControl ctl = rsrc.getControl();
PatchSet ps = psUtil.current(db.get(), ctl.getNotes());
ProjectControl projectControl = ctl.getProjectControl();
Change change = ctl.getChange();
Project.NameKey project = change.getProject();
Branch.NameKey dest = change.getDest();
try (Repository git = gitManager.openRepository(project);
ObjectInserter oi = git.newObjectInserter();
ObjectReader reader = oi.newReader();
RevWalk rw = new RevWalk(reader)) {
RevCommit sourceCommit = MergeUtil.resolveCommit(git, rw, merge.source);
if (!projectControl.canReadCommit(db.get(), git, sourceCommit)) {
throw new ResourceNotFoundException("cannot find source commit: " + merge.source + " to merge.");
}
RevCommit currentPsCommit = rw.parseCommit(ObjectId.fromString(ps.getRevision().get()));
Timestamp now = TimeUtil.nowTs();
IdentifiedUser me = user.get().asIdentifiedUser();
PersonIdent author = me.newCommitterIdent(now, serverTimeZone);
RevCommit newCommit = createMergeCommit(in, projectControl, dest, git, oi, rw, currentPsCommit, sourceCommit, author, ObjectId.fromString(change.getKey().get().substring(1)));
PatchSet.Id nextPsId = ChangeUtil.nextPatchSetId(ps.getId());
PatchSetInserter psInserter = patchSetInserterFactory.create(ctl, nextPsId, newCommit);
try (BatchUpdate bu = updateFactory.create(db.get(), project, me, now)) {
bu.setRepository(git, rw, oi);
bu.addOp(ctl.getId(), psInserter.setMessage("Uploaded patch set " + nextPsId.get() + ".").setDraft(ps.isDraft()).setNotify(NotifyHandling.NONE).setCheckAddPatchSetPermission(false));
bu.execute();
}
ChangeJson json = jsonFactory.create(ListChangesOption.CURRENT_REVISION);
return Response.ok(json.format(psInserter.getChange()));
}
}
use of com.google.gerrit.server.update.BatchUpdate in project gerrit by GerritCodeReview.
the class DeleteVote method applyImpl.
@Override
protected Response<?> applyImpl(BatchUpdate.Factory updateFactory, VoteResource rsrc, DeleteVoteInput input) throws RestApiException, UpdateException {
if (input == null) {
input = new DeleteVoteInput();
}
if (input.label != null && !rsrc.getLabel().equals(input.label)) {
throw new BadRequestException("label must match URL");
}
if (input.notify == null) {
input.notify = NotifyHandling.ALL;
}
ReviewerResource r = rsrc.getReviewer();
Change change = r.getChange();
if (r.getRevisionResource() != null && !r.getRevisionResource().isCurrent()) {
throw new MethodNotAllowedException("Cannot delete vote on non-current patch set");
}
try (BatchUpdate bu = updateFactory.create(db.get(), change.getProject(), r.getControl().getUser(), TimeUtil.nowTs())) {
bu.addOp(change.getId(), new Op(r.getReviewerUser().getAccountId(), rsrc.getLabel(), input));
bu.execute();
}
return Response.none();
}
use of com.google.gerrit.server.update.BatchUpdate in project gerrit by GerritCodeReview.
the class DeleteChange method applyImpl.
@Override
protected Response<?> applyImpl(BatchUpdate.Factory updateFactory, ChangeResource rsrc, Input input) throws RestApiException, UpdateException, PermissionBackendException {
if (rsrc.getChange().getStatus() == Change.Status.MERGED) {
throw new MethodNotAllowedException("delete not permitted");
} else if (!allowDrafts && rsrc.getChange().getStatus() == Change.Status.DRAFT) {
// If drafts are disabled, only an administrator can delete a draft.
try {
permissionBackend.user(user).check(GlobalPermission.ADMINISTRATE_SERVER);
} catch (AuthException e) {
throw new MethodNotAllowedException("Draft workflow is disabled");
}
} else {
rsrc.permissions().database(db).check(ChangePermission.DELETE);
}
try (BatchUpdate bu = updateFactory.create(db.get(), rsrc.getProject(), rsrc.getUser(), TimeUtil.nowTs())) {
Change.Id id = rsrc.getChange().getId();
bu.setOrder(Order.DB_BEFORE_REPO);
bu.addOp(id, opProvider.get());
bu.execute();
}
return Response.none();
}
use of com.google.gerrit.server.update.BatchUpdate in project gerrit by GerritCodeReview.
the class DeleteComment method applyImpl.
@Override
public CommentInfo applyImpl(BatchUpdate.Factory batchUpdateFactory, CommentResource rsrc, DeleteCommentInput input) throws RestApiException, IOException, ConfigInvalidException, OrmException, PermissionBackendException, UpdateException {
CurrentUser user = userProvider.get();
permissionBackend.user(user).check(GlobalPermission.ADMINISTRATE_SERVER);
String newMessage = getCommentNewMessage(user.asIdentifiedUser().getName(), input.reason);
DeleteCommentOp deleteCommentOp = new DeleteCommentOp(rsrc, newMessage);
try (BatchUpdate batchUpdate = batchUpdateFactory.create(dbProvider.get(), rsrc.getRevisionResource().getProject(), user, TimeUtil.nowTs())) {
batchUpdate.addOp(rsrc.getRevisionResource().getChange().getId(), deleteCommentOp).execute();
}
ChangeNotes updatedNotes = notesFactory.createChecked(rsrc.getRevisionResource().getChange().getId());
List<Comment> changeComments = commentsUtil.publishedByChange(dbProvider.get(), updatedNotes);
Optional<Comment> updatedComment = changeComments.stream().filter(c -> c.key.equals(rsrc.getComment().key)).findFirst();
if (!updatedComment.isPresent()) {
// This should not happen as this endpoint should not remove the whole comment.
throw new ResourceNotFoundException("comment not found: " + rsrc.getComment().key);
}
return commentJson.get().newCommentFormatter().format(updatedComment.get());
}
use of com.google.gerrit.server.update.BatchUpdate in project gerrit by GerritCodeReview.
the class ConsistencyChecker method deletePatchSets.
private void deletePatchSets(List<DeletePatchSetFromDbOp> ops) {
try (BatchUpdate bu = newBatchUpdate()) {
bu.setRepository(repo, rw, oi);
for (DeletePatchSetFromDbOp op : ops) {
checkArgument(op.psId.getParentKey().equals(ctl.getId()));
bu.addOp(ctl.getId(), op);
}
bu.addOp(ctl.getId(), new UpdateCurrentPatchSetOp(ops));
bu.execute();
} catch (NoPatchSetsWouldRemainException e) {
for (DeletePatchSetFromDbOp op : ops) {
op.p.status = Status.FIX_FAILED;
op.p.outcome = e.getMessage();
}
} catch (UpdateException | RestApiException e) {
String msg = "Error deleting patch set";
log.warn(msg + " of change " + ops.get(0).psId.getParentKey(), e);
for (DeletePatchSetFromDbOp op : ops) {
// Overwrite existing statuses that were set before the transaction was
// rolled back.
op.p.status = Status.FIX_FAILED;
op.p.outcome = msg;
}
}
}
Aggregations