Search in sources :

Example 66 with ResourceConflictException

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);
}
Also used : ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) MergeConflictException(com.google.gerrit.extensions.restapi.MergeConflictException) ObjectId(org.eclipse.jgit.lib.ObjectId) CommitBuilder(org.eclipse.jgit.lib.CommitBuilder) ThreeWayMerger(org.eclipse.jgit.merge.ThreeWayMerger) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 67 with ResourceConflictException

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;
}
Also used : ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) ValidationException(com.google.gerrit.server.validators.ValidationException) AssigneeValidationListener(com.google.gerrit.server.validators.AssigneeValidationListener) ChangeUpdate(com.google.gerrit.server.notedb.ChangeUpdate)

Example 68 with ResourceConflictException

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();
}
Also used : SubmitRecord(com.google.gerrit.common.data.SubmitRecord) ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) PatchSet(com.google.gerrit.reviewdb.client.PatchSet)

Example 69 with ResourceConflictException

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();
}
Also used : Comment(com.google.gerrit.reviewdb.client.Comment) ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) ArrayList(java.util.ArrayList) FixReplacement(com.google.gerrit.reviewdb.client.FixReplacement)

Example 70 with ResourceConflictException

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

Aggregations

ResourceConflictException (com.google.gerrit.extensions.restapi.ResourceConflictException)75 AuthException (com.google.gerrit.extensions.restapi.AuthException)25 BadRequestException (com.google.gerrit.extensions.restapi.BadRequestException)21 Change (com.google.gerrit.reviewdb.client.Change)19 ResourceNotFoundException (com.google.gerrit.extensions.restapi.ResourceNotFoundException)18 Project (com.google.gerrit.reviewdb.client.Project)17 IOException (java.io.IOException)17 PatchSet (com.google.gerrit.reviewdb.client.PatchSet)14 Repository (org.eclipse.jgit.lib.Repository)14 UnprocessableEntityException (com.google.gerrit.extensions.restapi.UnprocessableEntityException)13 BatchUpdate (com.google.gerrit.server.update.BatchUpdate)12 OrmException (com.google.gwtorm.server.OrmException)12 ConfigInvalidException (org.eclipse.jgit.errors.ConfigInvalidException)12 ObjectId (org.eclipse.jgit.lib.ObjectId)12 RevWalk (org.eclipse.jgit.revwalk.RevWalk)12 AbstractDaemonTest (com.google.gerrit.acceptance.AbstractDaemonTest)10 ArrayList (java.util.ArrayList)10 RepositoryNotFoundException (org.eclipse.jgit.errors.RepositoryNotFoundException)10 ProjectConfig (com.google.gerrit.server.git.ProjectConfig)9 RevCommit (org.eclipse.jgit.revwalk.RevCommit)9