]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-6824 Use SCM Info repo 598/head
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Mon, 26 Oct 2015 14:27:03 +0000 (15:27 +0100)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Tue, 27 Oct 2015 08:06:33 +0000 (09:06 +0100)
server/sonar-server/src/main/java/org/sonar/server/computation/source/LastCommitVisitor.java
server/sonar-server/src/test/java/org/sonar/server/computation/source/LastCommitVisitorTest.java

index 5c312104dfee99e1f9092e7b5d321dc8c665dd11..f2299f4f69b37cf35eb7c9edcfb6f86a7183e537 100644 (file)
@@ -21,8 +21,6 @@ package org.sonar.server.computation.source;
 
 import com.google.common.base.Optional;
 import org.sonar.api.measures.CoreMetrics;
-import org.sonar.batch.protocol.output.BatchReport;
-import org.sonar.server.computation.batch.BatchReportReader;
 import org.sonar.server.computation.component.Component;
 import org.sonar.server.computation.component.CrawlerDepthLimit;
 import org.sonar.server.computation.component.PathAwareVisitorAdapter;
@@ -30,17 +28,18 @@ import org.sonar.server.computation.measure.Measure;
 import org.sonar.server.computation.measure.MeasureRepository;
 import org.sonar.server.computation.metric.Metric;
 import org.sonar.server.computation.metric.MetricRepository;
+import org.sonar.server.computation.scm.ScmInfo;
+import org.sonar.server.computation.scm.ScmInfoRepository;
 
 import static org.sonar.server.computation.component.ComponentVisitor.Order.POST_ORDER;
 
 public class LastCommitVisitor extends PathAwareVisitorAdapter<LastCommitVisitor.LastCommit> {
 
-  private final BatchReportReader reportReader;
   private final MeasureRepository measureRepository;
+  private final ScmInfoRepository scmInfoRepository;
   private final Metric lastCommitDateMetric;
 
-  public LastCommitVisitor(BatchReportReader reportReader, MetricRepository metricRepository,
-    MeasureRepository measureRepository) {
+  public LastCommitVisitor(MetricRepository metricRepository, MeasureRepository measureRepository, ScmInfoRepository scmInfoRepository) {
     super(CrawlerDepthLimit.LEAVES, POST_ORDER, new SimpleStackElementFactory<LastCommit>() {
       @Override
       public LastCommit createForAny(Component component) {
@@ -53,8 +52,8 @@ public class LastCommitVisitor extends PathAwareVisitorAdapter<LastCommitVisitor
         return null;
       }
     });
-    this.reportReader = reportReader;
     this.measureRepository = measureRepository;
+    this.scmInfoRepository = scmInfoRepository;
     this.lastCommitDateMetric = metricRepository.getByKey(CoreMetrics.LAST_COMMIT_DATE_KEY);
   }
 
@@ -79,21 +78,10 @@ public class LastCommitVisitor extends PathAwareVisitorAdapter<LastCommitVisitor
     // since previous analysis (optimization to decrease execution of blame commands). In this case
     // the date is loaded from database, as it did not change from previous analysis.
 
-    // TODO We should use ScmInfoRepository instead of reading the report
-    // (but should only be done when the repo is only used once per component,
-    // as it's done with ComponentIssuesRepository, to not increase number of calls to file_sources)
-    BatchReport.Changesets changesets = reportReader.readChangesets(file.getReportAttributes().getRef());
-    if (changesets == null) {
-      Optional<Measure> baseMeasure = measureRepository.getBaseMeasure(file, lastCommitDateMetric);
-      if (baseMeasure.isPresent()) {
-        path.current().addDate(baseMeasure.get().getLongValue());
-      }
-    } else {
-      for (BatchReport.Changesets.Changeset changeset : changesets.getChangesetList()) {
-        if (changeset.hasDate()) {
-          path.current().addDate(changeset.getDate());
-        }
-      }
+    Optional<ScmInfo> scmInfoOptional = scmInfoRepository.getScmInfo(file);
+    if (scmInfoOptional.isPresent()) {
+      ScmInfo scmInfo = scmInfoOptional.get();
+      path.current().addDate(scmInfo.getLatestChangeset().getDate());
     }
     saveAndAggregate(file, path);
   }
index 9647c66fda3de031651d7cffca49c43f409cfc27..7cd63ec15c63d0d8bc380e1bb78e227bdda85872 100644 (file)
@@ -24,8 +24,6 @@ import com.google.common.collect.Lists;
 import org.junit.Rule;
 import org.junit.Test;
 import org.sonar.api.measures.CoreMetrics;
-import org.sonar.batch.protocol.output.BatchReport;
-import org.sonar.server.computation.batch.BatchReportReaderRule;
 import org.sonar.server.computation.batch.TreeRootHolderRule;
 import org.sonar.server.computation.component.Component;
 import org.sonar.server.computation.component.ComponentVisitor;
@@ -36,6 +34,8 @@ import org.sonar.server.computation.component.VisitorsCrawler;
 import org.sonar.server.computation.measure.Measure;
 import org.sonar.server.computation.measure.MeasureRepositoryRule;
 import org.sonar.server.computation.metric.MetricRepositoryRule;
+import org.sonar.server.computation.scm.Changeset;
+import org.sonar.server.computation.scm.ScmInfoRepositoryRule;
 
 import static org.assertj.core.api.Assertions.assertThat;
 import static org.sonar.api.measures.CoreMetrics.LAST_COMMIT_DATE_KEY;
@@ -62,9 +62,6 @@ public class LastCommitVisitorTest {
   @Rule
   public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule();
 
-  @Rule
-  public BatchReportReaderRule reportReader = new BatchReportReaderRule();
-
   @Rule
   public MetricRepositoryRule metricRepository = new MetricRepositoryRule()
     .add(CoreMetrics.LAST_COMMIT_DATE);
@@ -72,6 +69,9 @@ public class LastCommitVisitorTest {
   @Rule
   public MeasureRepositoryRule measureRepository = MeasureRepositoryRule.create(treeRootHolder, metricRepository);
 
+  @Rule
+  public ScmInfoRepositoryRule scmInfoRepository = new ScmInfoRepositoryRule();
+
   @Test
   public void aggregate_date_of_last_commit_to_directories_and_project() {
     final long FILE_1_DATE = 1_100_000_000_000L;
@@ -81,7 +81,7 @@ public class LastCommitVisitorTest {
     final long FILE_3_DATE = 1_300_000_000_000L;
 
     // simulate the output of visitFile()
-    LastCommitVisitor visitor = new LastCommitVisitor(reportReader, metricRepository, measureRepository) {
+    LastCommitVisitor visitor = new LastCommitVisitor(metricRepository, measureRepository, scmInfoRepository) {
       @Override
       public void visitFile(Component file, Path<LastCommit> path) {
         long fileDate;
@@ -168,7 +168,7 @@ public class LastCommitVisitorTest {
     measureRepository.addRawMeasure(PROJECT_2_REF, LAST_COMMIT_DATE_KEY, newMeasureBuilder().create(PROJECT_2_DATE));
     measureRepository.addRawMeasure(PROJECT_3_REF, LAST_COMMIT_DATE_KEY, newMeasureBuilder().create(PROJECT_3_DATE));
 
-    VisitorsCrawler underTest = new VisitorsCrawler(Lists.<ComponentVisitor>newArrayList(new LastCommitVisitor(reportReader, metricRepository, measureRepository)));
+    VisitorsCrawler underTest = new VisitorsCrawler(Lists.<ComponentVisitor>newArrayList(new LastCommitVisitor(metricRepository, measureRepository, scmInfoRepository)));
     underTest.visit(view);
 
     // second level of sub-views
@@ -183,64 +183,39 @@ public class LastCommitVisitorTest {
   }
 
   @Test
-  public void compute_date_of_file_from_blame_info_of_report() throws Exception {
-    VisitorsCrawler underTest = new VisitorsCrawler(Lists.<ComponentVisitor>newArrayList(new LastCommitVisitor(reportReader, metricRepository, measureRepository)));
+  public void compute_date_of_file_from_scm_repo() throws Exception {
+    VisitorsCrawler underTest = new VisitorsCrawler(Lists.<ComponentVisitor>newArrayList(new LastCommitVisitor(metricRepository, measureRepository, scmInfoRepository)));
 
-    BatchReport.Changesets changesets = BatchReport.Changesets.newBuilder()
-      .setComponentRef(FILE_1_REF)
-      .addChangeset(BatchReport.Changesets.Changeset.newBuilder()
+    scmInfoRepository.setScmInfo(FILE_1_REF,
+      Changeset.newChangesetBuilder()
         .setAuthor("john")
         .setDate(1_500_000_000_000L)
         .setRevision("rev-1")
-        .build())
-      .addChangeset(BatchReport.Changesets.Changeset.newBuilder()
+        .build(),
+      Changeset.newChangesetBuilder()
         .setAuthor("tom")
         // this is the most recent change
         .setDate(1_600_000_000_000L)
         .setRevision("rev-2")
-        .build())
-      .addChangeset(BatchReport.Changesets.Changeset.newBuilder()
+        .build(),
+      Changeset.newChangesetBuilder()
         .setAuthor("john")
         .setDate(1_500_000_000_000L)
         .setRevision("rev-1")
-        .build())
-      .addChangesetIndexByLine(0)
-      .build();
-    reportReader.putChangesets(changesets);
-    ReportComponent file = createFileComponent(FILE_1_REF);
-    treeRootHolder.setRoot(file);
-
-    underTest.visit(file);
-
-    assertDate(FILE_1_REF, 1_600_000_000_000L);
-  }
-
-  private void assertDate(int componentRef, long expectedDate) {
-    Optional<Measure> measure = measureRepository.getAddedRawMeasure(componentRef, LAST_COMMIT_DATE_KEY);
-    assertThat(measure.isPresent()).isTrue();
-    assertThat(measure.get().getLongValue()).isEqualTo(expectedDate);
-  }
+        .build()
+      );
 
-  /**
-   * When the file was not changed since previous analysis, than the report may not contain
-   * the SCM blame information. In this case the date of last commit is loaded
-   * from the base measure of previous analysis, directly from database
-   */
-  @Test
-  public void reuse_date_of_previous_analysis_if_blame_info_is_not_in_report() throws Exception {
-    VisitorsCrawler underTest = new VisitorsCrawler(Lists.<ComponentVisitor>newArrayList(new LastCommitVisitor(reportReader, metricRepository, measureRepository)));
     ReportComponent file = createFileComponent(FILE_1_REF);
     treeRootHolder.setRoot(file);
-    measureRepository.addBaseMeasure(FILE_1_REF, LAST_COMMIT_DATE_KEY, newMeasureBuilder().create(1_500_000_000L));
 
     underTest.visit(file);
 
-    assertDate(FILE_1_REF, 1_500_000_000L);
+    assertDate(FILE_1_REF, 1_600_000_000_000L);
   }
 
   @Test
-  public void date_is_not_computed_on_file_if_blame_is_not_in_report_nor_in_previous_analysis() throws Exception {
-    VisitorsCrawler underTest = new VisitorsCrawler(Lists.<ComponentVisitor>newArrayList(new LastCommitVisitor(reportReader, metricRepository, measureRepository)));
+  public void date_is_not_computed_on_file_if_blame_is_not_in_scm_repo() throws Exception {
+    VisitorsCrawler underTest = new VisitorsCrawler(Lists.<ComponentVisitor>newArrayList(new LastCommitVisitor(metricRepository, measureRepository, scmInfoRepository)));
     ReportComponent file = createFileComponent(FILE_1_REF);
     treeRootHolder.setRoot(file);
 
@@ -250,6 +225,12 @@ public class LastCommitVisitorTest {
     assertThat(measure.isPresent()).isFalse();
   }
 
+  private void assertDate(int componentRef, long expectedDate) {
+    Optional<Measure> measure = measureRepository.getAddedRawMeasure(componentRef, LAST_COMMIT_DATE_KEY);
+    assertThat(measure.isPresent()).isTrue();
+    assertThat(measure.get().getLongValue()).isEqualTo(expectedDate);
+  }
+
   private ReportComponent createFileComponent(int fileRef) {
     return ReportComponent.builder(FILE, fileRef).setFileAttributes(new FileAttributes(false, "js")).build();
   }