Search in sources :

Example 1 with DashboardInfo

use of com.google.gerrit.server.project.DashboardsCollection.DashboardInfo in project gerrit by GerritCodeReview.

the class ListDashboards method scanDashboards.

private List<DashboardInfo> scanDashboards(Project definingProject, Repository git, RevWalk rw, Ref ref, String project, boolean setDefault) throws IOException {
    List<DashboardInfo> list = new ArrayList<>();
    try (TreeWalk tw = new TreeWalk(rw.getObjectReader())) {
        tw.addTree(rw.parseTree(ref.getObjectId()));
        tw.setRecursive(true);
        while (tw.next()) {
            if (tw.getFileMode(0) == FileMode.REGULAR_FILE) {
                try {
                    list.add(DashboardsCollection.parse(definingProject, ref.getName().substring(REFS_DASHBOARDS.length()), tw.getPathString(), new BlobBasedConfig(null, git, tw.getObjectId(0)), project, setDefault));
                } catch (ConfigInvalidException e) {
                    log.warn(String.format("Cannot parse dashboard %s:%s:%s: %s", definingProject.getName(), ref.getName(), tw.getPathString(), e.getMessage()));
                }
            }
        }
    }
    return list;
}
Also used : ConfigInvalidException(org.eclipse.jgit.errors.ConfigInvalidException) ArrayList(java.util.ArrayList) TreeWalk(org.eclipse.jgit.treewalk.TreeWalk) BlobBasedConfig(org.eclipse.jgit.lib.BlobBasedConfig) DashboardInfo(com.google.gerrit.server.project.DashboardsCollection.DashboardInfo)

Example 2 with DashboardInfo

use of com.google.gerrit.server.project.DashboardsCollection.DashboardInfo in project gerrit by GerritCodeReview.

the class ListDashboards method apply.

@Override
public List<?> apply(ProjectResource rsrc) throws ResourceNotFoundException, IOException, PermissionBackendException {
    String project = rsrc.getName();
    if (!inherited) {
        return scan(rsrc.getControl(), project, true);
    }
    List<List<DashboardInfo>> all = new ArrayList<>();
    boolean setDefault = true;
    for (ProjectState ps : tree(rsrc)) {
        List<DashboardInfo> list = scan(ps.controlFor(user.get()), project, setDefault);
        for (DashboardInfo d : list) {
            if (d.isDefault != null && Boolean.TRUE.equals(d.isDefault)) {
                setDefault = false;
            }
        }
        if (!list.isEmpty()) {
            all.add(list);
        }
    }
    return all;
}
Also used : ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) DashboardInfo(com.google.gerrit.server.project.DashboardsCollection.DashboardInfo)

Example 3 with DashboardInfo

use of com.google.gerrit.server.project.DashboardsCollection.DashboardInfo in project gerrit by GerritCodeReview.

the class SetDefaultDashboard method apply.

@Override
public Response<DashboardInfo> apply(DashboardResource resource, Input input) throws AuthException, BadRequestException, ResourceConflictException, ResourceNotFoundException, IOException {
    if (input == null) {
        // Delete would set input to null.
        input = new Input();
    }
    input.id = Strings.emptyToNull(input.id);
    ProjectControl ctl = resource.getControl();
    if (!ctl.isOwner()) {
        throw new AuthException("not project owner");
    }
    DashboardResource target = null;
    if (input.id != null) {
        try {
            target = dashboards.parse(new ProjectResource(ctl), IdString.fromUrl(input.id));
        } catch (ResourceNotFoundException e) {
            throw new BadRequestException("dashboard " + input.id + " not found");
        } catch (ConfigInvalidException e) {
            throw new ResourceConflictException(e.getMessage());
        }
    }
    try (MetaDataUpdate md = updateFactory.create(ctl.getProject().getNameKey())) {
        ProjectConfig config = ProjectConfig.read(md);
        Project project = config.getProject();
        if (inherited) {
            project.setDefaultDashboard(input.id);
        } else {
            project.setLocalDefaultDashboard(input.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(ctl.getUser().asIdentifiedUser());
        md.setMessage(msg);
        config.commit(md);
        cache.evict(ctl.getProject());
        if (target != null) {
            DashboardInfo info = get.get().apply(target);
            info.isDefault = true;
            return Response.ok(info);
        }
        return Response.none();
    } catch (RepositoryNotFoundException notFound) {
        throw new ResourceNotFoundException(ctl.getProject().getName());
    } catch (ConfigInvalidException e) {
        throw new ResourceConflictException(String.format("invalid project.config: %s", e.getMessage()));
    }
}
Also used : ConfigInvalidException(org.eclipse.jgit.errors.ConfigInvalidException) AuthException(com.google.gerrit.extensions.restapi.AuthException) IdString(com.google.gerrit.extensions.restapi.IdString) RepositoryNotFoundException(org.eclipse.jgit.errors.RepositoryNotFoundException) DashboardInfo(com.google.gerrit.server.project.DashboardsCollection.DashboardInfo) ProjectConfig(com.google.gerrit.server.git.ProjectConfig) Project(com.google.gerrit.reviewdb.client.Project) Input(com.google.gerrit.server.project.SetDashboard.Input) ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) ResourceNotFoundException(com.google.gerrit.extensions.restapi.ResourceNotFoundException) MetaDataUpdate(com.google.gerrit.server.git.MetaDataUpdate)

Aggregations

DashboardInfo (com.google.gerrit.server.project.DashboardsCollection.DashboardInfo)3 ArrayList (java.util.ArrayList)2 ConfigInvalidException (org.eclipse.jgit.errors.ConfigInvalidException)2 AuthException (com.google.gerrit.extensions.restapi.AuthException)1 BadRequestException (com.google.gerrit.extensions.restapi.BadRequestException)1 IdString (com.google.gerrit.extensions.restapi.IdString)1 ResourceConflictException (com.google.gerrit.extensions.restapi.ResourceConflictException)1 ResourceNotFoundException (com.google.gerrit.extensions.restapi.ResourceNotFoundException)1 Project (com.google.gerrit.reviewdb.client.Project)1 MetaDataUpdate (com.google.gerrit.server.git.MetaDataUpdate)1 ProjectConfig (com.google.gerrit.server.git.ProjectConfig)1 Input (com.google.gerrit.server.project.SetDashboard.Input)1 List (java.util.List)1 RepositoryNotFoundException (org.eclipse.jgit.errors.RepositoryNotFoundException)1 BlobBasedConfig (org.eclipse.jgit.lib.BlobBasedConfig)1 TreeWalk (org.eclipse.jgit.treewalk.TreeWalk)1