]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-11039 Fix api/navigation/component to return ALM infos on branches
authorJulien HENRY <julien.henry@sonarsource.com>
Mon, 6 Aug 2018 09:55:40 +0000 (11:55 +0200)
committerSonarTech <sonartech@sonarsource.com>
Fri, 10 Aug 2018 18:21:31 +0000 (20:21 +0200)
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_alm_infos_on_branch.json [new file with mode: 0644]
server/sonar-server/src/test/resources/org/sonar/server/ui/ws/ComponentActionTest/return_alm_infos_on_module.json [new file with mode: 0644]
server/sonar-server/src/test/resources/org/sonar/server/ui/ws/ComponentActionTest/return_alm_infos_on_project.json [new file with mode: 0644]
server/sonar-server/src/test/resources/org/sonar/server/ui/ws/ComponentActionTest/return_component_info_with_alm.json [deleted file]

index b526c287961d5aa28f0407c14b0986c2b6c46dfa..e67b014dc309ed1174d0046be1054461295aa563 100644 (file)
@@ -145,7 +145,9 @@ public class ComponentAction implements NavigationWsAction {
       String branch = request.param(PARAM_BRANCH);
       String pullRequest = request.param(PARAM_PULL_REQUEST);
       ComponentDto component = componentFinder.getByKeyAndOptionalBranchOrPullRequest(session, componentKey, branch, pullRequest);
-      ComponentDto project = component.getMainBranchProjectUuid() == null ? component : componentFinder.getByUuid(session, component.getMainBranchProjectUuid());
+      ComponentDto rootProjectOrBranch = getRootProjectOrBranch(component, session);
+      ComponentDto rootProject = rootProjectOrBranch.getMainBranchProjectUuid() == null ? rootProjectOrBranch
+        : componentFinder.getByUuid(session, rootProjectOrBranch.getMainBranchProjectUuid());
       if (!userSession.hasComponentPermission(USER, component) &&
         !userSession.hasComponentPermission(ADMIN, component) &&
         !userSession.isSystemAdministrator()) {
@@ -157,9 +159,9 @@ public class ComponentAction implements NavigationWsAction {
       JsonWriter json = response.newJsonWriter();
       json.beginObject();
       writeComponent(json, session, component, org, analysis.orElse(null));
-      writeAlmDetails(json, session, component);
+      writeAlmDetails(json, session, rootProject);
       writeProfiles(json, session, component);
-      writeQualityGate(json, session, org, project);
+      writeQualityGate(json, session, org, rootProject);
       if (userSession.hasComponentPermission(ADMIN, component) ||
         userSession.hasPermission(ADMINISTER_QUALITY_PROFILES, org) ||
         userSession.hasPermission(ADMINISTER_QUALITY_GATES, org)) {
@@ -170,6 +172,14 @@ public class ComponentAction implements NavigationWsAction {
     }
   }
 
+  private ComponentDto getRootProjectOrBranch(ComponentDto component, DbSession session) {
+    if (!component.isRootProject()) {
+      return dbClient.componentDao().selectOrFailByUuid(session, component.projectUuid());
+    } else {
+      return component;
+    }
+  }
+
   private void writeAlmDetails(JsonWriter json, DbSession session, ComponentDto component) {
     Optional<ProjectAlmBindingDto> bindingOpt = dbClient.projectAlmBindingsDao().selectByProjectUuid(session, component.uuid());
     bindingOpt.ifPresent(b -> json.prop("almId", b.getAlmId())
index 4465e76135b23b9611db3e3c38f908a2838870e7..6c190302f17a6714fc4897f01eeca9560ce415ad 100644 (file)
@@ -35,6 +35,7 @@ import org.sonar.api.web.page.Page;
 import org.sonar.api.web.page.Page.Qualifier;
 import org.sonar.api.web.page.PageDefinition;
 import org.sonar.core.component.DefaultResourceTypes;
+import org.sonar.core.extension.CoreExtensionRepository;
 import org.sonar.core.platform.PluginInfo;
 import org.sonar.core.platform.PluginRepository;
 import org.sonar.db.DbClient;
@@ -55,7 +56,6 @@ import org.sonar.db.user.UserDto;
 import org.sonar.server.component.ComponentFinder;
 import org.sonar.server.exceptions.ForbiddenException;
 import org.sonar.server.exceptions.NotFoundException;
-import org.sonar.core.extension.CoreExtensionRepository;
 import org.sonar.server.organization.BillingValidations;
 import org.sonar.server.organization.BillingValidationsProxy;
 import org.sonar.server.qualitygate.QualityGateFinder;
@@ -589,14 +589,42 @@ public class ComponentActionTest {
   }
 
   @Test
-  public void return_component_alm_info() {
+  public void return_alm_info_on_project() {
+    ComponentDto project = insertOrganizationAndProject();
+    dbClient.projectAlmBindingsDao().insertOrUpdate(db.getSession(), ALM.BITBUCKETCLOUD, "{123456789}", project.uuid(), null, "http://bitbucket.org/foo/bar");
+    db.getSession().commit();
+    userSession.addProjectPermission(UserRole.USER, project);
+    init();
+
+    executeAndVerify(project.getDbKey(), "return_alm_infos_on_project.json");
+  }
+
+  @Test
+  public void return_alm_info_on_module() {
     ComponentDto project = insertOrganizationAndProject();
+    ComponentDto module = componentDbTester.insertComponent(newModuleDto("bcde", project).setDbKey("palap").setName("Palap"));
     dbClient.projectAlmBindingsDao().insertOrUpdate(db.getSession(), ALM.BITBUCKETCLOUD, "{123456789}", project.uuid(), null, "http://bitbucket.org/foo/bar");
     db.getSession().commit();
     userSession.addProjectPermission(UserRole.USER, project);
     init();
 
-    executeAndVerify(project.getDbKey(), "return_component_info_with_alm.json");
+    executeAndVerify(module.getDbKey(), "return_alm_infos_on_module.json");
+  }
+
+  @Test
+  public void return_alm_info_on_branch() {
+    ComponentDto project = insertOrganizationAndProject();
+    ComponentDto branch = componentDbTester.insertProjectBranch(project, b -> b.setKey("feature1").setUuid("xyz"));
+    dbClient.projectAlmBindingsDao().insertOrUpdate(db.getSession(), ALM.BITBUCKETCLOUD, "{123456789}", project.uuid(), null, "http://bitbucket.org/foo/bar");
+    db.getSession().commit();
+    userSession.addProjectPermission(UserRole.USER, project);
+    init();
+
+    verify(ws.newRequest()
+      .setParam("componentKey", project.getDbKey())
+      .setParam("branch", branch.getBranch())
+      .execute()
+      .getInput(), "return_alm_infos_on_branch.json");
   }
 
   private ComponentDto insertOrganizationAndProject() {
diff --git a/server/sonar-server/src/test/resources/org/sonar/server/ui/ws/ComponentActionTest/return_alm_infos_on_branch.json b/server/sonar-server/src/test/resources/org/sonar/server/ui/ws/ComponentActionTest/return_alm_infos_on_branch.json
new file mode 100644 (file)
index 0000000..968b109
--- /dev/null
@@ -0,0 +1,10 @@
+{
+  "organization": "my-org",
+  "key": "polop",
+  "id": "xyz",
+  "branch": "feature1",
+  "name": "Polop",
+  "description": "test project",
+  "almId": "bitbucketcloud",
+  "almRepoUrl": "http://bitbucket.org/foo/bar"
+}
diff --git a/server/sonar-server/src/test/resources/org/sonar/server/ui/ws/ComponentActionTest/return_alm_infos_on_module.json b/server/sonar-server/src/test/resources/org/sonar/server/ui/ws/ComponentActionTest/return_alm_infos_on_module.json
new file mode 100644 (file)
index 0000000..0078c9f
--- /dev/null
@@ -0,0 +1,8 @@
+{
+  "organization": "my-org",
+  "key": "palap",
+  "id": "bcde",
+  "name": "Palap",
+  "almId": "bitbucketcloud",
+  "almRepoUrl": "http://bitbucket.org/foo/bar"
+}
diff --git a/server/sonar-server/src/test/resources/org/sonar/server/ui/ws/ComponentActionTest/return_alm_infos_on_project.json b/server/sonar-server/src/test/resources/org/sonar/server/ui/ws/ComponentActionTest/return_alm_infos_on_project.json
new file mode 100644 (file)
index 0000000..6ac0435
--- /dev/null
@@ -0,0 +1,9 @@
+{
+  "organization": "my-org",
+  "key": "polop",
+  "id": "abcd",
+  "name": "Polop",
+  "description": "test project",
+  "almId": "bitbucketcloud",
+  "almRepoUrl": "http://bitbucket.org/foo/bar"
+}
diff --git a/server/sonar-server/src/test/resources/org/sonar/server/ui/ws/ComponentActionTest/return_component_info_with_alm.json b/server/sonar-server/src/test/resources/org/sonar/server/ui/ws/ComponentActionTest/return_component_info_with_alm.json
deleted file mode 100644 (file)
index e28c5cf..0000000
+++ /dev/null
@@ -1,16 +0,0 @@
-{
-  "organization": "my-org",
-  "key": "polop",
-  "id": "abcd",
-  "name": "Polop",
-  "description": "test project",
-  "almId": "bitbucketcloud",
-  "almRepoUrl": "http://bitbucket.org/foo/bar",
-  "breadcrumbs": [
-    {
-      "key": "polop",
-      "name": "Polop",
-      "qualifier": "TRK"
-    }
-  ]
-}