Search in sources :

Example 1 with ProjectConfig

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

the class CreateGroupPermissionSyncer method syncIfNeeded.

/**
 * Checks if {@code GlobalCapability.CREATE_GROUP} and {@code CREATE} permission on {@code
 * refs/groups/*} have diverged and syncs them by applying the {@code CREATE} permission to {@code
 * refs/groups/*}.
 */
public void syncIfNeeded() throws IOException, ConfigInvalidException {
    ProjectState allProjectsState = projectCache.getAllProjects();
    ProjectState allUsersState = projectCache.getAllUsers();
    Set<PermissionRule> createGroupsGlobal = new HashSet<>(allProjectsState.getCapabilityCollection().createGroup);
    Set<PermissionRule> createGroupsRef = new HashSet<>();
    Optional<AccessSection> allUsersCreateGroupAccessSection = allUsersState.getConfig().getAccessSection(RefNames.REFS_GROUPS + "*");
    if (allUsersCreateGroupAccessSection.isPresent()) {
        Permission create = allUsersCreateGroupAccessSection.get().getPermission(Permission.CREATE);
        if (create != null && create.getRules() != null) {
            createGroupsRef.addAll(create.getRules());
        }
    }
    if (Sets.symmetricDifference(createGroupsGlobal, createGroupsRef).isEmpty()) {
        // Nothing to sync
        return;
    }
    try (MetaDataUpdate md = metaDataUpdateFactory.get().create(allUsers)) {
        ProjectConfig config = projectConfigFactory.read(md);
        config.upsertAccessSection(RefNames.REFS_GROUPS + "*", refsGroupsAccessSectionBuilder -> {
            if (createGroupsGlobal.isEmpty()) {
                refsGroupsAccessSectionBuilder.modifyPermissions(permissions -> {
                    permissions.removeIf(p -> Permission.CREATE.equals(p.getName()));
                });
            } else {
                // The create permission is managed by Gerrit at this point only so there is no
                // concern of overwriting user-defined permissions here.
                Permission.Builder createGroupPermission = Permission.builder(Permission.CREATE);
                refsGroupsAccessSectionBuilder.remove(createGroupPermission);
                refsGroupsAccessSectionBuilder.addPermission(createGroupPermission);
                createGroupsGlobal.stream().map(p -> p.toBuilder()).forEach(createGroupPermission::add);
            }
        });
        config.commit(md);
        projectCache.evictAndReindex(config.getProject());
    }
}
Also used : ProjectConfig(com.google.gerrit.server.project.ProjectConfig) AllUsersName(com.google.gerrit.server.config.AllUsersName) ProjectConfig(com.google.gerrit.server.project.ProjectConfig) ConfigInvalidException(org.eclipse.jgit.errors.ConfigInvalidException) ProjectCache(com.google.gerrit.server.project.ProjectCache) AccessSection(com.google.gerrit.entities.AccessSection) Inject(com.google.inject.Inject) Permission(com.google.gerrit.entities.Permission) ProjectState(com.google.gerrit.server.project.ProjectState) Set(java.util.Set) IOException(java.io.IOException) Sets(com.google.common.collect.Sets) ChangeMergedListener(com.google.gerrit.extensions.events.ChangeMergedListener) HashSet(java.util.HashSet) Provider(com.google.inject.Provider) AllProjectsName(com.google.gerrit.server.config.AllProjectsName) PermissionRule(com.google.gerrit.entities.PermissionRule) RefNames(com.google.gerrit.entities.RefNames) Optional(java.util.Optional) FluentLogger(com.google.common.flogger.FluentLogger) MetaDataUpdate(com.google.gerrit.server.git.meta.MetaDataUpdate) Singleton(com.google.inject.Singleton) PermissionRule(com.google.gerrit.entities.PermissionRule) Permission(com.google.gerrit.entities.Permission) ProjectState(com.google.gerrit.server.project.ProjectState) AccessSection(com.google.gerrit.entities.AccessSection) HashSet(java.util.HashSet) MetaDataUpdate(com.google.gerrit.server.git.meta.MetaDataUpdate)

Example 2 with ProjectConfig

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

the class AbstractDaemonTest method setRequireChangeId.

protected void setRequireChangeId(InheritableBoolean value) throws Exception {
    try (MetaDataUpdate md = metaDataUpdateFactory.create(project)) {
        ProjectConfig config = projectConfigFactory.read(md);
        config.updateProject(p -> p.setBooleanConfig(BooleanProjectConfig.REQUIRE_CHANGE_ID, value));
        config.commit(md);
        projectCache.evictAndReindex(config.getProject());
    }
}
Also used : BooleanProjectConfig(com.google.gerrit.entities.BooleanProjectConfig) ProjectConfig(com.google.gerrit.server.project.ProjectConfig) MetaDataUpdate(com.google.gerrit.server.git.meta.MetaDataUpdate)

Example 3 with ProjectConfig

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

the class ProjectIT method renamingGroupGetsPersisted.

@Test
public void renamingGroupGetsPersisted() throws Exception {
    String name = name("Name1");
    GroupInfo group = gApi.groups().create(name).get();
    // Use group in a permission
    projectOperations.project(project).forUpdate().add(allow(Permission.READ).ref(RefNames.REFS_CONFIG).group(AccountGroup.uuid(group.id))).update();
    Optional<String> beforeRename = projectCache.get(project).get().getLocalGroups().stream().filter(g -> g.getUUID().get().equals(group.id)).map(GroupReference::getName).findAny();
    // Groups created with ProjectOperations always have their UUID as local name
    assertThat(beforeRename).hasValue(group.id);
    // Rename the group directly on the project config
    String newName = name("Name2");
    try (MetaDataUpdate md = metaDataUpdateFactory.create(project)) {
        ProjectConfig config = projectConfigFactory.read(md);
        config.renameGroup(AccountGroup.uuid(group.id), newName);
        config.commit(md);
        projectCache.evictAndReindex(config.getProject());
    }
    Optional<String> afterRename = projectCache.get(project).get().getLocalGroups().stream().filter(g -> g.getUUID().get().equals(group.id)).map(GroupReference::getName).findAny();
    assertThat(afterRename).hasValue(newName);
}
Also used : ProjectConfig(com.google.gerrit.server.project.ProjectConfig) GroupInfo(com.google.gerrit.extensions.common.GroupInfo) MetaDataUpdate(com.google.gerrit.server.git.meta.MetaDataUpdate) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest) Test(org.junit.Test)

Example 4 with ProjectConfig

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

the class AgreementsIT method setUseContributorAgreements.

protected void setUseContributorAgreements(InheritableBoolean value) throws Exception {
    try (MetaDataUpdate md = metaDataUpdateFactory.create(project)) {
        ProjectConfig config = projectConfigFactory.read(md);
        config.updateProject(p -> p.setBooleanConfig(BooleanProjectConfig.USE_CONTRIBUTOR_AGREEMENTS, value));
        config.commit(md);
        projectCache.evictAndReindex(config.getProject());
    }
}
Also used : ProjectConfig(com.google.gerrit.server.project.ProjectConfig) BooleanProjectConfig(com.google.gerrit.entities.BooleanProjectConfig) MetaDataUpdate(com.google.gerrit.server.git.meta.MetaDataUpdate)

Example 5 with ProjectConfig

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

the class LabelNormalizerTest method loadAllProjects.

private ProjectConfig loadAllProjects() throws Exception {
    try (Repository repo = repoManager.openRepository(allProjects)) {
        ProjectConfig pc = projectConfigFactory.create(allProjects);
        pc.load(repo);
        return pc;
    }
}
Also used : ProjectConfig(com.google.gerrit.server.project.ProjectConfig) Repository(org.eclipse.jgit.lib.Repository)

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