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 index eafd29fa31..5cdb5833c1 100644 --- 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 @@ -23,16 +23,23 @@ import com.google.gerrit.common.data.Permission; import com.google.gerrit.extensions.api.projects.BranchInput; import com.google.gerrit.extensions.api.projects.DashboardInfo; import com.google.gerrit.extensions.restapi.BadRequestException; +import com.google.gerrit.extensions.restapi.ResourceConflictException; import com.google.gerrit.extensions.restapi.ResourceNotFoundException; import com.google.gerrit.server.project.DashboardsCollection; import java.util.List; import org.eclipse.jgit.junit.TestRepository; import org.eclipse.jgit.lib.Repository; import org.eclipse.jgit.revwalk.RevCommit; +import org.junit.Before; import org.junit.Test; @NoHttpd public class DashboardIT extends AbstractDaemonTest { + @Before + public void setup() throws Exception { + allow("refs/meta/dashboards/*", Permission.CREATE, REGISTERED_USERS); + } + @Test public void defaultDashboardDoesNotExist() throws Exception { exception.expect(ResourceNotFoundException.class); @@ -58,6 +65,31 @@ public class DashboardIT extends AbstractDaemonTest { assertThat(dashboards()).hasSize(1); } + @Test + public void setDefaultDashboard() throws Exception { + DashboardInfo info = createDashboard(DashboardsCollection.DEFAULT_DASHBOARD_NAME, "test"); + assertThat(info.isDefault).isNull(); + gApi.projects().name(project.get()).dashboard(info.id).setDefault(); + assertThat(gApi.projects().name(project.get()).dashboard(info.id).get().isDefault).isTrue(); + assertThat(gApi.projects().name(project.get()).defaultDashboard().get().id).isEqualTo(info.id); + } + + @Test + public void replaceDefaultDashboard() throws Exception { + DashboardInfo d1 = createDashboard(DashboardsCollection.DEFAULT_DASHBOARD_NAME, "test1"); + DashboardInfo d2 = createDashboard(DashboardsCollection.DEFAULT_DASHBOARD_NAME, "test2"); + assertThat(d1.isDefault).isNull(); + assertThat(d2.isDefault).isNull(); + gApi.projects().name(project.get()).dashboard(d1.id).setDefault(); + assertThat(gApi.projects().name(project.get()).dashboard(d1.id).get().isDefault).isTrue(); + assertThat(gApi.projects().name(project.get()).dashboard(d2.id).get().isDefault).isNull(); + assertThat(gApi.projects().name(project.get()).defaultDashboard().get().id).isEqualTo(d1.id); + gApi.projects().name(project.get()).dashboard(d2.id).setDefault(); + assertThat(gApi.projects().name(project.get()).defaultDashboard().get().id).isEqualTo(d2.id); + assertThat(gApi.projects().name(project.get()).dashboard(d1.id).get().isDefault).isNull(); + assertThat(gApi.projects().name(project.get()).dashboard(d2.id).get().isDefault).isTrue(); + } + @Test public void cannotGetDashboardWithInheritedForNonDefault() throws Exception { DashboardInfo info = createDashboard(DashboardsCollection.DEFAULT_DASHBOARD_NAME, "test"); @@ -73,8 +105,14 @@ public class DashboardIT extends AbstractDaemonTest { private DashboardInfo createDashboard(String ref, String path) throws Exception { DashboardInfo info = DashboardsCollection.newDashboardInfo(ref, path); String canonicalRef = DashboardsCollection.normalizeDashboardRef(info.ref); - allow("refs/meta/dashboards/*", Permission.CREATE, REGISTERED_USERS); - gApi.projects().name(project.get()).branch(canonicalRef).create(new BranchInput()); + try { + gApi.projects().name(project.get()).branch(canonicalRef).create(new BranchInput()); + } catch (ResourceConflictException e) { + // The branch already exists if this method has already been called once. + if (!e.getMessage().contains("already exists")) { + throw e; + } + } try (Repository r = repoManager.openRepository(project)) { TestRepository.CommitBuilder cb = new TestRepository<>(r).branch(canonicalRef).commit(); 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 index a411e0eafe..3cde570913 100644 --- 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 @@ -23,6 +23,8 @@ public interface DashboardApi { DashboardInfo get(boolean inherited) throws RestApiException; + void setDefault() throws RestApiException; + /** * A default implementation which allows source compatibility when adding new methods to the * interface. @@ -37,5 +39,10 @@ public interface DashboardApi { public DashboardInfo get(boolean inherited) throws RestApiException { throw new NotImplementedException(); } + + @Override + public void setDefault() 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 index 58cb59e7c3..f0a6009aa1 100644 --- 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 @@ -18,6 +18,7 @@ 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.common.SetDashboardInput; import com.google.gerrit.extensions.restapi.IdString; import com.google.gerrit.extensions.restapi.ResourceNotFoundException; import com.google.gerrit.extensions.restapi.RestApiException; @@ -26,6 +27,7 @@ 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.gerrit.server.project.SetDashboard; import com.google.inject.Inject; import com.google.inject.Provider; import com.google.inject.assistedinject.Assisted; @@ -39,6 +41,7 @@ public class DashboardApiImpl implements DashboardApi { private final DashboardsCollection dashboards; private final Provider get; + private final SetDashboard set; private final ProjectResource project; private final String id; @@ -46,10 +49,12 @@ public class DashboardApiImpl implements DashboardApi { DashboardApiImpl( DashboardsCollection dashboards, Provider get, + SetDashboard set, @Assisted ProjectResource project, @Assisted String id) { this.dashboards = dashboards; this.get = get; + this.set = set; this.project = project; this.id = id; } @@ -68,6 +73,17 @@ public class DashboardApiImpl implements DashboardApi { } } + @Override + public void setDefault() throws RestApiException { + SetDashboardInput input = new SetDashboardInput(); + input.id = id; + try { + set.apply(DashboardResource.projectDefault(project.getControl()), input); + } catch (Exception e) { + throw asRestApiException("Cannot set default dashboard", e); + } + } + private DashboardResource resource() throws ResourceNotFoundException, IOException, ConfigInvalidException, PermissionBackendException { diff --git a/gerrit-server/src/main/java/com/google/gerrit/server/project/SetDashboard.java b/gerrit-server/src/main/java/com/google/gerrit/server/project/SetDashboard.java index a9683cf2fa..21ec077621 100644 --- a/gerrit-server/src/main/java/com/google/gerrit/server/project/SetDashboard.java +++ b/gerrit-server/src/main/java/com/google/gerrit/server/project/SetDashboard.java @@ -27,7 +27,7 @@ import com.google.inject.Singleton; import java.io.IOException; @Singleton -class SetDashboard implements RestModifyView { +public class SetDashboard implements RestModifyView { private final Provider defaultSetter; @Inject