Search in sources :

Example 71 with ResourceConflictException

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

the class MergeOp method checkSubmitRulesAndState.

private void checkSubmitRulesAndState(ChangeSet cs) throws ResourceConflictException {
    checkArgument(!cs.furtherHiddenChanges(), "checkSubmitRulesAndState called for topic with hidden change");
    for (ChangeData cd : cs.changes()) {
        try {
            if (cd.change().getStatus() != Change.Status.NEW) {
                commitStatus.problem(cd.getId(), "Change " + cd.getId() + " is " + cd.change().getStatus().toString().toLowerCase());
            } else if (cd.change().isWorkInProgress()) {
                commitStatus.problem(cd.getId(), "Change " + cd.getId() + " is work in progress");
            } else {
                checkSubmitRule(cd);
            }
        } catch (ResourceConflictException e) {
            commitStatus.problem(cd.getId(), e.getMessage());
        } catch (OrmException e) {
            String msg = "Error checking submit rules for change";
            log.warn(msg + " " + cd.getId(), e);
            commitStatus.problem(cd.getId(), msg);
        }
    }
    commitStatus.maybeFailVerbose();
}
Also used : ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) OrmException(com.google.gwtorm.server.OrmException) ChangeData(com.google.gerrit.server.query.change.ChangeData)

Example 72 with ResourceConflictException

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

the class PutConfig method apply.

public ConfigInfo apply(ProjectControl ctrl, ConfigInput input) throws ResourceNotFoundException, BadRequestException, ResourceConflictException {
    Project.NameKey projectName = ctrl.getProject().getNameKey();
    if (input == null) {
        throw new BadRequestException("config is required");
    }
    try (MetaDataUpdate md = metaDataUpdateFactory.get().create(projectName)) {
        ProjectConfig projectConfig = ProjectConfig.read(md);
        Project p = projectConfig.getProject();
        p.setDescription(Strings.emptyToNull(input.description));
        if (input.useContributorAgreements != null) {
            p.setUseContributorAgreements(input.useContributorAgreements);
        }
        if (input.useContentMerge != null) {
            p.setUseContentMerge(input.useContentMerge);
        }
        if (input.useSignedOffBy != null) {
            p.setUseSignedOffBy(input.useSignedOffBy);
        }
        if (input.createNewChangeForAllNotInTarget != null) {
            p.setCreateNewChangeForAllNotInTarget(input.createNewChangeForAllNotInTarget);
        }
        if (input.requireChangeId != null) {
            p.setRequireChangeID(input.requireChangeId);
        }
        if (serverEnableSignedPush) {
            if (input.enableSignedPush != null) {
                p.setEnableSignedPush(input.enableSignedPush);
            }
            if (input.requireSignedPush != null) {
                p.setRequireSignedPush(input.requireSignedPush);
            }
        }
        if (input.rejectImplicitMerges != null) {
            p.setRejectImplicitMerges(input.rejectImplicitMerges);
        }
        if (input.maxObjectSizeLimit != null) {
            p.setMaxObjectSizeLimit(input.maxObjectSizeLimit);
        }
        if (input.submitType != null) {
            p.setSubmitType(input.submitType);
        }
        if (input.state != null) {
            p.setState(input.state);
        }
        if (input.enableReviewerByEmail != null) {
            p.setEnableReviewerByEmail(input.enableReviewerByEmail);
        }
        if (input.pluginConfigValues != null) {
            setPluginConfigValues(ctrl.getProjectState(), projectConfig, input.pluginConfigValues);
        }
        md.setMessage("Modified project settings\n");
        try {
            projectConfig.commit(md);
            projectCache.evict(projectConfig.getProject());
            md.getRepository().setGitwebDescription(p.getDescription());
        } catch (IOException e) {
            if (e.getCause() instanceof ConfigInvalidException) {
                throw new ResourceConflictException("Cannot update " + projectName + ": " + e.getCause().getMessage());
            }
            log.warn(String.format("Failed to update config of project %s.", projectName), e);
            throw new ResourceConflictException("Cannot update " + projectName);
        }
        ProjectState state = projectStateFactory.create(projectConfig);
        return new ConfigInfoImpl(serverEnableSignedPush, state.controlFor(user.get()), config, pluginConfigEntries, cfgFactory, allProjects, uiActions, views);
    } catch (RepositoryNotFoundException notFound) {
        throw new ResourceNotFoundException(projectName.get());
    } catch (ConfigInvalidException err) {
        throw new ResourceConflictException("Cannot read project " + projectName, err);
    } catch (IOException err) {
        throw new ResourceConflictException("Cannot update project " + projectName, err);
    }
}
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) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) IOException(java.io.IOException) RepositoryNotFoundException(org.eclipse.jgit.errors.RepositoryNotFoundException) ResourceNotFoundException(com.google.gerrit.extensions.restapi.ResourceNotFoundException) MetaDataUpdate(com.google.gerrit.server.git.MetaDataUpdate)

Example 73 with ResourceConflictException

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

the class SetDefaultDashboard method apply.

@Override
public Response<DashboardInfo> apply(DashboardResource resource, Input input) throws AuthException, BadRequestException, ResourceConflictException, ResourceNotFoundException, IOException {
    if (input == null) {
        // Delete would set input to null.
        input = new Input();
    }
    input.id = Strings.emptyToNull(input.id);
    ProjectControl ctl = resource.getControl();
    if (!ctl.isOwner()) {
        throw new AuthException("not project owner");
    }
    DashboardResource target = null;
    if (input.id != null) {
        try {
            target = dashboards.parse(new ProjectResource(ctl), IdString.fromUrl(input.id));
        } catch (ResourceNotFoundException e) {
            throw new BadRequestException("dashboard " + input.id + " not found");
        } catch (ConfigInvalidException e) {
            throw new ResourceConflictException(e.getMessage());
        }
    }
    try (MetaDataUpdate md = updateFactory.create(ctl.getProject().getNameKey())) {
        ProjectConfig config = ProjectConfig.read(md);
        Project project = config.getProject();
        if (inherited) {
            project.setDefaultDashboard(input.id);
        } else {
            project.setLocalDefaultDashboard(input.id);
        }
        String msg = MoreObjects.firstNonNull(Strings.emptyToNull(input.commitMessage), input.id == null ? "Removed default dashboard.\n" : String.format("Changed default dashboard to %s.\n", input.id));
        if (!msg.endsWith("\n")) {
            msg += "\n";
        }
        md.setAuthor(ctl.getUser().asIdentifiedUser());
        md.setMessage(msg);
        config.commit(md);
        cache.evict(ctl.getProject());
        if (target != null) {
            DashboardInfo info = get.get().apply(target);
            info.isDefault = true;
            return Response.ok(info);
        }
        return Response.none();
    } catch (RepositoryNotFoundException notFound) {
        throw new ResourceNotFoundException(ctl.getProject().getName());
    } catch (ConfigInvalidException e) {
        throw new ResourceConflictException(String.format("invalid project.config: %s", e.getMessage()));
    }
}
Also used : ConfigInvalidException(org.eclipse.jgit.errors.ConfigInvalidException) AuthException(com.google.gerrit.extensions.restapi.AuthException) IdString(com.google.gerrit.extensions.restapi.IdString) RepositoryNotFoundException(org.eclipse.jgit.errors.RepositoryNotFoundException) DashboardInfo(com.google.gerrit.server.project.DashboardsCollection.DashboardInfo) ProjectConfig(com.google.gerrit.server.git.ProjectConfig) Project(com.google.gerrit.reviewdb.client.Project) Input(com.google.gerrit.server.project.SetDashboard.Input) ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) ResourceNotFoundException(com.google.gerrit.extensions.restapi.ResourceNotFoundException) MetaDataUpdate(com.google.gerrit.server.git.MetaDataUpdate)

Example 74 with ResourceConflictException

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

the class SetParent method validateParentUpdate.

public void validateParentUpdate(final ProjectControl ctl, String newParent, boolean checkIfAdmin) throws AuthException, ResourceConflictException, UnprocessableEntityException, PermissionBackendException {
    IdentifiedUser user = ctl.getUser().asIdentifiedUser();
    if (checkIfAdmin) {
        permissionBackend.user(user).check(GlobalPermission.ADMINISTRATE_SERVER);
    }
    if (ctl.getProject().getNameKey().equals(allProjects)) {
        throw new ResourceConflictException("cannot set parent of " + allProjects.get());
    }
    newParent = Strings.emptyToNull(newParent);
    if (newParent != null) {
        ProjectState parent = cache.get(new Project.NameKey(newParent));
        if (parent == null) {
            throw new UnprocessableEntityException("parent project " + newParent + " not found");
        }
        if (Iterables.tryFind(parent.tree(), p -> {
            return p.getProject().getNameKey().equals(ctl.getProject().getNameKey());
        }).isPresent()) {
            throw new ResourceConflictException("cycle exists between " + ctl.getProject().getName() + " and " + parent.getProject().getName());
        }
    }
}
Also used : ResourceNotFoundException(com.google.gerrit.extensions.restapi.ResourceNotFoundException) Project(com.google.gerrit.reviewdb.client.Project) Iterables(com.google.common.collect.Iterables) GlobalPermission(com.google.gerrit.server.permissions.GlobalPermission) PermissionBackendException(com.google.gerrit.server.permissions.PermissionBackendException) ConfigInvalidException(org.eclipse.jgit.errors.ConfigInvalidException) UnprocessableEntityException(com.google.gerrit.extensions.restapi.UnprocessableEntityException) RepositoryNotFoundException(org.eclipse.jgit.errors.RepositoryNotFoundException) MetaDataUpdate(com.google.gerrit.server.git.MetaDataUpdate) Inject(com.google.inject.Inject) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) MoreObjects(com.google.common.base.MoreObjects) IOException(java.io.IOException) PermissionBackend(com.google.gerrit.server.permissions.PermissionBackend) DefaultInput(com.google.gerrit.extensions.restapi.DefaultInput) RestModifyView(com.google.gerrit.extensions.restapi.RestModifyView) Strings(com.google.common.base.Strings) AllProjectsName(com.google.gerrit.server.config.AllProjectsName) ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) IdentifiedUser(com.google.gerrit.server.IdentifiedUser) AuthException(com.google.gerrit.extensions.restapi.AuthException) ProjectConfig(com.google.gerrit.server.git.ProjectConfig) Input(com.google.gerrit.server.project.SetParent.Input) Singleton(com.google.inject.Singleton) Project(com.google.gerrit.reviewdb.client.Project) UnprocessableEntityException(com.google.gerrit.extensions.restapi.UnprocessableEntityException) ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) IdentifiedUser(com.google.gerrit.server.IdentifiedUser)

Example 75 with ResourceConflictException

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

the class SetParent method apply.

public String apply(ProjectResource rsrc, Input input, boolean checkIfAdmin) throws AuthException, ResourceConflictException, ResourceNotFoundException, UnprocessableEntityException, IOException, PermissionBackendException {
    ProjectControl ctl = rsrc.getControl();
    String parentName = MoreObjects.firstNonNull(Strings.emptyToNull(input.parent), allProjects.get());
    validateParentUpdate(ctl, parentName, checkIfAdmin);
    try (MetaDataUpdate md = updateFactory.create(rsrc.getNameKey())) {
        ProjectConfig config = ProjectConfig.read(md);
        Project project = config.getProject();
        project.setParentName(parentName);
        String msg = Strings.emptyToNull(input.commitMessage);
        if (msg == null) {
            msg = String.format("Changed parent to %s.\n", parentName);
        } else if (!msg.endsWith("\n")) {
            msg += "\n";
        }
        md.setAuthor(ctl.getUser().asIdentifiedUser());
        md.setMessage(msg);
        config.commit(md);
        cache.evict(ctl.getProject());
        Project.NameKey parent = project.getParent(allProjects);
        checkNotNull(parent);
        return parent.get();
    } catch (RepositoryNotFoundException notFound) {
        throw new ResourceNotFoundException(rsrc.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) RepositoryNotFoundException(org.eclipse.jgit.errors.RepositoryNotFoundException) ResourceNotFoundException(com.google.gerrit.extensions.restapi.ResourceNotFoundException) MetaDataUpdate(com.google.gerrit.server.git.MetaDataUpdate)

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