]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-9616 add WS api/project_branches/list
authorSimon Brandhof <simon.brandhof@sonarsource.com>
Tue, 1 Aug 2017 21:18:43 +0000 (23:18 +0200)
committerJanos Gyerik <janos.gyerik@sonarsource.com>
Tue, 12 Sep 2017 08:55:10 +0000 (10:55 +0200)
16 files changed:
server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java
server/sonar-server/src/main/java/org/sonar/server/project/ws/BranchesAction.java [deleted file]
server/sonar-server/src/main/java/org/sonar/server/project/ws/ProjectsWsModule.java
server/sonar-server/src/main/java/org/sonar/server/projectbranch/package-info.java [new file with mode: 0644]
server/sonar-server/src/main/java/org/sonar/server/projectbranch/ws/BranchWsAction.java [new file with mode: 0644]
server/sonar-server/src/main/java/org/sonar/server/projectbranch/ws/BranchWsModule.java [new file with mode: 0644]
server/sonar-server/src/main/java/org/sonar/server/projectbranch/ws/BranchesWs.java [new file with mode: 0644]
server/sonar-server/src/main/java/org/sonar/server/projectbranch/ws/ListAction.java [new file with mode: 0644]
server/sonar-server/src/main/java/org/sonar/server/projectbranch/ws/package-info.java [new file with mode: 0644]
server/sonar-server/src/main/resources/org/sonar/server/projectbranch/ws/list-example.json [new file with mode: 0644]
server/sonar-server/src/test/java/org/sonar/server/project/ws/ProjectsWsModuleTest.java
server/sonar-server/src/test/java/org/sonar/server/projectbranch/ws/BranchWsModuleTest.java [new file with mode: 0644]
server/sonar-server/src/test/java/org/sonar/server/projectbranch/ws/BranchesWsTest.java [new file with mode: 0644]
server/sonar-server/src/test/java/org/sonar/server/projectbranch/ws/ListActionTest.java [new file with mode: 0644]
sonar-ws/src/main/protobuf/ws-projectbranches.proto [new file with mode: 0644]
sonar-ws/src/main/protobuf/ws-projects.proto

index a7683cebc35fa1382f0676ee8b8f7157e28d91f8..05a52047308d834d21f774b6fc49d45f24f1a52c 100644 (file)
@@ -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/BranchesAction.java b/server/sonar-server/src/main/java/org/sonar/server/project/ws/BranchesAction.java
deleted file mode 100644 (file)
index 83c6eee..0000000
+++ /dev/null
@@ -1,85 +0,0 @@
-/*
- * 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.project.ws;
-
-import java.util.Collection;
-import org.sonar.api.server.ws.Request;
-import org.sonar.api.server.ws.Response;
-import org.sonar.api.server.ws.WebService;
-import org.sonar.api.web.UserRole;
-import org.sonar.db.DbClient;
-import org.sonar.db.DbSession;
-import org.sonar.db.component.BranchDto;
-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 static org.sonar.core.util.Protobuf.setNullable;
-
-public class BranchesAction implements ProjectsWsAction {
-
-  private static final String PROJECT_PARAM = "project";
-
-  private final DbClient dbClient;
-  private final UserSession userSession;
-
-  public BranchesAction(DbClient dbClient, UserSession userSession) {
-    this.dbClient = dbClient;
-    this.userSession = userSession;
-  }
-
-  @Override
-  public void define(WebService.NewController context) {
-    WebService.NewAction action = context.createAction("branches")
-      .setSince("6.6")
-      .setHandler(this);
-
-    action
-      .createParam(PROJECT_PARAM)
-      .setRequired(true);
-  }
-
-  @Override
-  public void handle(Request request, Response response) throws Exception {
-    String projectKey = request.mandatoryParam(PROJECT_PARAM);
-
-    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);
-
-      WsProjects.BranchesWsResponse.Builder protobufResponse = WsProjects.BranchesWsResponse.newBuilder();
-      branches.stream()
-        .filter(b -> b.getKeeType().equals(BranchKeyType.BRANCH))
-        .forEach(b -> addToProtobuf(protobufResponse, b));
-      WsUtils.writeProtobuf(protobufResponse.build(), request, response);
-    }
-  }
-
-  private static void addToProtobuf(WsProjects.BranchesWsResponse.Builder response, BranchDto branch) {
-    WsProjects.BranchesWsResponse.Branch.Builder builder = response.addBranchesBuilder();
-    setNullable(branch.getKey(), builder::setName);
-    builder.setIsMain(branch.isMain());
-    builder.setType(WsProjects.BranchesWsResponse.BranchType.valueOf(branch.getBranchType().name()));
-    builder.build();
-  }
-}
index bb8379878d3f6e2408ff867360373aa35055defc..48ce49927e7544e57f6de2a69ee3eab4c797adaa 100644 (file)
@@ -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 (file)
index 0000000..6aa3bb3
--- /dev/null
@@ -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 (file)
index 0000000..7a2c3f9
--- /dev/null
@@ -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 (file)
index 0000000..cfee795
--- /dev/null
@@ -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 (file)
index 0000000..258bcce
--- /dev/null
@@ -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/projectbranch/ws/ListAction.java b/server/sonar-server/src/main/java/org/sonar/server/projectbranch/ws/ListAction.java
new file mode 100644 (file)
index 0000000..1e65b16
--- /dev/null
@@ -0,0 +1,95 @@
+/*
+ * 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 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;
+import org.sonar.api.web.UserRole;
+import org.sonar.db.DbClient;
+import org.sonar.db.DbSession;
+import org.sonar.db.component.BranchDto;
+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.WsBranches;
+
+import static org.sonar.core.util.Protobuf.setNullable;
+import static org.sonar.server.ws.KeyExamples.KEY_PROJECT_EXAMPLE_001;
+
+public class ListAction implements BranchWsAction {
+
+  private static final String PROJECT_PARAM = "project";
+
+  private final DbClient dbClient;
+  private final 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("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);
+  }
+
+  @Override
+  public void handle(Request request, Response response) throws Exception {
+    String projectKey = request.mandatoryParam(PROJECT_PARAM);
+
+    try (DbSession dbSession = dbClient.openSession(false)) {
+      ComponentDto project = dbClient.componentDao().selectOrFailByKey(dbSession, projectKey);
+      userSession.checkComponentPermission(UserRole.USER, project);
+      if (!project.isEnabled() || !Qualifiers.PROJECT.equals(project.qualifier())) {
+        throw new IllegalArgumentException("Invalid project key");
+      }
+
+      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));
+      WsUtils.writeProtobuf(protobufResponse.build(), request, response);
+    }
+  }
+
+  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(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 (file)
index 0000000..86bd1e8
--- /dev/null
@@ -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 (file)
index 0000000..a7db235
--- /dev/null
@@ -0,0 +1,14 @@
+{
+  "branches": [
+    {
+      "name": "feature/bar",
+      "isMain": false,
+      "type": "LONG"
+    },
+    {
+      "name": "feature/foo",
+      "isMain": false,
+      "type": "LONG"
+    }
+  ]
+}
index 49170c2652a977d7758ddd0ee10874954640b2ca..dd594cda0990cdf22804feebaa47833c5ba46223 100644 (file)
@@ -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 (file)
index 0000000..35d8690
--- /dev/null
@@ -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 (file)
index 0000000..9938bd9
--- /dev/null
@@ -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 (file)
index 0000000..53f7a58
--- /dev/null
@@ -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 (file)
index 0000000..7b57be2
--- /dev/null
@@ -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;
+  }
+
+}
index 069db2f37f7ba8e8d940a6dedd9b72de32788372..8668ffc355a34fd08683c6da23ea5a19267e644c 100644 (file)
@@ -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;
-  }
-
-}