use of com.google.gerrit.server.notedb.ChangeUpdate in project gerrit by GerritCodeReview.
the class WorkInProgressOp method updateChange.
@Override
public boolean updateChange(ChangeContext ctx) throws OrmException {
Change change = ctx.getChange();
ChangeUpdate update = ctx.getUpdate(change.currentPatchSetId());
change.setWorkInProgress(workInProgress);
change.setLastUpdatedOn(ctx.getWhen());
update.setWorkInProgress(workInProgress);
addMessage(ctx, update);
return true;
}
use of com.google.gerrit.server.notedb.ChangeUpdate in project gerrit by GerritCodeReview.
the class TestChanges method newUpdate.
public static ChangeUpdate newUpdate(Injector injector, Change c, final CurrentUser user) throws Exception {
injector = injector.createChildInjector(new FactoryModule() {
@Override
public void configure() {
bind(CurrentUser.class).toInstance(user);
}
});
ChangeUpdate update = injector.getInstance(ChangeUpdate.Factory.class).create(stubChangeControl(injector.getInstance(AbstractChangeNotes.Args.class), c, user), TimeUtil.nowTs(), Ordering.<String>natural());
ChangeNotes notes = update.getNotes();
boolean hasPatchSets = notes.getPatchSets() != null && !notes.getPatchSets().isEmpty();
NotesMigration migration = injector.getInstance(NotesMigration.class);
if (hasPatchSets || !migration.readChanges()) {
return update;
}
// Change doesn't exist yet. NoteDb requires that there be a commit for the
// first patch set, so create one.
GitRepositoryManager repoManager = injector.getInstance(GitRepositoryManager.class);
try (Repository repo = repoManager.openRepository(c.getProject())) {
TestRepository<Repository> tr = new TestRepository<>(repo);
PersonIdent ident = user.asIdentifiedUser().newCommitterIdent(update.getWhen(), TimeZone.getDefault());
TestRepository<Repository>.CommitBuilder<Repository> cb = tr.commit().author(ident).committer(ident).message(firstNonNull(c.getSubject(), "Test change"));
Ref parent = repo.exactRef(c.getDest().get());
if (parent != null) {
cb.parent(tr.getRevWalk().parseCommit(parent.getObjectId()));
}
update.setBranch(c.getDest().get());
update.setChangeId(c.getKey().get());
update.setCommit(tr.getRevWalk(), cb.create());
return update;
}
}
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 MergedByPushOp method updateChange.
@Override
public boolean updateChange(ChangeContext ctx) throws OrmException, IOException {
change = ctx.getChange();
correctBranch = refName.equals(change.getDest().get());
if (!correctBranch) {
return false;
}
if (patchSetProvider != null) {
// Caller might have also arranged for construction of a new patch set
// that is not present in the old notes so we can't use PatchSetUtil.
patchSet = patchSetProvider.get();
} else {
patchSet = checkNotNull(psUtil.get(ctx.getDb(), ctx.getNotes(), psId), "patch set %s not found", psId);
}
info = getPatchSetInfo(ctx);
ChangeUpdate update = ctx.getUpdate(psId);
Change.Status status = change.getStatus();
if (status == Change.Status.MERGED) {
return true;
}
if (status.isOpen()) {
change.setCurrentPatchSet(info);
change.setStatus(Change.Status.MERGED);
// we cannot reconstruct the submit records for when this change was
// submitted, this is why we must fix the status
update.fixStatus(Change.Status.MERGED);
update.setCurrentPatchSet();
}
StringBuilder msgBuf = new StringBuilder();
msgBuf.append("Change has been successfully pushed");
if (!refName.equals(change.getDest().get())) {
msgBuf.append(" into ");
if (refName.startsWith(Constants.R_HEADS)) {
msgBuf.append("branch ");
msgBuf.append(Repository.shortenRefName(refName));
} else {
msgBuf.append(refName);
}
}
msgBuf.append(".");
ChangeMessage msg = ChangeMessagesUtil.newMessage(psId, ctx.getUser(), ctx.getWhen(), msgBuf.toString(), ChangeMessagesUtil.TAG_MERGED);
cmUtil.addChangeMessage(ctx.getDb(), update, msg);
PatchSetApproval submitter = ApprovalsUtil.newApproval(change.currentPatchSetId(), ctx.getUser(), LabelId.legacySubmit(), 1, ctx.getWhen());
update.putApproval(submitter.getLabel(), submitter.getValue());
ctx.getDb().patchSetApprovals().upsert(Collections.singleton(submitter));
return true;
}
Aggregations