]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-15259 Purge project from portfolios when removed
authorJacek <jacek.poreda@sonarsource.com>
Fri, 3 Sep 2021 12:56:15 +0000 (14:56 +0200)
committersonartech <sonartech@sonarsource.com>
Wed, 8 Sep 2021 20:03:35 +0000 (20:03 +0000)
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/purge/PurgeMapper.xml
server/sonar-db-dao/src/test/java/org/sonar/db/purge/PurgeCommandsTest.java
server/sonar-db-dao/src/test/java/org/sonar/db/purge/PurgeDaoTest.java

index 8eec8a70106e7e3181af37df9ad5eeb0d7c6294b..6f71d4584c4f25399e768bf10f855f272b6e2e60 100644 (file)
@@ -453,4 +453,10 @@ class PurgeCommands {
     profiler.stop();
   }
 
+  public void deleteProjectInPortfolios(String projectUuid) {
+    profiler.start("deleteProjectInPortfolios (portfolio_projects)");
+    purgeMapper.deletePortfolioProjectsByProjectUuid(projectUuid);
+    session.commit();
+    profiler.stop();
+  }
 }
index 46b3d523ac47b10ac6443c02a37b475a10638f6f..49dc462904ca86af1d6c7eefdb1c0e8d0c2105ac 100644 (file)
@@ -232,6 +232,7 @@ public class PurgeDao implements Dao {
     commands.deleteBranch(rootUuid);
     commands.deleteApplicationBranchProjects(rootUuid);
     commands.deleteApplicationProjects(rootUuid);
+    commands.deleteProjectInPortfolios(rootUuid);
     commands.deleteComponents(rootUuid);
     commands.deleteComponentsByMainBranchProjectUuid(rootUuid);
     commands.deleteProject(rootUuid);
index 4d7c72313f1cbbd3e71a878015d02d9ef9776b3f..5569cea219241d329882c7d6e46a93930f748d70 100644 (file)
@@ -147,6 +147,8 @@ public interface PurgeMapper {
 
   void deleteApplicationBranchProjectBranchesByProjectBranchUuid(@Param("projectBranchUuid") String projectBranchUuid);
 
+  void deletePortfolioProjectsByProjectUuid(@Param("projectUuid") String projectUuid);
+
   void deleteBranchByUuid(@Param("uuid") String uuid);
 
   void deleteLiveMeasuresByProjectUuid(@Param("projectUuid") String projectUuid);
@@ -158,4 +160,5 @@ public interface PurgeMapper {
   void deleteProjectAlmSettingsByProjectUuid(@Param("projectUuid") String projectUuid);
 
   void deleteUserDismissedMessagesByProjectUuid(@Param("projectUuid") String projectUuid);
+
 }
index 75c93120ff2094980756c2f44057c573bda84428..997914ba086996ff6a51209cabba3a30ae362436 100644 (file)
     where project_branch_uuid=#{projectBranchUuid,jdbcType=VARCHAR}
   </delete>
 
+  <delete id="deletePortfolioProjectsByProjectUuid" parameterType="map">
+    DELETE
+    FROM portfolio_projects
+    WHERE
+    project_uuid=#{projectUuid,jdbcType=VARCHAR}
+  </delete>
+
   <delete id="deleteIssueChangesFromIssueKeys" parameterType="map">
     DELETE FROM issue_changes
     WHERE issue_key IN
index e90f267625ff22b8b6c7ad9d8bd4ef0adb9bf9ab..794e3a90721928b9a81feb5356080ade929eb10a 100644 (file)
@@ -47,6 +47,7 @@ import org.sonar.db.issue.IssueDto;
 import org.sonar.db.metric.MetricDto;
 import org.sonar.db.newcodeperiod.NewCodePeriodType;
 import org.sonar.db.permission.GlobalPermission;
+import org.sonar.db.portfolio.PortfolioProjectDto;
 import org.sonar.db.project.ProjectDto;
 import org.sonar.db.rule.RuleDefinitionDto;
 import org.sonar.db.user.GroupDto;
@@ -57,6 +58,7 @@ import static java.util.Arrays.asList;
 import static java.util.Collections.singletonList;
 import static org.apache.commons.lang.RandomStringUtils.randomAlphabetic;
 import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.tuple;
 import static org.sonar.db.component.ComponentTesting.newBranchDto;
 import static org.sonar.db.component.ComponentTesting.newFileDto;
 import static org.sonar.db.component.ComponentTesting.newProjectCopy;
@@ -648,6 +650,27 @@ public class PurgeCommandsTest {
     assertThat(dbTester.countRowsOfTable("user_dismissed_messages")).isEqualTo(1);
   }
 
+  @Test
+  public void deleteProjectInPortfolios_deletes_project_from_portfolios() {
+    var portfolio1 = dbTester.components().insertPrivatePortfolio();
+    var portfolio2 = dbTester.components().insertPrivatePortfolio();
+    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());
+
+    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()));
+  }
+
   private void addPermissions(ComponentDto root) {
     if (!root.isPrivate()) {
       dbTester.users().insertProjectPermissionOnAnyone("foo1", root);
@@ -666,10 +689,6 @@ public class PurgeCommandsTest {
     assertThat(dbTester.countRowsOfTable("user_roles")).isEqualTo(2);
   }
 
-  private int countMeasures(SnapshotDto analysis, MetricDto metric) {
-    return dbTester.countSql("select count(*) from project_measures where analysis_uuid='" + analysis.getUuid() + "' and metric_id=" + metric.getUuid());
-  }
-
   private int countComponentOfRoot(ComponentDto projectOrView) {
     return dbTester.countSql("select count(1) from components where project_uuid='" + projectOrView.uuid() + "'");
   }
index 2e27954c64464ce8f4d4abda673a79892335934a..5809ea842ea3657be6b37a143485b7bc91d12eeb 100644 (file)
@@ -73,6 +73,7 @@ import org.sonar.db.measure.MeasureDto;
 import org.sonar.db.metric.MetricDto;
 import org.sonar.db.newcodeperiod.NewCodePeriodDto;
 import org.sonar.db.newcodeperiod.NewCodePeriodType;
+import org.sonar.db.portfolio.PortfolioProjectDto;
 import org.sonar.db.project.ProjectDto;
 import org.sonar.db.property.PropertyDto;
 import org.sonar.db.rule.RuleDefinitionDto;
@@ -89,7 +90,7 @@ import static java.util.Collections.emptySet;
 import static java.util.Collections.singletonList;
 import static org.apache.commons.lang.RandomStringUtils.randomAlphabetic;
 import static org.assertj.core.api.Assertions.assertThat;
-import static org.mockito.ArgumentMatchers.any;
+import static org.assertj.core.groups.Tuple.tuple;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.verifyZeroInteractions;
@@ -400,7 +401,7 @@ public class PurgeDaoTest {
 
   @Test
   public void selectPurgeableAnalyses() {
-    SnapshotDto[] analyses = new SnapshotDto[]{
+    SnapshotDto[] analyses = new SnapshotDto[] {
       newSnapshot()
         .setUuid("u1")
         .setComponentUuid(PROJECT_UUID)
@@ -1370,6 +1371,24 @@ public class PurgeDaoTest {
     assertThat(dbClient.projectAlmSettingDao().selectByProject(dbSession, otherProject)).isNotEmpty();
   }
 
+  @Test
+  public void deleteProject_deletes_portfolio_projects() {
+    ComponentDto portfolio1 = db.components().insertPrivatePortfolio();
+    ComponentDto portfolio2 = db.components().insertPrivatePortfolio();
+
+    ProjectDto project = db.components().insertPublicProjectDto();
+    ProjectDto otherProject = db.components().insertPublicProjectDto();
+
+    db.components().addPortfolioProject(portfolio1, project.getUuid(), otherProject.getUuid());
+    db.components().addPortfolioProject(portfolio2, project.getUuid());
+
+    underTest.deleteProject(dbSession, project.getUuid(), project.getQualifier(), project.getName(), project.getKey());
+
+    assertThat(dbClient.portfolioDao().selectAllPortfolioProjects(dbSession))
+      .extracting(PortfolioProjectDto::getPortfolioUuid, PortfolioProjectDto::getProjectUuid)
+      .containsExactlyInAnyOrder(tuple(portfolio1.uuid(), otherProject.getUuid()));
+  }
+
   @Test
   public void deleteNonRootComponents_has_no_effect_when_parameter_is_empty() {
     DbSession dbSession = mock(DbSession.class);
@@ -1643,18 +1662,18 @@ public class PurgeDaoTest {
 
   private void insertPropertyFor(ComponentDto... components) {
     Stream.of(components).forEach(componentDto -> db.properties().insertProperty(new PropertyDto()
-        .setKey(randomAlphabetic(3))
-        .setValue(randomAlphabetic(3))
-        .setComponentUuid(componentDto.uuid()),
+      .setKey(randomAlphabetic(3))
+      .setValue(randomAlphabetic(3))
+      .setComponentUuid(componentDto.uuid()),
       componentDto.getKey(), componentDto.name(), componentDto.qualifier(), null));
   }
 
   private void insertPropertyFor(Collection<BranchDto> branches) {
     branches.stream().forEach(branchDto -> db.properties().insertProperty(new PropertyDto()
-        .setKey(randomAlphabetic(3))
-        .setValue(randomAlphabetic(3))
-        .setComponentUuid(branchDto.getUuid()),
-      null, branchDto.getKey(), null,null));
+      .setKey(randomAlphabetic(3))
+      .setValue(randomAlphabetic(3))
+      .setComponentUuid(branchDto.getUuid()),
+      null, branchDto.getKey(), null, null));
   }
 
   private Stream<String> getComponentUuidsOfMeasures() {