From 2fad0bfc3b20e8effb7e192d1eab210419149236 Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Wed, 18 Oct 2017 11:52:57 +0200 Subject: [PATCH] Set branches WS as public and fix description --- .../server/projectbranch/ws/BranchesWs.java | 10 +++++--- .../server/projectbranch/ws/DeleteAction.java | 9 ++++--- .../server/projectbranch/ws/ListAction.java | 12 +++------ .../server/projectbranch/ws/RenameAction.java | 25 +++++++++++-------- .../projectbranch/ws/DeleteActionTest.java | 2 +- .../projectbranch/ws/ListActionTest.java | 2 +- .../projectbranch/ws/RenameActionTest.java | 22 ++++++++-------- server/sonar-web/src/main/js/api/branches.ts | 4 +-- .../ProjectBranchesParameters.java | 1 + .../ProjectBranchesService.java | 9 ++++--- .../ProjectBranchesServiceTest.java | 3 ++- 11 files changed, 55 insertions(+), 44 deletions(-) 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 index 1492cef02dd..64657b98a15 100644 --- 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 @@ -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); } diff --git a/server/sonar-server/src/main/java/org/sonar/server/projectbranch/ws/DeleteAction.java b/server/sonar-server/src/main/java/org/sonar/server/projectbranch/ws/DeleteAction.java index c9a16b50e53..50c41916245 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/projectbranch/ws/DeleteAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/projectbranch/ws/DeleteAction.java @@ -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.
" + + "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 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 index 7e3486eb313..cc575a3868a 100644 --- 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 @@ -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.
" + + "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 diff --git a/server/sonar-server/src/main/java/org/sonar/server/projectbranch/ws/RenameAction.java b/server/sonar-server/src/main/java/org/sonar/server/projectbranch/ws/RenameAction.java index 8ffa8abb564..3928c0aae00 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/projectbranch/ws/RenameAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/projectbranch/ws/RenameAction.java @@ -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.
" + .setDescription("Rename the main branch of a project.
" + "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 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 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(); } diff --git a/server/sonar-server/src/test/java/org/sonar/server/projectbranch/ws/DeleteActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/projectbranch/ws/DeleteActionTest.java index b467725bb7a..9112fe35ffa 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/projectbranch/ws/DeleteActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/projectbranch/ws/DeleteActionTest.java @@ -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"); } 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 index 229936f100c..81174c3c290 100644 --- 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 @@ -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"); } diff --git a/server/sonar-server/src/test/java/org/sonar/server/projectbranch/ws/RenameActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/projectbranch/ws/RenameActionTest.java index bab33b6927e..e1dca4371db 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/projectbranch/ws/RenameActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/projectbranch/ws/RenameActionTest.java @@ -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(); } } diff --git a/server/sonar-web/src/main/js/api/branches.ts b/server/sonar-web/src/main/js/api/branches.ts index ec3e79e0932..fa0e7a12061 100644 --- a/server/sonar-web/src/main/js/api/branches.ts +++ b/server/sonar-web/src/main/js/api/branches.ts @@ -28,6 +28,6 @@ export function deleteBranch(project: string, branch: string): Promise { - return post('/api/project_branches/rename', { project, branch }).catch(throwGlobalError); +export function renameBranch(project: string, name: string): Promise { + return post('/api/project_branches/rename', { project, name }).catch(throwGlobalError); } diff --git a/sonar-ws/src/main/java/org/sonarqube/ws/client/projectbranches/ProjectBranchesParameters.java b/sonar-ws/src/main/java/org/sonarqube/ws/client/projectbranches/ProjectBranchesParameters.java index 32501503293..1df51cf37d0 100644 --- a/sonar-ws/src/main/java/org/sonarqube/ws/client/projectbranches/ProjectBranchesParameters.java +++ b/sonar-ws/src/main/java/org/sonarqube/ws/client/projectbranches/ProjectBranchesParameters.java @@ -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 diff --git a/sonar-ws/src/main/java/org/sonarqube/ws/client/projectbranches/ProjectBranchesService.java b/sonar-ws/src/main/java/org/sonarqube/ws/client/projectbranches/ProjectBranchesService.java index 52d25fb4614..75ad3bdaec7 100644 --- a/sonar-ws/src/main/java/org/sonarqube/ws/client/projectbranches/ProjectBranchesService.java +++ b/sonar-ws/src/main/java/org/sonarqube/ws/client/projectbranches/ProjectBranchesService.java @@ -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); } diff --git a/sonar-ws/src/test/java/org/sonarqube/ws/client/projectbranches/ProjectBranchesServiceTest.java b/sonar-ws/src/test/java/org/sonarqube/ws/client/projectbranches/ProjectBranchesServiceTest.java index a1596299511..eb8f57f3444 100644 --- a/sonar-ws/src/test/java/org/sonarqube/ws/client/projectbranches/ProjectBranchesServiceTest.java +++ b/sonar-ws/src/test/java/org/sonarqube/ws/client/projectbranches/ProjectBranchesServiceTest.java @@ -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(); } -- 2.39.5