]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-8306 don't fail inserting scanner context if task is restarted
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Thu, 20 Oct 2016 13:08:12 +0000 (15:08 +0200)
committerSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Fri, 21 Oct 2016 14:47:41 +0000 (16:47 +0200)
server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/step/PersistScannerContextStep.java
server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/step/PersistScannerContextStepTest.java

index 3d9e98232211e4cffc1b6fcc0b51a7afc7c28cf2..9be3a55f28826303c7c98bb449cc7203a6a261c3 100644 (file)
@@ -26,6 +26,8 @@ import org.sonar.db.DbSession;
 import org.sonar.server.computation.task.projectanalysis.batch.BatchReportReader;
 import org.sonar.server.computation.task.step.ComputationStep;
 
+import static java.util.Collections.singleton;
+
 public class PersistScannerContextStep implements ComputationStep {
   private final BatchReportReader reportReader;
   private final DbClient dbClient;
@@ -47,6 +49,10 @@ public class PersistScannerContextStep implements ComputationStep {
     try (CloseableIterator<String> logsIterator = reportReader.readScannerLogs()) {
       if (logsIterator.hasNext()) {
         try (DbSession dbSession = dbClient.openSession(false)) {
+          // in case the task was restarted, the context might have been already persisted
+          // for total reliability, we rather delete the existing row as we don't want to assume the content
+          // consistent with the report
+          dbClient.ceScannerContextDao().deleteByUuids(dbSession, singleton(ceTask.getUuid()));
           dbClient.ceScannerContextDao().insert(dbSession, ceTask.getUuid(), logsIterator);
           dbSession.commit();
         }
index 2a8f9d73592dc69a74f980676283fd778a3e0ce0..ec75a9d4b5f04182bca57559d98e65f02338df16 100644 (file)
  */
 package org.sonar.server.computation.task.projectanalysis.step;
 
+import java.util.Arrays;
 import org.junit.ClassRule;
 import org.junit.Rule;
 import org.junit.Test;
 import org.sonar.api.utils.System2;
 import org.sonar.ce.queue.CeTask;
+import org.sonar.core.util.CloseableIterator;
 import org.sonar.db.DbClient;
 import org.sonar.db.DbTester;
 import org.sonar.server.computation.task.projectanalysis.analysis.AnalysisMetadataHolderRule;
@@ -78,4 +80,19 @@ public class PersistScannerContextStepTest {
       .isEmpty();
   }
 
+  /**
+   * @see SONAR-8306
+   */
+  @Test
+  public void execute_does_not_fail_if_scanner_context_has_already_been_persisted() {
+    dbClient.ceScannerContextDao().insert(dbTester.getSession(), ANALYSIS_UUID, CloseableIterator.from(Arrays.asList("a", "b", "c").iterator()));
+    dbTester.commit();
+    reportReader.setScannerLogs(asList("1", "2", "3"));
+    when(ceTask.getUuid()).thenReturn(ANALYSIS_UUID);
+
+    underTest.execute();
+
+    assertThat(dbClient.ceScannerContextDao().selectScannerContext(dbTester.getSession(), ANALYSIS_UUID))
+      .contains("1" + '\n' + "2" + '\n' + "3");
+  }
 }