From 583e42c16797bf57162ebd502e12241d8765a8fc Mon Sep 17 00:00:00 2001 From: Janos Gyerik Date: Mon, 2 Oct 2017 10:26:08 +0200 Subject: [PATCH] SONAR-9875 Add branch= param to /api/navigation/component (#2555) * Add branch= param to /api/navigation/component * Use key instead of dbkey in issues/search --- .../server/issue/ws/SearchResponseFormat.java | 4 +- .../sonar/server/ui/ws/ComponentAction.java | 13 +++++- .../server/ui/ws/ComponentActionTest.java | 40 +++++++++++++++++++ ...rn_component_info_when_file_on_branch.json | 26 ++++++++++++ ...rn_component_info_when_file_on_master.json | 26 ++++++++++++ 5 files changed, 105 insertions(+), 4 deletions(-) create mode 100644 server/sonar-server/src/test/resources/org/sonar/server/ui/ws/ComponentActionTest/return_component_info_when_file_on_branch.json create mode 100644 server/sonar-server/src/test/resources/org/sonar/server/ui/ws/ComponentActionTest/return_component_info_when_file_on_master.json diff --git a/server/sonar-server/src/main/java/org/sonar/server/issue/ws/SearchResponseFormat.java b/server/sonar-server/src/main/java/org/sonar/server/issue/ws/SearchResponseFormat.java index 7825f08c4ee..3c9dc8e26da 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/issue/ws/SearchResponseFormat.java +++ b/server/sonar-server/src/main/java/org/sonar/server/issue/ws/SearchResponseFormat.java @@ -164,10 +164,10 @@ public class SearchResponseFormat { setNullable(component.getBranch(), issueBuilder::setBranch); ComponentDto project = data.getComponentByUuid(dto.getProjectUuid()); if (project != null) { - issueBuilder.setProject(project.getDbKey()); + issueBuilder.setProject(project.getKey()); ComponentDto subProject = data.getComponentByUuid(dto.getModuleUuid()); if (subProject != null && !subProject.getDbKey().equals(project.getDbKey())) { - issueBuilder.setSubProject(subProject.getDbKey()); + issueBuilder.setSubProject(subProject.getKey()); } } issueBuilder.setRule(dto.getRuleKey().toString()); diff --git a/server/sonar-server/src/main/java/org/sonar/server/ui/ws/ComponentAction.java b/server/sonar-server/src/main/java/org/sonar/server/ui/ws/ComponentAction.java index bf5b9654495..a55d2ebcfe8 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/ui/ws/ComponentAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/ui/ws/ComponentAction.java @@ -69,11 +69,13 @@ import static org.sonar.api.web.UserRole.USER; import static org.sonar.db.permission.OrganizationPermission.ADMINISTER_QUALITY_GATES; import static org.sonar.db.permission.OrganizationPermission.ADMINISTER_QUALITY_PROFILES; import static org.sonar.server.user.AbstractUserSession.insufficientPrivilegesException; +import static org.sonar.server.ws.KeyExamples.KEY_BRANCH_EXAMPLE_001; import static org.sonar.server.ws.KeyExamples.KEY_PROJECT_EXAMPLE_001; public class ComponentAction implements NavigationWsAction { static final String PARAM_COMPONENT = "component"; + static final String PARAM_BRANCH = "branch"; private static final String PROPERTY_CONFIGURABLE = "configurable"; private static final String PROPERTY_HAS_ROLE_POLICY = "hasRolePolicy"; @@ -119,13 +121,20 @@ public class ComponentAction implements NavigationWsAction { .setDescription("A component key.") .setDeprecatedKey("componentKey", "6.4") .setExampleValue(KEY_PROJECT_EXAMPLE_001); + + projectNavigation + .createParam(PARAM_BRANCH) + .setDescription("Branch key") + .setInternal(true) + .setExampleValue(KEY_BRANCH_EXAMPLE_001); } @Override public void handle(Request request, Response response) throws Exception { String componentKey = request.mandatoryParam(PARAM_COMPONENT); try (DbSession session = dbClient.openSession(false)) { - ComponentDto component = componentFinder.getByKey(session, componentKey); + String branch = request.param(PARAM_BRANCH); + ComponentDto component = componentFinder.getByKeyAndOptionalBranch(session, componentKey, branch); if (!userSession.hasComponentPermission(USER, component) && !userSession.hasComponentPermission(ADMIN, component) && !userSession.isSystemAdministrator()) { @@ -281,7 +290,7 @@ public class ComponentAction implements NavigationWsAction { for (ComponentDto c : breadcrumb) { json.beginObject() - .prop("key", c.getDbKey()) + .prop("key", c.getKey()) .prop("name", c.name()) .prop("qualifier", c.qualifier()) .endObject(); diff --git a/server/sonar-server/src/test/java/org/sonar/server/ui/ws/ComponentActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/ui/ws/ComponentActionTest.java index dac9421f6d7..536795e12b1 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/ui/ws/ComponentActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/ui/ws/ComponentActionTest.java @@ -218,6 +218,46 @@ public class ComponentActionTest { executeAndVerify(project.getDbKey(), "return_component_info_when_snapshot.json"); } + @Test + public void return_component_info_when_file_on_master() throws Exception { + init(); + OrganizationDto organization = dbTester.organizations().insertForKey("my-org2"); + ComponentDto main = componentDbTester.insertMainBranch(organization, p -> p.setName("Sample"), p -> p.setDbKey("sample")); + userSession.addProjectPermission(UserRole.USER, main); + + ComponentDto dirDto = componentDbTester.insertComponent(newDirectory(main, "src")); + + ComponentDto fileDto = componentDbTester.insertComponent(newFileDto(main, dirDto) + .setUuid("abcd") + .setName("Main.xoo") + .setDbKey("sample:src/Main.xoo")); + + executeAndVerify(fileDto.getDbKey(), "return_component_info_when_file_on_master.json"); + } + + @Test + public void return_component_info_when_file_on_branch() throws Exception { + init(); + OrganizationDto organization = dbTester.organizations().insertForKey("my-org2"); + ComponentDto project = componentDbTester.insertMainBranch(organization, p -> p.setName("Sample"), p -> p.setDbKey("sample")); + ComponentDto branch = componentDbTester.insertProjectBranch(project, b -> b.setKey("feature1")); + userSession.addProjectPermission(UserRole.USER, project); + + ComponentDto dirDto = componentDbTester.insertComponent(newDirectory(branch, "src")); + + ComponentDto fileDto = componentDbTester.insertComponent(newFileDto(branch, dirDto) + .setUuid("abcd") + .setName("Main.xoo") + .setDbKey("sample:src/Main.xoo")); + + String json = ws.newRequest() + .setParam("componentKey", fileDto.getDbKey()) + .setParam("branch", branch.getBranch()) + .execute() + .getInput(); + verify(json, "return_component_info_when_file_on_branch.json"); + } + @Test public void return_quality_profiles() throws Exception { init(); diff --git a/server/sonar-server/src/test/resources/org/sonar/server/ui/ws/ComponentActionTest/return_component_info_when_file_on_branch.json b/server/sonar-server/src/test/resources/org/sonar/server/ui/ws/ComponentActionTest/return_component_info_when_file_on_branch.json new file mode 100644 index 00000000000..6bbea627ada --- /dev/null +++ b/server/sonar-server/src/test/resources/org/sonar/server/ui/ws/ComponentActionTest/return_component_info_when_file_on_branch.json @@ -0,0 +1,26 @@ +{ + "key": "sample:src/Main.xoo", + "organization": "my-org2", + "id": "abcd", + "name": "Main.xoo", + "isFavorite": false, + "extensions": [], + "qualityProfiles": [], + "breadcrumbs": [ + { + "key": "sample", + "name": "Sample", + "qualifier": "TRK" + }, + { + "key": "sample:src", + "name": "src", + "qualifier": "DIR" + }, + { + "key": "sample:src/Main.xoo", + "name": "Main.xoo", + "qualifier": "FIL" + } + ] +} diff --git a/server/sonar-server/src/test/resources/org/sonar/server/ui/ws/ComponentActionTest/return_component_info_when_file_on_master.json b/server/sonar-server/src/test/resources/org/sonar/server/ui/ws/ComponentActionTest/return_component_info_when_file_on_master.json new file mode 100644 index 00000000000..6bbea627ada --- /dev/null +++ b/server/sonar-server/src/test/resources/org/sonar/server/ui/ws/ComponentActionTest/return_component_info_when_file_on_master.json @@ -0,0 +1,26 @@ +{ + "key": "sample:src/Main.xoo", + "organization": "my-org2", + "id": "abcd", + "name": "Main.xoo", + "isFavorite": false, + "extensions": [], + "qualityProfiles": [], + "breadcrumbs": [ + { + "key": "sample", + "name": "Sample", + "qualifier": "TRK" + }, + { + "key": "sample:src", + "name": "src", + "qualifier": "DIR" + }, + { + "key": "sample:src/Main.xoo", + "name": "Main.xoo", + "qualifier": "FIL" + } + ] +} -- 2.39.5