Search in sources :

Example 21 with MetaDataUpdate

use of com.google.gerrit.server.git.meta.MetaDataUpdate in project gerrit by GerritCodeReview.

the class ExternalIdIT method unsetHttpPassword.

@Test
public void unsetHttpPassword() throws Exception {
    ExternalId extId = externalIdFactory.createWithPassword(externalIdKeyFactory.create("y", "1"), user.id(), null, "secret");
    insertExtId(extId);
    ExternalId extIdWithoutPassword = externalIdFactory.create("y", "1", user.id());
    try (Repository allUsersRepo = repoManager.openRepository(allUsers);
        MetaDataUpdate md = metaDataUpdateFactory.create(allUsers)) {
        ExternalIdNotes extIdNotes = externalIdNotesFactory.load(allUsersRepo);
        extIdNotes.upsert(extIdWithoutPassword);
        extIdNotes.commit(md);
        assertThat(extIdNotes.get(extId.key())).hasValue(extIdWithoutPassword);
    }
}
Also used : InMemoryRepository(org.eclipse.jgit.internal.storage.dfs.InMemoryRepository) TestRepository(org.eclipse.jgit.junit.TestRepository) Repository(org.eclipse.jgit.lib.Repository) ExternalIdNotes(com.google.gerrit.server.account.externalids.ExternalIdNotes) ExternalId(com.google.gerrit.server.account.externalids.ExternalId) MetaDataUpdate(com.google.gerrit.server.git.meta.MetaDataUpdate) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest) Test(org.junit.Test)

Example 22 with MetaDataUpdate

use of com.google.gerrit.server.git.meta.MetaDataUpdate in project gerrit by GerritCodeReview.

the class ProjectCreator method createProjectConfig.

private void createProjectConfig(CreateProjectArgs args) throws IOException, ConfigInvalidException {
    try (MetaDataUpdate md = metaDataUpdateFactory.create(args.getProject())) {
        ProjectConfig config = projectConfigFactory.read(md);
        config.updateProject(newProject -> {
            newProject.setDescription(Strings.nullToEmpty(args.projectDescription));
            newProject.setSubmitType(MoreObjects.firstNonNull(args.submitType, repositoryCfg.getDefaultSubmitType(args.getProject())));
            newProject.setBooleanConfig(BooleanProjectConfig.USE_CONTRIBUTOR_AGREEMENTS, args.contributorAgreements);
            newProject.setBooleanConfig(BooleanProjectConfig.USE_SIGNED_OFF_BY, args.signedOffBy);
            newProject.setBooleanConfig(BooleanProjectConfig.USE_CONTENT_MERGE, args.contentMerge);
            newProject.setBooleanConfig(BooleanProjectConfig.CREATE_NEW_CHANGE_FOR_ALL_NOT_IN_TARGET, args.newChangeForAllNotInTarget);
            newProject.setBooleanConfig(BooleanProjectConfig.REQUIRE_CHANGE_ID, args.changeIdRequired);
            newProject.setBooleanConfig(BooleanProjectConfig.REJECT_EMPTY_COMMIT, args.rejectEmptyCommit);
            newProject.setMaxObjectSizeLimit(args.maxObjectSizeLimit);
            newProject.setBooleanConfig(BooleanProjectConfig.ENABLE_SIGNED_PUSH, args.enableSignedPush);
            newProject.setBooleanConfig(BooleanProjectConfig.REQUIRE_SIGNED_PUSH, args.requireSignedPush);
            if (args.newParent != null) {
                newProject.setParent(args.newParent);
            }
        });
        if (!args.ownerIds.isEmpty()) {
            config.upsertAccessSection(AccessSection.ALL, all -> {
                for (AccountGroup.UUID ownerId : args.ownerIds) {
                    GroupDescription.Basic g = groupBackend.get(ownerId);
                    if (g != null) {
                        GroupReference group = config.resolve(GroupReference.forGroup(g));
                        all.upsertPermission(Permission.OWNER).add(PermissionRule.builder(group));
                    }
                }
            });
        }
        md.setMessage("Created project\n");
        config.commit(md);
        md.getRepository().setGitwebDescription(args.projectDescription);
    }
    projectCache.onCreateProject(args.getProject());
}
Also used : BooleanProjectConfig(com.google.gerrit.entities.BooleanProjectConfig) GroupDescription(com.google.gerrit.entities.GroupDescription) AccountGroup(com.google.gerrit.entities.AccountGroup) GroupReference(com.google.gerrit.entities.GroupReference) MetaDataUpdate(com.google.gerrit.server.git.meta.MetaDataUpdate)

Example 23 with MetaDataUpdate

use of com.google.gerrit.server.git.meta.MetaDataUpdate 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 24 with MetaDataUpdate

use of com.google.gerrit.server.git.meta.MetaDataUpdate in project gerrit by GerritCodeReview.

the class AllUsersCreator method initAllUsers.

private void initAllUsers(Repository git) throws IOException, ConfigInvalidException {
    try (MetaDataUpdate md = new MetaDataUpdate(GitReferenceUpdated.DISABLED, allUsersName, git)) {
        md.getCommitBuilder().setAuthor(serverUser);
        md.getCommitBuilder().setCommitter(serverUser);
        md.setMessage("Initialized Gerrit Code Review " + Version.getVersion());
        ProjectConfig config = projectConfigFactory.read(md);
        config.updateProject(p -> p.setDescription("Individual user settings and preferences."));
        config.upsertAccessSection(RefNames.REFS_USERS + "${" + RefPattern.USERID_SHARDED + "}", users -> {
            grant(config, users, Permission.READ, false, true, registered);
            grant(config, users, Permission.PUSH, false, true, registered);
            grant(config, users, Permission.SUBMIT, false, true, registered);
            grant(config, users, codeReviewLabel, -2, 2, true, registered);
        });
        // Initialize "Code-Review" label.
        config.upsertLabelType(codeReviewLabel);
        if (admin != null) {
            config.upsertAccessSection(RefNames.REFS_USERS_DEFAULT, defaults -> {
                defaults.upsertPermission(Permission.READ).setExclusiveGroup(true);
                grant(config, defaults, Permission.READ, admin);
                defaults.upsertPermission(Permission.PUSH).setExclusiveGroup(true);
                grant(config, defaults, Permission.PUSH, admin);
                defaults.upsertPermission(Permission.CREATE).setExclusiveGroup(true);
                grant(config, defaults, Permission.CREATE, admin);
            });
        }
        // Grant read permissions on the group branches to all users.
        // This allows group owners to see the group refs. VisibleRefFilter ensures that read
        // permissions for non-group-owners are ignored.
        config.upsertAccessSection(RefNames.REFS_GROUPS + "*", groups -> {
            grant(config, groups, Permission.READ, false, true, registered);
        });
        config.commit(md);
    }
}
Also used : ProjectConfig(com.google.gerrit.server.project.ProjectConfig) MetaDataUpdate(com.google.gerrit.server.git.meta.MetaDataUpdate)

Example 25 with MetaDataUpdate

use of com.google.gerrit.server.git.meta.MetaDataUpdate in project gerrit by GerritCodeReview.

the class SetLabel method apply.

@Override
public Response<LabelDefinitionInfo> apply(LabelResource rsrc, LabelDefinitionInput input) throws AuthException, BadRequestException, ResourceConflictException, PermissionBackendException, IOException, ConfigInvalidException {
    if (!user.get().isIdentifiedUser()) {
        throw new AuthException("Authentication required");
    }
    permissionBackend.currentUser().project(rsrc.getProject().getNameKey()).check(ProjectPermission.WRITE_CONFIG);
    if (input == null) {
        input = new LabelDefinitionInput();
    }
    LabelType labelType = rsrc.getLabelType();
    try (MetaDataUpdate md = updateFactory.create(rsrc.getProject().getNameKey())) {
        ProjectConfig config = projectConfigFactory.read(md);
        if (updateLabel(config, labelType, input)) {
            if (input.commitMessage != null) {
                md.setMessage(Strings.emptyToNull(input.commitMessage.trim()));
            } else {
                md.setMessage("Update label");
            }
            String newName = Strings.nullToEmpty(input.name).trim();
            labelType = config.getLabelSections().get(newName.isEmpty() ? labelType.getName() : newName);
            config.commit(md);
            projectCache.evictAndReindex(rsrc.getProject().getProjectState().getProject());
        }
    }
    return Response.ok(LabelDefinitionJson.format(rsrc.getProject().getNameKey(), labelType));
}
Also used : ProjectConfig(com.google.gerrit.server.project.ProjectConfig) LabelType(com.google.gerrit.entities.LabelType) AuthException(com.google.gerrit.extensions.restapi.AuthException) LabelDefinitionInput(com.google.gerrit.extensions.common.LabelDefinitionInput) MetaDataUpdate(com.google.gerrit.server.git.meta.MetaDataUpdate)

Aggregations

MetaDataUpdate (com.google.gerrit.server.git.meta.MetaDataUpdate)84 Test (org.junit.Test)39 Repository (org.eclipse.jgit.lib.Repository)36 ExternalIdNotes (com.google.gerrit.server.account.externalids.ExternalIdNotes)28 ProjectConfig (com.google.gerrit.server.project.ProjectConfig)26 AbstractDaemonTest (com.google.gerrit.acceptance.AbstractDaemonTest)22 TestRepository (org.eclipse.jgit.junit.TestRepository)17 GerritConfig (com.google.gerrit.acceptance.config.GerritConfig)15 InMemoryRepository (org.eclipse.jgit.internal.storage.dfs.InMemoryRepository)15 ConfigInvalidException (org.eclipse.jgit.errors.ConfigInvalidException)14 Account (com.google.gerrit.entities.Account)13 ExternalId (com.google.gerrit.server.account.externalids.ExternalId)12 PersonIdent (org.eclipse.jgit.lib.PersonIdent)11 RepositoryNotFoundException (org.eclipse.jgit.errors.RepositoryNotFoundException)7 Project (com.google.gerrit.entities.Project)6 LightweightPluginDaemonTest (com.google.gerrit.acceptance.LightweightPluginDaemonTest)5 TestAccount (com.google.gerrit.acceptance.TestAccount)5 AccessSection (com.google.gerrit.entities.AccessSection)5 GroupReference (com.google.gerrit.entities.GroupReference)5 LabelType (com.google.gerrit.entities.LabelType)5