]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-9875 Add branch= param to /api/navigation/component (#2555)
authorJanos Gyerik <janos.gyerik@sonarsource.com>
Mon, 2 Oct 2017 08:26:08 +0000 (10:26 +0200)
committerGitHub <noreply@github.com>
Mon, 2 Oct 2017 08:26:08 +0000 (10:26 +0200)
* Add branch= param to /api/navigation/component

* Use key instead of dbkey in issues/search

server/sonar-server/src/main/java/org/sonar/server/issue/ws/SearchResponseFormat.java
server/sonar-server/src/main/java/org/sonar/server/ui/ws/ComponentAction.java
server/sonar-server/src/test/java/org/sonar/server/ui/ws/ComponentActionTest.java
server/sonar-server/src/test/resources/org/sonar/server/ui/ws/ComponentActionTest/return_component_info_when_file_on_branch.json [new file with mode: 0644]
server/sonar-server/src/test/resources/org/sonar/server/ui/ws/ComponentActionTest/return_component_info_when_file_on_master.json [new file with mode: 0644]

index 7825f08c4ee9ec9629691e9b857e22e33a83481b..3c9dc8e26da970327c992ed4de26180d097b5761 100644 (file)
@@ -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());
index bf5b9654495f8ae9b0129814911cdf0aaf8f80a4..a55d2ebcfe89ebe952f0d2bfcb7098c6838d3195 100644 (file)
@@ -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();
index dac9421f6d7b3319db44655c1cdc549ddf761edc..536795e12b1273365fb54b1831ab34cd01a95e93 100644 (file)
@@ -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 (file)
index 0000000..6bbea62
--- /dev/null
@@ -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 (file)
index 0000000..6bbea62
--- /dev/null
@@ -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"
+    }
+  ]
+}