]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-15726 Inform previously selected projects in applications within the portfolio...
authorZipeng WU <zipeng.wu@sonarsource.com>
Thu, 2 Dec 2021 16:59:56 +0000 (17:59 +0100)
committersonartech <sonartech@sonarsource.com>
Sun, 5 Dec 2021 20:03:19 +0000 (20:03 +0000)
server/sonar-db-dao/src/main/java/org/sonar/db/MyBatis.java
server/sonar-db-dao/src/main/java/org/sonar/db/portfolio/PortfolioDao.java
server/sonar-db-dao/src/main/java/org/sonar/db/portfolio/PortfolioMapper.java
server/sonar-db-dao/src/main/java/org/sonar/db/project/ApplicationProjectDto.java [new file with mode: 0644]
server/sonar-db-dao/src/main/resources/org/sonar/db/portfolio/PortfolioMapper.xml
server/sonar-db-dao/src/test/java/org/sonar/db/portfolio/PortfolioDaoTest.java

index 4db01625cceb98e665ae38e443449a9605335eb1..bb512f589f6982b39d72197e3cccbbd7330c1048 100644 (file)
@@ -100,6 +100,7 @@ import org.sonar.db.portfolio.PortfolioDto;
 import org.sonar.db.portfolio.PortfolioMapper;
 import org.sonar.db.portfolio.PortfolioProjectDto;
 import org.sonar.db.portfolio.PortfolioReferenceDto;
+import org.sonar.db.project.ApplicationProjectDto;
 import org.sonar.db.project.ProjectBadgeTokenDto;
 import org.sonar.db.project.ProjectBadgeTokenMapper;
 import org.sonar.db.project.ProjectDto;
@@ -179,6 +180,7 @@ public class MyBatis implements Startable {
     // DTO aliases, keep them sorted alphabetically
     confBuilder.loadAlias("ActiveRule", ActiveRuleDto.class);
     confBuilder.loadAlias("ActiveRuleParam", ActiveRuleParamDto.class);
+    confBuilder.loadAlias("ApplicationProject", ApplicationProjectDto.class);
     confBuilder.loadAlias("CeTaskCharacteristic", CeTaskCharacteristicDto.class);
     confBuilder.loadAlias("Component", ComponentDto.class);
     confBuilder.loadAlias("ComponentWithModuleUuid", ComponentWithModuleUuidDto.class);
index 0b8209472f8b540fc878345cfab0127e23d95b31..d69f36e8c1a8fd603d8e7869a8242cc131974d98 100644 (file)
@@ -30,6 +30,7 @@ import org.sonar.db.Dao;
 import org.sonar.db.DbSession;
 import org.sonar.db.audit.AuditPersister;
 import org.sonar.db.audit.model.ComponentNewValue;
+import org.sonar.db.project.ApplicationProjectDto;
 
 import static com.google.common.base.Preconditions.checkArgument;
 import static java.lang.String.format;
@@ -55,6 +56,17 @@ public class PortfolioDao implements Dao {
     return mapper(dbSession).selectAllRoots();
   }
 
+  /**
+   * select all application projects belong to the hierarchy of a portfolio
+   *
+   * @param dbSession
+   * @param rootPortfolioUuid
+   * @return
+   */
+  public List<ApplicationProjectDto> selectAllApplicationProjects(DbSession dbSession, String rootPortfolioUuid) {
+    return mapper(dbSession).selectAllApplicationProjects(rootPortfolioUuid);
+  }
+
   public List<PortfolioDto> selectAll(DbSession dbSession) {
     return mapper(dbSession).selectAll();
   }
index 0554a04213d4d79cd9a3b392d9ad3ac4f3bbd003..fe77eabae7a5ff7996e3fe768ebe8b7adbee6bad 100644 (file)
@@ -24,6 +24,7 @@ import java.util.List;
 import java.util.Set;
 import javax.annotation.CheckForNull;
 import org.apache.ibatis.annotations.Param;
+import org.sonar.db.project.ApplicationProjectDto;
 
 public interface PortfolioMapper {
   @CheckForNull
@@ -66,6 +67,8 @@ public interface PortfolioMapper {
 
   List<PortfolioDto> selectAllRoots();
 
+  List<ApplicationProjectDto> selectAllApplicationProjects(String rootPortfolioUuid);
+
   List<PortfolioDto> selectAll();
 
   List<PortfolioDto> selectRootOfReferencers(String referenceUuid);
diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/project/ApplicationProjectDto.java b/server/sonar-db-dao/src/main/java/org/sonar/db/project/ApplicationProjectDto.java
new file mode 100644 (file)
index 0000000..069fc47
--- /dev/null
@@ -0,0 +1,43 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2021 SonarSource SA
+ * mailto:info AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+package org.sonar.db.project;
+
+public class ApplicationProjectDto {
+  private String appUuid;
+  private String appKey;
+  private String projectUuid;
+
+  public ApplicationProjectDto() {
+    // Do Nothing
+  }
+
+  public String getAppUuid() {
+    return appUuid;
+  }
+
+  public String getAppKey() {
+    return appKey;
+  }
+
+  public String getProjectUuid() {
+    return projectUuid;
+  }
+
+}
index 08789406dd4dce319ac01323074a81760843067d..e894c7afecd329eae04b3dc8d2edcf9d144a8c88 100644 (file)
     pp.project_uuid as projectUuid,
     pp.portfolio_uuid as portfolioUuid,
     pf.kee as portfolioKey,
-    pp.project_uuid as projectUuid,
     p.kee as projectKey,
     pp.created_at as createdAt,
     ppb.branch_uuid as branchUuid
   </sql>
 
   <resultMap id="portfolioProjectResult" type="PortfolioProject" autoMapping="true">
-    <id property="uuid" column="portfolioProject_uuid" />
+    <id property="uuid" column="portfolioProject_uuid"/>
     <collection property="branchUuids" ofType="string">
-      <result column="branchUuid" />
+      <result column="branchUuid"/>
     </collection>
   </resultMap>
 
 
   <select id="selectReferenceUuids" resultType="String">
     SELECT
-      p.reference_uuid
+    p.reference_uuid
     FROM portfolio_references p
     where
-      p.portfolio_uuid=#{portfolioUuid,jdbcType=VARCHAR}
+    p.portfolio_uuid=#{portfolioUuid,jdbcType=VARCHAR}
   </select>
 
   <select id="selectRootOfReferencers" resultType="Portfolio">
-    SELECT distinct <include refid="portfolioColumns"/>
+    SELECT distinct
+    <include refid="portfolioColumns"/>
     FROM portfolios c
     INNER JOIN portfolio_references pr on pr.portfolio_uuid = c.uuid
     INNER JOIN portfolios p on p.uuid = c.root_uuid
     where
-      pr.reference_uuid=#{referenceUuid,jdbcType=VARCHAR}
+    pr.reference_uuid=#{referenceUuid,jdbcType=VARCHAR}
+  </select>
+
+  <sql id="applicationProjectColumns">
+    ap.application_uuid as appUuid,
+    ap.project_uuid as projectUuid,
+    p.kee as appKey
+  </sql>
+
+  <select id="selectAllApplicationProjects" parameterType="String" resultType="ApplicationProject">
+    SELECT
+    <include refid="applicationProjectColumns"/>
+    FROM portfolio_references pr
+    INNER JOIN projects p ON p.uuid = pr.reference_uuid
+    INNER JOIN portfolios pf ON pr.portfolio_uuid = pf.uuid
+    INNER JOIN app_projects ap ON ap.application_uuid = p.uuid
+    WHERE pf.root_uuid = #{rootPortfolioUuid}
   </select>
 
   <select id="selectTree" parameterType="String" resultType="Portfolio">
-    SELECT <include refid="portfolioColumns"/>
+    SELECT
+    <include refid="portfolioColumns"/>
     FROM portfolios p
     INNER JOIN portfolios p2 ON p.root_uuid = p2.root_uuid
     WHERE p2.uuid=#{portfolioUuid,jdbcType=VARCHAR}
   </select>
 
- <select id="selectReferencers" resultType="Portfolio">
 <select id="selectReferencers" resultType="Portfolio">
     SELECT
-      <include refid="portfolioColumns"/>
+    <include refid="portfolioColumns"/>
     FROM portfolios p
     INNER JOIN portfolio_references pr ON p.uuid = pr.portfolio_uuid
     WHERE pr.reference_uuid=#{referenceUuid,jdbcType=VARCHAR}
   <delete id="deleteReference">
     DELETE FROM portfolio_references
     WHERE
-      portfolio_uuid = #{portfolioUuid,jdbcType=VARCHAR}
+    portfolio_uuid = #{portfolioUuid,jdbcType=VARCHAR}
     AND reference_uuid = #{referenceUuid,jdbcType=VARCHAR}
   </delete>
 
index b4ca9c42ee725617a8428994bba2f11f22abecdb..74d282a5b04ba9a85e6c30e6b900bf245b7f03c1 100644 (file)
@@ -28,6 +28,7 @@ import org.sonar.core.util.UuidFactoryFast;
 import org.sonar.db.DbSession;
 import org.sonar.db.DbTester;
 import org.sonar.db.audit.AuditPersister;
+import org.sonar.db.project.ApplicationProjectDto;
 import org.sonar.db.project.ProjectDto;
 
 import static java.util.Collections.emptySet;
@@ -248,6 +249,26 @@ public class PortfolioDaoTest {
       .containsOnly(tuple("portfolio3", app1.getUuid()));
   }
 
+  @Test
+  public void selectAllApplicationProjectsBelongToTheSamePortfolio() {
+    var portfolio = db.components().insertPrivatePortfolioDto("portfolio1");
+    var app1 = db.components().insertPrivateApplicationDto(p -> p.setDbKey("app1"));
+    var app2 = db.components().insertPrivateApplicationDto(p -> p.setDbKey("app2"));
+    var project1 = db.components().insertPrivateProjectDto(p -> p.setDbKey("project:one").setName("Projet Un"));
+    var project2 = db.components().insertPrivateProjectDto(p -> p.setDbKey("project:two").setName("Projet Deux"));
+
+    db.components().addApplicationProject(app1, project1);
+    db.components().addApplicationProject(app2, project2);
+    db.components().addPortfolioReference(portfolio, app1.getUuid());
+    db.components().addPortfolioReference(portfolio, app2.getUuid());
+
+    assertThat(portfolioDao.selectAllApplicationProjects(session, portfolio.getRootUuid()))
+      .extracting(ApplicationProjectDto::getAppUuid, ApplicationProjectDto::getAppKey, ApplicationProjectDto::getProjectUuid)
+      .containsOnly(
+        tuple(app1.getUuid(), "app1", project1.getUuid()),
+        tuple(app2.getUuid(), "app2", project2.getUuid()));
+  }
+
   @Test
   public void add_and_select_references_by_uuid() {
     assertThat(portfolioDao.selectPortfolioProjects(session, "portfolio1")).isEmpty();