Search in sources :

Example 1 with UpdateParentFailedException

use of com.google.gerrit.common.errors.UpdateParentFailedException in project gerrit by GerritCodeReview.

the class ProjectAccessHandler method call.

@Override
public final T call() throws NoSuchProjectException, IOException, ConfigInvalidException, InvalidNameException, NoSuchGroupException, OrmException, UpdateParentFailedException, PermissionDeniedException, PermissionBackendException {
    final ProjectControl projectControl = projectControlFactory.controlFor(projectName);
    Capable r = projectControl.canPushToAtLeastOneRef();
    if (r != Capable.OK) {
        throw new PermissionDeniedException(r.getMessage());
    }
    try (MetaDataUpdate md = metaDataUpdateFactory.create(projectName)) {
        ProjectConfig config = ProjectConfig.read(md, base);
        Set<String> toDelete = scanSectionNames(config);
        for (AccessSection section : mergeSections(sectionList)) {
            String name = section.getName();
            if (AccessSection.GLOBAL_CAPABILITIES.equals(name)) {
                if (checkIfOwner && !projectControl.isOwner()) {
                    continue;
                }
                replace(config, toDelete, section);
            } else if (AccessSection.isValid(name)) {
                if (checkIfOwner && !projectControl.controlForRef(name).isOwner()) {
                    continue;
                }
                RefPattern.validate(name);
                replace(config, toDelete, section);
            }
        }
        for (String name : toDelete) {
            if (AccessSection.GLOBAL_CAPABILITIES.equals(name)) {
                if (!checkIfOwner || projectControl.isOwner()) {
                    config.remove(config.getAccessSection(name));
                }
            } else if (!checkIfOwner || projectControl.controlForRef(name).isOwner()) {
                config.remove(config.getAccessSection(name));
            }
        }
        boolean parentProjectUpdate = false;
        if (!config.getProject().getNameKey().equals(allProjects) && !config.getProject().getParent(allProjects).equals(parentProjectName)) {
            parentProjectUpdate = true;
            try {
                setParent.get().validateParentUpdate(projectControl, MoreObjects.firstNonNull(parentProjectName, allProjects).get(), checkIfOwner);
            } catch (AuthException e) {
                throw new UpdateParentFailedException("You are not allowed to change the parent project since you are " + "not an administrator. You may save the modifications for review " + "so that an administrator can approve them.", e);
            } catch (ResourceConflictException | UnprocessableEntityException e) {
                throw new UpdateParentFailedException(e.getMessage(), e);
            }
            config.getProject().setParentName(parentProjectName);
        }
        if (message != null && !message.isEmpty()) {
            if (!message.endsWith("\n")) {
                message += "\n";
            }
            md.setMessage(message);
        } else {
            md.setMessage("Modify access rules\n");
        }
        return updateProjectConfig(projectControl, config, md, parentProjectUpdate);
    } catch (RepositoryNotFoundException notFound) {
        throw new NoSuchProjectException(projectName);
    }
}
Also used : UnprocessableEntityException(com.google.gerrit.extensions.restapi.UnprocessableEntityException) NoSuchProjectException(com.google.gerrit.server.project.NoSuchProjectException) UpdateParentFailedException(com.google.gerrit.common.errors.UpdateParentFailedException) AuthException(com.google.gerrit.extensions.restapi.AuthException) RepositoryNotFoundException(org.eclipse.jgit.errors.RepositoryNotFoundException) ProjectControl(com.google.gerrit.server.project.ProjectControl) AccessSection(com.google.gerrit.common.data.AccessSection) ProjectConfig(com.google.gerrit.server.git.ProjectConfig) ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) Capable(com.google.gerrit.common.data.Capable) PermissionDeniedException(com.google.gerrit.common.errors.PermissionDeniedException) MetaDataUpdate(com.google.gerrit.server.git.MetaDataUpdate)

Aggregations

AccessSection (com.google.gerrit.common.data.AccessSection)1 Capable (com.google.gerrit.common.data.Capable)1 PermissionDeniedException (com.google.gerrit.common.errors.PermissionDeniedException)1 UpdateParentFailedException (com.google.gerrit.common.errors.UpdateParentFailedException)1 AuthException (com.google.gerrit.extensions.restapi.AuthException)1 ResourceConflictException (com.google.gerrit.extensions.restapi.ResourceConflictException)1 UnprocessableEntityException (com.google.gerrit.extensions.restapi.UnprocessableEntityException)1 MetaDataUpdate (com.google.gerrit.server.git.MetaDataUpdate)1 ProjectConfig (com.google.gerrit.server.git.ProjectConfig)1 NoSuchProjectException (com.google.gerrit.server.project.NoSuchProjectException)1 ProjectControl (com.google.gerrit.server.project.ProjectControl)1 RepositoryNotFoundException (org.eclipse.jgit.errors.RepositoryNotFoundException)1