aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSimon Brandhof <simon.brandhof@sonarsource.com>2017-08-01 23:18:43 +0200
committerJanos Gyerik <janos.gyerik@sonarsource.com>2017-09-12 10:55:10 +0200
commitbd0860b8fcf21d0dc7b394028a763f562fcbee41 (patch)
tree17293cb6a32fe4ac8e31cd86ce906222547bee43
parentef43a88439cf19c0f38c0d1d3d1df27adb82840f (diff)
downloadsonarqube-bd0860b8fcf21d0dc7b394028a763f562fcbee41.tar.gz
sonarqube-bd0860b8fcf21d0dc7b394028a763f562fcbee41.zip
SONAR-9616 add WS api/project_branches/list
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java2
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/project/ws/ProjectsWsModule.java1
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/projectbranch/package-info.java23
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/projectbranch/ws/BranchWsAction.java26
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/projectbranch/ws/BranchWsModule.java31
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/projectbranch/ws/BranchesWs.java41
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/projectbranch/ws/ListAction.java (renamed from server/sonar-server/src/main/java/org/sonar/server/project/ws/BranchesAction.java)30
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/projectbranch/ws/package-info.java23
-rw-r--r--server/sonar-server/src/main/resources/org/sonar/server/projectbranch/ws/list-example.json14
-rw-r--r--server/sonar-server/src/test/java/org/sonar/server/project/ws/ProjectsWsModuleTest.java2
-rw-r--r--server/sonar-server/src/test/java/org/sonar/server/projectbranch/ws/BranchWsModuleTest.java35
-rw-r--r--server/sonar-server/src/test/java/org/sonar/server/projectbranch/ws/BranchesWsTest.java51
-rw-r--r--server/sonar-server/src/test/java/org/sonar/server/projectbranch/ws/ListActionTest.java154
-rw-r--r--sonar-ws/src/main/protobuf/ws-projectbranches.proto47
-rw-r--r--sonar-ws/src/main/protobuf/ws-projects.proto21
15 files changed, 468 insertions, 33 deletions
diff --git a/server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java b/server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java
index a7683cebc35..05a52047308 100644
--- a/server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java
+++ b/server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java
@@ -142,6 +142,7 @@ import org.sonar.server.plugins.ws.UninstallAction;
import org.sonar.server.plugins.ws.UpdatesAction;
import org.sonar.server.project.ws.ProjectsWsModule;
import org.sonar.server.projectanalysis.ProjectAnalysisModule;
+import org.sonar.server.projectbranch.ws.BranchWsModule;
import org.sonar.server.projectlink.ws.ProjectLinksModule;
import org.sonar.server.projecttag.ws.ProjectTagsWsModule;
import org.sonar.server.property.InternalPropertiesImpl;
@@ -367,6 +368,7 @@ public class PlatformLevel4 extends PlatformLevel {
GroupPermissionChanger.class,
// components
+ BranchWsModule.class,
ProjectsWsModule.class,
ProjectsEsModule.class,
ProjectTagsWsModule.class,
diff --git a/server/sonar-server/src/main/java/org/sonar/server/project/ws/ProjectsWsModule.java b/server/sonar-server/src/main/java/org/sonar/server/project/ws/ProjectsWsModule.java
index bb8379878d3..48ce49927e7 100644
--- a/server/sonar-server/src/main/java/org/sonar/server/project/ws/ProjectsWsModule.java
+++ b/server/sonar-server/src/main/java/org/sonar/server/project/ws/ProjectsWsModule.java
@@ -29,7 +29,6 @@ public class ProjectsWsModule extends Module {
ProjectsWs.class,
CreateAction.class,
IndexAction.class,
- BranchesAction.class,
BulkDeleteAction.class,
DeleteAction.class,
UpdateKeyAction.class,
diff --git a/server/sonar-server/src/main/java/org/sonar/server/projectbranch/package-info.java b/server/sonar-server/src/main/java/org/sonar/server/projectbranch/package-info.java
new file mode 100644
index 00000000000..6aa3bb36dc4
--- /dev/null
+++ b/server/sonar-server/src/main/java/org/sonar/server/projectbranch/package-info.java
@@ -0,0 +1,23 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2017 SonarSource SA
+ * mailto:info AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+@ParametersAreNonnullByDefault
+package org.sonar.server.projectbranch;
+
+import javax.annotation.ParametersAreNonnullByDefault;
diff --git a/server/sonar-server/src/main/java/org/sonar/server/projectbranch/ws/BranchWsAction.java b/server/sonar-server/src/main/java/org/sonar/server/projectbranch/ws/BranchWsAction.java
new file mode 100644
index 00000000000..7a2c3f983b0
--- /dev/null
+++ b/server/sonar-server/src/main/java/org/sonar/server/projectbranch/ws/BranchWsAction.java
@@ -0,0 +1,26 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2017 SonarSource SA
+ * mailto:info AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.server.projectbranch.ws;
+
+import org.sonar.server.ws.WsAction;
+
+public interface BranchWsAction extends WsAction {
+ // marker interface
+}
diff --git a/server/sonar-server/src/main/java/org/sonar/server/projectbranch/ws/BranchWsModule.java b/server/sonar-server/src/main/java/org/sonar/server/projectbranch/ws/BranchWsModule.java
new file mode 100644
index 00000000000..cfee79582ef
--- /dev/null
+++ b/server/sonar-server/src/main/java/org/sonar/server/projectbranch/ws/BranchWsModule.java
@@ -0,0 +1,31 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2017 SonarSource SA
+ * mailto:info AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.server.projectbranch.ws;
+
+import org.sonar.core.platform.Module;
+
+public class BranchWsModule extends Module {
+ @Override
+ protected void configureModule() {
+ add(
+ ListAction.class,
+ BranchesWs.class);
+ }
+}
diff --git a/server/sonar-server/src/main/java/org/sonar/server/projectbranch/ws/BranchesWs.java b/server/sonar-server/src/main/java/org/sonar/server/projectbranch/ws/BranchesWs.java
new file mode 100644
index 00000000000..258bcce04df
--- /dev/null
+++ b/server/sonar-server/src/main/java/org/sonar/server/projectbranch/ws/BranchesWs.java
@@ -0,0 +1,41 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2017 SonarSource SA
+ * mailto:info AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.server.projectbranch.ws;
+
+import java.util.Arrays;
+import org.sonar.api.server.ws.WebService;
+
+public class BranchesWs implements WebService {
+
+ private final BranchWsAction[] actions;
+
+ public BranchesWs(BranchWsAction... actions) {
+ this.actions = actions;
+ }
+
+ @Override
+ public void define(Context context) {
+ NewController controller = context.createController("api/project_branches")
+ .setSince("6.6");
+ Arrays.stream(actions).forEach(action -> action.define(controller));
+ controller.done();
+ }
+
+}
diff --git a/server/sonar-server/src/main/java/org/sonar/server/project/ws/BranchesAction.java b/server/sonar-server/src/main/java/org/sonar/server/projectbranch/ws/ListAction.java
index 83c6eee4a25..1e65b16598a 100644
--- a/server/sonar-server/src/main/java/org/sonar/server/project/ws/BranchesAction.java
+++ b/server/sonar-server/src/main/java/org/sonar/server/projectbranch/ws/ListAction.java
@@ -17,9 +17,11 @@
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
-package org.sonar.server.project.ws;
+package org.sonar.server.projectbranch.ws;
+import com.google.common.io.Resources;
import java.util.Collection;
+import org.sonar.api.resources.Qualifiers;
import org.sonar.api.server.ws.Request;
import org.sonar.api.server.ws.Response;
import org.sonar.api.server.ws.WebService;
@@ -31,30 +33,35 @@ import org.sonar.db.component.BranchKeyType;
import org.sonar.db.component.ComponentDto;
import org.sonar.server.user.UserSession;
import org.sonar.server.ws.WsUtils;
-import org.sonarqube.ws.WsProjects;
+import org.sonarqube.ws.WsBranches;
import static org.sonar.core.util.Protobuf.setNullable;
+import static org.sonar.server.ws.KeyExamples.KEY_PROJECT_EXAMPLE_001;
-public class BranchesAction implements ProjectsWsAction {
+public class ListAction implements BranchWsAction {
private static final String PROJECT_PARAM = "project";
private final DbClient dbClient;
private final UserSession userSession;
- public BranchesAction(DbClient dbClient, UserSession userSession) {
+ public ListAction(DbClient dbClient, UserSession userSession) {
this.dbClient = dbClient;
this.userSession = userSession;
}
@Override
public void define(WebService.NewController context) {
- WebService.NewAction action = context.createAction("branches")
+ WebService.NewAction action = context.createAction("list")
.setSince("6.6")
+ .setDescription("List the branches of a project")
+ .setResponseExample(Resources.getResource(getClass(), "list-example.json"))
.setHandler(this);
action
.createParam(PROJECT_PARAM)
+ .setDescription("Project key")
+ .setExampleValue(KEY_PROJECT_EXAMPLE_001)
.setRequired(true);
}
@@ -65,9 +72,12 @@ public class BranchesAction implements ProjectsWsAction {
try (DbSession dbSession = dbClient.openSession(false)) {
ComponentDto project = dbClient.componentDao().selectOrFailByKey(dbSession, projectKey);
userSession.checkComponentPermission(UserRole.USER, project);
- Collection<BranchDto> branches = dbClient.branchDao().selectByComponent(dbSession, project);
+ if (!project.isEnabled() || !Qualifiers.PROJECT.equals(project.qualifier())) {
+ throw new IllegalArgumentException("Invalid project key");
+ }
- WsProjects.BranchesWsResponse.Builder protobufResponse = WsProjects.BranchesWsResponse.newBuilder();
+ Collection<BranchDto> branches = dbClient.branchDao().selectByComponent(dbSession, project);
+ WsBranches.ListWsResponse.Builder protobufResponse = WsBranches.ListWsResponse.newBuilder();
branches.stream()
.filter(b -> b.getKeeType().equals(BranchKeyType.BRANCH))
.forEach(b -> addToProtobuf(protobufResponse, b));
@@ -75,11 +85,11 @@ public class BranchesAction implements ProjectsWsAction {
}
}
- private static void addToProtobuf(WsProjects.BranchesWsResponse.Builder response, BranchDto branch) {
- WsProjects.BranchesWsResponse.Branch.Builder builder = response.addBranchesBuilder();
+ private static void addToProtobuf(WsBranches.ListWsResponse.Builder response, BranchDto branch) {
+ WsBranches.ListWsResponse.Branch.Builder builder = response.addBranchesBuilder();
setNullable(branch.getKey(), builder::setName);
builder.setIsMain(branch.isMain());
- builder.setType(WsProjects.BranchesWsResponse.BranchType.valueOf(branch.getBranchType().name()));
+ builder.setType(WsBranches.ListWsResponse.BranchType.valueOf(branch.getBranchType().name()));
builder.build();
}
}
diff --git a/server/sonar-server/src/main/java/org/sonar/server/projectbranch/ws/package-info.java b/server/sonar-server/src/main/java/org/sonar/server/projectbranch/ws/package-info.java
new file mode 100644
index 00000000000..86bd1e8363d
--- /dev/null
+++ b/server/sonar-server/src/main/java/org/sonar/server/projectbranch/ws/package-info.java
@@ -0,0 +1,23 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2017 SonarSource SA
+ * mailto:info AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+@ParametersAreNonnullByDefault
+package org.sonar.server.projectbranch.ws;
+
+import javax.annotation.ParametersAreNonnullByDefault;
diff --git a/server/sonar-server/src/main/resources/org/sonar/server/projectbranch/ws/list-example.json b/server/sonar-server/src/main/resources/org/sonar/server/projectbranch/ws/list-example.json
new file mode 100644
index 00000000000..a7db235d028
--- /dev/null
+++ b/server/sonar-server/src/main/resources/org/sonar/server/projectbranch/ws/list-example.json
@@ -0,0 +1,14 @@
+{
+ "branches": [
+ {
+ "name": "feature/bar",
+ "isMain": false,
+ "type": "LONG"
+ },
+ {
+ "name": "feature/foo",
+ "isMain": false,
+ "type": "LONG"
+ }
+ ]
+}
diff --git a/server/sonar-server/src/test/java/org/sonar/server/project/ws/ProjectsWsModuleTest.java b/server/sonar-server/src/test/java/org/sonar/server/project/ws/ProjectsWsModuleTest.java
index 49170c2652a..dd594cda099 100644
--- a/server/sonar-server/src/test/java/org/sonar/server/project/ws/ProjectsWsModuleTest.java
+++ b/server/sonar-server/src/test/java/org/sonar/server/project/ws/ProjectsWsModuleTest.java
@@ -30,6 +30,6 @@ public class ProjectsWsModuleTest {
public void verify_count_of_added_components() {
ComponentContainer container = new ComponentContainer();
new ProjectsWsModule().configure(container);
- assertThat(container.size()).isEqualTo(COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER + 15);
+ assertThat(container.size()).isEqualTo(COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER + 14);
}
}
diff --git a/server/sonar-server/src/test/java/org/sonar/server/projectbranch/ws/BranchWsModuleTest.java b/server/sonar-server/src/test/java/org/sonar/server/projectbranch/ws/BranchWsModuleTest.java
new file mode 100644
index 00000000000..35d8690dd29
--- /dev/null
+++ b/server/sonar-server/src/test/java/org/sonar/server/projectbranch/ws/BranchWsModuleTest.java
@@ -0,0 +1,35 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2017 SonarSource SA
+ * mailto:info AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.server.projectbranch.ws;
+
+import org.junit.Test;
+import org.sonar.core.platform.ComponentContainer;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.sonar.core.platform.ComponentContainer.COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER;
+
+public class BranchWsModuleTest {
+ @Test
+ public void verify_count_of_added_components() {
+ ComponentContainer container = new ComponentContainer();
+ new BranchWsModule().configure(container);
+ assertThat(container.size()).isEqualTo(COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER + 2);
+ }
+}
diff --git a/server/sonar-server/src/test/java/org/sonar/server/projectbranch/ws/BranchesWsTest.java b/server/sonar-server/src/test/java/org/sonar/server/projectbranch/ws/BranchesWsTest.java
new file mode 100644
index 00000000000..9938bd991c0
--- /dev/null
+++ b/server/sonar-server/src/test/java/org/sonar/server/projectbranch/ws/BranchesWsTest.java
@@ -0,0 +1,51 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2017 SonarSource SA
+ * mailto:info AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.server.projectbranch.ws;
+
+import org.junit.Test;
+import org.sonar.api.server.ws.Request;
+import org.sonar.api.server.ws.Response;
+import org.sonar.api.server.ws.WebService;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class BranchesWsTest {
+
+ @Test
+ public void define_ws() {
+ BranchesWs underTest = new BranchesWs(new BranchWsAction() {
+ @Override
+ public void define(WebService.NewController context) {
+ context.createAction("foo").setHandler(this);
+ }
+
+ @Override
+ public void handle(Request request, Response response) throws Exception {
+
+ }
+ });
+
+ WebService.Context context = new WebService.Context();
+ underTest.define(context);
+
+ assertThat(context.controller("api/project_branches").action("foo")).isNotNull();
+ }
+
+}
diff --git a/server/sonar-server/src/test/java/org/sonar/server/projectbranch/ws/ListActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/projectbranch/ws/ListActionTest.java
new file mode 100644
index 00000000000..53f7a58cf60
--- /dev/null
+++ b/server/sonar-server/src/test/java/org/sonar/server/projectbranch/ws/ListActionTest.java
@@ -0,0 +1,154 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2017 SonarSource SA
+ * mailto:info AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.server.projectbranch.ws;
+
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.sonar.api.server.ws.WebService;
+import org.sonar.api.utils.System2;
+import org.sonar.api.web.UserRole;
+import org.sonar.db.DbTester;
+import org.sonar.db.RowNotFoundException;
+import org.sonar.db.component.BranchDto;
+import org.sonar.db.component.BranchKeyType;
+import org.sonar.db.component.BranchType;
+import org.sonar.db.component.ComponentDto;
+import org.sonar.db.component.ComponentTesting;
+import org.sonar.server.tester.UserSessionRule;
+import org.sonar.server.ws.WsActionTester;
+import org.sonarqube.ws.MediaTypes;
+import org.sonarqube.ws.WsBranches.ListWsResponse;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.tuple;
+import static org.sonar.test.JsonAssert.assertJson;
+
+public class ListActionTest {
+
+ @Rule
+ public ExpectedException expectedException = ExpectedException.none();
+
+ @Rule
+ public DbTester db = DbTester.create(System2.INSTANCE);
+
+ @Rule
+ public UserSessionRule userSession = UserSessionRule.standalone();
+
+ public WsActionTester tester = new WsActionTester(new ListAction(db.getDbClient(), userSession));
+
+ @Test
+ public void test_definition() {
+ WebService.Action definition = tester.getDef();
+ assertThat(definition.key()).isEqualTo("list");
+ assertThat(definition.isPost()).isFalse();
+ assertThat(definition.params()).extracting(WebService.Param::key).containsExactlyInAnyOrder("project");
+ assertThat(definition.since()).isEqualTo("6.6");
+ }
+
+ @Test
+ public void fail_if_missing_project_parameter() {
+ expectedException.expect(IllegalArgumentException.class);
+ expectedException.expectMessage("The 'project' parameter is missing");
+
+ tester.newRequest().execute();
+ }
+
+ @Test
+ public void fail_if_not_a_reference_on_project() {
+ ComponentDto project = db.components().insertPrivateProject();
+ ComponentDto file = db.components().insertComponent(ComponentTesting.newFileDto(project));
+ userSession.logIn().addProjectPermission(UserRole.USER, project);
+
+ expectedException.expect(IllegalArgumentException.class);
+ expectedException.expectMessage("Invalid project key");
+
+ tester.newRequest()
+ .setParam("project", file.getDbKey())
+ .execute();
+ }
+
+ @Test
+ public void fail_if_project_does_not_exist() {
+ expectedException.expect(RowNotFoundException.class);
+ expectedException.expectMessage("Component key 'foo' not found");
+
+ tester.newRequest()
+ .setParam("project", "foo")
+ .execute();
+ }
+
+ @Test
+ public void test_project_with_zero_branches() {
+ ComponentDto project = db.components().insertPrivateProject();
+ userSession.logIn().addProjectPermission(UserRole.USER, project);
+
+ String json = tester.newRequest()
+ .setParam("project", project.getDbKey())
+ .setMediaType(MediaTypes.JSON)
+ .execute()
+ .getInput();
+ assertJson(json).isSimilarTo("{\"branches\": []}");
+ }
+
+ @Test
+ public void test_project_with_branches() {
+ ComponentDto project = db.components().insertPrivateProject();
+ insertBranch(project, "feature/bar");
+ insertBranch(project, "feature/foo");
+ userSession.logIn().addProjectPermission(UserRole.USER, project);
+
+ ListWsResponse response = tester.newRequest()
+ .setParam("project", project.getDbKey())
+ .executeProtobuf(ListWsResponse.class);
+
+ assertThat(response.getBranchesList())
+ .extracting(ListWsResponse.Branch::getName, ListWsResponse.Branch::getType)
+ .containsExactlyInAnyOrder(
+ tuple("feature/foo", ListWsResponse.BranchType.LONG), tuple("feature/bar", ListWsResponse.BranchType.LONG));
+ }
+
+ @Test
+ public void test_example() {
+ ComponentDto project = db.components().insertPrivateProject();
+ insertBranch(project, "feature/bar");
+ insertBranch(project, "feature/foo");
+ userSession.logIn().addProjectPermission(UserRole.USER, project);
+
+ String json = tester.newRequest()
+ .setParam("project", project.getDbKey())
+ .execute()
+ .getInput();
+
+ assertJson(json).isSimilarTo(tester.getDef().responseExampleAsString());
+ }
+
+ private void insertBranch(ComponentDto project, String branchName) {
+ ComponentDto branch = db.components().insertProjectBranch(project, branchName);
+ BranchDto branchDto = new BranchDto();
+ branchDto.setUuid(branch.uuid());
+ branchDto.setProjectUuid(project.uuid());
+ branchDto.setBranchType(BranchType.LONG);
+ branchDto.setKeeType(BranchKeyType.BRANCH);
+ branchDto.setKey(branchName);
+ db.getDbClient().branchDao().insert(db.getSession(), branchDto);
+ db.commit();
+ }
+}
diff --git a/sonar-ws/src/main/protobuf/ws-projectbranches.proto b/sonar-ws/src/main/protobuf/ws-projectbranches.proto
new file mode 100644
index 00000000000..7b57be2e420
--- /dev/null
+++ b/sonar-ws/src/main/protobuf/ws-projectbranches.proto
@@ -0,0 +1,47 @@
+// SonarQube, open source software quality management tool.
+// Copyright (C) 2008-2016 SonarSource
+// mailto:contact AT sonarsource DOT com
+//
+// SonarQube is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 3 of the License, or (at your option) any later version.
+//
+// SonarQube is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with this program; if not, write to the Free Software Foundation,
+// Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+
+syntax = "proto2";
+
+package sonarqube.ws.projectbranch;
+
+option java_package = "org.sonarqube.ws";
+option java_outer_classname = "WsBranches";
+option optimize_for = SPEED;
+
+
+// WS api/project_branches/list
+message ListWsResponse {
+ repeated Branch branches = 1;
+
+ message Branch {
+ optional string name = 1;
+ optional bool isMain = 2;
+ optional BranchType type = 3;
+ }
+
+ enum BranchType {
+ // Zero is required in order to not get MAINTAINABILITY as default value
+ // See http://androiddevblog.com/protocol-buffers-pitfall-adding-enum-values/
+ UNKNOWN = 0;
+
+ LONG = 1;
+ SHORT = 2;
+ }
+
+}
diff --git a/sonar-ws/src/main/protobuf/ws-projects.proto b/sonar-ws/src/main/protobuf/ws-projects.proto
index 069db2f37f7..8668ffc355a 100644
--- a/sonar-ws/src/main/protobuf/ws-projects.proto
+++ b/sonar-ws/src/main/protobuf/ws-projects.proto
@@ -84,24 +84,3 @@ message BulkUpdateKeyWsResponse {
optional bool duplicate = 3;
}
}
-
-// WS api/projects/branches
-message BranchesWsResponse {
- repeated Branch branches = 1;
-
- message Branch {
- optional string name = 1;
- optional bool isMain = 2;
- optional BranchType type = 3;
- }
-
- enum BranchType {
- // Zero is required in order to not get MAINTAINABILITY as default value
- // See http://androiddevblog.com/protocol-buffers-pitfall-adding-enum-values/
- UNKNOWN = 0;
-
- LONG = 1;
- SHORT = 2;
- }
-
-}