]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-15499 Show portfolio branch selection in views/show WS and clean selected branc...
authorDuarte Meneses <duarte.meneses@sonarsource.com>
Fri, 29 Oct 2021 16:12:24 +0000 (11:12 -0500)
committersonartech <sonartech@sonarsource.com>
Tue, 9 Nov 2021 20:03:16 +0000 (20:03 +0000)
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/purge/PurgeCommands.java
server/sonar-db-dao/src/main/java/org/sonar/db/purge/PurgeDao.java
server/sonar-db-dao/src/main/java/org/sonar/db/purge/PurgeMapper.java
server/sonar-db-dao/src/main/resources/org/sonar/db/portfolio/PortfolioMapper.xml
server/sonar-db-dao/src/main/resources/org/sonar/db/purge/PurgeMapper.xml
server/sonar-db-dao/src/test/java/org/sonar/db/portfolio/PortfolioDaoTest.java
server/sonar-db-dao/src/test/java/org/sonar/db/purge/PurgeCommandsTest.java

index 6c008b9186aef2893866aff56555414cfd751f88..c7e2fc968bffd3c1ff823cde2732ed405ce65994 100644 (file)
@@ -91,12 +91,14 @@ public class PortfolioDao implements Dao {
   }
 
   public void delete(DbSession dbSession, PortfolioDto portfolio) {
-    mapper(dbSession).deletePortfoliosByUuids(singleton(portfolio.getUuid()));
     mapper(dbSession).deleteReferencesByPortfolioOrReferenceUuids(singleton(portfolio.getUuid()));
-    mapper(dbSession).deleteProjectsByPortfolioUuids(singleton(portfolio.getUuid()));
+    mapper(dbSession).deletePortfolio(portfolio.getUuid());
     auditPersister.deleteComponent(dbSession, toComponentNewValue(portfolio));
   }
 
+  /**
+   * Does NOT delete related references and project/branch selections!
+   */
   public void deleteAllDescendantPortfolios(DbSession dbSession, String rootUuid) {
     // not audited but it's part of DefineWs
     mapper(dbSession).deleteAllDescendantPortfolios(rootUuid);
@@ -198,10 +200,6 @@ public class PortfolioDao implements Dao {
     mapper(dbSession).deleteAllProjects();
   }
 
-  public Set<String> selectBranches(DbSession dbSession, String portfolioProjectUuid) {
-    return mapper(dbSession).selectBranches(portfolioProjectUuid);
-  }
-
   public void addBranch(DbSession dbSession, String portfolioProjectUuid, String branchUuid) {
     mapper(dbSession).insertBranch(uuidFactory.create(), portfolioProjectUuid, branchUuid, system2.now());
   }
index 0e848ccb50a2b1c370b287f7a4decc973e9c0af3..6abdcca9acb07478786d1053a014421d68df0459 100644 (file)
@@ -36,14 +36,10 @@ public interface PortfolioMapper {
 
   void insert(PortfolioDto portfolio);
 
-  void deleteByUuids(@Param("uuids") Set<String> uuids);
-
-  void deletePortfoliosByUuids(@Param("uuids") Set<String> uuids);
+  void deletePortfolio(@Param("uuid") String uuid);
 
   void deleteReferencesByPortfolioOrReferenceUuids(@Param("uuids") Set<String> uuids);
 
-  void deleteProjectsByPortfolioUuids(@Param("uuids") Set<String> uuids);
-
   void insertReference(PortfolioReferenceDto portfolioReference);
 
   void insertProject(@Param("uuid") String uuid, @Param("portfolioUuid") String portfolioUuid, @Param("projectUuid") String projectUuid, @Param("createdAt") long createdAt);
@@ -94,12 +90,8 @@ public interface PortfolioMapper {
 
   List<ReferenceDto> selectAllReferencesInHierarchy(String rootUuid);
 
-  void deleteBranch(@Param("portfolioProjectUuid") String portfolioProjectUuid, @Param("branchUuid") String branchUuid);
-
   void deleteBranch(@Param("portfolioUuid") String portfolioUuid, @Param("projectUuid") String projectUuid, @Param("branchUuid") String branchUuid);
 
   void insertBranch(@Param("uuid") String uuid, @Param("portfolioProjectUuid") String portfolioProjectUuid, @Param("branchUuid") String branchUuid,
     @Param("createdAt") long createdAt);
-
-  Set<String> selectBranches(String portfolioProjectUuid);
 }
index 6f71d4584c4f25399e768bf10f855f272b6e2e60..feba85ea9e39ec37542458532fc924d9ec6bf29a 100644 (file)
@@ -453,9 +453,10 @@ class PurgeCommands {
     profiler.stop();
   }
 
-  public void deleteProjectInPortfolios(String projectUuid) {
+  public void deleteProjectInPortfolios(String rootUuid) {
     profiler.start("deleteProjectInPortfolios (portfolio_projects)");
-    purgeMapper.deletePortfolioProjectsByProjectUuid(projectUuid);
+    purgeMapper.deletePortfolioProjectBranchesByBranchUuid(rootUuid);
+    purgeMapper.deletePortfolioProjectsByProjectUuid(rootUuid);
     session.commit();
     profiler.stop();
   }
index 49dc462904ca86af1d6c7eefdb1c0e8d0c2105ac..11a0ee7e67cc87a3c078c27c1760e6baf707687d 100644 (file)
@@ -78,7 +78,7 @@ public class PurgeDao implements Dao {
 
   private static void purgeStaleBranches(PurgeCommands commands, PurgeConfiguration conf, PurgeMapper mapper, String rootUuid) {
     Optional<Date> maxDate = conf.maxLiveDateOfInactiveBranches();
-    if (!maxDate.isPresent()) {
+    if (maxDate.isEmpty()) {
       // not available if branch plugin is not installed
       return;
     }
index 5569cea219241d329882c7d6e46a93930f748d70..5270cec0721dc124f535785f22d32906b51299e4 100644 (file)
@@ -149,6 +149,8 @@ public interface PurgeMapper {
 
   void deletePortfolioProjectsByProjectUuid(@Param("projectUuid") String projectUuid);
 
+  void deletePortfolioProjectBranchesByBranchUuid(@Param("branchUuid") String branchUuid);
+
   void deleteBranchByUuid(@Param("uuid") String uuid);
 
   void deleteLiveMeasuresByProjectUuid(@Param("projectUuid") String projectUuid);
index d25f5a4b6a83afe85a947ebefcb108617b5b92ce..85db3d297fa6580b00d37cbe66319097c7d683e3 100644 (file)
     )
   </insert>
 
-  <delete id="delete" parameterType="String">
-    DELETE FROM portfolios WHERE uuid = #{portfolioUuid,jdbcType=VARCHAR};
-    DELETE FROM portfolio_references WHERE portfolio_uuid = #{portfolioUuid,jdbcType=VARCHAR};
-    DELETE FROM portfolio_projects WHERE portfolio_uuid = #{portfolioUuid,jdbcType=VARCHAR};
-  </delete>
-
   <select id="selectAllReferencesToPortfolios" resultType="org.sonar.db.portfolio.ReferenceDto">
     SELECT
       source.uuid as sourceUuid,
     AND source.root_uuid = #{rootUuid,jdbcType=VARCHAR})
   </select>
 
-  <delete id="deletePortfoliosByUuids" parameterType="String">
-    DELETE FROM portfolios WHERE uuid in
-    <foreach collection="uuids" open="(" close=")" item="uuid" separator=",">#{uuid,jdbcType=VARCHAR}</foreach>
+  <delete id="deletePortfolio" parameterType="String">
+    DELETE FROM portfolio_proj_branches
+    WHERE portfolio_project_uuid in (select uuid FROM portfolio_projects WHERE portfolio_uuid = #{uuid,jdbcType=VARCHAR});
+
+    DELETE FROM portfolio_projects
+    WHERE portfolio_uuid = #{uuid,jdbcType=VARCHAR};
+
+    DELETE FROM portfolios WHERE uuid = #{uuid,jdbcType=VARCHAR};
   </delete>
 
   <delete id="deleteReferencesByPortfolioOrReferenceUuids" parameterType="String">
     <foreach collection="uuids" open="(" close=")" item="uuid" separator=",">#{uuid,jdbcType=VARCHAR}</foreach>
   </delete>
 
-  <delete id="deleteProjectsByPortfolioUuids" parameterType="String">
-    delete from portfolio_proj_branches
-    where portfolio_project_uuid in
-      (select uuid from portfolio_projects
-      where portfolio_uuid in
-    <foreach collection="uuids" open="(" close=")" item="uuid" separator=",">#{uuid,jdbcType=VARCHAR}</foreach>);
-
-    DELETE FROM portfolio_projects WHERE portfolio_uuid in
-    <foreach collection="uuids" open="(" close=")" item="uuid" separator=",">#{uuid,jdbcType=VARCHAR}</foreach>
-  </delete>
-
   <delete id="deleteAllDescendantPortfolios" parameterType="String">
     DELETE FROM portfolios WHERE root_uuid = #{rootUuid,jdbcType=VARCHAR} and parent_uuid is not null
   </delete>
       <include refid="portfolioColumns"/>
     FROM portfolios p
     INNER JOIN portfolio_references pr ON p.uuid = pr.portfolio_uuid
-    where pr.reference_uuid =  #{referenceUuid,jdbcType=VARCHAR}
+    WHERE pr.reference_uuid =  #{referenceUuid,jdbcType=VARCHAR}
   </select>
 
   <delete id="deleteReferencesTo" parameterType="String">
-    delete from portfolio_references
-    where reference_uuid =  #{referenceUuid,jdbcType=VARCHAR}
+    DELETE FROM portfolio_references
+    WHERE reference_uuid =  #{referenceUuid,jdbcType=VARCHAR}
   </delete>
 
   <delete id="deleteProjects" parameterType="String">
-    delete from portfolio_proj_branches
-    where portfolio_project_uuid =
-      (select uuid from portfolio_projects
-      where portfolio_uuid = #{portfolioUuid,jdbcType=VARCHAR});
+    DELETE FROM portfolio_proj_branches
+    WHERE portfolio_project_uuid in
+      (SELECT uuid FROM portfolio_projects
+      WHERE portfolio_uuid = #{portfolioUuid,jdbcType=VARCHAR});
 
-    delete from portfolio_projects
-    where portfolio_uuid = #{portfolioUuid,jdbcType=VARCHAR}
+    DELETE FROM portfolio_projects
+    WHERE portfolio_uuid = #{portfolioUuid,jdbcType=VARCHAR}
   </delete>
 
   <delete id="deleteProject" parameterType="map">
-    delete from portfolio_proj_branches
-    where portfolio_project_uuid =
-      (select uuid from portfolio_projects
-      where portfolio_uuid = #{portfolioUuid,jdbcType=VARCHAR}
-      and project_uuid = #{projectUuid,jdbcType=VARCHAR});
-
-    delete from portfolio_projects
-    where portfolio_uuid = #{portfolioUuid,jdbcType=VARCHAR}
-      and project_uuid = #{projectUuid,jdbcType=VARCHAR}
+    DELETE FROM portfolio_proj_branches
+    WHERE portfolio_project_uuid =
+      (SELECT uuid FROM portfolio_projects
+      WHERE portfolio_uuid = #{portfolioUuid,jdbcType=VARCHAR}
+      AND project_uuid = #{projectUuid,jdbcType=VARCHAR});
+
+    DELETE FROM portfolio_projects
+    WHERE portfolio_uuid = #{portfolioUuid,jdbcType=VARCHAR}
+      AND project_uuid = #{projectUuid,jdbcType=VARCHAR}
   </delete>
 
   <insert id="insertProject" parameterType="map">
   </insert>
 
   <delete id="deleteBranch" parameterType="map">
-    delete from portfolio_proj_branches
-    where portfolio_project_uuid =
-      (select uuid from portfolio_projects
-      where portfolio_uuid = #{portfolioUuid,jdbcType=VARCHAR}
-      and project_uuid = #{projectUuid,jdbcType=VARCHAR})
-      and branch_uuid = #{branchUuid,jdbcType=VARCHAR}
+    DELETE FROM portfolio_proj_branches
+    WHERE portfolio_project_uuid =
+      (SELECT uuid FROM portfolio_projects
+      WHERE portfolio_uuid = #{portfolioUuid,jdbcType=VARCHAR}
+      AND project_uuid = #{projectUuid,jdbcType=VARCHAR})
+      AND branch_uuid = #{branchUuid,jdbcType=VARCHAR}
   </delete>
 
   <update id="update" parameterType="Portfolio">
-    update portfolios set
+    UPDATE portfolios SET
     name = #{name,jdbcType=VARCHAR},
     description = #{description,jdbcType=VARCHAR},
     selection_mode = #{selectionMode,jdbcType=VARCHAR},
     branch_key = #{branchKey,jdbcType=VARCHAR},
     root_uuid = #{rootUuid,jdbcType=VARCHAR},
     updated_at = #{updatedAt,jdbcType=BIGINT}
-    where
+    WHERE
     uuid = #{uuid,jdbcType=VARCHAR}
   </update>
 </mapper>
index 997914ba086996ff6a51209cabba3a30ae362436..32267058e04b1c7cb387bbd77780eccac42d1c56 100644 (file)
   <delete id="deleteApplicationProjectsByApplicationUuid" parameterType="map">
     DELETE
     FROM app_projects
-    where
+    WHERE
       application_uuid=#{applicationUuid,jdbcType=VARCHAR}
   </delete>
 
   <delete id="deleteApplicationBranchProjectBranchesByApplicationUuid" parameterType="map">
     DELETE
     FROM app_branch_project_branch
-    where
+    WHERE
       application_uuid=#{applicationUuid,jdbcType=VARCHAR}
   </delete>
 
   <delete id="deleteApplicationBranchProjects" parameterType="String">
     DELETE
     FROM app_branch_project_branch
-    where
+    WHERE
       application_branch_uuid=#{branchUuid,jdbcType=VARCHAR}
   </delete>
 
   <delete id="deleteApplicationBranchProjectBranchesByProjectBranchUuid" parameterType="String">
     DELETE
     FROM app_branch_project_branch
-    where project_branch_uuid=#{projectBranchUuid,jdbcType=VARCHAR}
+    WHERE project_branch_uuid=#{projectBranchUuid,jdbcType=VARCHAR}
   </delete>
 
   <delete id="deletePortfolioProjectsByProjectUuid" parameterType="map">
     DELETE
     FROM portfolio_projects
-    WHERE
-    project_uuid=#{projectUuid,jdbcType=VARCHAR}
+    WHERE project_uuid=#{projectUuid,jdbcType=VARCHAR}
+  </delete>
+
+  <delete id="deletePortfolioProjectBranchesByBranchUuid" parameterType="map">
+    DELETE FROM portfolio_proj_branches
+    WHERE portfolio_project_uuid IN (SELECT uuid FROM portfolio_projects WHERE project_uuid = #{branchUuid,jdbcType=VARCHAR})
+    OR branch_uuid = #{branchUuid,jdbcType=VARCHAR}
   </delete>
 
   <delete id="deleteIssueChangesFromIssueKeys" parameterType="map">
index c8c086f392be26b9c4f247de7fc2f3125b6b8581..3ded65b6e54fe7d93a3b2b36940850f75b82100a 100644 (file)
@@ -398,7 +398,7 @@ public class PortfolioDaoTest {
   }
 
   @Test
-  public void delete_projects() {
+  public void deleteProjects_deletes_selected_projects_and_branches() {
     db.components().insertPrivatePortfolioDto("portfolio1");
     db.components().insertPrivatePortfolioDto("portfolio2");
 
@@ -406,14 +406,40 @@ public class PortfolioDaoTest {
     db.components().insertPrivateProjectDto("project2");
 
     assertThat(portfolioDao.selectPortfolioProjects(session, "portfolio1")).isEmpty();
-    portfolioDao.addProject(session, "portfolio1", "project1");
+    String uuid = portfolioDao.addProject(session, "portfolio1", "project1");
+    portfolioDao.addBranch(session, uuid, "project1Branch");
     portfolioDao.addProject(session, "portfolio1", "project2");
     portfolioDao.addProject(session, "portfolio2", "project2");
+
+    session.commit();
     assertThat(portfolioDao.selectPortfolioProjects(session, "portfolio1")).isNotEmpty();
+    assertThat(db.countRowsOfTable("portfolio_proj_branches")).isOne();
 
     portfolioDao.deleteProjects(session, "portfolio1");
+    session.commit();
     assertThat(portfolioDao.selectPortfolioProjects(session, "portfolio1")).isEmpty();
     assertThat(portfolioDao.selectPortfolioProjects(session, "portfolio2")).extracting(PortfolioProjectDto::getProjectUuid).containsOnly("project2");
+    assertThat(db.countRowsOfTable("portfolio_proj_branches")).isZero();
+  }
+
+  @Test
+  public void deleteProject_deletes_selected_branches() {
+    db.components().insertPrivatePortfolioDto("portfolio1");
+
+    db.components().insertPrivateProjectDto("project1");
+    db.components().insertPrivateProjectDto("project2");
+
+    String uuid1 = portfolioDao.addProject(session, "portfolio1", "project1");
+    portfolioDao.addBranch(session, uuid1, "project1Branch");
+    String uuid2 = portfolioDao.addProject(session, "portfolio1", "project2");
+    portfolioDao.addBranch(session, uuid2, "project2Branch");
+    session.commit();
+    assertThat(db.countRowsOfTable("portfolio_proj_branches")).isEqualTo(2);
+
+    portfolioDao.deleteProject(session, "portfolio1", "project2");
+    assertThat(portfolioDao.selectPortfolioProjects(session, "portfolio1"))
+      .extracting(PortfolioProjectDto::getProjectUuid, PortfolioProjectDto::getBranchUuids)
+      .containsOnly(tuple("project1", Set.of("project1Branch")));
   }
 
   @Test
@@ -442,25 +468,6 @@ public class PortfolioDaoTest {
       .doesNotThrowAnyException();
   }
 
-  @Test
-  public void delete_project() {
-    db.components().insertPrivatePortfolioDto("portfolio1");
-    db.components().insertPrivatePortfolioDto("portfolio2");
-
-    db.components().insertPrivateProjectDto("project1");
-    db.components().insertPrivateProjectDto("project2");
-
-    assertThat(portfolioDao.selectPortfolioProjects(session, "portfolio1")).isEmpty();
-    portfolioDao.addProject(session, "portfolio1", "project1");
-    portfolioDao.addProject(session, "portfolio1", "project2");
-    portfolioDao.addProject(session, "portfolio2", "project2");
-    assertThat(portfolioDao.selectPortfolioProjects(session, "portfolio1")).isNotEmpty();
-
-    portfolioDao.deleteProject(session, "portfolio1", "project2");
-    assertThat(portfolioDao.selectPortfolioProjects(session, "portfolio1")).extracting(PortfolioProjectDto::getProjectUuid).containsOnly("project1");
-    assertThat(portfolioDao.selectPortfolioProjects(session, "portfolio2")).extracting(PortfolioProjectDto::getProjectUuid).containsOnly("project2");
-  }
-
   @Test
   public void selectAllProjectsInHierarchy() {
     ProjectDto p1 = db.components().insertPrivateProjectDto("p1");
index 794e3a90721928b9a81feb5356080ade929eb10a..33c0475e5e5820bfeaa3774b4770d758907cd2f9 100644 (file)
@@ -25,6 +25,7 @@ import com.tngtech.java.junit.dataprovider.UseDataProvider;
 import java.util.Arrays;
 import java.util.List;
 import java.util.Random;
+import java.util.Set;
 import java.util.function.Consumer;
 import java.util.stream.IntStream;
 import java.util.stream.Stream;
@@ -55,6 +56,8 @@ import org.sonar.db.user.UserDto;
 
 import static com.google.common.collect.Lists.newArrayList;
 import static java.util.Arrays.asList;
+import static java.util.Collections.emptyList;
+import static java.util.Collections.singleton;
 import static java.util.Collections.singletonList;
 import static org.apache.commons.lang.RandomStringUtils.randomAlphabetic;
 import static org.assertj.core.api.Assertions.assertThat;
@@ -651,24 +654,44 @@ public class PurgeCommandsTest {
   }
 
   @Test
-  public void deleteProjectInPortfolios_deletes_project_from_portfolios() {
-    var portfolio1 = dbTester.components().insertPrivatePortfolio();
-    var portfolio2 = dbTester.components().insertPrivatePortfolio();
+  public void deleteProjectInPortfolios_deletes_project_and_branch_from_portfolios() {
+    var portfolio1 = dbTester.components().insertPrivatePortfolioDto();
+    var portfolio2 = dbTester.components().insertPrivatePortfolioDto();
     dbTester.components().insertPrivatePortfolio();
-
     ProjectDto project = dbTester.components().insertPrivateProjectDto();
     ProjectDto anotherProject = dbTester.components().insertPrivateProjectDto();
 
-    dbTester.components().addPortfolioProject(portfolio1, project.getUuid(), anotherProject.getUuid());
-    dbTester.components().addPortfolioProject(portfolio2, project.getUuid());
+    dbTester.components().addPortfolioProject(portfolio1, project, anotherProject);
+    dbTester.components().addPortfolioProjectBranch(portfolio1, project, "projectBranch");
+    dbTester.components().addPortfolioProjectBranch(portfolio1, anotherProject, "anotherProjectBranch");
+
+    dbTester.components().addPortfolioProject(portfolio2, project);
 
     PurgeCommands purgeCommands = new PurgeCommands(dbTester.getSession(), profiler, system2);
 
     purgeCommands.deleteProjectInPortfolios(project.getUuid());
 
     assertThat(dbTester.getDbClient().portfolioDao().selectAllPortfolioProjects(dbTester.getSession()))
-      .extracting(PortfolioProjectDto::getPortfolioUuid, PortfolioProjectDto::getProjectUuid)
-      .containsExactlyInAnyOrder(tuple(portfolio1.uuid(), anotherProject.getUuid()));
+      .extracting(PortfolioProjectDto::getPortfolioUuid, PortfolioProjectDto::getProjectUuid, PortfolioProjectDto::getBranchUuids)
+      .containsExactlyInAnyOrder(tuple(portfolio1.getUuid(), anotherProject.getUuid(), singleton("anotherProjectBranch")));
+  }
+
+  @Test
+  public void deleteProjectInPortfolios_deletes_branch_from_portfolios_if_root_is_branch() {
+    var portfolio1 = dbTester.components().insertPrivatePortfolioDto();
+    ProjectDto project = dbTester.components().insertPrivateProjectDto();
+
+    dbTester.components().addPortfolioProject(portfolio1, project);
+    dbTester.components().addPortfolioProjectBranch(portfolio1, project, "projectBranch");
+    dbTester.components().addPortfolioProjectBranch(portfolio1, project, "anotherBranch");
+
+    PurgeCommands purgeCommands = new PurgeCommands(dbTester.getSession(), profiler, system2);
+
+    purgeCommands.deleteProjectInPortfolios("projectBranch");
+
+    assertThat(dbTester.getDbClient().portfolioDao().selectAllPortfolioProjects(dbTester.getSession()))
+      .extracting(PortfolioProjectDto::getPortfolioUuid, PortfolioProjectDto::getProjectUuid, PortfolioProjectDto::getBranchUuids)
+      .containsExactlyInAnyOrder(tuple(portfolio1.getUuid(), project.getUuid(), Set.of("anotherBranch")));
   }
 
   private void addPermissions(ComponentDto root) {