Search in sources :

Example 11 with ResourceConflictException

use of com.google.gerrit.extensions.restapi.ResourceConflictException in project gerrit by GerritCodeReview.

the class PutDescription method apply.

@Override
public Response<String> apply(ProjectResource resource, DescriptionInput input) throws AuthException, ResourceConflictException, ResourceNotFoundException, IOException {
    if (input == null) {
        // Delete would set description to null.
        input = new DescriptionInput();
    }
    ProjectControl ctl = resource.getControl();
    IdentifiedUser user = ctl.getUser().asIdentifiedUser();
    if (!ctl.isOwner()) {
        throw new AuthException("not project owner");
    }
    try (MetaDataUpdate md = updateFactory.create(resource.getNameKey())) {
        ProjectConfig config = ProjectConfig.read(md);
        Project project = config.getProject();
        project.setDescription(Strings.emptyToNull(input.description));
        String msg = MoreObjects.firstNonNull(Strings.emptyToNull(input.commitMessage), "Updated description.\n");
        if (!msg.endsWith("\n")) {
            msg += "\n";
        }
        md.setAuthor(user);
        md.setMessage(msg);
        config.commit(md);
        cache.evict(ctl.getProject());
        md.getRepository().setGitwebDescription(project.getDescription());
        return Strings.isNullOrEmpty(project.getDescription()) ? Response.<String>none() : Response.ok(project.getDescription());
    } catch (RepositoryNotFoundException notFound) {
        throw new ResourceNotFoundException(resource.getName());
    } catch (ConfigInvalidException e) {
        throw new ResourceConflictException(String.format("invalid project.config: %s", e.getMessage()));
    }
}
Also used : ProjectConfig(com.google.gerrit.server.git.ProjectConfig) Project(com.google.gerrit.reviewdb.client.Project) ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) ConfigInvalidException(org.eclipse.jgit.errors.ConfigInvalidException) DescriptionInput(com.google.gerrit.extensions.api.projects.DescriptionInput) AuthException(com.google.gerrit.extensions.restapi.AuthException) RepositoryNotFoundException(org.eclipse.jgit.errors.RepositoryNotFoundException) IdentifiedUser(com.google.gerrit.server.IdentifiedUser) ResourceNotFoundException(com.google.gerrit.extensions.restapi.ResourceNotFoundException) MetaDataUpdate(com.google.gerrit.server.git.MetaDataUpdate)

Example 12 with ResourceConflictException

use of com.google.gerrit.extensions.restapi.ResourceConflictException in project gerrit by GerritCodeReview.

the class MergeOp method merge.

/**
   * Merges the given change.
   *
   * <p>Depending on the server configuration, more changes may be affected, e.g. by submission of a
   * topic or via superproject subscriptions. All affected changes are integrated using the projects
   * integration strategy.
   *
   * @param db the review database.
   * @param change the change to be merged.
   * @param caller the identity of the caller
   * @param checkSubmitRules whether the prolog submit rules should be evaluated
   * @param submitInput parameters regarding the merge
   * @throws OrmException an error occurred reading or writing the database.
   * @throws RestApiException if an error occurred.
   */
public void merge(ReviewDb db, Change change, IdentifiedUser caller, boolean checkSubmitRules, SubmitInput submitInput, boolean dryrun) throws OrmException, RestApiException {
    this.submitInput = submitInput;
    this.accountsToNotify = notifyUtil.resolveAccounts(submitInput.notifyDetails);
    this.dryrun = dryrun;
    this.caller = caller;
    this.ts = TimeUtil.nowTs();
    submissionId = RequestId.forChange(change);
    this.db = db;
    orm.setContext(db, ts, caller, submissionId);
    logDebug("Beginning integration of {}", change);
    try {
        ChangeSet cs = mergeSuperSet.setMergeOpRepoManager(orm).completeChangeSet(db, change, caller);
        checkState(cs.ids().contains(change.getId()), "change %s missing from %s", change.getId(), cs);
        if (cs.furtherHiddenChanges()) {
            throw new AuthException("A change to be submitted with " + change.getId() + " is not visible");
        }
        this.commitStatus = new CommitStatus(cs);
        MergeSuperSet.reloadChanges(cs);
        logDebug("Calculated to merge {}", cs);
        if (checkSubmitRules) {
            logDebug("Checking submit rules and state");
            checkSubmitRulesAndState(cs);
        } else {
            logDebug("Bypassing submit rules");
            bypassSubmitRules(cs);
        }
        try {
            integrateIntoHistory(cs);
        } catch (IntegrationException e) {
            logError("Error from integrateIntoHistory", e);
            throw new ResourceConflictException(e.getMessage(), e);
        }
    } catch (IOException e) {
        // Anything before the merge attempt is an error
        throw new OrmException(e);
    }
}
Also used : ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) OrmException(com.google.gwtorm.server.OrmException) AuthException(com.google.gerrit.extensions.restapi.AuthException) IOException(java.io.IOException)

Example 13 with ResourceConflictException

use of com.google.gerrit.extensions.restapi.ResourceConflictException in project gerrit by GerritCodeReview.

the class FixReplacementInterpreter method toTreeModifications.

/**
   * Transforms the given {@code FixReplacement}s into {@code TreeModification}s.
   *
   * @param repository the affected Git repository
   * @param projectState the affected project
   * @param patchSetCommitId the patch set which should be modified
   * @param fixReplacements the replacements which should be applied
   * @return a list of {@code TreeModification}s representing the given replacements
   * @throws ResourceNotFoundException if a file to which one of the replacements refers doesn't
   *     exist
   * @throws ResourceConflictException if the replacements can't be transformed into {@code
   *     TreeModification}s
   */
public List<TreeModification> toTreeModifications(Repository repository, ProjectState projectState, ObjectId patchSetCommitId, List<FixReplacement> fixReplacements) throws ResourceNotFoundException, IOException, ResourceConflictException {
    checkNotNull(fixReplacements, "Fix replacements must not be null");
    Map<String, List<FixReplacement>> fixReplacementsPerFilePath = fixReplacements.stream().collect(Collectors.groupingBy(fixReplacement -> fixReplacement.path));
    List<TreeModification> treeModifications = new ArrayList<>();
    for (Map.Entry<String, List<FixReplacement>> entry : fixReplacementsPerFilePath.entrySet()) {
        TreeModification treeModification = toTreeModification(repository, projectState, patchSetCommitId, entry.getKey(), entry.getValue());
        treeModifications.add(treeModification);
    }
    return treeModifications;
}
Also used : ResourceNotFoundException(com.google.gerrit.extensions.restapi.ResourceNotFoundException) TreeModification(com.google.gerrit.server.edit.tree.TreeModification) Inject(com.google.inject.Inject) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) RawInputUtil(com.google.gerrit.common.RawInputUtil) ChangeFileContentModification(com.google.gerrit.server.edit.tree.ChangeFileContentModification) ProjectState(com.google.gerrit.server.project.ProjectState) FixReplacement(com.google.gerrit.reviewdb.client.FixReplacement) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) ObjectId(org.eclipse.jgit.lib.ObjectId) Comment(com.google.gerrit.reviewdb.client.Comment) BinaryResult(com.google.gerrit.extensions.restapi.BinaryResult) List(java.util.List) ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) FileContentUtil(com.google.gerrit.server.change.FileContentUtil) Map(java.util.Map) Comparator(java.util.Comparator) Repository(org.eclipse.jgit.lib.Repository) Singleton(com.google.inject.Singleton) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) TreeModification(com.google.gerrit.server.edit.tree.TreeModification) Map(java.util.Map)

Example 14 with ResourceConflictException

use of com.google.gerrit.extensions.restapi.ResourceConflictException in project gerrit by GerritCodeReview.

the class AbandonOp method updateChange.

@Override
public boolean updateChange(ChangeContext ctx) throws OrmException, ResourceConflictException {
    change = ctx.getChange();
    PatchSet.Id psId = change.currentPatchSetId();
    ChangeUpdate update = ctx.getUpdate(psId);
    if (!change.getStatus().isOpen()) {
        throw new ResourceConflictException("change is " + ChangeUtil.status(change));
    } else if (change.getStatus() == Change.Status.DRAFT) {
        throw new ResourceConflictException("draft changes cannot be abandoned");
    }
    patchSet = psUtil.get(ctx.getDb(), ctx.getNotes(), psId);
    change.setStatus(Change.Status.ABANDONED);
    change.setLastUpdatedOn(ctx.getWhen());
    update.setStatus(change.getStatus());
    message = newMessage(ctx);
    cmUtil.addChangeMessage(ctx.getDb(), update, message);
    return true;
}
Also used : ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) PatchSet(com.google.gerrit.reviewdb.client.PatchSet) ChangeUpdate(com.google.gerrit.server.notedb.ChangeUpdate)

Example 15 with ResourceConflictException

use of com.google.gerrit.extensions.restapi.ResourceConflictException in project gerrit by GerritCodeReview.

the class DeleteBranchesIT method deleteBranchesForbidden.

@Test
public void deleteBranchesForbidden() throws Exception {
    DeleteBranchesInput input = new DeleteBranchesInput();
    input.branches = BRANCHES;
    setApiUser(user);
    try {
        project().deleteBranches(input);
        fail("Expected ResourceConflictException");
    } catch (ResourceConflictException e) {
        assertThat(e).hasMessageThat().isEqualTo(errorMessageForBranches(BRANCHES));
    }
    setApiUser(admin);
    assertBranches(BRANCHES);
}
Also used : ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) DeleteBranchesInput(com.google.gerrit.extensions.api.projects.DeleteBranchesInput) Test(org.junit.Test) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest)

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