Search in sources :

Example 1 with DashboardResource

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

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

the class DashboardsCollection method parse.

private DashboardResource parse(ProjectState parent, ProjectState current, CurrentUser user, DashboardInfo info) throws ResourceNotFoundException, IOException, AmbiguousObjectException, IncorrectObjectTypeException, ConfigInvalidException, PermissionBackendException, ResourceConflictException {
    String ref = normalizeDashboardRef(info.ref);
    try {
        permissionBackend.user(user).project(parent.getNameKey()).ref(ref).check(RefPermission.READ);
    } catch (AuthException e) {
        // Don't leak the project's existence
        throw new ResourceNotFoundException(info.id, e);
    }
    if (!Repository.isValidRefName(ref)) {
        throw new ResourceNotFoundException(info.id);
    }
    current.checkStatePermitsRead();
    try (Repository git = gitManager.openRepository(parent.getNameKey())) {
        ObjectId objId = git.resolve(ref + ":" + info.path);
        if (objId == null) {
            throw new ResourceNotFoundException(info.id);
        }
        BlobBasedConfig cfg = new BlobBasedConfig(null, git, objId);
        return new DashboardResource(current, user, ref, info.path, cfg, false);
    } catch (RepositoryNotFoundException e) {
        throw new ResourceNotFoundException(info.id, e);
    }
}
Also used : Repository(org.eclipse.jgit.lib.Repository) ObjectId(org.eclipse.jgit.lib.ObjectId) AuthException(com.google.gerrit.extensions.restapi.AuthException) DashboardResource(com.google.gerrit.server.project.DashboardResource) IdString(com.google.gerrit.extensions.restapi.IdString) RepositoryNotFoundException(org.eclipse.jgit.errors.RepositoryNotFoundException) ResourceNotFoundException(com.google.gerrit.extensions.restapi.ResourceNotFoundException) BlobBasedConfig(org.eclipse.jgit.lib.BlobBasedConfig)

Aggregations

IdString (com.google.gerrit.extensions.restapi.IdString)2 ResourceNotFoundException (com.google.gerrit.extensions.restapi.ResourceNotFoundException)2 DashboardResource (com.google.gerrit.server.project.DashboardResource)2 RepositoryNotFoundException (org.eclipse.jgit.errors.RepositoryNotFoundException)2 DashboardInfo (com.google.gerrit.extensions.api.projects.DashboardInfo)1 SetDashboardInput (com.google.gerrit.extensions.api.projects.SetDashboardInput)1 AuthException (com.google.gerrit.extensions.restapi.AuthException)1 BadRequestException (com.google.gerrit.extensions.restapi.BadRequestException)1 ResourceConflictException (com.google.gerrit.extensions.restapi.ResourceConflictException)1 MetaDataUpdate (com.google.gerrit.server.git.meta.MetaDataUpdate)1 ProjectConfig (com.google.gerrit.server.project.ProjectConfig)1 ProjectResource (com.google.gerrit.server.project.ProjectResource)1 ConfigInvalidException (org.eclipse.jgit.errors.ConfigInvalidException)1 BlobBasedConfig (org.eclipse.jgit.lib.BlobBasedConfig)1 ObjectId (org.eclipse.jgit.lib.ObjectId)1 Repository (org.eclipse.jgit.lib.Repository)1