use of com.google.gerrit.extensions.restapi.ResourceConflictException in project gerrit by GerritCodeReview.
the class RebaseChangeOp method rebaseCommit.
/**
* Rebase a commit.
*
* @param ctx repo context.
* @param original the commit to rebase.
* @param base base to rebase against.
* @return the rebased commit.
* @throws MergeConflictException the rebase failed due to a merge conflict.
* @throws IOException the merge failed for another reason.
*/
private RevCommit rebaseCommit(RepoContext ctx, RevCommit original, ObjectId base, String commitMessage) throws ResourceConflictException, IOException {
RevCommit parentCommit = original.getParent(0);
if (base.equals(parentCommit)) {
throw new ResourceConflictException("Change is already up to date.");
}
ThreeWayMerger merger = newMergeUtil().newThreeWayMerger(ctx.getInserter(), ctx.getRepoView().getConfig());
merger.setBase(parentCommit);
merger.merge(original, base);
if (merger.getResultTreeId() == null) {
throw new MergeConflictException("The change could not be rebased due to a conflict during merge.");
}
CommitBuilder cb = new CommitBuilder();
cb.setTreeId(merger.getResultTreeId());
cb.setParentId(base);
cb.setAuthor(original.getAuthorIdent());
cb.setMessage(commitMessage);
if (committerIdent != null) {
cb.setCommitter(committerIdent);
} else {
cb.setCommitter(ctx.getIdentifiedUser().newCommitterIdent(ctx.getWhen(), ctx.getTimeZone()));
}
ObjectId objectId = ctx.getInserter().insert(cb);
ctx.getInserter().flush();
return ctx.getRevWalk().parseCommit(objectId);
}
use of com.google.gerrit.extensions.restapi.ResourceConflictException in project gerrit by GerritCodeReview.
the class SetAssigneeOp method updateChange.
@Override
public boolean updateChange(ChangeContext ctx) throws OrmException, RestApiException {
change = ctx.getChange();
if (newAssignee.getAccountId().equals(change.getAssignee())) {
return false;
}
try {
for (AssigneeValidationListener validator : validationListeners) {
validator.validateAssignee(change, newAssignee.getAccount());
}
} catch (ValidationException e) {
throw new ResourceConflictException(e.getMessage());
}
if (change.getAssignee() != null) {
oldAssignee = userFactory.create(change.getAssignee());
}
ChangeUpdate update = ctx.getUpdate(change.currentPatchSetId());
// notedb
update.setAssignee(newAssignee.getAccountId());
// reviewdb
change.setAssignee(newAssignee.getAccountId());
addMessage(ctx, update);
return true;
}
use of com.google.gerrit.extensions.restapi.ResourceConflictException in project gerrit by GerritCodeReview.
the class MergeOp method checkSubmitRule.
public static void checkSubmitRule(ChangeData cd) throws ResourceConflictException, OrmException {
PatchSet patchSet = cd.currentPatchSet();
if (patchSet == null) {
throw new ResourceConflictException("missing current patch set for change " + cd.getId());
}
List<SubmitRecord> results = getSubmitRecords(cd);
if (SubmitRecord.findOkRecord(results).isPresent()) {
// Rules supplied a valid solution.
return;
} else if (results.isEmpty()) {
throw new IllegalStateException(String.format("SubmitRuleEvaluator.evaluate for change %s returned empty list for %s in %s", cd.getId(), patchSet.getId(), cd.change().getProject().get()));
}
for (SubmitRecord record : results) {
switch(record.status) {
case CLOSED:
throw new ResourceConflictException("change is closed");
case RULE_ERROR:
throw new ResourceConflictException("submit rule error: " + record.errorMessage);
case NOT_READY:
throw new ResourceConflictException(describeLabels(cd, record.labels));
case FORCED:
case OK:
default:
throw new IllegalStateException(String.format("Unexpected SubmitRecord status %s for %s in %s", record.status, patchSet.getId().getId(), cd.change().getProject().get()));
}
}
throw new IllegalStateException();
}
use of com.google.gerrit.extensions.restapi.ResourceConflictException in project gerrit by GerritCodeReview.
the class FixReplacementInterpreter method getNewFileContent.
private static String getNewFileContent(String fileContent, List<FixReplacement> fixReplacements) throws ResourceConflictException {
List<FixReplacement> sortedReplacements = new ArrayList<>(fixReplacements);
sortedReplacements.sort(ASC_RANGE_FIX_REPLACEMENT_COMPARATOR);
LineIdentifier lineIdentifier = new LineIdentifier(fileContent);
StringModifier fileContentModifier = new StringModifier(fileContent);
for (FixReplacement fixReplacement : sortedReplacements) {
Comment.Range range = fixReplacement.range;
try {
int startLineIndex = lineIdentifier.getStartIndexOfLine(range.startLine);
int startLineLength = lineIdentifier.getLengthOfLine(range.startLine);
int endLineIndex = lineIdentifier.getStartIndexOfLine(range.endLine);
int endLineLength = lineIdentifier.getLengthOfLine(range.endLine);
if (range.startChar > startLineLength || range.endChar > endLineLength) {
throw new ResourceConflictException(String.format("Range %s refers to a non-existent offset (start line length: %s," + " end line length: %s)", toString(range), startLineLength, endLineLength));
}
int startIndex = startLineIndex + range.startChar;
int endIndex = endLineIndex + range.endChar;
fileContentModifier.replace(startIndex, endIndex, fixReplacement.replacement);
} catch (StringIndexOutOfBoundsException e) {
// need to map this exception and thus provide a meaningful error.
throw new ResourceConflictException(String.format("Cannot apply fix replacement for range %s", toString(range)), e);
}
}
return fileContentModifier.getResult();
}
use of com.google.gerrit.extensions.restapi.ResourceConflictException 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());
}
}
Aggregations