]> source.dussan.org Git - sonarqube.git/commitdiff
Fix handling of ScmInfo
authorDuarte Meneses <duarte.meneses@sonarsource.com>
Fri, 28 Jul 2017 12:13:40 +0000 (14:13 +0200)
committerJulien HENRY <julien.henry@sonarsource.com>
Mon, 7 Aug 2017 09:44:06 +0000 (11:44 +0200)
server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/scm/ScmInfoRepositoryImpl.java
server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/scm/ScmInfoRepositoryImplTest.java

index 4d50f32733b1ed3ba543f628f7cafaa1a17b9b4f..5cfc4e5ec185cc4d5ab244f6e04609468ec56816 100644 (file)
@@ -31,6 +31,7 @@ import org.sonar.scanner.protocol.output.ScannerReport;
 import org.sonar.server.computation.task.projectanalysis.analysis.AnalysisMetadataHolder;
 import org.sonar.server.computation.task.projectanalysis.batch.BatchReportReader;
 import org.sonar.server.computation.task.projectanalysis.component.Component;
+import org.sonar.server.computation.task.projectanalysis.component.Component.Status;
 import org.sonar.server.computation.task.projectanalysis.source.SourceHashRepository;
 
 import static java.util.Objects.requireNonNull;
@@ -100,13 +101,20 @@ public class ScmInfoRepositoryImpl implements ScmInfoRepository {
     LOGGER.trace("Reading SCM info from db for file '{}'", file.getKey());
     try (DbSession dbSession = dbClient.openSession(false)) {
       FileSourceDto dto = dbClient.fileSourceDao().selectSourceByFileUuid(dbSession, file.getUuid());
-      if (dto == null || !sourceHashRepository.getRawSourceHash(file).equals(dto.getSrcHash())) {
+      if (dto == null || !isDtoValid(file, dto)) {
         return NoScmInfo.INSTANCE;
       }
       return DbScmInfo.create(file, dto.getSourceData().getLinesList()).or(NoScmInfo.INSTANCE);
     }
   }
 
+  private boolean isDtoValid(Component file, FileSourceDto dto) {
+    if (analysisMetadataHolder.isIncrementalAnalysis() && file.getStatus() == Status.SAME) {
+      return true;
+    }
+    return sourceHashRepository.getRawSourceHash(file).equals(dto.getSrcHash());
+  }
+
   private static ScmInfo getScmInfoFromReport(Component file, ScannerReport.Changesets changesets) {
     LOGGER.trace("Reading SCM info from report for file '{}'", file.getKey());
     return new ReportScmInfo(changesets);
index 2b5da1dab3468bceacd6a77fb762c284d10fba68..980c00bc6729be81d947dd8653f20de6d6973c2e 100644 (file)
  */
 package org.sonar.server.computation.task.projectanalysis.scm;
 
-import com.google.common.collect.ImmutableList;
-import com.tngtech.java.junit.dataprovider.DataProvider;
-import com.tngtech.java.junit.dataprovider.DataProviderRunner;
-import com.tngtech.java.junit.dataprovider.UseDataProvider;
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.guava.api.Assertions.assertThat;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verifyNoMoreInteractions;
+import static org.sonar.api.utils.log.LoggerLevel.TRACE;
+import static org.sonar.server.computation.task.projectanalysis.component.ReportComponent.builder;
+
 import java.util.EnumSet;
 import java.util.Iterator;
 import java.util.List;
+
 import javax.annotation.Nullable;
+
 import org.junit.Rule;
 import org.junit.Test;
 import org.junit.rules.ExpectedException;
@@ -45,18 +50,17 @@ import org.sonar.server.computation.task.projectanalysis.analysis.AnalysisMetada
 import org.sonar.server.computation.task.projectanalysis.batch.BatchReportReader;
 import org.sonar.server.computation.task.projectanalysis.batch.BatchReportReaderRule;
 import org.sonar.server.computation.task.projectanalysis.component.Component;
+import org.sonar.server.computation.task.projectanalysis.component.Component.Status;
 import org.sonar.server.computation.task.projectanalysis.component.ReportComponent;
 import org.sonar.server.computation.task.projectanalysis.component.ViewsComponent;
 import org.sonar.server.computation.task.projectanalysis.source.SourceHashRepository;
 import org.sonar.server.computation.task.projectanalysis.source.SourceHashRepositoryImpl;
 import org.sonar.server.computation.task.projectanalysis.source.SourceLinesRepositoryImpl;
 
-import static org.assertj.core.api.Assertions.assertThat;
-import static org.assertj.guava.api.Assertions.assertThat;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.verifyNoMoreInteractions;
-import static org.sonar.api.utils.log.LoggerLevel.TRACE;
-import static org.sonar.server.computation.task.projectanalysis.component.ReportComponent.builder;
+import com.google.common.collect.ImmutableList;
+import com.tngtech.java.junit.dataprovider.DataProvider;
+import com.tngtech.java.junit.dataprovider.DataProviderRunner;
+import com.tngtech.java.junit.dataprovider.UseDataProvider;
 
 @RunWith(DataProviderRunner.class)
 public class ScmInfoRepositoryImplTest {
@@ -88,6 +92,21 @@ public class ScmInfoRepositoryImplTest {
   ScmInfoRepositoryImpl underTest = new ScmInfoRepositoryImpl(reportReader, analysisMetadataHolder, dbClient,
     new SourceHashRepositoryImpl(new SourceLinesRepositoryImpl(reportReader)));
 
+  @Test
+  public void dont_check_hash_for_unmodified_files_incremental_analysis() {
+    analysisMetadataHolder.setIncrementalAnalysis(true);
+    analysisMetadataHolder.setBaseAnalysis(baseProjectAnalysis);
+
+    addFileSourceInDb("henry", DATE_1, "rev-1", computeSourceHash(1));
+    addCopyFromPreviousChangesetInReport();
+
+    Component file = builder(Component.Type.FILE, FILE_REF).setKey("FILE_KEY").setUuid("FILE_UUID").setStatus(Status.SAME).build();
+    ScmInfo scmInfo = underTest.getScmInfo(file).get();
+    assertThat(scmInfo.getAllChangesets()).hasSize(1);
+
+    assertThat(logTester.logs(TRACE)).containsOnly("Reading SCM info from db for file 'FILE_KEY'");
+  }
+
   @Test
   public void read_from_report() throws Exception {
     analysisMetadataHolder.setBaseAnalysis(baseProjectAnalysis);
@@ -112,6 +131,8 @@ public class ScmInfoRepositoryImplTest {
   @Test
   public void getScmInfo_returns_ScmInfo_from_DB_CopyFromPrevious_is_true_if_hashes_are_the_same() throws Exception {
     analysisMetadataHolder.setBaseAnalysis(baseProjectAnalysis);
+    analysisMetadataHolder.setIncrementalAnalysis(false);
+
     addFileSourceInDb("henry", DATE_1, "rev-1", computeSourceHash(1));
     addFileSourceInReport(1);
     addCopyFromPreviousChangesetInReport();
@@ -125,6 +146,8 @@ public class ScmInfoRepositoryImplTest {
   @Test
   public void getScmInfo_returns_absent_when_CopyFromPrevious_is_true_but_hashes_are_not_the_same() throws Exception {
     analysisMetadataHolder.setBaseAnalysis(baseProjectAnalysis);
+    analysisMetadataHolder.setIncrementalAnalysis(false);
+
     addFileSourceInDb("henry", DATE_1, "rev-1", computeSourceHash(1) + "_different");
     addFileSourceInReport(1);
     addCopyFromPreviousChangesetInReport();
@@ -151,6 +174,8 @@ public class ScmInfoRepositoryImplTest {
   @Test
   public void read_from_db_even_if_data_in_report_exists_when_CopyFromPrevious_is_true() throws Exception {
     analysisMetadataHolder.setBaseAnalysis(baseProjectAnalysis);
+    analysisMetadataHolder.setIncrementalAnalysis(false);
+
     addFileSourceInDb("henry", DATE_1, "rev-1", computeSourceHash(1));
     addFileSourceInReport(1);
     addChangesetInReport("john", DATE_2, "rev-2", true);