]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-11031 make ProjectAlmBindingDto return ALM enum
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Thu, 2 Aug 2018 15:29:56 +0000 (17:29 +0200)
committerSonarTech <sonartech@sonarsource.com>
Fri, 10 Aug 2018 18:21:32 +0000 (20:21 +0200)
server/sonar-db-dao/src/main/java/org/sonar/db/alm/ALM.java
server/sonar-db-dao/src/main/java/org/sonar/db/alm/ProjectAlmBindingDto.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

index 0e5dd4ee2be56794f8462436d888566ff6e1f4fb..a76b362d0e3c6d291cee2e9271a46d23cc0eda1f 100644 (file)
@@ -25,7 +25,7 @@ public enum ALM {
   BITBUCKETCLOUD,
   GITHUB;
 
-  String getId() {
+  public String getId() {
     return this.name().toLowerCase(Locale.ENGLISH);
   }
 }
index db42d29e607854f6d100749a51cc86bdd24a8136..4de6b287353965a146c6825f0f07bc65e206db48 100644 (file)
  */
 package org.sonar.db.alm;
 
+import java.util.Arrays;
+import java.util.Optional;
+
+/**
+ * DTO is used only for select, hence no setters (MyBatis populates field by reflection).
+ */
 public class ProjectAlmBindingDto {
   private String uuid;
-  private String almId;
+  private String rawAlmId;
   private String repoId;
   private String projectUuid;
   private String githubSlug;
   private String url;
 
-  public String getAlmId() {
-    return almId;
-  }
+  public Optional<ALM> getAlm() {
+    if (rawAlmId == null) {
+      return Optional.empty();
+    }
 
-  public ProjectAlmBindingDto setAlmId(String almId) {
-    this.almId = almId;
-    return this;
+    return Arrays.stream(ALM.values())
+      .filter(a -> a.getId().equals(rawAlmId))
+      .findAny();
   }
 
   public String getRepoId() {
     return repoId;
   }
 
-  public ProjectAlmBindingDto setRepoId(String repoId) {
-    this.repoId = repoId;
-    return this;
-  }
-
   public String getProjectUuid() {
     return projectUuid;
   }
 
-  public ProjectAlmBindingDto setProjectUuid(String projectUuid) {
-    this.projectUuid = projectUuid;
-    return this;
-  }
-
   public String getGithubSlug() {
     return githubSlug;
   }
 
-  public ProjectAlmBindingDto setGithubSlug(String githubSlug) {
-    this.githubSlug = githubSlug;
-    return this;
-  }
-
   public String getUrl() {
     return url;
   }
 
-  public ProjectAlmBindingDto setUrl(String url) {
-    this.url = url;
-    return this;
-  }
-
   public String getUuid() {
     return uuid;
   }
 
-  public ProjectAlmBindingDto setUuid(String uuid) {
-    this.uuid = uuid;
-    return this;
-  }
 }
index 1cb6e67e3b5952ed9997e6e049a9f2dcbfc71feb..27f5d1f720a2663ceb36243293915517c2935c84 100644 (file)
@@ -3,6 +3,15 @@
 
 <mapper namespace="org.sonar.db.alm.ProjectAlmBindingsMapper">
 
+  <sql id="columns">
+    uuid,
+    alm_id as rawAlmId,
+    repo_id as repoId,
+    project_uuid as projectUuid,
+    github_slug as githubSlug,
+    url
+  </sql>
+
   <select id="bindingCount" parameterType="Map" resultType="int">
     select
       count(*) as count
@@ -14,7 +23,7 @@
   </select>
 
   <insert id="insert" parameterType="Map" useGeneratedKeys="false">
-    INSERT INTO project_alm_bindings
+    insert into project_alm_bindings
     (
       uuid,
       alm_id,
@@ -25,7 +34,7 @@
       created_at,
       updated_at
     )
-    VALUES (
+    values (
       #{uuid, jdbcType=VARCHAR},
       #{almId, jdbcType=VARCHAR},
       #{repoId, jdbcType=VARCHAR},
@@ -38,7 +47,8 @@
   </insert>
 
   <update id="update" parameterType="map">
-    update project_alm_bindings set
+    update project_alm_bindings
+    set
       project_uuid = #{projectUuid, jdbcType=VARCHAR},
       github_slug = #{githubSlug, jdbcType=VARCHAR},
       url = #{url, jdbcType=VARCHAR},
 
   <select id="selectByRepoIds" 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
+      <include refid="columns"/>
+    from
+      project_alm_bindings
     where
       alm_id = #{almId, jdbcType=VARCHAR}
       and repo_id in
 
   <select id="selectByRepoId" 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
+      <include refid="columns"/>
+    from
+      project_alm_bindings
     where
-    alm_id = #{almId, jdbcType=VARCHAR}
-    and repo_id = #{repoId, jdbcType=VARCHAR}
+      alm_id = #{almId, jdbcType=VARCHAR}
+      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
+      <include refid="columns"/>
+    from
+      project_alm_bindings
     where
-    project_uuid = #{projectUuid, jdbcType=VARCHAR}
+      project_uuid = #{projectUuid, jdbcType=VARCHAR}
   </select>
 
   <select id="selectProjectKey" parameterType="Map" resultType="String">
     select
       p.kee as projectKey
     from
-      project_alm_bindings b join projects p
+      project_alm_bindings b
+    inner join projects p
       on b.project_uuid = p.project_uuid
     where
       alm_id = #{almId, jdbcType=VARCHAR}
index f7705701a0875a833f702e2e7694de5f416871b5..0db93b0bac7dfdf7992e2a602d5895a6964b6e32 100644 (file)
@@ -193,7 +193,7 @@ public class ProjectAlmBindingsDaoTest {
     Optional<ProjectAlmBindingDto> dto = underTest.selectByRepoId(dbSession, GITHUB, A_REPO);
     assertThat(dto).isPresent();
     assertThat(dto.get().getUuid()).isEqualTo("uuid1");
-    assertThat(dto.get().getAlmId()).isEqualTo(GITHUB.getId());
+    assertThat(dto.get().getAlm()).contains(GITHUB);
     assertThat(dto.get().getRepoId()).isEqualTo(A_REPO);
     assertThat(dto.get().getProjectUuid()).isEqualTo(A_UUID);
     assertThat(dto.get().getUrl()).isEqualTo(A_URL);
@@ -207,16 +207,16 @@ public class ProjectAlmBindingsDaoTest {
       .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");
+    underTest.insertOrUpdate(dbSession, BITBUCKETCLOUD, A_REPO, A_UUID, A_GITHUB_SLUG, A_URL);
+    underTest.insertOrUpdate(dbSession, BITBUCKETCLOUD, ANOTHER_REPO, ANOTHER_UUID, null, ANOTHER_URL);
+    underTest.insertOrUpdate(dbSession, GITHUB, 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().getAlm()).contains(BITBUCKETCLOUD);
     assertThat(dto.get().getRepoId()).isEqualTo(A_REPO);
     assertThat(dto.get().getProjectUuid()).isEqualTo(A_UUID);
     assertThat(dto.get().getUrl()).isEqualTo(A_URL);
@@ -236,11 +236,11 @@ public class ProjectAlmBindingsDaoTest {
     underTest.insertOrUpdate(dbSession, BITBUCKETCLOUD, ANOTHER_REPO, "foo", null, "http://foo");
 
     assertThat(underTest.selectByRepoIds(dbSession, GITHUB, Arrays.asList(A_REPO, ANOTHER_REPO, "foo")))
-      .extracting(ProjectAlmBindingDto::getUuid, ProjectAlmBindingDto::getAlmId, ProjectAlmBindingDto::getRepoId, ProjectAlmBindingDto::getProjectUuid,
+      .extracting(ProjectAlmBindingDto::getUuid, t -> t.getAlm().get(), ProjectAlmBindingDto::getRepoId, ProjectAlmBindingDto::getProjectUuid,
         ProjectAlmBindingDto::getUrl, ProjectAlmBindingDto::getGithubSlug)
       .containsExactlyInAnyOrder(
-        tuple("uuid1", GITHUB.getId(), A_REPO, A_UUID, A_URL, A_GITHUB_SLUG),
-        tuple("uuid2", GITHUB.getId(), ANOTHER_REPO, ANOTHER_UUID, ANOTHER_URL, null));
+        tuple("uuid1", GITHUB, A_REPO, A_UUID, A_URL, A_GITHUB_SLUG),
+        tuple("uuid2", GITHUB, ANOTHER_REPO, ANOTHER_UUID, ANOTHER_URL, null));
   }
 
   @Test
index e67b014dc309ed1174d0046be1054461295aa563..32a79ea029a92fcc4f53bca3de34ee9995bf6706 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.ALM;
 import org.sonar.db.alm.ProjectAlmBindingDto;
 import org.sonar.db.component.ComponentDto;
 import org.sonar.db.component.SnapshotDto;
@@ -182,8 +183,14 @@ 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()));
+    bindingOpt.ifPresent(b -> {
+      String almId = b.getAlm()
+        .map(ALM::getId)
+        .map(String::valueOf)
+        .orElseThrow(() -> new IllegalStateException("Alm binding id DB has no ALM id"));
+      json.prop("almId", almId)
+        .prop("almRepoUrl", b.getUrl());
+    });
   }
 
   private static Consumer<QualityProfile> writeToJson(JsonWriter json) {