Search in sources :

Example 86 with ResourceConflictException

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

the class PureRevert method get.

public boolean get(ChangeNotes notes, Optional<String> claimedOriginal) throws IOException, BadRequestException, ResourceConflictException {
    PatchSet currentPatchSet = notes.getCurrentPatchSet();
    if (currentPatchSet == null) {
        throw new ResourceConflictException("current revision is missing");
    }
    if (!claimedOriginal.isPresent()) {
        return pureRevertCache.isPureRevert(notes);
    }
    ObjectId claimedOriginalObjectId;
    try {
        claimedOriginalObjectId = ObjectId.fromString(claimedOriginal.get());
    } catch (InvalidObjectIdException e) {
        throw new BadRequestException("invalid object ID", e);
    }
    return pureRevertCache.isPureRevert(notes.getProjectName(), notes.getCurrentPatchSet().commitId(), claimedOriginalObjectId);
}
Also used : ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) ObjectId(org.eclipse.jgit.lib.ObjectId) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) PatchSet(com.google.gerrit.entities.PatchSet) InvalidObjectIdException(org.eclipse.jgit.errors.InvalidObjectIdException)

Example 87 with ResourceConflictException

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

the class SetParent method validateParentUpdate.

public void validateParentUpdate(Project.NameKey project, IdentifiedUser user, String newParent, boolean checkIfAdmin) throws AuthException, ResourceConflictException, UnprocessableEntityException, PermissionBackendException, BadRequestException {
    if (checkIfAdmin) {
        if (allowProjectOwnersToChangeParent) {
            permissionBackend.user(user).project(project).check(ProjectPermission.WRITE_CONFIG);
        } else {
            permissionBackend.user(user).check(GlobalPermission.ADMINISTRATE_SERVER);
        }
    }
    if (project.equals(allUsers) && !allProjects.get().equals(newParent)) {
        throw new BadRequestException(String.format("%s must inherit from %s", allUsers.get(), allProjects.get()));
    }
    if (project.equals(allProjects)) {
        throw new ResourceConflictException("cannot set parent of " + allProjects.get());
    }
    if (allUsers.get().equals(newParent)) {
        throw new ResourceConflictException(String.format("Cannot inherit from '%s' project", allUsers.get()));
    }
    newParent = Strings.emptyToNull(newParent);
    if (newParent != null) {
        Project.NameKey newParentNameKey = Project.nameKey(newParent);
        ProjectState parent = cache.get(newParentNameKey).orElseThrow(() -> new UnprocessableEntityException("parent project " + newParentNameKey + " not found"));
        if (parent.getName().equals(project.get())) {
            throw new ResourceConflictException("cannot set parent to self");
        }
        if (Iterables.tryFind(parent.tree(), p -> p.getNameKey().equals(project)).isPresent()) {
            throw new ResourceConflictException("cycle exists between " + project.get() + " and " + parent.getName());
        }
    }
}
Also used : ResourceNotFoundException(com.google.gerrit.extensions.restapi.ResourceNotFoundException) AllUsersName(com.google.gerrit.server.config.AllUsersName) Iterables(com.google.common.collect.Iterables) PermissionBackendException(com.google.gerrit.server.permissions.PermissionBackendException) ProjectConfig(com.google.gerrit.server.project.ProjectConfig) ConfigInvalidException(org.eclipse.jgit.errors.ConfigInvalidException) ProjectCache(com.google.gerrit.server.project.ProjectCache) UnprocessableEntityException(com.google.gerrit.extensions.restapi.UnprocessableEntityException) GerritConfigListener(com.google.gerrit.server.config.GerritConfigListener) RepositoryNotFoundException(org.eclipse.jgit.errors.RepositoryNotFoundException) Inject(com.google.inject.Inject) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) Multimap(com.google.common.collect.Multimap) Response(com.google.gerrit.extensions.restapi.Response) PermissionBackend(com.google.gerrit.server.permissions.PermissionBackend) ConfigUpdateEntry(com.google.gerrit.server.config.ConfigUpdatedEvent.ConfigUpdateEntry) RestModifyView(com.google.gerrit.extensions.restapi.RestModifyView) Strings(com.google.common.base.Strings) Config(org.eclipse.jgit.lib.Config) ParentInput(com.google.gerrit.extensions.api.projects.ParentInput) Objects.requireNonNull(java.util.Objects.requireNonNull) AuthException(com.google.gerrit.extensions.restapi.AuthException) ConfigUpdatedEvent(com.google.gerrit.server.config.ConfigUpdatedEvent) GlobalPermission(com.google.gerrit.server.permissions.GlobalPermission) GerritServerConfig(com.google.gerrit.server.config.GerritServerConfig) ProjectState(com.google.gerrit.server.project.ProjectState) MoreObjects(com.google.common.base.MoreObjects) ProjectResource(com.google.gerrit.server.project.ProjectResource) IOException(java.io.IOException) UpdateResult(com.google.gerrit.server.config.ConfigUpdatedEvent.UpdateResult) ConfigKey(com.google.gerrit.server.config.ConfigKey) ProjectPermission(com.google.gerrit.server.permissions.ProjectPermission) Provider(com.google.inject.Provider) AllProjectsName(com.google.gerrit.server.config.AllProjectsName) ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) IdentifiedUser(com.google.gerrit.server.IdentifiedUser) Project(com.google.gerrit.entities.Project) MetaDataUpdate(com.google.gerrit.server.git.meta.MetaDataUpdate) Singleton(com.google.inject.Singleton) Project(com.google.gerrit.entities.Project) UnprocessableEntityException(com.google.gerrit.extensions.restapi.UnprocessableEntityException) ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) ProjectState(com.google.gerrit.server.project.ProjectState)

Example 88 with ResourceConflictException

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

the class PutConfig method apply.

public ConfigInfo apply(ProjectState projectState, ConfigInput input) throws ResourceNotFoundException, BadRequestException, ResourceConflictException {
    Project.NameKey projectName = projectState.getNameKey();
    if (input == null) {
        throw new BadRequestException("config is required");
    }
    try (MetaDataUpdate md = metaDataUpdateFactory.get().create(projectName)) {
        ProjectConfig projectConfig = projectConfigFactory.read(md);
        projectConfig.updateProject(p -> {
            p.setDescription(Strings.emptyToNull(input.description));
            for (BooleanProjectConfig cfg : BooleanProjectConfig.values()) {
                InheritableBoolean val = BooleanProjectConfigTransformations.get(cfg, input);
                if (val != null) {
                    p.setBooleanConfig(cfg, val);
                }
            }
            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.pluginConfigValues != null) {
            setPluginConfigValues(projectState, projectConfig, input.pluginConfigValues);
        }
        if (input.commentLinks != null) {
            updateCommentLinks(projectConfig, input.commentLinks);
        }
        md.setMessage("Modified project settings\n");
        try {
            projectConfig.commit(md);
            projectCache.evictAndReindex(projectConfig.getProject());
            md.getRepository().setGitwebDescription(projectConfig.getProject().getDescription());
        } catch (IOException e) {
            if (e.getCause() instanceof ConfigInvalidException) {
                throw new ResourceConflictException("Cannot update " + projectName + ": " + e.getCause().getMessage());
            }
            logger.atWarning().withCause(e).log("Failed to update config of project %s.", projectName);
            throw new ResourceConflictException("Cannot update " + projectName);
        }
        ProjectState state = projectStateFactory.create(projectConfigFactory.read(md).getCacheable());
        return ConfigInfoCreator.constructInfo(serverEnableSignedPush, state, user.get(), pluginConfigEntries, cfgFactory, allProjects, uiActions, views);
    } catch (RepositoryNotFoundException notFound) {
        throw new ResourceNotFoundException(projectName.get(), notFound);
    } catch (ConfigInvalidException err) {
        throw new ResourceConflictException("Cannot read project " + projectName, err);
    } catch (IOException err) {
        throw new ResourceConflictException("Cannot update project " + projectName, err);
    }
}
Also used : ConfigInvalidException(org.eclipse.jgit.errors.ConfigInvalidException) InheritableBoolean(com.google.gerrit.extensions.client.InheritableBoolean) IOException(java.io.IOException) RepositoryNotFoundException(org.eclipse.jgit.errors.RepositoryNotFoundException) ProjectConfig(com.google.gerrit.server.project.ProjectConfig) BooleanProjectConfig(com.google.gerrit.entities.BooleanProjectConfig) Project(com.google.gerrit.entities.Project) ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) ProjectState(com.google.gerrit.server.project.ProjectState) ResourceNotFoundException(com.google.gerrit.extensions.restapi.ResourceNotFoundException) MetaDataUpdate(com.google.gerrit.server.git.meta.MetaDataUpdate) BooleanProjectConfig(com.google.gerrit.entities.BooleanProjectConfig)

Example 89 with ResourceConflictException

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

the class GroupsIT method createGroupWithConfiguredNameOfSystemGroup_Conflict.

@Test
@GerritConfig(name = "groups.global:Anonymous-Users.name", value = "All Users")
public void createGroupWithConfiguredNameOfSystemGroup_Conflict() throws Exception {
    ResourceConflictException thrown = assertThrows(ResourceConflictException.class, () -> gApi.groups().create("all users"));
    assertThat(thrown).hasMessageThat().contains("group 'All Users' already exists");
}
Also used : ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) GerritConfig(com.google.gerrit.acceptance.config.GerritConfig) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest) Test(org.junit.Test)

Example 90 with ResourceConflictException

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

the class GroupsIT method createGroupWithDefaultNameOfSystemGroup_Conflict.

@Test
@GerritConfig(name = "groups.global:Anonymous-Users.name", value = "All Users")
public void createGroupWithDefaultNameOfSystemGroup_Conflict() throws Exception {
    ResourceConflictException thrown = assertThrows(ResourceConflictException.class, () -> gApi.groups().create("anonymous users"));
    assertThat(thrown).hasMessageThat().contains("group name 'Anonymous Users' is reserved");
}
Also used : ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) GerritConfig(com.google.gerrit.acceptance.config.GerritConfig) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest) Test(org.junit.Test)

Aggregations

ResourceConflictException (com.google.gerrit.extensions.restapi.ResourceConflictException)252 Test (org.junit.Test)106 AbstractDaemonTest (com.google.gerrit.acceptance.AbstractDaemonTest)102 BadRequestException (com.google.gerrit.extensions.restapi.BadRequestException)57 AuthException (com.google.gerrit.extensions.restapi.AuthException)46 PushOneCommit (com.google.gerrit.acceptance.PushOneCommit)44 ResourceNotFoundException (com.google.gerrit.extensions.restapi.ResourceNotFoundException)42 IOException (java.io.IOException)39 ObjectId (org.eclipse.jgit.lib.ObjectId)36 Repository (org.eclipse.jgit.lib.Repository)34 Change (com.google.gerrit.entities.Change)29 UnprocessableEntityException (com.google.gerrit.extensions.restapi.UnprocessableEntityException)28 RevCommit (org.eclipse.jgit.revwalk.RevCommit)27 ConfigInvalidException (org.eclipse.jgit.errors.ConfigInvalidException)26 RevWalk (org.eclipse.jgit.revwalk.RevWalk)25 BatchUpdate (com.google.gerrit.server.update.BatchUpdate)23 ArrayList (java.util.ArrayList)21 PatchSet (com.google.gerrit.entities.PatchSet)20 PermissionBackendException (com.google.gerrit.server.permissions.PermissionBackendException)20 ProjectState (com.google.gerrit.server.project.ProjectState)19