ProjectApi: Add method to list project dashboards

In the initial implementation, support for the "inherit" flag
is not included.

Change-Id: I0c615236f395db36e8a35318b221c3127a2bd78d
This commit is contained in:
David Pursehouse
2017-10-01 11:53:35 +01:00
parent 112cd283c8
commit bcf0c7cc3b
4 changed files with 52 additions and 1 deletions

View File

@@ -25,6 +25,7 @@ import com.google.gerrit.extensions.api.projects.DashboardInfo;
import com.google.gerrit.extensions.restapi.BadRequestException; import com.google.gerrit.extensions.restapi.BadRequestException;
import com.google.gerrit.extensions.restapi.ResourceNotFoundException; import com.google.gerrit.extensions.restapi.ResourceNotFoundException;
import com.google.gerrit.server.project.DashboardsCollection; import com.google.gerrit.server.project.DashboardsCollection;
import java.util.List;
import org.eclipse.jgit.junit.TestRepository; import org.eclipse.jgit.junit.TestRepository;
import org.eclipse.jgit.lib.Repository; import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevCommit; import org.eclipse.jgit.revwalk.RevCommit;
@@ -46,6 +47,7 @@ public class DashboardIT extends AbstractDaemonTest {
@Test @Test
public void getDashboard() throws Exception { public void getDashboard() throws Exception {
assertThat(dashboards()).isEmpty();
DashboardInfo info = createDashboard(DashboardsCollection.DEFAULT_DASHBOARD_NAME, "test"); DashboardInfo info = createDashboard(DashboardsCollection.DEFAULT_DASHBOARD_NAME, "test");
DashboardInfo result = gApi.projects().name(project.get()).dashboard(info.id).get(); DashboardInfo result = gApi.projects().name(project.get()).dashboard(info.id).get();
assertThat(result.id).isEqualTo(info.id); assertThat(result.id).isEqualTo(info.id);
@@ -53,6 +55,7 @@ public class DashboardIT extends AbstractDaemonTest {
assertThat(result.ref).isEqualTo(info.ref); assertThat(result.ref).isEqualTo(info.ref);
assertThat(result.project).isEqualTo(project.get()); assertThat(result.project).isEqualTo(project.get());
assertThat(result.definingProject).isEqualTo(project.get()); assertThat(result.definingProject).isEqualTo(project.get());
assertThat(dashboards()).hasSize(1);
} }
@Test @Test
@@ -63,6 +66,10 @@ public class DashboardIT extends AbstractDaemonTest {
gApi.projects().name(project.get()).dashboard(info.id).get(true); gApi.projects().name(project.get()).dashboard(info.id).get(true);
} }
private List<DashboardInfo> dashboards() throws Exception {
return gApi.projects().name(project.get()).dashboards().get();
}
private DashboardInfo createDashboard(String ref, String path) throws Exception { private DashboardInfo createDashboard(String ref, String path) throws Exception {
DashboardInfo info = DashboardsCollection.newDashboardInfo(ref, path); DashboardInfo info = DashboardsCollection.newDashboardInfo(ref, path);
String canonicalRef = DashboardsCollection.normalizeDashboardRef(info.ref); String canonicalRef = DashboardsCollection.normalizeDashboardRef(info.ref);

View File

@@ -154,6 +154,12 @@ public interface ProjectApi {
*/ */
DashboardApi defaultDashboard() throws RestApiException; DashboardApi defaultDashboard() throws RestApiException;
abstract class ListDashboardsRequest {
public abstract List<DashboardInfo> get() throws RestApiException;
}
ListDashboardsRequest dashboards() throws RestApiException;
/** /**
* A default implementation which allows source compatibility when adding new methods to the * A default implementation which allows source compatibility when adding new methods to the
* interface. * interface.
@@ -273,5 +279,10 @@ public interface ProjectApi {
public DashboardApi defaultDashboard() throws RestApiException { public DashboardApi defaultDashboard() throws RestApiException {
throw new NotImplementedException(); throw new NotImplementedException();
} }
@Override
public ListDashboardsRequest dashboards() throws RestApiException {
throw new NotImplementedException();
}
} }
} }

View File

@@ -16,6 +16,7 @@ package com.google.gerrit.server.api.projects;
import static com.google.gerrit.server.api.ApiUtil.asRestApiException; import static com.google.gerrit.server.api.ApiUtil.asRestApiException;
import static com.google.gerrit.server.project.DashboardsCollection.DEFAULT_DASHBOARD_NAME; import static com.google.gerrit.server.project.DashboardsCollection.DEFAULT_DASHBOARD_NAME;
import static java.util.stream.Collectors.toList;
import com.google.gerrit.extensions.api.access.ProjectAccessInfo; import com.google.gerrit.extensions.api.access.ProjectAccessInfo;
import com.google.gerrit.extensions.api.access.ProjectAccessInput; import com.google.gerrit.extensions.api.access.ProjectAccessInput;
@@ -28,6 +29,7 @@ import com.google.gerrit.extensions.api.projects.CommitApi;
import com.google.gerrit.extensions.api.projects.ConfigInfo; import com.google.gerrit.extensions.api.projects.ConfigInfo;
import com.google.gerrit.extensions.api.projects.ConfigInput; import com.google.gerrit.extensions.api.projects.ConfigInput;
import com.google.gerrit.extensions.api.projects.DashboardApi; import com.google.gerrit.extensions.api.projects.DashboardApi;
import com.google.gerrit.extensions.api.projects.DashboardInfo;
import com.google.gerrit.extensions.api.projects.DeleteBranchesInput; import com.google.gerrit.extensions.api.projects.DeleteBranchesInput;
import com.google.gerrit.extensions.api.projects.DeleteTagsInput; import com.google.gerrit.extensions.api.projects.DeleteTagsInput;
import com.google.gerrit.extensions.api.projects.DescriptionInput; import com.google.gerrit.extensions.api.projects.DescriptionInput;
@@ -39,6 +41,7 @@ import com.google.gerrit.extensions.common.ChangeInfo;
import com.google.gerrit.extensions.common.ProjectInfo; import com.google.gerrit.extensions.common.ProjectInfo;
import com.google.gerrit.extensions.restapi.BadRequestException; import com.google.gerrit.extensions.restapi.BadRequestException;
import com.google.gerrit.extensions.restapi.IdString; import com.google.gerrit.extensions.restapi.IdString;
import com.google.gerrit.extensions.restapi.NotImplementedException;
import com.google.gerrit.extensions.restapi.ResourceConflictException; import com.google.gerrit.extensions.restapi.ResourceConflictException;
import com.google.gerrit.extensions.restapi.ResourceNotFoundException; import com.google.gerrit.extensions.restapi.ResourceNotFoundException;
import com.google.gerrit.extensions.restapi.RestApiException; import com.google.gerrit.extensions.restapi.RestApiException;
@@ -58,6 +61,7 @@ import com.google.gerrit.server.project.GetConfig;
import com.google.gerrit.server.project.GetDescription; import com.google.gerrit.server.project.GetDescription;
import com.google.gerrit.server.project.ListBranches; import com.google.gerrit.server.project.ListBranches;
import com.google.gerrit.server.project.ListChildProjects; import com.google.gerrit.server.project.ListChildProjects;
import com.google.gerrit.server.project.ListDashboards;
import com.google.gerrit.server.project.ListTags; import com.google.gerrit.server.project.ListTags;
import com.google.gerrit.server.project.ProjectJson; import com.google.gerrit.server.project.ProjectJson;
import com.google.gerrit.server.project.ProjectResource; import com.google.gerrit.server.project.ProjectResource;
@@ -68,6 +72,7 @@ import com.google.gerrit.server.project.SetAccess;
import com.google.inject.Provider; import com.google.inject.Provider;
import com.google.inject.assistedinject.Assisted; import com.google.inject.assistedinject.Assisted;
import com.google.inject.assistedinject.AssistedInject; import com.google.inject.assistedinject.AssistedInject;
import java.util.Collections;
import java.util.List; import java.util.List;
public class ProjectApiImpl implements ProjectApi { public class ProjectApiImpl implements ProjectApi {
@@ -104,6 +109,7 @@ public class ProjectApiImpl implements ProjectApi {
private final CommitApiImpl.Factory commitApi; private final CommitApiImpl.Factory commitApi;
private final DashboardApiImpl.Factory dashboardApi; private final DashboardApiImpl.Factory dashboardApi;
private final CheckAccess checkAccess; private final CheckAccess checkAccess;
private final Provider<ListDashboards> listDashboards;
@AssistedInject @AssistedInject
ProjectApiImpl( ProjectApiImpl(
@@ -132,6 +138,7 @@ public class ProjectApiImpl implements ProjectApi {
CommitApiImpl.Factory commitApi, CommitApiImpl.Factory commitApi,
DashboardApiImpl.Factory dashboardApi, DashboardApiImpl.Factory dashboardApi,
CheckAccess checkAccess, CheckAccess checkAccess,
Provider<ListDashboards> listDashboards,
@Assisted ProjectResource project) { @Assisted ProjectResource project) {
this( this(
user, user,
@@ -160,6 +167,7 @@ public class ProjectApiImpl implements ProjectApi {
commitApi, commitApi,
dashboardApi, dashboardApi,
checkAccess, checkAccess,
listDashboards,
null); null);
} }
@@ -190,6 +198,7 @@ public class ProjectApiImpl implements ProjectApi {
CommitApiImpl.Factory commitApi, CommitApiImpl.Factory commitApi,
DashboardApiImpl.Factory dashboardApi, DashboardApiImpl.Factory dashboardApi,
CheckAccess checkAccess, CheckAccess checkAccess,
Provider<ListDashboards> listDashboards,
@Assisted String name) { @Assisted String name) {
this( this(
user, user,
@@ -218,6 +227,7 @@ public class ProjectApiImpl implements ProjectApi {
commitApi, commitApi,
dashboardApi, dashboardApi,
checkAccess, checkAccess,
listDashboards,
name); name);
} }
@@ -248,6 +258,7 @@ public class ProjectApiImpl implements ProjectApi {
CommitApiImpl.Factory commitApi, CommitApiImpl.Factory commitApi,
DashboardApiImpl.Factory dashboardApi, DashboardApiImpl.Factory dashboardApi,
CheckAccess checkAccess, CheckAccess checkAccess,
Provider<ListDashboards> listDashboards,
String name) { String name) {
this.user = user; this.user = user;
this.permissionBackend = permissionBackend; this.permissionBackend = permissionBackend;
@@ -275,6 +286,7 @@ public class ProjectApiImpl implements ProjectApi {
this.createAccessChange = createAccessChange; this.createAccessChange = createAccessChange;
this.dashboardApi = dashboardApi; this.dashboardApi = dashboardApi;
this.checkAccess = checkAccess; this.checkAccess = checkAccess;
this.listDashboards = listDashboards;
this.name = name; this.name = name;
} }
@@ -473,6 +485,27 @@ public class ProjectApiImpl implements ProjectApi {
return dashboard(DEFAULT_DASHBOARD_NAME); return dashboard(DEFAULT_DASHBOARD_NAME);
} }
@Override
public ListDashboardsRequest dashboards() throws RestApiException {
return new ListDashboardsRequest() {
@Override
public List<DashboardInfo> get() throws RestApiException {
try {
List<?> r = listDashboards.get().apply(checkExists());
if (r.isEmpty()) {
return Collections.emptyList();
}
if (r.get(0) instanceof DashboardInfo) {
return r.stream().map(i -> (DashboardInfo) i).collect(toList());
}
throw new NotImplementedException("list with inheritance");
} catch (Exception e) {
throw asRestApiException("Cannot list dashboards", e);
}
}
};
}
private ProjectResource checkExists() throws ResourceNotFoundException { private ProjectResource checkExists() throws ResourceNotFoundException {
if (project == null) { if (project == null) {
throw new ResourceNotFoundException(name); throw new ResourceNotFoundException(name);

View File

@@ -46,7 +46,7 @@ import org.kohsuke.args4j.Option;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
class ListDashboards implements RestReadView<ProjectResource> { public class ListDashboards implements RestReadView<ProjectResource> {
private static final Logger log = LoggerFactory.getLogger(ListDashboards.class); private static final Logger log = LoggerFactory.getLogger(ListDashboards.class);
private final GitRepositoryManager gitManager; private final GitRepositoryManager gitManager;