Search in sources :

Example 1 with SetDashboardInput

use of com.google.gerrit.extensions.api.projects.SetDashboardInput in project gerrit by GerritCodeReview.

the class DashboardApiImpl method setDefault.

@Override
public void setDefault() throws RestApiException {
    SetDashboardInput input = new SetDashboardInput();
    input.id = id;
    try {
        set.apply(DashboardResource.projectDefault(project.getProjectState(), project.getUser()), input);
    } catch (Exception e) {
        String msg = String.format("Cannot %s default dashboard", id != null ? "set" : "remove");
        throw asRestApiException(msg, e);
    }
}
Also used : SetDashboardInput(com.google.gerrit.extensions.api.projects.SetDashboardInput) IdString(com.google.gerrit.extensions.restapi.IdString) ApiUtil.asRestApiException(com.google.gerrit.server.api.ApiUtil.asRestApiException) PermissionBackendException(com.google.gerrit.server.permissions.PermissionBackendException) ConfigInvalidException(org.eclipse.jgit.errors.ConfigInvalidException) IOException(java.io.IOException) RestApiException(com.google.gerrit.extensions.restapi.RestApiException)

Example 2 with SetDashboardInput

use of com.google.gerrit.extensions.api.projects.SetDashboardInput 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 3 with SetDashboardInput

use of com.google.gerrit.extensions.api.projects.SetDashboardInput in project gerrit by GerritCodeReview.

the class DeleteDashboard method apply.

@Override
public Response<DashboardInfo> apply(DashboardResource resource, SetDashboardInput input) throws Exception {
    if (resource.isProjectDefault()) {
        SetDashboardInput in = new SetDashboardInput();
        in.commitMessage = input != null ? input.commitMessage : null;
        return defaultSetter.get().apply(resource, in);
    }
    // TODO: Implement delete of dashboards by API.
    throw new MethodNotAllowedException("cannot delete non-default dashboard");
}
Also used : MethodNotAllowedException(com.google.gerrit.extensions.restapi.MethodNotAllowedException) SetDashboardInput(com.google.gerrit.extensions.api.projects.SetDashboardInput)

Aggregations

SetDashboardInput (com.google.gerrit.extensions.api.projects.SetDashboardInput)3 IdString (com.google.gerrit.extensions.restapi.IdString)2 ConfigInvalidException (org.eclipse.jgit.errors.ConfigInvalidException)2 DashboardInfo (com.google.gerrit.extensions.api.projects.DashboardInfo)1 BadRequestException (com.google.gerrit.extensions.restapi.BadRequestException)1 MethodNotAllowedException (com.google.gerrit.extensions.restapi.MethodNotAllowedException)1 ResourceConflictException (com.google.gerrit.extensions.restapi.ResourceConflictException)1 ResourceNotFoundException (com.google.gerrit.extensions.restapi.ResourceNotFoundException)1 RestApiException (com.google.gerrit.extensions.restapi.RestApiException)1 ApiUtil.asRestApiException (com.google.gerrit.server.api.ApiUtil.asRestApiException)1 MetaDataUpdate (com.google.gerrit.server.git.meta.MetaDataUpdate)1 PermissionBackendException (com.google.gerrit.server.permissions.PermissionBackendException)1 DashboardResource (com.google.gerrit.server.project.DashboardResource)1 ProjectConfig (com.google.gerrit.server.project.ProjectConfig)1 ProjectResource (com.google.gerrit.server.project.ProjectResource)1 IOException (java.io.IOException)1 RepositoryNotFoundException (org.eclipse.jgit.errors.RepositoryNotFoundException)1