]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-10782 delete stale pull requests at the end of an analysis (#299)
authorGuillaume Jambet <guillaume.jambet@gmail.com>
Tue, 5 Jun 2018 15:47:43 +0000 (17:47 +0200)
committerSonarTech <sonartech@sonarsource.com>
Tue, 5 Jun 2018 18:20:51 +0000 (20:20 +0200)
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/PurgeDaoTest.java

index 1270cdde24127e0c5c239e933d1bb503272ca798..0c8316a92e80519336a9b22f27c2bea05c50b4ef 100644 (file)
@@ -78,7 +78,7 @@ public class PurgeDao implements Dao {
     }
     LOG.debug("<- Purge stale branches");
 
-    List<String> branchUuids = mapper.selectStaleShortLivingBranches(rootUuid, dateToLong(maxDate.get()));
+    List<String> branchUuids = mapper.selectStaleShortLivingBranchesAndPullRequests(rootUuid, dateToLong(maxDate.get()));
 
     for (String branchUuid : branchUuids) {
       deleteRootComponent(branchUuid, mapper, commands);
index bbf0be4b2d25c2dba2c057b420712386983c1eea..510377605facb02566483e000e25a86f98ec8c5f 100644 (file)
@@ -80,7 +80,7 @@ public interface PurgeMapper {
 
   List<String> selectOldClosedIssueKeys(@Param("projectUuid") String projectUuid, @Nullable @Param("toDate") Long toDate);
 
-  List<String> selectStaleShortLivingBranches(@Param("mainBranchProjectUuid") String mainBranchProjectUuid, @Param("toDate") Long toDate);
+  List<String> selectStaleShortLivingBranchesAndPullRequests(@Param("mainBranchProjectUuid") String mainBranchProjectUuid, @Param("toDate") Long toDate);
 
   void deleteIssuesFromKeys(@Param("keys") List<String> keys);
 
index b1657baf79b0a21d7ab22de2be7b8c479dbe5fae..3216e49cf6459bd1202493ef9e7cb811e558cf2d 100644 (file)
       and not exists(select e.id from events e where e.analysis_uuid=s.uuid)
   </select>
 
-    <select id="selectStaleShortLivingBranches" parameterType="map" resultType="String">
+    <select id="selectStaleShortLivingBranchesAndPullRequests" parameterType="map" resultType="String">
     select
       pb.uuid
     from
       project_branches pb
     where
         pb.project_uuid=#{mainBranchProjectUuid,jdbcType=VARCHAR}
-        and pb.branch_type='SHORT'
+        and (pb.branch_type='SHORT' or pb.branch_type='PULL_REQUEST')
         and pb.updated_at &lt; #{toDate}
   </select>
 
index e40a4b59d284a9c2410d5095cfffbe4be9b9a218..75530fc48711103b66c7dc203f772c969de72e8f 100644 (file)
@@ -136,6 +136,32 @@ public class PurgeDaoTest {
     assertThat(getUuidsInTableProjects()).containsOnly(project.uuid(), longBranch.uuid(), recentShortBranch.uuid());
   }
 
+  @Test
+  public void purge_inactive_pull_request() {
+    when(system2.now()).thenReturn(new Date().getTime());
+    RuleDefinitionDto rule = dbTester.rules().insert();
+    ComponentDto project = dbTester.components().insertMainBranch();
+    ComponentDto longBranch = dbTester.components().insertProjectBranch(project);
+    ComponentDto recentPullRequest = dbTester.components().insertProjectBranch(project, b -> b.setBranchType(BranchType.PULL_REQUEST));
+
+    // pull request with other components and issues, updated 31 days ago
+    when(system2.now()).thenReturn(DateUtils.addDays(new Date(), -31).getTime());
+    ComponentDto pullRequest = dbTester.components().insertProjectBranch(project, b -> b.setBranchType(BranchType.PULL_REQUEST));
+    ComponentDto module = dbTester.components().insertComponent(newModuleDto(pullRequest));
+    ComponentDto subModule = dbTester.components().insertComponent(newModuleDto(module));
+    ComponentDto file = dbTester.components().insertComponent(newFileDto(subModule));
+    dbTester.issues().insert(rule, pullRequest, file);
+    dbTester.issues().insert(rule, pullRequest, subModule);
+    dbTester.issues().insert(rule, pullRequest, module);
+
+    // back to present
+    when(system2.now()).thenReturn(new Date().getTime());
+    underTest.purge(dbSession, newConfigurationWith30Days(system2, project.uuid()), PurgeListener.EMPTY, new PurgeProfiler());
+    dbSession.commit();
+
+    assertThat(getUuidsInTableProjects()).containsOnly(project.uuid(), longBranch.uuid(), recentPullRequest.uuid());
+  }
+
   @Test
   public void shouldDeleteHistoricalDataOfDirectoriesAndFiles() {
     dbTester.prepareDbUnit(getClass(), "shouldDeleteHistoricalDataOfDirectoriesAndFiles.xml");