import org.sonar.server.computation.batch.TreeRootHolderRule;
import org.sonar.server.computation.component.Component;
import org.sonar.server.computation.component.ReportComponent;
+import org.sonar.server.computation.scm.ScmInfoRepositoryImpl;
import org.sonar.server.computation.source.SourceLinesRepositoryImpl;
import org.sonar.server.computation.step.PersistFileSourcesStep;
+import org.sonar.server.source.SourceService;
import static org.assertj.core.api.Assertions.assertThat;
BatchReportDirectoryHolderImpl batchReportDirectoryHolder = new BatchReportDirectoryHolderImpl();
batchReportDirectoryHolder.setDirectory(reportDir);
org.sonar.server.computation.batch.BatchReportReader batchReportReader = new BatchReportReaderImpl(batchReportDirectoryHolder);
- PersistFileSourcesStep step = new PersistFileSourcesStep(dbClient, System2.INSTANCE, treeRootHolder, batchReportReader, new SourceLinesRepositoryImpl(batchReportReader));
+ SourceService sourceService = new SourceService(dbClient, null);
+ ScmInfoRepositoryImpl scmInfoRepository = new ScmInfoRepositoryImpl(batchReportReader, dbClient, sourceService);
+ PersistFileSourcesStep step = new PersistFileSourcesStep(dbClient, System2.INSTANCE, treeRootHolder, batchReportReader,
+ new SourceLinesRepositoryImpl(batchReportReader), scmInfoRepository);
step.execute();
long end = System.currentTimeMillis();
package org.sonar.server.computation.source;
import javax.annotation.CheckForNull;
-import org.sonar.batch.protocol.output.BatchReport;
import org.sonar.db.protobuf.DbFileSources;
-
-import static com.google.common.base.Preconditions.checkArgument;
+import org.sonar.server.computation.scm.Changeset;
+import org.sonar.server.computation.scm.ScmInfo;
public class ScmLineReader implements LineReader {
- private final BatchReport.Changesets scmReport;
+ private final ScmInfo scmReport;
@CheckForNull
- private BatchReport.Changesets.Changeset latestChange;
+ private Changeset latestChange;
- public ScmLineReader(BatchReport.Changesets scmReport) {
+ public ScmLineReader(ScmInfo scmReport) {
this.scmReport = scmReport;
}
@Override
public void read(DbFileSources.Line.Builder lineBuilder) {
- int changeSetIndex = scmReport.getChangesetIndexByLine(lineBuilder.getLine() - 1);
- BatchReport.Changesets.Changeset changeset = scmReport.getChangeset(changeSetIndex);
- boolean hasAuthor = changeset.hasAuthor();
- if (hasAuthor) {
- lineBuilder.setScmAuthor(changeset.getAuthor());
- }
- boolean hasRevision = changeset.hasRevision();
- if (hasRevision) {
- lineBuilder.setScmRevision(changeset.getRevision());
+ Changeset changeset = scmReport.getChangesetForLine(lineBuilder.getLine());
+ String author = changeset.getAuthor();
+ if (author != null) {
+ lineBuilder.setScmAuthor(author);
}
- boolean hasDate = changeset.hasDate();
- if (hasDate) {
- lineBuilder.setScmDate(changeset.getDate());
- }
-
- checkArgument(
- hasAuthor || hasRevision || hasDate,
- "A changeset must contain at least one of : author, revision or date");
+ lineBuilder.setScmRevision(changeset.getRevision());
+ lineBuilder.setScmDate(changeset.getDate());
updateLatestChange(changeset);
}
- private void updateLatestChange(BatchReport.Changesets.Changeset newChangeSet) {
- if (this.latestChange == null) {
- this.latestChange = newChangeSet;
- } else if (newChangeSet.hasDate() && this.latestChange.hasDate()
- && newChangeSet.getDate() > this.latestChange.getDate()) {
- this.latestChange = newChangeSet;
+ private void updateLatestChange(Changeset newChangeSet) {
+ if (latestChange == null) {
+ latestChange = newChangeSet;
+ } else {
+ long newChangesetDate = newChangeSet.getDate();
+ long latestChangeDate = latestChange.getDate();
+ if (newChangesetDate > latestChangeDate) {
+ latestChange = newChangeSet;
+ }
}
}
@CheckForNull
- public BatchReport.Changesets.Changeset getLatestChange() {
+ public Changeset getLatestChange() {
return latestChange;
}
}
package org.sonar.server.computation.step;
+import com.google.common.base.Optional;
import com.google.common.collect.ImmutableMap;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-import javax.annotation.CheckForNull;
-import javax.annotation.Nullable;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.ibatis.session.ResultContext;
import org.apache.ibatis.session.ResultHandler;
import org.sonar.server.computation.component.DepthTraversalTypeAwareCrawler;
import org.sonar.server.computation.component.TreeRootHolder;
import org.sonar.server.computation.component.TypeAwareVisitorAdapter;
+import org.sonar.server.computation.scm.Changeset;
+import org.sonar.server.computation.scm.ScmInfo;
+import org.sonar.server.computation.scm.ScmInfoRepository;
import org.sonar.server.computation.source.ComputeFileSourceData;
import org.sonar.server.computation.source.CoverageLineReader;
import org.sonar.server.computation.source.DuplicationLineReader;
import org.sonar.server.computation.source.SourceLinesRepository;
import org.sonar.server.computation.source.SymbolsLineReader;
+import javax.annotation.CheckForNull;
+import javax.annotation.Nullable;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
import static org.sonar.server.computation.component.ComponentVisitor.Order.PRE_ORDER;
public class PersistFileSourcesStep implements ComputationStep {
private final TreeRootHolder treeRootHolder;
private final BatchReportReader reportReader;
private final SourceLinesRepository sourceLinesRepository;
+ private final ScmInfoRepository scmInfoRepository;
- public PersistFileSourcesStep(DbClient dbClient, System2 system2, TreeRootHolder treeRootHolder, BatchReportReader reportReader, SourceLinesRepository sourceLinesRepository) {
+ public PersistFileSourcesStep(DbClient dbClient, System2 system2, TreeRootHolder treeRootHolder, BatchReportReader reportReader, SourceLinesRepository sourceLinesRepository,
+ ScmInfoRepository scmInfoRepository) {
this.dbClient = dbClient;
this.system2 = system2;
this.treeRootHolder = treeRootHolder;
this.reportReader = reportReader;
this.sourceLinesRepository = sourceLinesRepository;
+ this.scmInfoRepository = scmInfoRepository;
}
@Override
int fileRef = file.getReportAttributes().getRef();
BatchReport.Component component = reportReader.readComponent(fileRef);
CloseableIterator<String> linesIterator = sourceLinesRepository.readLines(file);
- LineReaders lineReaders = new LineReaders(reportReader, fileRef);
+ LineReaders lineReaders = new LineReaders(reportReader, scmInfoRepository, file);
try {
ComputeFileSourceData computeFileSourceData = new ComputeFileSourceData(linesIterator, lineReaders.readers(), component.getLines());
ComputeFileSourceData.Data fileSourceData = computeFileSourceData.compute();
}
private void persistSource(ComputeFileSourceData.Data fileSourceData, String componentUuid,
- @Nullable BatchReport.Changesets.Changeset latestChange) {
+ @Nullable Changeset latestChange) {
DbFileSources.Data fileData = fileSourceData.getFileSourceData();
byte[] data = FileSourceDto.encodeSourceData(fileData);
}
@CheckForNull
- private static String computeRevision(FileSourceDto previousDto, @Nullable BatchReport.Changesets.Changeset latestChange) {
+ private static String computeRevision(FileSourceDto previousDto, @Nullable Changeset latestChange) {
if (latestChange == null) {
return previousDto.getRevision();
}
}
@CheckForNull
- private static String computeRevision(@Nullable BatchReport.Changesets.Changeset latestChange) {
+ private static String computeRevision(@Nullable Changeset latestChange) {
if (latestChange == null) {
return null;
}
@CheckForNull
private final ScmLineReader scmLineReader;
- LineReaders(BatchReportReader reportReader, int componentRef) {
+ LineReaders(BatchReportReader reportReader, ScmInfoRepository scmInfoRepository, Component component) {
+ int componentRef = component.getReportAttributes().getRef();
CloseableIterator<BatchReport.Coverage> coverageIt = reportReader.readComponentCoverage(componentRef);
closeables.add(coverageIt);
readers.add(new CoverageLineReader(coverageIt));
- BatchReport.Changesets scmReport = reportReader.readChangesets(componentRef);
- if (scmReport != null) {
- this.scmLineReader = new ScmLineReader(scmReport);
+ Optional<ScmInfo> scmInfoOptional = scmInfoRepository.getScmInfo(component);
+ if (scmInfoOptional.isPresent()) {
+ this.scmLineReader = new ScmLineReader(scmInfoOptional.get());
readers.add(scmLineReader);
} else {
this.scmLineReader = null;
}
@CheckForNull
- public BatchReport.Changesets.Changeset getLatestChange() {
+ public Changeset getLatestChange() {
if (scmLineReader == null) {
return null;
}
package org.sonar.server.computation.source;
+import com.google.common.collect.ImmutableList;
+import java.util.List;
import org.junit.Test;
-import org.sonar.batch.protocol.output.BatchReport;
import org.sonar.db.protobuf.DbFileSources;
+import org.sonar.server.computation.scm.Changeset;
+import org.sonar.server.computation.scm.ScmInfo;
+import org.sonar.server.computation.scm.ScmInfoImpl;
+import static com.google.common.collect.Lists.newArrayList;
import static org.assertj.core.api.Assertions.assertThat;
-import static org.assertj.core.api.Assertions.failBecauseExceptionWasNotThrown;
public class ScmLineReaderTest {
@Test
public void set_scm() {
- BatchReport.Changesets scmReport = BatchReport.Changesets.newBuilder()
- .addChangeset(newChangeSetBuilder()
+ ScmInfo scmInfo = new ScmInfoImpl(newArrayList(
+ Changeset.newChangesetBuilder()
.setAuthor("john")
.setDate(123456789L)
.setRevision("rev-1")
- .build())
- .addChangesetIndexByLine(0)
- .build();
+ .build()
+ ));
- ScmLineReader lineScm = new ScmLineReader(scmReport);
+ ScmLineReader lineScm = new ScmLineReader(scmInfo);
DbFileSources.Line.Builder lineBuilder = DbFileSources.Data.newBuilder().addLinesBuilder().setLine(1);
lineScm.read(lineBuilder);
}
@Test
- public void set_only_author() {
- BatchReport.Changesets scmReport = BatchReport.Changesets.newBuilder()
- .addChangeset(newChangeSetBuilder()
- .setAuthor("john")
- .build())
- .addChangesetIndexByLine(0)
- .build();
-
- ScmLineReader lineScm = new ScmLineReader(scmReport);
-
- DbFileSources.Line.Builder lineBuilder = DbFileSources.Data.newBuilder().addLinesBuilder().setLine(1);
- lineScm.read(lineBuilder);
-
- assertThat(lineBuilder.getScmAuthor()).isEqualTo("john");
- assertThat(lineBuilder.hasScmDate()).isFalse();
- assertThat(lineBuilder.hasScmRevision()).isFalse();
- }
-
- @Test
- public void set_only_date() {
- BatchReport.Changesets scmReport = BatchReport.Changesets.newBuilder()
- .addChangeset(newChangeSetBuilder()
+ public void set_scm_with_minim_fields() {
+ ScmInfo scmInfo = new ScmInfoImpl(newArrayList(
+ Changeset.newChangesetBuilder()
.setDate(123456789L)
- .build())
- .addChangesetIndexByLine(0)
- .build();
-
- ScmLineReader lineScm = new ScmLineReader(scmReport);
-
- DbFileSources.Line.Builder lineBuilder = DbFileSources.Data.newBuilder().addLinesBuilder().setLine(1);
- lineScm.read(lineBuilder);
-
- assertThat(lineBuilder.hasScmAuthor()).isFalse();
- assertThat(lineBuilder.getScmDate()).isEqualTo(123456789L);
- assertThat(lineBuilder.hasScmRevision()).isFalse();
- }
-
- @Test
- public void set_only_revision() {
- BatchReport.Changesets scmReport = BatchReport.Changesets.newBuilder()
- .addChangeset(newChangeSetBuilder()
.setRevision("rev-1")
- .build())
- .addChangesetIndexByLine(0)
- .build();
+ .build()
+ ));
- ScmLineReader lineScm = new ScmLineReader(scmReport);
+ ScmLineReader lineScm = new ScmLineReader(scmInfo);
DbFileSources.Line.Builder lineBuilder = DbFileSources.Data.newBuilder().addLinesBuilder().setLine(1);
lineScm.read(lineBuilder);
assertThat(lineBuilder.hasScmAuthor()).isFalse();
- assertThat(lineBuilder.hasScmDate()).isFalse();
+ assertThat(lineBuilder.getScmDate()).isEqualTo(123456789L);
assertThat(lineBuilder.getScmRevision()).isEqualTo("rev-1");
}
- @Test
- public void fail_when_changeset_is_empty() {
- BatchReport.Changesets scmReport = BatchReport.Changesets.newBuilder()
- .addChangeset(newChangeSetBuilder()
- .build())
- .addChangesetIndexByLine(0)
- .build();
-
- ScmLineReader lineScm = new ScmLineReader(scmReport);
-
- DbFileSources.Line.Builder lineBuilder = DbFileSources.Data.newBuilder().addLinesBuilder().setLine(1);
- try {
- lineScm.read(lineBuilder);
- failBecauseExceptionWasNotThrown(IllegalArgumentException.class);
- } catch (IllegalArgumentException e) {
- assertThat(e).hasMessage("A changeset must contain at least one of : author, revision or date");
- }
- }
-
@Test
public void getLatestChange_returns_changeset_with_highest_date_of_read_lines() {
long refDate = 123456789L;
- BatchReport.Changesets.Changeset changeset0 = newChangeSetBuilder().setDate(refDate - 636).build();
- BatchReport.Changesets.Changeset changeset1 = newChangeSetBuilder().setDate(refDate + 1).build();
- BatchReport.Changesets.Changeset changeset2 = newChangeSetBuilder().setDate(refDate + 2).build();
- BatchReport.Changesets scmReport = setup8LinesChangeset(changeset0, changeset1, changeset2);
+ Changeset changeset0 = Changeset.newChangesetBuilder().setDate(refDate - 636).setRevision("rev-1").build();
+ Changeset changeset1 = Changeset.newChangesetBuilder().setDate(refDate + 1).setRevision("rev-2").build();
+ Changeset changeset2 = Changeset.newChangesetBuilder().setDate(refDate + 2).setRevision("rev-3").build();
+ ScmInfo scmInfo = new ScmInfoImpl(setup8LinesChangeset(changeset0, changeset1, changeset2));
- ScmLineReader lineScm = new ScmLineReader(scmReport);
+ ScmLineReader lineScm = new ScmLineReader(scmInfo);
// before any line is read, the latest change is null
assertThat(lineScm.getLatestChange()).isNull();
readLineAndAssertLatestChangeDate(lineScm, 8, changeset2);
}
- @Test
- public void getLatestChange_returns_first_changeset_when_none_have_dates() {
- BatchReport.Changesets.Changeset changeset0 = newChangeSetBuilder().setRevision("0").build();
- BatchReport.Changesets.Changeset changeset1 = newChangeSetBuilder().setRevision("1").build();
- BatchReport.Changesets.Changeset changeset2 = newChangeSetBuilder().setRevision("2").build();
- BatchReport.Changesets scmReport = setup8LinesChangeset(changeset0, changeset1, changeset2);
-
- ScmLineReader lineScm = new ScmLineReader(scmReport);
-
- // before any line is read, the latest change is null
- assertThat(lineScm.getLatestChange()).isNull();
-
- // read lines 1 to 8, no date => changeset 0
- readLineAndAssertLatestChangeDate(lineScm, 1, changeset0);
- readLineAndAssertLatestChangeDate(lineScm, 2, changeset0);
- readLineAndAssertLatestChangeDate(lineScm, 3, changeset0);
- readLineAndAssertLatestChangeDate(lineScm, 4, changeset0);
- readLineAndAssertLatestChangeDate(lineScm, 5, changeset0);
- readLineAndAssertLatestChangeDate(lineScm, 6, changeset0);
- readLineAndAssertLatestChangeDate(lineScm, 7, changeset0);
- readLineAndAssertLatestChangeDate(lineScm, 8, changeset0);
+ private static List<Changeset> setup8LinesChangeset(Changeset changeset0, Changeset changeset1, Changeset changeset2) {
+ return ImmutableList.of(changeset0, changeset1, changeset1, changeset2, changeset0, changeset1, changeset0, changeset0);
}
- @Test
- public void getLatestChange_returns_first_changeset_when_the_first_one_has_no_date() {
- BatchReport.Changesets.Changeset changeset0 = newChangeSetBuilder().setRevision("0").build();
- BatchReport.Changesets.Changeset changeset1 = newChangeSetBuilder().setRevision("1").setDate(95454154L).build();
- BatchReport.Changesets.Changeset changeset2 = newChangeSetBuilder().setRevision("2").setDate(9654545444L).build();
- BatchReport.Changesets scmReport = setup8LinesChangeset(changeset0, changeset1, changeset2);
-
- ScmLineReader lineScm = new ScmLineReader(scmReport);
-
- // before any line is read, the latest change is null
- assertThat(lineScm.getLatestChange()).isNull();
-
- // read lines 1 to 8, first encountered changeset has no date => changeset 0
- readLineAndAssertLatestChangeDate(lineScm, 1, changeset0);
- readLineAndAssertLatestChangeDate(lineScm, 2, changeset0);
- readLineAndAssertLatestChangeDate(lineScm, 3, changeset0);
- readLineAndAssertLatestChangeDate(lineScm, 4, changeset0);
- readLineAndAssertLatestChangeDate(lineScm, 5, changeset0);
- readLineAndAssertLatestChangeDate(lineScm, 6, changeset0);
- readLineAndAssertLatestChangeDate(lineScm, 7, changeset0);
- readLineAndAssertLatestChangeDate(lineScm, 8, changeset0);
- }
-
- private static BatchReport.Changesets setup8LinesChangeset(BatchReport.Changesets.Changeset changeset0,
- BatchReport.Changesets.Changeset changeset1,
- BatchReport.Changesets.Changeset changeset2) {
- return BatchReport.Changesets.newBuilder()
- .addChangeset(changeset0)
- .addChangeset(changeset1)
- .addChangeset(changeset2)
- .addChangesetIndexByLine(0)
- .addChangesetIndexByLine(1)
- .addChangesetIndexByLine(1)
- .addChangesetIndexByLine(2)
- .addChangesetIndexByLine(0)
- .addChangesetIndexByLine(1)
- .addChangesetIndexByLine(0)
- .addChangesetIndexByLine(0)
- .build();
- }
-
- private void readLineAndAssertLatestChangeDate(ScmLineReader lineScm, int line, BatchReport.Changesets.Changeset expectedChangeset) {
+ private void readLineAndAssertLatestChangeDate(ScmLineReader lineScm, int line, Changeset expectedChangeset) {
DbFileSources.Line.Builder lineBuilder = DbFileSources.Data.newBuilder().addLinesBuilder().setLine(line);
lineScm.read(lineBuilder);
assertThat(lineScm.getLatestChange()).isSameAs(expectedChangeset);
}
- private static BatchReport.Changesets.Changeset.Builder newChangeSetBuilder() {
- return BatchReport.Changesets.Changeset.newBuilder();
- }
-
}
import org.sonar.server.computation.batch.TreeRootHolderRule;
import org.sonar.server.computation.component.Component;
import org.sonar.server.computation.component.ReportComponent;
+import org.sonar.server.computation.scm.Changeset;
+import org.sonar.server.computation.scm.ScmInfoRepositoryRule;
import org.sonar.server.computation.source.SourceLinesRepositoryRule;
import org.sonar.test.DbTests;
@Rule
public BatchReportReaderRule reportReader = new BatchReportReaderRule();
+ @Rule
+ public ScmInfoRepositoryRule scmInfoRepository = new ScmInfoRepositoryRule();
+
@Rule
public SourceLinesRepositoryRule fileSourceRepository = new SourceLinesRepositoryRule();
public void setup() {
dbTester.truncateTables();
when(system2.now()).thenReturn(now);
- underTest = new PersistFileSourcesStep(dbClient, system2, treeRootHolder, reportReader, fileSourceRepository);
+ underTest = new PersistFileSourcesStep(dbClient, system2, treeRootHolder, reportReader, fileSourceRepository, scmInfoRepository);
}
@Override
@Test
public void persist_scm() {
initBasicReport(1);
-
- reportReader.putChangesets(BatchReport.Changesets.newBuilder()
- .setComponentRef(FILE_REF)
- .addChangeset(BatchReport.Changesets.Changeset.newBuilder()
- .setAuthor("john")
- .setDate(123456789L)
- .setRevision("rev-1")
- .build())
- .addChangesetIndexByLine(0)
+ scmInfoRepository.setScmInfo(FILE_REF, Changeset.newChangesetBuilder()
+ .setAuthor("john")
+ .setDate(123456789L)
+ .setRevision("rev-1")
.build());
underTest.execute();
initBasicReport(1);
- reportReader.putChangesets(BatchReport.Changesets.newBuilder()
- .setComponentRef(FILE_REF)
- .addChangeset(BatchReport.Changesets.Changeset.newBuilder()
- .setAuthor("john")
- .setDate(123456789L)
- .setRevision("rev-1")
- .build())
- .addChangesetIndexByLine(0)
- .build());
+ scmInfoRepository.setScmInfo(FILE_REF, Changeset.newChangesetBuilder()
+ .setAuthor("john")
+ .setDate(123456789L)
+ .setRevision("rev-1")
+ .build());
+
underTest.execute();