diff --git a/gerrit-acceptance-tests/src/test/java/com/google/gerrit/acceptance/api/project/DashboardIT.java b/gerrit-acceptance-tests/src/test/java/com/google/gerrit/acceptance/api/project/DashboardIT.java new file mode 100644 index 0000000000..f55665dcf0 --- /dev/null +++ b/gerrit-acceptance-tests/src/test/java/com/google/gerrit/acceptance/api/project/DashboardIT.java @@ -0,0 +1,29 @@ +// Copyright (C) 2017 The Android Open Source Project +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package com.google.gerrit.acceptance.api.project; + +import com.google.gerrit.acceptance.AbstractDaemonTest; +import com.google.gerrit.acceptance.NoHttpd; +import com.google.gerrit.extensions.restapi.ResourceNotFoundException; +import org.junit.Test; + +@NoHttpd +public class DashboardIT extends AbstractDaemonTest { + @Test + public void defaultDashboardDoesNotExist() throws Exception { + exception.expect(ResourceNotFoundException.class); + gApi.projects().name(project.get()).dashboard("default").get(); + } +} diff --git a/gerrit-extension-api/src/main/java/com/google/gerrit/extensions/api/projects/DashboardApi.java b/gerrit-extension-api/src/main/java/com/google/gerrit/extensions/api/projects/DashboardApi.java new file mode 100644 index 0000000000..a411e0eafe --- /dev/null +++ b/gerrit-extension-api/src/main/java/com/google/gerrit/extensions/api/projects/DashboardApi.java @@ -0,0 +1,41 @@ +// Copyright (C) 2017 The Android Open Source Project +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package com.google.gerrit.extensions.api.projects; + +import com.google.gerrit.extensions.restapi.NotImplementedException; +import com.google.gerrit.extensions.restapi.RestApiException; + +public interface DashboardApi { + + DashboardInfo get() throws RestApiException; + + DashboardInfo get(boolean inherited) throws RestApiException; + + /** + * A default implementation which allows source compatibility when adding new methods to the + * interface. + */ + class NotImplemented implements DashboardApi { + @Override + public DashboardInfo get() throws RestApiException { + throw new NotImplementedException(); + } + + @Override + public DashboardInfo get(boolean inherited) throws RestApiException { + throw new NotImplementedException(); + } + } +} diff --git a/gerrit-extension-api/src/main/java/com/google/gerrit/extensions/api/projects/ProjectApi.java b/gerrit-extension-api/src/main/java/com/google/gerrit/extensions/api/projects/ProjectApi.java index 734acb15b0..61e8cba2c9 100644 --- a/gerrit-extension-api/src/main/java/com/google/gerrit/extensions/api/projects/ProjectApi.java +++ b/gerrit-extension-api/src/main/java/com/google/gerrit/extensions/api/projects/ProjectApi.java @@ -135,6 +135,14 @@ public interface ProjectApi { */ CommitApi commit(String commit) throws RestApiException; + /** + * Lookup a dashboard by its name. + * + * @param name the name. + * @return API for accessing the dashboard. + */ + DashboardApi dashboard(String name) throws RestApiException; + /** * A default implementation which allows source compatibility when adding new methods to the * interface. @@ -239,5 +247,10 @@ public interface ProjectApi { public CommitApi commit(String commit) throws RestApiException { throw new NotImplementedException(); } + + @Override + public DashboardApi dashboard(String name) throws RestApiException { + throw new NotImplementedException(); + } } } diff --git a/gerrit-server/src/main/java/com/google/gerrit/server/api/projects/DashboardApiImpl.java b/gerrit-server/src/main/java/com/google/gerrit/server/api/projects/DashboardApiImpl.java new file mode 100644 index 0000000000..28ed49409e --- /dev/null +++ b/gerrit-server/src/main/java/com/google/gerrit/server/api/projects/DashboardApiImpl.java @@ -0,0 +1,76 @@ +// Copyright (C) 2017 The Android Open Source Project +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package com.google.gerrit.server.api.projects; + +import static com.google.gerrit.server.api.ApiUtil.asRestApiException; + +import com.google.gerrit.extensions.api.projects.DashboardApi; +import com.google.gerrit.extensions.api.projects.DashboardInfo; +import com.google.gerrit.extensions.restapi.IdString; +import com.google.gerrit.extensions.restapi.ResourceNotFoundException; +import com.google.gerrit.extensions.restapi.RestApiException; +import com.google.gerrit.server.permissions.PermissionBackendException; +import com.google.gerrit.server.project.DashboardResource; +import com.google.gerrit.server.project.DashboardsCollection; +import com.google.gerrit.server.project.GetDashboard; +import com.google.gerrit.server.project.ProjectResource; +import com.google.inject.Inject; +import com.google.inject.Provider; +import com.google.inject.assistedinject.Assisted; +import java.io.IOException; +import org.eclipse.jgit.errors.ConfigInvalidException; + +public class DashboardApiImpl implements DashboardApi { + interface Factory { + DashboardApiImpl create(ProjectResource project, String name); + } + + private final DashboardsCollection dashboards; + private final Provider getDashboard; + private final ProjectResource project; + private final String name; + + @Inject + DashboardApiImpl( + DashboardsCollection dashboards, + Provider getDashboard, + @Assisted ProjectResource project, + @Assisted String name) { + this.dashboards = dashboards; + this.getDashboard = getDashboard; + this.project = project; + this.name = name; + } + + @Override + public DashboardInfo get() throws RestApiException { + return get(false); + } + + @Override + public DashboardInfo get(boolean inherited) throws RestApiException { + try { + return getDashboard.get().setInherited(inherited).apply(resource()); + } catch (IOException | PermissionBackendException | ConfigInvalidException e) { + throw asRestApiException("Cannot read dashboard", e); + } + } + + private DashboardResource resource() + throws ResourceNotFoundException, IOException, ConfigInvalidException, + PermissionBackendException { + return dashboards.parse(project, IdString.fromDecoded(name)); + } +} diff --git a/gerrit-server/src/main/java/com/google/gerrit/server/api/projects/Module.java b/gerrit-server/src/main/java/com/google/gerrit/server/api/projects/Module.java index a4fe39b49e..f1e21d2806 100644 --- a/gerrit-server/src/main/java/com/google/gerrit/server/api/projects/Module.java +++ b/gerrit-server/src/main/java/com/google/gerrit/server/api/projects/Module.java @@ -27,5 +27,6 @@ public class Module extends FactoryModule { factory(ProjectApiImpl.Factory.class); factory(ChildProjectApiImpl.Factory.class); factory(CommitApiImpl.Factory.class); + factory(DashboardApiImpl.Factory.class); } } diff --git a/gerrit-server/src/main/java/com/google/gerrit/server/api/projects/ProjectApiImpl.java b/gerrit-server/src/main/java/com/google/gerrit/server/api/projects/ProjectApiImpl.java index 650924753b..ea1176c14e 100644 --- a/gerrit-server/src/main/java/com/google/gerrit/server/api/projects/ProjectApiImpl.java +++ b/gerrit-server/src/main/java/com/google/gerrit/server/api/projects/ProjectApiImpl.java @@ -24,6 +24,7 @@ import com.google.gerrit.extensions.api.projects.ChildProjectApi; import com.google.gerrit.extensions.api.projects.CommitApi; import com.google.gerrit.extensions.api.projects.ConfigInfo; import com.google.gerrit.extensions.api.projects.ConfigInput; +import com.google.gerrit.extensions.api.projects.DashboardApi; import com.google.gerrit.extensions.api.projects.DeleteBranchesInput; import com.google.gerrit.extensions.api.projects.DeleteTagsInput; import com.google.gerrit.extensions.api.projects.DescriptionInput; @@ -96,6 +97,7 @@ public class ProjectApiImpl implements ProjectApi { private final DeleteTags deleteTags; private final CommitsCollection commitsCollection; private final CommitApiImpl.Factory commitApi; + private final DashboardApiImpl.Factory dashboardApi; @AssistedInject ProjectApiImpl( @@ -122,6 +124,7 @@ public class ProjectApiImpl implements ProjectApi { DeleteTags deleteTags, CommitsCollection commitsCollection, CommitApiImpl.Factory commitApi, + DashboardApiImpl.Factory dashboardApi, @Assisted ProjectResource project) { this( user, @@ -148,6 +151,7 @@ public class ProjectApiImpl implements ProjectApi { project, commitsCollection, commitApi, + dashboardApi, null); } @@ -176,6 +180,7 @@ public class ProjectApiImpl implements ProjectApi { DeleteTags deleteTags, CommitsCollection commitsCollection, CommitApiImpl.Factory commitApi, + DashboardApiImpl.Factory dashboardApi, @Assisted String name) { this( user, @@ -202,6 +207,7 @@ public class ProjectApiImpl implements ProjectApi { null, commitsCollection, commitApi, + dashboardApi, name); } @@ -230,6 +236,7 @@ public class ProjectApiImpl implements ProjectApi { ProjectResource project, CommitsCollection commitsCollection, CommitApiImpl.Factory commitApi, + DashboardApiImpl.Factory dashboardApi, String name) { this.user = user; this.permissionBackend = permissionBackend; @@ -256,6 +263,7 @@ public class ProjectApiImpl implements ProjectApi { this.commitsCollection = commitsCollection; this.commitApi = commitApi; this.createAccessChange = createAccessChange; + this.dashboardApi = dashboardApi; } @Override @@ -447,6 +455,15 @@ public class ProjectApiImpl implements ProjectApi { } } + @Override + public DashboardApi dashboard(String name) throws RestApiException { + try { + return dashboardApi.create(checkExists(), name); + } catch (Exception e) { + throw asRestApiException("Cannot parse dashboard", e); + } + } + private ProjectResource checkExists() throws ResourceNotFoundException { if (project == null) { throw new ResourceNotFoundException(name); diff --git a/gerrit-server/src/main/java/com/google/gerrit/server/project/DashboardsCollection.java b/gerrit-server/src/main/java/com/google/gerrit/server/project/DashboardsCollection.java index eccbd93bae..9151ed10d2 100644 --- a/gerrit-server/src/main/java/com/google/gerrit/server/project/DashboardsCollection.java +++ b/gerrit-server/src/main/java/com/google/gerrit/server/project/DashboardsCollection.java @@ -54,7 +54,7 @@ import org.eclipse.jgit.lib.ObjectId; import org.eclipse.jgit.lib.Repository; @Singleton -class DashboardsCollection +public class DashboardsCollection implements ChildCollection, AcceptsCreate { private static final String DEFAULT_DASHBOARD_NAME = "default"; diff --git a/gerrit-server/src/main/java/com/google/gerrit/server/project/GetDashboard.java b/gerrit-server/src/main/java/com/google/gerrit/server/project/GetDashboard.java index fd99ac40f6..adca214fb7 100644 --- a/gerrit-server/src/main/java/com/google/gerrit/server/project/GetDashboard.java +++ b/gerrit-server/src/main/java/com/google/gerrit/server/project/GetDashboard.java @@ -33,7 +33,7 @@ import java.util.List; import org.eclipse.jgit.errors.ConfigInvalidException; import org.kohsuke.args4j.Option; -class GetDashboard implements RestReadView { +public class GetDashboard implements RestReadView { private final DashboardsCollection dashboards; @Option(name = "--inherited", usage = "include inherited dashboards") @@ -44,6 +44,11 @@ class GetDashboard implements RestReadView { this.dashboards = dashboards; } + public GetDashboard setInherited(boolean inherited) { + this.inherited = inherited; + return this; + } + @Override public DashboardInfo apply(DashboardResource resource) throws ResourceNotFoundException, ResourceConflictException, IOException,