aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGuillaume Jambet <guillaume.jambet@gmail.com>2018-06-07 15:17:48 +0200
committerSonarTech <sonartech@sonarsource.com>2018-06-07 20:20:47 +0200
commit4b95ba35055b4177816a4857fad6413ea12def73 (patch)
treeaacc4ce1568c7f01ef223c268c0a7c0361c18b2c
parentb9cd4425d5c2ab63672b8c18bb2812451a685beb (diff)
downloadsonarqube-4b95ba35055b4177816a4857fad6413ea12def73.tar.gz
sonarqube-4b95ba35055b4177816a4857fad6413ea12def73.zip
SONAR-10782 delete stale pull requests at the end of an analysis (#336)
-rw-r--r--server/sonar-db-dao/src/main/java/org/sonar/db/purge/PurgeDao.java2
-rw-r--r--server/sonar-db-dao/src/main/java/org/sonar/db/purge/PurgeMapper.java2
-rw-r--r--server/sonar-db-dao/src/main/resources/org/sonar/db/purge/PurgeMapper.xml4
-rw-r--r--server/sonar-db-dao/src/test/java/org/sonar/db/purge/PurgeDaoTest.java26
4 files changed, 30 insertions, 4 deletions
diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/purge/PurgeDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/purge/PurgeDao.java
index 1270cdde241..0c8316a92e8 100644
--- a/server/sonar-db-dao/src/main/java/org/sonar/db/purge/PurgeDao.java
+++ b/server/sonar-db-dao/src/main/java/org/sonar/db/purge/PurgeDao.java
@@ -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);
diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/purge/PurgeMapper.java b/server/sonar-db-dao/src/main/java/org/sonar/db/purge/PurgeMapper.java
index bbf0be4b2d2..510377605fa 100644
--- a/server/sonar-db-dao/src/main/java/org/sonar/db/purge/PurgeMapper.java
+++ b/server/sonar-db-dao/src/main/java/org/sonar/db/purge/PurgeMapper.java
@@ -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);
diff --git a/server/sonar-db-dao/src/main/resources/org/sonar/db/purge/PurgeMapper.xml b/server/sonar-db-dao/src/main/resources/org/sonar/db/purge/PurgeMapper.xml
index b1657baf79b..3216e49cf64 100644
--- a/server/sonar-db-dao/src/main/resources/org/sonar/db/purge/PurgeMapper.xml
+++ b/server/sonar-db-dao/src/main/resources/org/sonar/db/purge/PurgeMapper.xml
@@ -46,14 +46,14 @@
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>
diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/purge/PurgeDaoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/purge/PurgeDaoTest.java
index e40a4b59d28..75530fc4871 100644
--- a/server/sonar-db-dao/src/test/java/org/sonar/db/purge/PurgeDaoTest.java
+++ b/server/sonar-db-dao/src/test/java/org/sonar/db/purge/PurgeDaoTest.java
@@ -137,6 +137,32 @@ public class PurgeDaoTest {
}
@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");
PurgeConfiguration conf = new PurgeConfiguration(new IdUuidPair(THE_PROJECT_ID, "PROJECT_UUID"), asList(Scopes.DIRECTORY, Scopes.FILE),