]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-17477 Analyzer cache not purged when branch is deleted
authorDuarte Meneses <duarte.meneses@sonarsource.com>
Fri, 14 Oct 2022 16:21:47 +0000 (11:21 -0500)
committersonartech <sonartech@sonarsource.com>
Mon, 17 Oct 2022 20:03:02 +0000 (20:03 +0000)
server/sonar-db-dao/src/main/java/org/sonar/db/purge/PurgeDao.java
server/sonar-db-dao/src/test/java/org/sonar/db/purge/PurgeDaoTest.java

index fc50e9d72e50691758fb452e5419ca66209a1303..1b07bd18e8575ddfdd70b3bcaf40905ab80f5907 100644 (file)
@@ -222,6 +222,7 @@ public class PurgeDao implements Dao {
   private static void deleteRootComponent(String rootUuid, PurgeMapper mapper, PurgeCommands commands) {
     List<String> rootAndModulesOrSubviews = mapper.selectRootAndModulesOrSubviewsByProjectUuid(rootUuid);
     commands.deleteLinks(rootUuid);
+    commands.deleteScannerCache(rootUuid);
     commands.deleteAnalyses(rootUuid);
     commands.deleteByRootAndModulesOrSubviews(rootAndModulesOrSubviews);
     commands.deleteIssues(rootUuid);
index 00826d4dbb50aad88981fca9681538ef40a1c57c..2cbdf4302a7d2fd3151e7a084aaa26afab6f4221 100644 (file)
@@ -21,6 +21,7 @@ package org.sonar.db.purge;
 
 import com.google.common.collect.ImmutableSet;
 import java.io.ByteArrayInputStream;
+import java.io.IOException;
 import java.time.LocalDateTime;
 import java.time.ZoneOffset;
 import java.util.Arrays;
@@ -35,11 +36,13 @@ import java.util.function.Consumer;
 import java.util.stream.Collectors;
 import java.util.stream.IntStream;
 import java.util.stream.Stream;
+import org.apache.commons.io.IOUtils;
 import org.apache.commons.lang.math.RandomUtils;
 import org.apache.commons.lang.time.DateUtils;
 import org.junit.Rule;
 import org.junit.Test;
 import org.sonar.api.issue.Issue;
+import org.sonar.api.resources.Qualifiers;
 import org.sonar.api.utils.System2;
 import org.sonar.core.util.CloseableIterator;
 import org.sonar.core.util.UuidFactoryFast;
@@ -83,6 +86,7 @@ import org.sonar.db.webhook.WebhookDeliveryLiteDto;
 import org.sonar.db.webhook.WebhookDto;
 
 import static com.google.common.base.MoreObjects.firstNonNull;
+import static java.nio.charset.StandardCharsets.UTF_8;
 import static java.time.ZoneOffset.UTC;
 import static java.util.Arrays.asList;
 import static java.util.Collections.emptySet;
@@ -1383,6 +1387,34 @@ public class PurgeDaoTest {
     assertThat(dbClient.projectAlmSettingDao().selectByProject(dbSession, otherProject)).isNotEmpty();
   }
 
+  @Test
+  public void deleteBranch_deletes_scanner_cache() throws IOException {
+    ProjectDto project = db.components().insertPublicProjectDto();
+    BranchDto branch = db.components().insertProjectBranch(project);
+    dbClient.scannerAnalysisCacheDao().insert(dbSession, branch.getUuid(), IOUtils.toInputStream("test1", UTF_8));
+    dbClient.scannerAnalysisCacheDao().insert(dbSession, project.getUuid(), IOUtils.toInputStream("test2", UTF_8));
+
+    underTest.deleteBranch(dbSession, branch.getUuid());
+
+    assertThat(dbClient.scannerAnalysisCacheDao().selectData(dbSession, branch.getUuid())).isNull();
+    DbInputStream mainBranchCache = dbClient.scannerAnalysisCacheDao().selectData(dbSession, project.getUuid());
+
+    assertThat(IOUtils.toString(mainBranchCache, UTF_8)).isEqualTo("test2");
+  }
+
+  @Test
+  public void deleteProject_deletes_scanner_cache() throws IOException {
+    ProjectDto project = db.components().insertPublicProjectDto();
+    BranchDto branch = db.components().insertProjectBranch(project);
+    dbClient.scannerAnalysisCacheDao().insert(dbSession, branch.getUuid(), IOUtils.toInputStream("test1", UTF_8));
+    dbClient.scannerAnalysisCacheDao().insert(dbSession, project.getUuid(), IOUtils.toInputStream("test2", UTF_8));
+
+    underTest.deleteProject(dbSession,project.getUuid(), Qualifiers.PROJECT, "project", "project");
+
+    assertThat(dbClient.scannerAnalysisCacheDao().selectData(dbSession, project.getUuid())).isNull();
+    assertThat(dbClient.scannerAnalysisCacheDao().selectData(dbSession, branch.getUuid())).isNull();
+  }
+
   @Test
   public void deleteProject_deletes_project_badge_tokens() {
     ProjectDto project = db.components().insertPublicProjectDto();