import org.sonar.api.utils.KeyValueFormat;
import org.sonar.api.utils.log.Logger;
import org.sonar.api.utils.log.Loggers;
-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;
import org.sonar.server.computation.metric.MetricRepository;
import org.sonar.server.computation.period.Period;
import org.sonar.server.computation.period.PeriodsHolder;
+import org.sonar.server.computation.scm.Changeset;
+import org.sonar.server.computation.scm.ScmInfo;
+import org.sonar.server.computation.scm.ScmInfoRepository;
import static com.google.common.collect.FluentIterable.from;
import static org.sonar.api.utils.KeyValueFormat.newIntegerConverter;
public class SqaleNewMeasuresVisitor extends PathAwareVisitorAdapter<SqaleNewMeasuresVisitor.NewDevelopmentCostCounter> {
private static final Logger LOG = Loggers.get(SqaleNewMeasuresVisitor.class);
- private final BatchReportReader batchReportReader;
+ private final ScmInfoRepository scmInfoRepository;
private final MeasureRepository measureRepository;
private final PeriodsHolder periodsHolder;
private final SqaleRatingSettings sqaleRatingSettings;
private final Metric nclocDataMetric;
private final Metric newDebtRatioMetric;
- public SqaleNewMeasuresVisitor(MetricRepository metricRepository, MeasureRepository measureRepository, BatchReportReader batchReportReader, PeriodsHolder periodsHolder,
+ public SqaleNewMeasuresVisitor(MetricRepository metricRepository, MeasureRepository measureRepository, ScmInfoRepository scmInfoRepository, PeriodsHolder periodsHolder,
SqaleRatingSettings sqaleRatingSettings) {
super(CrawlerDepthLimit.FILE, POST_ORDER, NewDevelopmentCostCounterFactory.INSTANCE);
- this.batchReportReader = batchReportReader;
this.measureRepository = measureRepository;
+ this.scmInfoRepository = scmInfoRepository;
this.periodsHolder = periodsHolder;
this.sqaleRatingSettings = sqaleRatingSettings;
return;
}
- BatchReport.Changesets changesets = batchReportReader.readChangesets(file.getReportAttributes().getRef());
- if (changesets == null) {
+ Optional<ScmInfo> scmInfoOptional = scmInfoRepository.getScmInfo(file);
+ if (!scmInfoOptional.isPresent()) {
LOG.trace(String.format("No changeset for file %s. Dev cost will be zero.", file.getKey()));
return;
}
- initNewDebtRatioCounter(path.current(), file.getFileAttributes().getLanguageKey(), nclocDataMeasure.get(), changesets);
+ ScmInfo scmInfo = scmInfoOptional.get();
+ initNewDebtRatioCounter(path.current(), file.getFileAttributes().getLanguageKey(), nclocDataMeasure.get(), scmInfo);
}
- private void initNewDebtRatioCounter(NewDevelopmentCostCounter devCostCounter, String languageKey, Measure nclocDataMeasure, BatchReport.Changesets changesets) {
+ private void initNewDebtRatioCounter(NewDevelopmentCostCounter devCostCounter, String languageKey, Measure nclocDataMeasure, ScmInfo scmInfo) {
long lineDevCost = sqaleRatingSettings.getDevCost(languageKey);
for (Integer nclocLineIndex : nclocLineIndexes(nclocDataMeasure)) {
- // lines are 0-based in changesetIndexByLine array
- int changesetIndex = changesets.getChangesetIndexByLine(nclocLineIndex - 1);
- BatchReport.Changesets.Changeset changeset = changesets.getChangeset(changesetIndex);
- if (!changeset.hasDate()) {
- continue;
- }
-
+ Changeset changeset = scmInfo.getChangesetForLine(nclocLineIndex);
for (Period period : periodsHolder.getPeriods()) {
if (isLineInPeriod(changeset.getDate(), period)) {
devCostCounter.increment(period, lineDevCost);
import org.junit.Test;
import org.sonar.api.measures.CoreMetrics;
import org.sonar.api.utils.KeyValueFormat;
-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;
import org.sonar.server.computation.metric.MetricRepositoryRule;
import org.sonar.server.computation.period.Period;
import org.sonar.server.computation.period.PeriodsHolderRule;
+import org.sonar.server.computation.scm.Changeset;
+import org.sonar.server.computation.scm.ScmInfoRepositoryRule;
import static com.google.common.base.Preconditions.checkArgument;
import static org.mockito.Mockito.mock;
private static final Offset<Double> VARIATION_COMPARISON_OFFSET = Offset.offset(0.01);
@Rule
- public BatchReportReaderRule reportReader = new BatchReportReaderRule();
+ public ScmInfoRepositoryRule scmInfoRepository = new ScmInfoRepositoryRule();
@Rule
public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule();
@Rule
private SqaleRatingSettings sqaleRatingSettings = mock(SqaleRatingSettings.class);
- private VisitorsCrawler underTest = new VisitorsCrawler(Arrays.<ComponentVisitor>asList(new SqaleNewMeasuresVisitor(metricRepository, measureRepository, reportReader,
+ private VisitorsCrawler underTest = new VisitorsCrawler(Arrays.<ComponentVisitor>asList(new SqaleNewMeasuresVisitor(metricRepository, measureRepository, scmInfoRepository,
periodsHolder, sqaleRatingSettings)));
@Before
);
measureRepository.addRawMeasure(LANGUAGE_1_FILE_REF, NEW_TECHNICAL_DEBT_KEY, createNewDebtMeasure(50, 12));
measureRepository.addRawMeasure(LANGUAGE_1_FILE_REF, NCLOC_DATA_KEY, createNclocDataMeasure(2, 3, 4));
- reportReader.putChangesets(createChangesets(LANGUAGE_1_FILE_REF, PERIOD_2_SNAPSHOT_DATE - 100, 4));
+ scmInfoRepository.setScmInfo(LANGUAGE_1_FILE_REF, createChangesets(PERIOD_2_SNAPSHOT_DATE - 100, 4));
underTest.visit(treeRootHolder.getRoot());
assertNewDebtRatioValues(ROOT_REF, 0, 0);
}
- @Test
- public void new_debt_ratio_is_0_when_file_has_empty_changesets() {
- when(sqaleRatingSettings.getDevCost(LANGUAGE_1_KEY)).thenReturn(LANGUAGE_1_DEV_COST);
- setupOneFileAloneInAProject(50, 12, Flag.SRC_FILE, Flag.WITH_NCLOC, Flag.NO_DATE_CHANGESET);
-
- underTest.visit(treeRootHolder.getRoot());
-
- assertNewDebtRatioValues(LANGUAGE_1_FILE_REF, 0, 0);
- assertNewDebtRatioValues(ROOT_REF, 0, 0);
- }
-
@Test
public void new_debt_ratio_is_0_when_there_is_no_ncloc_in_file() {
when(sqaleRatingSettings.getDevCost(LANGUAGE_1_KEY)).thenReturn(LANGUAGE_1_DEV_COST);
// 4 lines file, only first one is not ncloc
measureRepository.addRawMeasure(LANGUAGE_1_FILE_REF, NCLOC_DATA_KEY, createNclocDataMeasure(2, 3, 4));
// first 2 lines are before all snapshots, 2 last lines are after PERIOD 2's snapshot date
- reportReader.putChangesets(createChangesets(LANGUAGE_1_FILE_REF, PERIOD_2_SNAPSHOT_DATE - 100, 2, PERIOD_2_SNAPSHOT_DATE + 100, 2));
+ scmInfoRepository.setScmInfo(LANGUAGE_1_FILE_REF, createChangesets(PERIOD_2_SNAPSHOT_DATE - 100, 2, PERIOD_2_SNAPSHOT_DATE + 100, 2));
underTest.visit(treeRootHolder.getRoot());
private void setupOneFileAloneInAProject(int newDebtPeriod2, int newDebtPeriod4, Flag isUnitTest, Flag withNclocLines, Flag withChangeSets) {
checkArgument(isUnitTest == Flag.UT_FILE || isUnitTest == Flag.SRC_FILE);
checkArgument(withNclocLines == Flag.WITH_NCLOC || withNclocLines == Flag.NO_NCLOC || withNclocLines == Flag.MISSING_MEASURE_NCLOC);
- checkArgument(withChangeSets == Flag.WITH_CHANGESET || withChangeSets == Flag.NO_CHANGESET || withChangeSets == Flag.NO_DATE_CHANGESET);
+ checkArgument(withChangeSets == Flag.WITH_CHANGESET || withChangeSets == Flag.NO_CHANGESET);
treeRootHolder.setRoot(
builder(PROJECT, ROOT_REF)
}
if (withChangeSets == Flag.WITH_CHANGESET) {
// first 2 lines are before all snapshots, 2 last lines are after PERIOD 2's snapshot date
- reportReader.putChangesets(createChangesets(LANGUAGE_1_FILE_REF, PERIOD_2_SNAPSHOT_DATE - 100, 2, PERIOD_2_SNAPSHOT_DATE + 100, 2));
- } else if (withChangeSets == Flag.NO_DATE_CHANGESET) {
- reportReader.putChangesets(createNoDateChangesets(LANGUAGE_1_FILE_REF, 4));
+ scmInfoRepository.setScmInfo(LANGUAGE_1_FILE_REF, createChangesets(PERIOD_2_SNAPSHOT_DATE - 100, 2, PERIOD_2_SNAPSHOT_DATE + 100, 2));
}
}
private enum Flag {
- UT_FILE, SRC_FILE, NO_CHANGESET, WITH_CHANGESET, NO_DATE_CHANGESET, WITH_NCLOC, NO_NCLOC, MISSING_MEASURE_NCLOC
+ UT_FILE, SRC_FILE, NO_CHANGESET, WITH_CHANGESET, WITH_NCLOC, NO_NCLOC, MISSING_MEASURE_NCLOC
}
public static ReportComponent.Builder builder(Component.Type type, int ref) {
}
/**
- * Creates a changeset of {@code lines} lines which all have the same date {@code scmDate}.
+ * Creates changesets of {@code lines} lines which all have the same date {@code scmDate}.
*/
- private static BatchReport.Changesets createChangesets(int componentRef, long scmDate, int lines) {
- BatchReport.Changesets.Builder builder = BatchReport.Changesets.newBuilder()
- .setComponentRef(componentRef);
- addChangeSet(builder, scmDate, lines);
- return builder.build();
+ private static Changeset[] createChangesets(long scmDate, int lines) {
+ Changeset changetset = Changeset.newChangesetBuilder().setDate(scmDate).setRevision("rev-1").build();
+ Changeset[] changesets = new Changeset[lines];
+ for (int i = 0; i < lines; i++) {
+ changesets[i] = changetset;
+ }
+ return changesets;
}
/**
* Creates a changeset of {@code lineCount} lines which have the date {@code scmDate} and {@code otherLineCount} lines which
* have the date {@code otherScmDate}.
*/
- private static BatchReport.Changesets createChangesets(int componentRef, long scmDate, int lineCount, long otherScmDate, int otherLineCount) {
- BatchReport.Changesets.Builder builder = BatchReport.Changesets.newBuilder()
- .setComponentRef(componentRef);
- addChangeSet(builder, scmDate, lineCount);
- addChangeSet(builder, otherScmDate, otherLineCount);
- return builder.build();
- }
-
- private static void addChangeSet(BatchReport.Changesets.Builder builder, long scmDate, int lines) {
- BatchReport.Changesets.Changeset.Builder changesetBuilder = BatchReport.Changesets.Changeset.newBuilder();
- changesetBuilder.setRevision("rev" + scmDate);
- changesetBuilder.setDate(scmDate);
- builder.addChangeset(changesetBuilder.build());
- for (int i = 0; i < lines; i++) {
- builder.addChangesetIndexByLine(builder.getChangesetCount() - 1);
- }
- }
-
- private BatchReport.Changesets createNoDateChangesets(int componentRef, int lineCount) {
- BatchReport.Changesets.Builder builder = BatchReport.Changesets.newBuilder().setComponentRef(componentRef);
-
- BatchReport.Changesets.Changeset.Builder changesetBuilder = BatchReport.Changesets.Changeset.newBuilder();
- changesetBuilder.setRevision("rev");
- builder.addChangeset(changesetBuilder.build());
+ private static Changeset[] createChangesets(long scmDate, int lineCount, long otherScmDate, int otherLineCount) {
+ Changeset[] changesets = new Changeset[lineCount + otherLineCount];
+ Changeset changetset1 = Changeset.newChangesetBuilder().setDate(scmDate).setRevision("rev-1").build();
for (int i = 0; i < lineCount; i++) {
- builder.addChangesetIndexByLine(builder.getChangesetCount() - 1);
+ changesets[i] = changetset1;
}
-
- return builder.build();
+ Changeset changetset2 = Changeset.newChangesetBuilder().setDate(otherScmDate).setRevision("rev-2").build();
+ for (int i = lineCount; i < lineCount + otherLineCount; i++) {
+ changesets[i] = changetset2;
+ }
+ return changesets;
}
private void assertNoNewDebtRatioMeasure(int componentRef) {