Search in sources :

Example 21 with ProjectConfig

use of com.google.gerrit.server.project.ProjectConfig in project gerrit by GerritCodeReview.

the class SetParent method apply.

public String apply(ProjectResource rsrc, ParentInput input, boolean checkIfAdmin) throws AuthException, ResourceConflictException, ResourceNotFoundException, UnprocessableEntityException, IOException, PermissionBackendException, BadRequestException {
    IdentifiedUser user = rsrc.getUser().asIdentifiedUser();
    String parentName = MoreObjects.firstNonNull(Strings.emptyToNull(input.parent), allProjects.get());
    validateParentUpdate(rsrc.getProjectState().getNameKey(), user, parentName, checkIfAdmin);
    try (MetaDataUpdate md = updateFactory.get().create(rsrc.getNameKey())) {
        ProjectConfig config = projectConfigFactory.read(md);
        config.updateProject(p -> p.setParent(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(user);
        md.setMessage(msg);
        config.commit(md);
        cache.evictAndReindex(rsrc.getProjectState().getProject());
        Project.NameKey parent = config.getProject().getParent(allProjects);
        requireNonNull(parent);
        return parent.get();
    } catch (RepositoryNotFoundException notFound) {
        throw new ResourceNotFoundException(rsrc.getName(), notFound);
    } catch (ConfigInvalidException e) {
        throw new ResourceConflictException(String.format("invalid project.config: %s", e.getMessage()));
    }
}
Also used : ProjectConfig(com.google.gerrit.server.project.ProjectConfig) Project(com.google.gerrit.entities.Project) ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) ConfigInvalidException(org.eclipse.jgit.errors.ConfigInvalidException) 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.meta.MetaDataUpdate)

Example 22 with ProjectConfig

use of com.google.gerrit.server.project.ProjectConfig in project gerrit by GerritCodeReview.

the class GrantRevertPermission method execute.

public void execute(Project.NameKey projectName) throws IOException, ConfigInvalidException {
    GroupReference registeredUsers = systemGroupBackend.getGroup(REGISTERED_USERS);
    try (Repository repo = repoManager.openRepository(projectName)) {
        MetaDataUpdate md = new MetaDataUpdate(GitReferenceUpdated.DISABLED, projectName, repo);
        ProjectConfig projectConfig = projectConfigFactory.read(md);
        AtomicBoolean shouldExit = new AtomicBoolean(false);
        projectConfig.upsertAccessSection(AccessSection.HEADS, heads -> {
            Permission permissionOnRefsHeads = heads.build().getPermission(Permission.REVERT);
            if (permissionOnRefsHeads != null) {
                if (permissionOnRefsHeads.getRule(registeredUsers) == null || permissionOnRefsHeads.getRules().size() > 1) {
                    // If admins already changed the permission, don't do anything.
                    shouldExit.set(true);
                    return;
                }
                // permission already exists in refs/heads/*, delete it for Registered Users.
                remove(projectConfig, heads, Permission.REVERT, registeredUsers);
            }
        });
        if (shouldExit.get()) {
            return;
        }
        projectConfig.upsertAccessSection(AccessSection.ALL, all -> {
            Permission permissionOnRefsStar = all.build().getPermission(Permission.REVERT);
            if (permissionOnRefsStar != null && permissionOnRefsStar.getRule(registeredUsers) != null) {
                // permission already exists in refs/*, don't do anything.
                return;
            }
            // If the permission doesn't exist of refs/* for Registered Users, grant it.
            grant(projectConfig, all, Permission.REVERT, registeredUsers);
        });
        md.getCommitBuilder().setAuthor(serverUser);
        md.getCommitBuilder().setCommitter(serverUser);
        md.setMessage("Add revert permission for all registered users\n");
        projectConfig.commit(md);
    }
}
Also used : ProjectConfig(com.google.gerrit.server.project.ProjectConfig) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Repository(org.eclipse.jgit.lib.Repository) Permission(com.google.gerrit.entities.Permission) GroupReference(com.google.gerrit.entities.GroupReference) MetaDataUpdate(com.google.gerrit.server.git.meta.MetaDataUpdate)

Example 23 with ProjectConfig

use of com.google.gerrit.server.project.ProjectConfig in project gerrit by GerritCodeReview.

the class QueryChangesIT method skipVisibility_noReadPermission.

@Test
@SuppressWarnings("unchecked")
public void skipVisibility_noReadPermission() throws Exception {
    createChange().getChangeId();
    requestScopeOperations.setApiUser(admin.id());
    QueryChanges queryChanges = queryChangesProvider.get();
    queryChanges.addQuery("is:open repo:" + project.get());
    List<List<ChangeInfo>> result = (List<List<ChangeInfo>>) queryChanges.apply(TopLevelResource.INSTANCE).value();
    assertThat(result).hasSize(1);
    try (ProjectConfigUpdate u = updateProject(allProjects)) {
        ProjectConfig cfg = u.getConfig();
        removeAllBranchPermissions(cfg, Permission.READ);
        u.save();
    }
    queryChanges = queryChangesProvider.get();
    queryChanges.addQuery("is:open repo:" + project.get());
    List<List<ChangeInfo>> result2 = (List<List<ChangeInfo>>) queryChanges.apply(TopLevelResource.INSTANCE).value();
    assertThat(result2).hasSize(0);
    queryChanges = queryChangesProvider.get();
    queryChanges.addQuery("is:open repo:" + project.get());
    queryChanges.skipVisibility(true);
    List<List<ChangeInfo>> result3 = (List<List<ChangeInfo>>) queryChanges.apply(TopLevelResource.INSTANCE).value();
    assertThat(result3).hasSize(1);
}
Also used : ProjectConfig(com.google.gerrit.server.project.ProjectConfig) ChangeInfo(com.google.gerrit.extensions.common.ChangeInfo) ImmutableList(com.google.common.collect.ImmutableList) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) QueryChanges(com.google.gerrit.server.restapi.change.QueryChanges) Test(org.junit.Test) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest)

Example 24 with ProjectConfig

use of com.google.gerrit.server.project.ProjectConfig in project gerrit by GerritCodeReview.

the class PluginConfig method withInheritance.

PluginConfig withInheritance(ProjectState.Factory projectStateFactory) {
    checkState(projectConfig().isPresent(), "no project config provided");
    ProjectState state = projectStateFactory.create(projectConfig().get());
    ProjectState parent = Iterables.getFirst(state.parents(), null);
    if (parent == null) {
        return this;
    }
    Map<AccountGroup.UUID, GroupReference> groupReferences = new HashMap<>();
    groupReferences.putAll(groupReferences());
    PluginConfig parentPluginConfig = parent.getPluginConfig(pluginName()).withInheritance(projectStateFactory);
    Set<String> allNames = cfg().getNames(PLUGIN, pluginName());
    Config newCfg = copyConfig(cfg());
    for (String name : parentPluginConfig.cfg().getNames(PLUGIN, pluginName())) {
        if (!allNames.contains(name)) {
            List<String> values = Arrays.asList(parentPluginConfig.cfg().getStringList(PLUGIN, pluginName(), name));
            for (String value : values) {
                Optional<GroupReference> groupRef = parentPluginConfig.projectConfig().get().getGroupByName(GroupReference.extractGroupName(value));
                if (groupRef.isPresent()) {
                    groupReferences.putIfAbsent(groupRef.get().getUUID(), groupRef.get());
                }
            }
            newCfg.setStringList(PLUGIN, pluginName(), name, values);
        }
    }
    return new AutoValue_PluginConfig(pluginName(), newCfg, projectConfig(), ImmutableMap.copyOf(groupReferences));
}
Also used : HashMap(java.util.HashMap) ProjectConfig(com.google.gerrit.server.project.ProjectConfig) Config(org.eclipse.jgit.lib.Config) CachedProjectConfig(com.google.gerrit.entities.CachedProjectConfig) ProjectState(com.google.gerrit.server.project.ProjectState) GroupReference(com.google.gerrit.entities.GroupReference)

Example 25 with ProjectConfig

use of com.google.gerrit.server.project.ProjectConfig in project gerrit by GerritCodeReview.

the class PostLabels method apply.

@Override
public Response<?> apply(ProjectResource rsrc, BatchLabelInput input) throws AuthException, UnprocessableEntityException, PermissionBackendException, IOException, ConfigInvalidException, BadRequestException, ResourceConflictException {
    if (!user.get().isIdentifiedUser()) {
        throw new AuthException("Authentication required");
    }
    permissionBackend.currentUser().project(rsrc.getNameKey()).check(ProjectPermission.WRITE_CONFIG);
    if (input == null) {
        input = new BatchLabelInput();
    }
    try (MetaDataUpdate md = updateFactory.create(rsrc.getNameKey())) {
        boolean dirty = false;
        ProjectConfig config = projectConfigFactory.read(md);
        if (input.delete != null && !input.delete.isEmpty()) {
            for (String labelName : input.delete) {
                if (!deleteLabel.deleteLabel(config, labelName.trim())) {
                    throw new UnprocessableEntityException(String.format("label %s not found", labelName));
                }
            }
            dirty = true;
        }
        if (input.create != null && !input.create.isEmpty()) {
            for (LabelDefinitionInput labelInput : input.create) {
                if (labelInput.name == null || labelInput.name.trim().isEmpty()) {
                    throw new BadRequestException("label name is required for new label");
                }
                if (labelInput.commitMessage != null) {
                    throw new BadRequestException("commit message on label definition input not supported");
                }
                createLabel.createLabel(config, labelInput.name.trim(), labelInput);
            }
            dirty = true;
        }
        if (input.update != null && !input.update.isEmpty()) {
            for (Map.Entry<String, LabelDefinitionInput> e : input.update.entrySet()) {
                LabelType labelType = config.getLabelSections().get(e.getKey().trim());
                if (labelType == null) {
                    throw new UnprocessableEntityException(String.format("label %s not found", e.getKey()));
                }
                if (e.getValue().commitMessage != null) {
                    throw new BadRequestException("commit message on label definition input not supported");
                }
                setLabel.updateLabel(config, labelType, e.getValue());
            }
            dirty = true;
        }
        if (input.commitMessage != null) {
            md.setMessage(Strings.emptyToNull(input.commitMessage.trim()));
        } else {
            md.setMessage("Update labels");
        }
        if (dirty) {
            config.commit(md);
            projectCache.evictAndReindex(rsrc.getProjectState().getProject());
        }
    }
    return Response.ok("");
}
Also used : ProjectConfig(com.google.gerrit.server.project.ProjectConfig) UnprocessableEntityException(com.google.gerrit.extensions.restapi.UnprocessableEntityException) LabelType(com.google.gerrit.entities.LabelType) AuthException(com.google.gerrit.extensions.restapi.AuthException) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) BatchLabelInput(com.google.gerrit.extensions.common.BatchLabelInput) LabelDefinitionInput(com.google.gerrit.extensions.common.LabelDefinitionInput) Map(java.util.Map) MetaDataUpdate(com.google.gerrit.server.git.meta.MetaDataUpdate)

Aggregations

ProjectConfig (com.google.gerrit.server.project.ProjectConfig)39 MetaDataUpdate (com.google.gerrit.server.git.meta.MetaDataUpdate)26 AuthException (com.google.gerrit.extensions.restapi.AuthException)8 ConfigInvalidException (org.eclipse.jgit.errors.ConfigInvalidException)8 Project (com.google.gerrit.entities.Project)7 BadRequestException (com.google.gerrit.extensions.restapi.BadRequestException)7 ResourceConflictException (com.google.gerrit.extensions.restapi.ResourceConflictException)7 Test (org.junit.Test)7 AbstractDaemonTest (com.google.gerrit.acceptance.AbstractDaemonTest)6 AccessSection (com.google.gerrit.entities.AccessSection)6 BooleanProjectConfig (com.google.gerrit.entities.BooleanProjectConfig)6 LabelType (com.google.gerrit.entities.LabelType)6 ResourceNotFoundException (com.google.gerrit.extensions.restapi.ResourceNotFoundException)6 Repository (org.eclipse.jgit.lib.Repository)6 GroupReference (com.google.gerrit.entities.GroupReference)5 Permission (com.google.gerrit.entities.Permission)5 IOException (java.io.IOException)5 ProjectState (com.google.gerrit.server.project.ProjectState)4 RepositoryNotFoundException (org.eclipse.jgit.errors.RepositoryNotFoundException)4 TestRepository (org.eclipse.jgit.junit.TestRepository)4