]> source.dussan.org Git - sonarqube.git/commitdiff
Set branches WS as public and fix description
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Wed, 18 Oct 2017 09:52:57 +0000 (11:52 +0200)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Thu, 19 Oct 2017 12:36:58 +0000 (14:36 +0200)
server/sonar-server/src/main/java/org/sonar/server/projectbranch/ws/BranchesWs.java
server/sonar-server/src/main/java/org/sonar/server/projectbranch/ws/DeleteAction.java
server/sonar-server/src/main/java/org/sonar/server/projectbranch/ws/ListAction.java
server/sonar-server/src/main/java/org/sonar/server/projectbranch/ws/RenameAction.java
server/sonar-server/src/test/java/org/sonar/server/projectbranch/ws/DeleteActionTest.java
server/sonar-server/src/test/java/org/sonar/server/projectbranch/ws/ListActionTest.java
server/sonar-server/src/test/java/org/sonar/server/projectbranch/ws/RenameActionTest.java
server/sonar-web/src/main/js/api/branches.ts
sonar-ws/src/main/java/org/sonarqube/ws/client/projectbranches/ProjectBranchesParameters.java
sonar-ws/src/main/java/org/sonarqube/ws/client/projectbranches/ProjectBranchesService.java
sonar-ws/src/test/java/org/sonarqube/ws/client/projectbranches/ProjectBranchesServiceTest.java

index 1492cef02dded5d412b01ef89113322b86855e9b..64657b98a1575f49f433edcb0b5830fbdd4417c3 100644 (file)
@@ -37,20 +37,24 @@ public class BranchesWs implements WebService {
   @Override
   public void define(Context context) {
     NewController controller = context.createController(CONTROLLER)
-      .setSince("6.6");
+      .setSince("6.6")
+      .setDescription("Manage branch (only available when the Branch plugin is installed)");
     Arrays.stream(actions).forEach(action -> action.define(controller));
     controller.done();
   }
 
-  static void addProjectBranchParams(NewAction action) {
+  static void addProjectParam(NewAction action) {
     action
       .createParam(PARAM_PROJECT)
       .setDescription("Project key")
       .setExampleValue(KEY_PROJECT_EXAMPLE_001)
       .setRequired(true);
+  }
+
+  static void addBranchParam(NewAction action) {
     action
       .createParam(PARAM_BRANCH)
-      .setDescription("Name of the branch to delete. Can't be the main branch of the project.")
+      .setDescription("Name of the branch")
       .setExampleValue("branch1")
       .setRequired(true);
   }
index c9a16b50e53689725b483e2a126f0e9437fd179b..50c419162456a47e019f67832a92e10969cffdf3 100644 (file)
@@ -33,6 +33,8 @@ import org.sonar.server.component.ComponentCleanerService;
 import org.sonar.server.component.ComponentFinder;
 import org.sonar.server.user.UserSession;
 
+import static org.sonar.server.projectbranch.ws.BranchesWs.addBranchParam;
+import static org.sonar.server.projectbranch.ws.BranchesWs.addProjectParam;
 import static org.sonar.server.ws.WsUtils.checkFoundWithOptional;
 import static org.sonarqube.ws.client.projectbranches.ProjectBranchesParameters.ACTION_DELETE;
 import static org.sonarqube.ws.client.projectbranches.ProjectBranchesParameters.PARAM_BRANCH;
@@ -55,13 +57,14 @@ public class DeleteAction implements BranchWsAction {
   public void define(NewController context) {
     WebService.NewAction action = context.createAction(ACTION_DELETE)
       .setSince("6.6")
-      .setDescription("Delete a non-main branch of a project. Requires permission to administer the project.")
+      .setDescription("Delete a non-main branch of a project.<br/>" +
+        "Requires 'Administer' rights on the specified project.")
       .setResponseExample(Resources.getResource(getClass(), "list-example.json"))
-      .setInternal(true)
       .setPost(true)
       .setHandler(this);
 
-    BranchesWs.addProjectBranchParams(action);
+    addProjectParam(action);
+    addBranchParam(action);
   }
 
   @Override
index 7e3486eb313f96ec4cc850e0824e505468ad4ec2..cc575a3868a6e9bb34a85fbf8901581626da8356 100644 (file)
@@ -57,7 +57,7 @@ import static org.sonar.core.util.stream.MoreCollectors.toList;
 import static org.sonar.core.util.stream.MoreCollectors.uniqueIndex;
 import static org.sonar.db.component.BranchType.LONG;
 import static org.sonar.db.component.BranchType.SHORT;
-import static org.sonar.server.ws.KeyExamples.KEY_PROJECT_EXAMPLE_001;
+import static org.sonar.server.projectbranch.ws.BranchesWs.addProjectParam;
 import static org.sonarqube.ws.client.projectbranches.ProjectBranchesParameters.ACTION_LIST;
 import static org.sonarqube.ws.client.projectbranches.ProjectBranchesParameters.PARAM_PROJECT;
 
@@ -79,16 +79,12 @@ public class ListAction implements BranchWsAction {
   public void define(WebService.NewController context) {
     WebService.NewAction action = context.createAction(ACTION_LIST)
       .setSince("6.6")
-      .setDescription("List the branches of a project")
+      .setDescription("List the branches of a project.<br/>" +
+        "Requires 'Administer' rights on the specified project.")
       .setResponseExample(Resources.getResource(getClass(), "list-example.json"))
-      .setInternal(true)
       .setHandler(this);
 
-    action
-      .createParam(PARAM_PROJECT)
-      .setDescription("Project key")
-      .setExampleValue(KEY_PROJECT_EXAMPLE_001)
-      .setRequired(true);
+    addProjectParam(action);
   }
 
   @Override
index 8ffa8abb5640bb015bc057055c8e1f6ecd7cb92e..3928c0aae001cbb2118d880df2cb7cfcaeb21ea2 100644 (file)
@@ -32,8 +32,10 @@ import org.sonar.db.component.ComponentDto;
 import org.sonar.server.component.ComponentFinder;
 import org.sonar.server.user.UserSession;
 
+import static com.google.common.base.Preconditions.checkArgument;
+import static org.sonar.server.projectbranch.ws.BranchesWs.addProjectParam;
 import static org.sonarqube.ws.client.projectbranches.ProjectBranchesParameters.ACTION_RENAME;
-import static org.sonarqube.ws.client.projectbranches.ProjectBranchesParameters.PARAM_BRANCH;
+import static org.sonarqube.ws.client.projectbranches.ProjectBranchesParameters.PARAM_NAME;
 import static org.sonarqube.ws.client.projectbranches.ProjectBranchesParameters.PARAM_PROJECT;
 
 public class RenameAction implements BranchWsAction {
@@ -51,31 +53,34 @@ public class RenameAction implements BranchWsAction {
   public void define(NewController context) {
     WebService.NewAction action = context.createAction(ACTION_RENAME)
       .setSince("6.6")
-      .setDescription("Rename the main branch of a project. <br />"
+      .setDescription("Rename the main branch of a project.<br/>"
         + "Requires 'Administer' permission on the specified project.")
-      .setInternal(true)
       .setPost(true)
       .setHandler(this);
 
-    BranchesWs.addProjectBranchParams(action);
+    addProjectParam(action);
+    action
+      .createParam(PARAM_NAME)
+      .setDescription("New name of the main branch")
+      .setExampleValue("branch1")
+      .setRequired(true);
   }
 
   @Override
   public void handle(Request request, Response response) throws Exception {
     userSession.checkLoggedIn();
     String projectKey = request.mandatoryParam(PARAM_PROJECT);
-    String branchKey = request.mandatoryParam(PARAM_BRANCH);
+    String newBranchName = request.mandatoryParam(PARAM_NAME);
 
     try (DbSession dbSession = dbClient.openSession(false)) {
       ComponentDto project = componentFinder.getRootComponentByUuidOrKey(dbSession, null, projectKey);
       checkPermission(project);
 
-      Optional<BranchDto> branch = dbClient.branchDao().selectByKey(dbSession, project.uuid(), branchKey);
-      if (branch.isPresent() && !branch.get().isMain()) {
-        throw new IllegalArgumentException("Impossible to update branch name: a branch with name \"" + branchKey + "\" already exists in the project.");
-      }
+      Optional<BranchDto> existingBranch = dbClient.branchDao().selectByKey(dbSession, project.uuid(), newBranchName);
+      checkArgument(!existingBranch.filter(b -> !b.isMain()).isPresent(),
+        "Impossible to update branch name: a branch with name \"%s\" already exists in the project.", newBranchName);
 
-      dbClient.branchDao().updateMainBranchName(dbSession, project.uuid(), branchKey);
+      dbClient.branchDao().updateMainBranchName(dbSession, project.uuid(), newBranchName);
       dbSession.commit();
       response.noContent();
     }
index b467725bb7a77c9a3a03bde8646e7d7c15b90511..9112fe35ffa165443a35d375cac3839547a2d678 100644 (file)
@@ -63,7 +63,7 @@ public class DeleteActionTest {
     WebService.Action definition = tester.getDef();
     assertThat(definition.key()).isEqualTo("delete");
     assertThat(definition.isPost()).isTrue();
-    assertThat(definition.isInternal()).isTrue();
+    assertThat(definition.isInternal()).isFalse();
     assertThat(definition.params()).extracting(WebService.Param::key).containsExactlyInAnyOrder("project", "branch");
     assertThat(definition.since()).isEqualTo("6.6");
   }
index 229936f100c4bc0128c286ccdacef58f2dd0abcc..81174c3c2908a9a1dc88c185f348cb62ae35578f 100644 (file)
@@ -102,7 +102,7 @@ public class ListActionTest {
     WebService.Action definition = ws.getDef();
     assertThat(definition.key()).isEqualTo("list");
     assertThat(definition.isPost()).isFalse();
-    assertThat(definition.isInternal()).isTrue();
+    assertThat(definition.isInternal()).isFalse();
     assertThat(definition.params()).extracting(WebService.Param::key).containsExactlyInAnyOrder("project");
     assertThat(definition.since()).isEqualTo("6.6");
   }
index bab33b6927ed5d46295a5c42a3a5aa83b6589ac1..e1dca4371db03b1d58d7b15824ffa2765f04f63c 100644 (file)
@@ -58,8 +58,8 @@ public class RenameActionTest {
     WebService.Action definition = tester.getDef();
     assertThat(definition.key()).isEqualTo("rename");
     assertThat(definition.isPost()).isTrue();
-    assertThat(definition.isInternal()).isTrue();
-    assertThat(definition.params()).extracting(WebService.Param::key).containsExactlyInAnyOrder("project", "branch");
+    assertThat(definition.isInternal()).isFalse();
+    assertThat(definition.params()).extracting(WebService.Param::key).containsExactlyInAnyOrder("project", "name");
     assertThat(definition.since()).isEqualTo("6.6");
   }
 
@@ -78,7 +78,7 @@ public class RenameActionTest {
     userSession.logIn();
 
     expectedException.expect(IllegalArgumentException.class);
-    expectedException.expectMessage("The 'branch' parameter is missing");
+    expectedException.expectMessage("The 'name' parameter is missing");
 
     tester.newRequest().setParam("project", "projectName").execute();
   }
@@ -101,12 +101,12 @@ public class RenameActionTest {
 
     tester.newRequest()
       .setParam("project", project.getKey())
-      .setParam("branch", "branch1")
+      .setParam("name", "branch1")
       .execute();
   }
 
   @Test
-  public void successfully_rename() {
+  public void rename() {
     userSession.logIn();
     ComponentDto project = db.components().insertMainBranch();
     ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey("branch"));
@@ -114,7 +114,7 @@ public class RenameActionTest {
 
     tester.newRequest()
       .setParam("project", project.getKey())
-      .setParam("branch", "master")
+      .setParam("name", "master")
       .execute();
 
     assertThat(db.countRowsOfTable("project_branches")).isEqualTo(2);
@@ -126,7 +126,7 @@ public class RenameActionTest {
   }
 
   @Test
-  public void successfully_rename_with_same_name() {
+  public void rename_with_same_name() {
     userSession.logIn();
     ComponentDto project = db.components().insertMainBranch();
     ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey("branch"));
@@ -134,12 +134,12 @@ public class RenameActionTest {
 
     tester.newRequest()
       .setParam("project", project.getKey())
-      .setParam("branch", "master")
+      .setParam("name", "master")
       .execute();
 
     tester.newRequest()
       .setParam("project", project.getKey())
-      .setParam("branch", "master")
+      .setParam("name", "master")
       .execute();
 
     assertThat(db.countRowsOfTable("project_branches")).isEqualTo(2);
@@ -162,7 +162,7 @@ public class RenameActionTest {
 
     tester.newRequest()
       .setParam("project", project.getKey())
-      .setParam("branch", "branch")
+      .setParam("name", "branch")
       .execute();
   }
 
@@ -175,7 +175,7 @@ public class RenameActionTest {
 
     tester.newRequest()
       .setParam("project", "foo")
-      .setParam("branch", "branch1")
+      .setParam("name", "branch1")
       .execute();
   }
 }
index ec3e79e093238e7a6014c7b4c48cd01e23c9bdca..fa0e7a12061977702d740db44e24f477b6ff02ef 100644 (file)
@@ -28,6 +28,6 @@ export function deleteBranch(project: string, branch: string): Promise<void | Re
   return post('/api/project_branches/delete', { project, branch }).catch(throwGlobalError);
 }
 
-export function renameBranch(project: string, branch: string): Promise<void | Response> {
-  return post('/api/project_branches/rename', { project, branch }).catch(throwGlobalError);
+export function renameBranch(project: string, name: string): Promise<void | Response> {
+  return post('/api/project_branches/rename', { project, name }).catch(throwGlobalError);
 }
index 32501503293c79344ff81e9bc0bffc6a3d41f3d6..1df51cf37d0da1afc8df58adeae25b4c85a97bb4 100644 (file)
@@ -33,6 +33,7 @@ public class ProjectBranchesParameters {
   public static final String PARAM_PROJECT = "project";
   public static final String PARAM_COMPONENT = "component";
   public static final String PARAM_BRANCH = "branch";
+  public static final String PARAM_NAME = "name";
 
   private ProjectBranchesParameters() {
     // static utility class
index 52d25fb461423e00d53447ab338db2795be6bcb8..75ad3bdaec791da9531fb410279ed9a34a45c45c 100644 (file)
@@ -26,12 +26,13 @@ import org.sonarqube.ws.client.GetRequest;
 import org.sonarqube.ws.client.PostRequest;
 import org.sonarqube.ws.client.WsConnector;
 
-import static org.sonarqube.ws.client.projectbranches.ProjectBranchesParameters.ACTION_LIST;
-import static org.sonarqube.ws.client.projectbranches.ProjectBranchesParameters.ACTION_SHOW;
 import static org.sonarqube.ws.client.projectbranches.ProjectBranchesParameters.ACTION_DELETE;
+import static org.sonarqube.ws.client.projectbranches.ProjectBranchesParameters.ACTION_LIST;
 import static org.sonarqube.ws.client.projectbranches.ProjectBranchesParameters.ACTION_RENAME;
+import static org.sonarqube.ws.client.projectbranches.ProjectBranchesParameters.ACTION_SHOW;
 import static org.sonarqube.ws.client.projectbranches.ProjectBranchesParameters.CONTROLLER;
 import static org.sonarqube.ws.client.projectbranches.ProjectBranchesParameters.PARAM_BRANCH;
+import static org.sonarqube.ws.client.projectbranches.ProjectBranchesParameters.PARAM_NAME;
 import static org.sonarqube.ws.client.projectbranches.ProjectBranchesParameters.PARAM_PROJECT;
 
 public class ProjectBranchesService extends BaseService {
@@ -60,10 +61,10 @@ public class ProjectBranchesService extends BaseService {
     call(post);
   }
 
-  public void rename(String project, String branch) {
+  public void rename(String project, String name) {
     PostRequest post = new PostRequest(path(ACTION_RENAME))
       .setParam(PARAM_PROJECT, project)
-      .setParam(PARAM_BRANCH, branch);
+      .setParam(PARAM_NAME, name);
     call(post);
   }
 
index a15962995116cdc4e02e638170f89192613a6755..eb8f57f344473c153261966c404a9fc59c260d8d 100644 (file)
@@ -32,6 +32,7 @@ import org.sonarqube.ws.client.WsConnector;
 import static org.assertj.core.api.Assertions.assertThat;
 import static org.mockito.Mockito.mock;
 import static org.sonarqube.ws.client.projectbranches.ProjectBranchesParameters.PARAM_BRANCH;
+import static org.sonarqube.ws.client.projectbranches.ProjectBranchesParameters.PARAM_NAME;
 import static org.sonarqube.ws.client.projectbranches.ProjectBranchesParameters.PARAM_PROJECT;
 
 public class ProjectBranchesServiceTest {
@@ -88,7 +89,7 @@ public class ProjectBranchesServiceTest {
     serviceTester.assertThat(postRequest)
       .hasPath("rename")
       .hasParam(PARAM_PROJECT, "projectKey")
-      .hasParam(PARAM_BRANCH, "my_branch")
+      .hasParam(PARAM_NAME, "my_branch")
       .andNoOtherParam();
   }