]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-11039 Return ALM link in api/navigation/component
authorJulien HENRY <julien.henry@sonarsource.com>
Mon, 30 Jul 2018 16:04:30 +0000 (18:04 +0200)
committerSonarTech <sonartech@sonarsource.com>
Fri, 10 Aug 2018 18:21:29 +0000 (20:21 +0200)
server/sonar-db-dao/src/main/java/org/sonar/db/alm/ProjectAlmBindingsDao.java
server/sonar-db-dao/src/main/java/org/sonar/db/alm/ProjectAlmBindingsMapper.java
server/sonar-db-dao/src/main/resources/org/sonar/db/alm/ProjectAlmBindingsMapper.xml
server/sonar-db-dao/src/test/java/org/sonar/db/alm/ProjectAlmBindingsDaoTest.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_with_alm.json [new file with mode: 0644]

index e4a77591ab0d81d91817d084870fa5bc3c347685..51d5789ae89529dfc3438acc1b7eeee292113444 100644 (file)
@@ -64,6 +64,10 @@ public class ProjectAlmBindingsDao implements Dao {
     return getMapper(dbSession).bindingCount(alm.getId(), repoId) == 1;
   }
 
+  public Optional<ProjectAlmBindingDto> selectByProjectUuid(DbSession session, String projectUuid) {
+    return Optional.ofNullable(getMapper(session).selectByProjectUuid(projectUuid));
+  }
+
   /**
    * Gets a list of bindings by their repo_id. The result does NOT contain {@code null} values for bindings not found, so
    * the size of result may be less than the number of ids.
@@ -88,4 +92,5 @@ public class ProjectAlmBindingsDao implements Dao {
   private static ProjectAlmBindingsMapper getMapper(DbSession dbSession) {
     return dbSession.getMapper(ProjectAlmBindingsMapper.class);
   }
+
 }
index b8c5464a254f6ecaeb040579b8e0773df60bf294..41eec6968e4e955cd0ad2230a6824bc5739bf6b6 100644 (file)
@@ -36,4 +36,6 @@ public interface ProjectAlmBindingsMapper {
   List<ProjectAlmBindingDto> selectByRepoIds(@Param("almId") String almId, @Param("repoIds") List<String> repoIds);
 
   ProjectAlmBindingDto selectByRepoId(@Param("almId") String almId, @Param("repoId") String repoId);
+
+  ProjectAlmBindingDto selectByProjectUuid(@Param("projectUuid") String projectUuid);
 }
index 9b6b65d2ae2ac0639d70685b853049db0991fa45..317e4038790c9181c0499bb1b3ff2cfba6d8875d 100644 (file)
     and repo_id = #{repoId, jdbcType=VARCHAR}
   </select>
 
+  <select id="selectByProjectUuid" parameterType="map" resultType="ProjectAlmBinding">
+    select
+    uuid,
+    alm_id as almId,
+    repo_id as repoId,
+    project_uuid as projectUuid,
+    github_slug as githubSlug,
+    url
+    from project_alm_bindings
+    where
+    project_uuid = #{projectUuid, jdbcType=VARCHAR}
+  </select>
+
 </mapper>
index 287582125c013ed46efd0cfe0c541b581ae1e36f..b434588cdabe98fca0e68c0047cd17e31b48e96d 100644 (file)
@@ -231,6 +231,29 @@ public class ProjectAlmBindingsDaoTest {
     assertThat(dto.get().getGithubSlug()).isEqualTo(A_GITHUB_SLUG);
   }
 
+  @Test
+  public void select_by_project_uuid() {
+    when(system2.now()).thenReturn(DATE);
+    when(uuidFactory.create())
+      .thenReturn("uuid1")
+      .thenReturn("uuid2")
+      .thenReturn("uuid3");
+    underTest.insertOrUpdate(dbSession, GITHUB, A_REPO, A_UUID, A_GITHUB_SLUG, A_URL);
+    underTest.insertOrUpdate(dbSession, GITHUB, ANOTHER_REPO, ANOTHER_UUID, null, ANOTHER_URL);
+    underTest.insertOrUpdate(dbSession, BITBUCKETCLOUD, ANOTHER_REPO, "foo", null, "http://foo");
+
+    assertThat(underTest.selectByProjectUuid(dbSession, "missing")).isNotPresent();
+
+    Optional<ProjectAlmBindingDto> dto = underTest.selectByProjectUuid(dbSession, A_UUID);
+    assertThat(dto).isPresent();
+    assertThat(dto.get().getUuid()).isEqualTo("uuid1");
+    assertThat(dto.get().getAlmId()).isEqualTo(GITHUB.getId());
+    assertThat(dto.get().getRepoId()).isEqualTo(A_REPO);
+    assertThat(dto.get().getProjectUuid()).isEqualTo(A_UUID);
+    assertThat(dto.get().getUrl()).isEqualTo(A_URL);
+    assertThat(dto.get().getGithubSlug()).isEqualTo(A_GITHUB_SLUG);
+  }
+
   @Test
   public void select_by_repo_ids() {
     when(system2.now()).thenReturn(DATE);
index c8964af2459282d129d61e0beed5d16574278346..b526c287961d5aa28f0407c14b0986c2b6c46dfa 100644 (file)
@@ -41,6 +41,7 @@ import org.sonar.api.web.UserRole;
 import org.sonar.api.web.page.Page;
 import org.sonar.db.DbClient;
 import org.sonar.db.DbSession;
+import org.sonar.db.alm.ProjectAlmBindingDto;
 import org.sonar.db.component.ComponentDto;
 import org.sonar.db.component.SnapshotDto;
 import org.sonar.db.measure.LiveMeasureDto;
@@ -116,7 +117,8 @@ public class ComponentAction implements NavigationWsAction {
       .setResponseExample(getClass().getResource("component-example.json"))
       .setSince("5.2")
       .setChangelog(
-        new Change("6.4", "The 'visibility' field is added"));
+        new Change("6.4", "The 'visibility' field is added"),
+        new Change("7.3", "The 'almRepoUrl' and 'almId' fields are added"));
 
     action.createParam(PARAM_COMPONENT)
       .setDescription("A component key.")
@@ -155,6 +157,7 @@ public class ComponentAction implements NavigationWsAction {
       JsonWriter json = response.newJsonWriter();
       json.beginObject();
       writeComponent(json, session, component, org, analysis.orElse(null));
+      writeAlmDetails(json, session, component);
       writeProfiles(json, session, component);
       writeQualityGate(json, session, org, project);
       if (userSession.hasComponentPermission(ADMIN, component) ||
@@ -167,6 +170,12 @@ public class ComponentAction implements NavigationWsAction {
     }
   }
 
+  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())
+      .prop("almRepoUrl", b.getUrl()));
+  }
+
   private static Consumer<QualityProfile> writeToJson(JsonWriter json) {
     return profile -> json.beginObject()
       .prop("key", profile.getQpKey())
index c214196f65c7691a20dfe1087bea2dc24ec590ed..4465e76135b23b9611db3e3c38f908a2838870e7 100644 (file)
@@ -39,6 +39,7 @@ import org.sonar.core.platform.PluginInfo;
 import org.sonar.core.platform.PluginRepository;
 import org.sonar.db.DbClient;
 import org.sonar.db.DbTester;
+import org.sonar.db.alm.ALM;
 import org.sonar.db.component.BranchType;
 import org.sonar.db.component.ComponentDbTester;
 import org.sonar.db.component.ComponentDto;
@@ -576,7 +577,8 @@ public class ComponentActionTest {
     assertThat(action.description()).isNotNull();
     assertThat(action.responseExample()).isNotNull();
     assertThat(action.changelog()).extracting(Change::getVersion, Change::getDescription).containsExactlyInAnyOrder(
-      tuple("6.4", "The 'visibility' field is added"));
+      tuple("6.4", "The 'visibility' field is added"),
+      tuple("7.3", "The 'almRepoUrl' and 'almId' fields are added"));
 
     WebService.Param componentId = action.param(PARAM_COMPONENT);
     assertThat(componentId.isRequired()).isFalse();
@@ -586,6 +588,17 @@ public class ComponentActionTest {
     assertThat(componentId.deprecatedKeySince()).isEqualTo("6.4");
   }
 
+  @Test
+  public void return_component_alm_info() {
+    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_component_info_with_alm.json");
+  }
+
   private ComponentDto insertOrganizationAndProject() {
     OrganizationDto organization = db.organizations().insert(o -> o.setKey("my-org"));
     db.qualityGates().createDefaultQualityGate(organization);
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
new file mode 100644 (file)
index 0000000..e28c5cf
--- /dev/null
@@ -0,0 +1,16 @@
+{
+  "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"
+    }
+  ]
+}