From 2e66147b1486e4cf7e7b62653e7a1d32d5f58240 Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Mon, 26 Oct 2015 15:27:03 +0100 Subject: [PATCH] SONAR-6824 Use SCM Info repo --- .../computation/source/LastCommitVisitor.java | 30 +++----- .../source/LastCommitVisitorTest.java | 71 +++++++------------ 2 files changed, 35 insertions(+), 66 deletions(-) diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/source/LastCommitVisitor.java b/server/sonar-server/src/main/java/org/sonar/server/computation/source/LastCommitVisitor.java index 5c312104dfe..f2299f4f69b 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/source/LastCommitVisitor.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/source/LastCommitVisitor.java @@ -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 { - 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() { @Override public LastCommit createForAny(Component component) { @@ -53,8 +52,8 @@ public class LastCommitVisitor extends PathAwareVisitorAdapter 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 scmInfoOptional = scmInfoRepository.getScmInfo(file); + if (scmInfoOptional.isPresent()) { + ScmInfo scmInfo = scmInfoOptional.get(); + path.current().addDate(scmInfo.getLatestChangeset().getDate()); } saveAndAggregate(file, path); } diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/source/LastCommitVisitorTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/source/LastCommitVisitorTest.java index 9647c66fda3..7cd63ec15c6 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/source/LastCommitVisitorTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/source/LastCommitVisitorTest.java @@ -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 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.newArrayList(new LastCommitVisitor(reportReader, metricRepository, measureRepository))); + VisitorsCrawler underTest = new VisitorsCrawler(Lists.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.newArrayList(new LastCommitVisitor(reportReader, metricRepository, measureRepository))); + public void compute_date_of_file_from_scm_repo() throws Exception { + VisitorsCrawler underTest = new VisitorsCrawler(Lists.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 = 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.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.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.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 = 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(); } -- 2.39.5