Search in sources :

Example 36 with MetaDataUpdate

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

the class AccountsUpdate method commitAccountConfig.

private void commitAccountConfig(String message, Repository allUsersRepo, BatchRefUpdate batchRefUpdate, AccountConfig accountConfig, boolean allowEmptyCommit) throws IOException {
    try (MetaDataUpdate md = createMetaDataUpdate(message, allUsersRepo, batchRefUpdate)) {
        md.setAllowEmpty(allowEmptyCommit);
        accountConfig.commit(md);
    }
}
Also used : MetaDataUpdate(com.google.gerrit.server.git.meta.MetaDataUpdate)

Example 37 with MetaDataUpdate

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

the class RenameGroupOp method run.

@Override
public void run() {
    Iterable<Project.NameKey> names = tryingAgain ? retryOn : projectCache.all();
    for (Project.NameKey projectName : names) {
        CachedProjectConfig config = projectCache.get(projectName).orElseThrow(illegalState(projectName)).getConfig();
        Optional<GroupReference> ref = config.getGroup(uuid);
        if (!ref.isPresent() || newName.equals(ref.get().getName())) {
            continue;
        }
        try (MetaDataUpdate md = metaDataUpdateFactory.create(projectName)) {
            rename(md);
        } catch (RepositoryNotFoundException noProject) {
            continue;
        } catch (ConfigInvalidException | IOException err) {
            logger.atSevere().withCause(err).log("Cannot rename group %s in %s", oldName, projectName);
        }
    }
    // another attempt. If it doesn't update after that, give up.
    if (!retryOn.isEmpty() && !tryingAgain) {
        tryingAgain = true;
        @SuppressWarnings("unused") Future<?> possiblyIgnoredError = start(5, TimeUnit.MINUTES);
    }
}
Also used : ConfigInvalidException(org.eclipse.jgit.errors.ConfigInvalidException) CachedProjectConfig(com.google.gerrit.entities.CachedProjectConfig) RepositoryNotFoundException(org.eclipse.jgit.errors.RepositoryNotFoundException) IOException(java.io.IOException) Project(com.google.gerrit.entities.Project) GroupReference(com.google.gerrit.entities.GroupReference) MetaDataUpdate(com.google.gerrit.server.git.meta.MetaDataUpdate)

Example 38 with MetaDataUpdate

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

the class PutDescription method apply.

@Override
public Response<String> apply(ProjectResource resource, DescriptionInput input) throws AuthException, ResourceConflictException, ResourceNotFoundException, IOException, PermissionBackendException {
    if (input == null) {
        // Delete would set description to null.
        input = new DescriptionInput();
    }
    IdentifiedUser user = resource.getUser().asIdentifiedUser();
    permissionBackend.user(user).project(resource.getNameKey()).check(ProjectPermission.WRITE_CONFIG);
    try (MetaDataUpdate md = updateFactory.get().create(resource.getNameKey())) {
        ProjectConfig config = projectConfigFactory.read(md);
        String desc = input.description;
        config.updateProject(p -> p.setDescription(Strings.emptyToNull(desc)));
        String msg = MoreObjects.firstNonNull(Strings.emptyToNull(input.commitMessage), "Update description\n");
        if (!msg.endsWith("\n")) {
            msg += "\n";
        }
        md.setAuthor(user);
        md.setMessage(msg);
        config.commit(md);
        cache.evictAndReindex(resource.getProjectState().getProject());
        md.getRepository().setGitwebDescription(config.getProject().getDescription());
        return Strings.isNullOrEmpty(config.getProject().getDescription()) ? Response.none() : Response.ok(config.getProject().getDescription());
    } catch (RepositoryNotFoundException notFound) {
        throw new ResourceNotFoundException(resource.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) ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) ConfigInvalidException(org.eclipse.jgit.errors.ConfigInvalidException) DescriptionInput(com.google.gerrit.extensions.api.projects.DescriptionInput) 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 39 with MetaDataUpdate

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

the class SetDefaultDashboard method apply.

@Override
public Response<DashboardInfo> apply(DashboardResource rsrc, SetDashboardInput input) throws RestApiException, IOException, PermissionBackendException {
    if (input == null) {
        // Delete would set input to null.
        input = new SetDashboardInput();
    }
    input.id = Strings.emptyToNull(input.id);
    permissionBackend.user(rsrc.getUser()).project(rsrc.getProjectState().getNameKey()).check(ProjectPermission.WRITE_CONFIG);
    DashboardResource target = null;
    if (input.id != null) {
        try {
            target = dashboards.parse(new ProjectResource(rsrc.getProjectState(), rsrc.getUser()), IdString.fromUrl(input.id));
        } catch (ResourceNotFoundException e) {
            throw new BadRequestException("dashboard " + input.id + " not found", e);
        } catch (ConfigInvalidException e) {
            throw new ResourceConflictException(e.getMessage());
        }
    }
    try (MetaDataUpdate md = updateFactory.create(rsrc.getProjectState().getNameKey())) {
        ProjectConfig config = projectConfigFactory.read(md);
        String id = input.id;
        if (inherited) {
            config.updateProject(p -> p.setDefaultDashboard(id));
        } else {
            config.updateProject(p -> p.setLocalDefaultDashboard(id));
        }
        String msg = MoreObjects.firstNonNull(Strings.emptyToNull(input.commitMessage), input.id == null ? "Removed default dashboard.\n" : String.format("Changed default dashboard to %s.\n", input.id));
        if (!msg.endsWith("\n")) {
            msg += "\n";
        }
        md.setAuthor(rsrc.getUser().asIdentifiedUser());
        md.setMessage(msg);
        config.commit(md);
        cache.evictAndReindex(rsrc.getProjectState().getProject());
        if (target != null) {
            Response<DashboardInfo> response = get.get().apply(target);
            response.value().isDefault = true;
            return response;
        }
        return Response.none();
    } catch (RepositoryNotFoundException notFound) {
        throw new ResourceNotFoundException(rsrc.getProjectState().getProject().getName(), notFound);
    } catch (ConfigInvalidException e) {
        throw new ResourceConflictException(String.format("invalid project.config: %s", e.getMessage()));
    }
}
Also used : ConfigInvalidException(org.eclipse.jgit.errors.ConfigInvalidException) IdString(com.google.gerrit.extensions.restapi.IdString) RepositoryNotFoundException(org.eclipse.jgit.errors.RepositoryNotFoundException) DashboardInfo(com.google.gerrit.extensions.api.projects.DashboardInfo) ProjectConfig(com.google.gerrit.server.project.ProjectConfig) ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) SetDashboardInput(com.google.gerrit.extensions.api.projects.SetDashboardInput) DashboardResource(com.google.gerrit.server.project.DashboardResource) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) ProjectResource(com.google.gerrit.server.project.ProjectResource) ResourceNotFoundException(com.google.gerrit.extensions.restapi.ResourceNotFoundException) MetaDataUpdate(com.google.gerrit.server.git.meta.MetaDataUpdate)

Example 40 with MetaDataUpdate

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

the class SetAccess method apply.

@Override
public Response<ProjectAccessInfo> apply(ProjectResource rsrc, ProjectAccessInput input) throws Exception {
    MetaDataUpdate.User metaDataUpdateUser = metaDataUpdateFactory.get();
    ProjectConfig config;
    List<AccessSection> removals = accessUtil.getAccessSections(input.remove);
    List<AccessSection> additions = accessUtil.getAccessSections(input.add);
    try (MetaDataUpdate md = metaDataUpdateUser.create(rsrc.getNameKey())) {
        config = projectConfigFactory.read(md);
        // Check that the user has the right permissions.
        boolean checkedAdmin = false;
        for (AccessSection section : Iterables.concat(additions, removals)) {
            boolean isGlobalCapabilities = AccessSection.GLOBAL_CAPABILITIES.equals(section.getName());
            if (isGlobalCapabilities) {
                if (!checkedAdmin) {
                    permissionBackend.currentUser().check(GlobalPermission.ADMINISTRATE_SERVER);
                    checkedAdmin = true;
                }
            } else {
                permissionBackend.currentUser().project(rsrc.getNameKey()).ref(section.getName()).check(RefPermission.WRITE_CONFIG);
            }
        }
        accessUtil.validateChanges(config, removals, additions);
        accessUtil.applyChanges(config, removals, additions);
        accessUtil.setParentName(identifiedUser.get(), config, rsrc.getNameKey(), input.parent == null ? null : Project.nameKey(input.parent), !checkedAdmin);
        if (!Strings.isNullOrEmpty(input.message)) {
            if (!input.message.endsWith("\n")) {
                input.message += "\n";
            }
            md.setMessage(input.message);
        } else {
            md.setMessage("Modify access rules\n");
        }
        config.commit(md);
        projectCache.evictAndReindex(config.getProject());
        createGroupPermissionSyncer.syncIfNeeded();
    } catch (InvalidNameException e) {
        throw new BadRequestException(e.toString());
    } catch (ConfigInvalidException e) {
        throw new ResourceConflictException(rsrc.getName(), e);
    }
    return Response.ok(getAccess.apply(rsrc.getNameKey()));
}
Also used : ProjectConfig(com.google.gerrit.server.project.ProjectConfig) ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) InvalidNameException(com.google.gerrit.exceptions.InvalidNameException) ConfigInvalidException(org.eclipse.jgit.errors.ConfigInvalidException) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) AccessSection(com.google.gerrit.entities.AccessSection) 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