Search in sources :

Example 16 with ProjectConfig

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

the class SubmitRequirementsValidationIT method updateProjectConfig.

private void updateProjectConfig(Consumer<Config> configUpdater) throws Exception {
    RevCommit head = getHead(testRepo.getRepository(), RefNames.REFS_CONFIG);
    Config projectConfig = readProjectConfig(head);
    configUpdater.accept(projectConfig);
    RevCommit commit = testRepo.update(RefNames.REFS_CONFIG, testRepo.commit().parent(head).message("Update project config").author(admin.newIdent()).committer(admin.newIdent()).add(ProjectConfig.PROJECT_CONFIG, projectConfig.toText()));
    testRepo.reset(commit);
}
Also used : ProjectConfig(com.google.gerrit.server.project.ProjectConfig) Config(org.eclipse.jgit.lib.Config) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 17 with ProjectConfig

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

the class AbstractQueryChangesTest method byLabelMulti.

@Test
public void byLabelMulti() throws Exception {
    TestRepository<Repo> repo = createProject("repo");
    Project.NameKey project = Project.nameKey(repo.getRepository().getDescription().getRepositoryName());
    LabelType verified = label(LabelId.VERIFIED, value(1, "Passes"), value(0, "No score"), value(-1, "Failed"));
    try (MetaDataUpdate md = metaDataUpdateFactory.create(project)) {
        ProjectConfig cfg = projectConfigFactory.create(project);
        cfg.load(md);
        cfg.upsertLabelType(verified);
        cfg.commit(md);
    }
    projectCache.evictAndReindex(project);
    String heads = RefNames.REFS_HEADS + "*";
    projectOperations.project(project).forUpdate().add(allowLabel(verified.getName()).ref(heads).group(REGISTERED_USERS).range(-1, 1)).update();
    ReviewInput reviewVerified = new ReviewInput().label(LabelId.VERIFIED, 1);
    ChangeInserter ins = newChange(repo);
    ChangeInserter ins2 = newChange(repo);
    ChangeInserter ins3 = newChange(repo);
    ChangeInserter ins4 = newChange(repo);
    ChangeInserter ins5 = newChange(repo);
    // CR+1
    Change reviewCRplus1 = insert(repo, ins);
    gApi.changes().id(reviewCRplus1.getId().get()).current().review(ReviewInput.recommend());
    // CR+2
    Change reviewCRplus2 = insert(repo, ins2);
    gApi.changes().id(reviewCRplus2.getId().get()).current().review(ReviewInput.approve());
    // CR+1 VR+1
    Change reviewCRplus1VRplus1 = insert(repo, ins3);
    gApi.changes().id(reviewCRplus1VRplus1.getId().get()).current().review(ReviewInput.recommend());
    gApi.changes().id(reviewCRplus1VRplus1.getId().get()).current().review(reviewVerified);
    // CR+2 VR+1
    Change reviewCRplus2VRplus1 = insert(repo, ins4);
    gApi.changes().id(reviewCRplus2VRplus1.getId().get()).current().review(ReviewInput.approve());
    gApi.changes().id(reviewCRplus2VRplus1.getId().get()).current().review(reviewVerified);
    // VR+1
    Change reviewVRplus1 = insert(repo, ins5);
    gApi.changes().id(reviewVRplus1.getId().get()).current().review(reviewVerified);
    assertQuery("label:Code-Review=+1", reviewCRplus1VRplus1, reviewCRplus1);
    assertQuery("label:Code-Review>=+1", reviewCRplus2VRplus1, reviewCRplus1VRplus1, reviewCRplus2, reviewCRplus1);
    assertQuery("label:Code-Review>=+2", reviewCRplus2VRplus1, reviewCRplus2);
    assertQuery("label:Code-Review>=+1 label:Verified=+1", reviewCRplus2VRplus1, reviewCRplus1VRplus1);
    assertQuery("label:Code-Review>=+2 label:Verified=+1", reviewCRplus2VRplus1);
}
Also used : ProjectConfig(com.google.gerrit.server.project.ProjectConfig) Project(com.google.gerrit.entities.Project) Repo(com.google.gerrit.testing.InMemoryRepositoryManager.Repo) LabelType(com.google.gerrit.entities.LabelType) ChangeInserter(com.google.gerrit.server.change.ChangeInserter) Change(com.google.gerrit.entities.Change) ReviewInput(com.google.gerrit.extensions.api.changes.ReviewInput) MetaDataUpdate(com.google.gerrit.server.git.meta.MetaDataUpdate) Test(org.junit.Test)

Example 18 with ProjectConfig

use of com.google.gerrit.server.project.ProjectConfig 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 19 with ProjectConfig

use of com.google.gerrit.server.project.ProjectConfig 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 20 with ProjectConfig

use of com.google.gerrit.server.project.ProjectConfig 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

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