Search in sources :

Example 6 with DashboardInfo

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

the class ListDashboards method scanDashboards.

private ImmutableList<DashboardInfo> scanDashboards(Project definingProject, Repository git, RevWalk rw, Ref ref, String project, boolean setDefault) throws IOException {
    ImmutableList.Builder<DashboardInfo> list = ImmutableList.builder();
    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) {
                    logger.atWarning().log("Cannot parse dashboard %s:%s:%s: %s", definingProject.getName(), ref.getName(), tw.getPathString(), e.getMessage());
                }
            }
        }
    }
    return list.build();
}
Also used : ConfigInvalidException(org.eclipse.jgit.errors.ConfigInvalidException) ImmutableList(com.google.common.collect.ImmutableList) TreeWalk(org.eclipse.jgit.treewalk.TreeWalk) BlobBasedConfig(org.eclipse.jgit.lib.BlobBasedConfig) DashboardInfo(com.google.gerrit.extensions.api.projects.DashboardInfo)

Example 7 with DashboardInfo

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

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

the class DashboardsCollection method parse.

@Override
public DashboardResource parse(ProjectResource parent, IdString id) throws RestApiException, IOException, ConfigInvalidException, PermissionBackendException {
    parent.getProjectState().checkStatePermitsRead();
    if (isDefaultDashboard(id)) {
        return DashboardResource.projectDefault(parent.getProjectState(), parent.getUser());
    }
    DashboardInfo info;
    try {
        info = newDashboardInfo(id.get());
    } catch (InvalidDashboardId e) {
        throw new ResourceNotFoundException(id, e);
    }
    for (ProjectState ps : parent.getProjectState().tree()) {
        try {
            return parse(ps, parent.getProjectState(), parent.getUser(), info);
        } catch (AmbiguousObjectException | ConfigInvalidException | IncorrectObjectTypeException e) {
            throw new ResourceNotFoundException(id, e);
        } catch (ResourceNotFoundException e) {
            continue;
        }
    }
    throw new ResourceNotFoundException(id);
}
Also used : AmbiguousObjectException(org.eclipse.jgit.errors.AmbiguousObjectException) ConfigInvalidException(org.eclipse.jgit.errors.ConfigInvalidException) ProjectState(com.google.gerrit.server.project.ProjectState) IncorrectObjectTypeException(org.eclipse.jgit.errors.IncorrectObjectTypeException) ResourceNotFoundException(com.google.gerrit.extensions.restapi.ResourceNotFoundException) DashboardInfo(com.google.gerrit.extensions.api.projects.DashboardInfo)

Example 9 with DashboardInfo

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

the class DashboardsCollection method parse.

static DashboardInfo parse(Project definingProject, String refName, String path, Config config, String project, boolean setDefault) {
    DashboardInfo info = newDashboardInfo(refName, path);
    info.project = project;
    info.definingProject = definingProject.getName();
    String title = config.getString("dashboard", null, "title");
    info.title = replace(project, title == null ? info.path : title);
    info.description = replace(project, config.getString("dashboard", null, "description"));
    info.foreach = config.getString("dashboard", null, "foreach");
    if (setDefault) {
        String id = refName + ":" + path;
        info.isDefault = id.equals(defaultOf(definingProject)) ? true : null;
    }
    UrlEncoded u = new UrlEncoded("/dashboard/");
    u.put("title", MoreObjects.firstNonNull(info.title, info.path));
    if (info.foreach != null) {
        u.put("foreach", replace(project, info.foreach));
    }
    for (String name : config.getSubsections("section")) {
        DashboardSectionInfo s = new DashboardSectionInfo();
        s.name = name;
        s.query = config.getString("section", name, "query");
        u.put(s.name, replace(project, s.query));
        info.sections.add(s);
    }
    info.url = u.toString().replace("%3A", ":");
    return info;
}
Also used : DashboardSectionInfo(com.google.gerrit.extensions.api.projects.DashboardSectionInfo) UrlEncoded(com.google.gerrit.server.UrlEncoded) IdString(com.google.gerrit.extensions.restapi.IdString) DashboardInfo(com.google.gerrit.extensions.api.projects.DashboardInfo)

Example 10 with DashboardInfo

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

the class ListDashboards method scan.

private ImmutableList<DashboardInfo> scan(ProjectState state, String project, boolean setDefault) throws ResourceNotFoundException, IOException, PermissionBackendException {
    if (!state.statePermitsRead()) {
        return ImmutableList.of();
    }
    PermissionBackend.ForProject perm = permissionBackend.currentUser().project(state.getNameKey());
    try (Repository git = gitManager.openRepository(state.getNameKey());
        RevWalk rw = new RevWalk(git)) {
        ImmutableList.Builder<DashboardInfo> all = ImmutableList.builder();
        for (Ref ref : git.getRefDatabase().getRefsByPrefix(REFS_DASHBOARDS)) {
            try {
                perm.ref(ref.getName()).check(RefPermission.READ);
                all.addAll(scanDashboards(state.getProject(), git, rw, ref, project, setDefault));
            } catch (AuthException e) {
            // Do nothing.
            }
        }
        return all.build();
    } catch (RepositoryNotFoundException e) {
        throw new ResourceNotFoundException(project, e);
    }
}
Also used : Repository(org.eclipse.jgit.lib.Repository) Ref(org.eclipse.jgit.lib.Ref) PermissionBackend(com.google.gerrit.server.permissions.PermissionBackend) ImmutableList(com.google.common.collect.ImmutableList) AuthException(com.google.gerrit.extensions.restapi.AuthException) RepositoryNotFoundException(org.eclipse.jgit.errors.RepositoryNotFoundException) RevWalk(org.eclipse.jgit.revwalk.RevWalk) ResourceNotFoundException(com.google.gerrit.extensions.restapi.ResourceNotFoundException) DashboardInfo(com.google.gerrit.extensions.api.projects.DashboardInfo)

Aggregations

DashboardInfo (com.google.gerrit.extensions.api.projects.DashboardInfo)16 AbstractDaemonTest (com.google.gerrit.acceptance.AbstractDaemonTest)8 Test (org.junit.Test)8 ImmutableList (com.google.common.collect.ImmutableList)4 ResourceNotFoundException (com.google.gerrit.extensions.restapi.ResourceNotFoundException)4 DashboardSectionInfo (com.google.gerrit.extensions.api.projects.DashboardSectionInfo)3 BadRequestException (com.google.gerrit.extensions.restapi.BadRequestException)3 IdString (com.google.gerrit.extensions.restapi.IdString)3 ConfigInvalidException (org.eclipse.jgit.errors.ConfigInvalidException)3 ResourceConflictException (com.google.gerrit.extensions.restapi.ResourceConflictException)2 ProjectState (com.google.gerrit.server.project.ProjectState)2 List (java.util.List)2 RepositoryNotFoundException (org.eclipse.jgit.errors.RepositoryNotFoundException)2 Repository (org.eclipse.jgit.lib.Repository)2 Truth.assertThat (com.google.common.truth.Truth.assertThat)1 NoHttpd (com.google.gerrit.acceptance.NoHttpd)1 ProjectOperations (com.google.gerrit.acceptance.testsuite.project.ProjectOperations)1 TestProjectUpdate.allow (com.google.gerrit.acceptance.testsuite.project.TestProjectUpdate.allow)1 Permission (com.google.gerrit.entities.Permission)1 BranchInput (com.google.gerrit.extensions.api.projects.BranchInput)1