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;
}
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;
}
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;
}
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);
}
}
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;
}
Aggregations