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();
}
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()));
}
}
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);
}
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;
}
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);
}
}
Aggregations