Search in sources :

Example 1 with ScmInfo

use of org.sonar.ce.task.projectanalysis.scm.ScmInfo in project sonarqube by SonarSource.

the class NewLinesRepository method computeNewLinesFromScm.

/**
 * If the changed lines are not in the report or if we are not analyzing a P/R or a branch using a "reference branch", we fall back to this method.
 * If there is a period and SCM information, we compare the change dates of each line with the start of the period to figure out if a line is new or not.
 */
private Optional<Set<Integer>> computeNewLinesFromScm(Component component) {
    Optional<ScmInfo> scmInfoOpt = scmInfoRepository.getScmInfo(component);
    if (scmInfoOpt.isEmpty()) {
        return Optional.empty();
    }
    ScmInfo scmInfo = scmInfoOpt.get();
    Changeset[] allChangesets = scmInfo.getAllChangesets();
    Set<Integer> lines = new HashSet<>();
    // in PRs, we consider changes introduced in this analysis as new, hence subtracting 1.
    long referenceDate = useAnalysisDateAsReferenceDate() ? (analysisMetadataHolder.getAnalysisDate() - 1) : periodHolder.getPeriod().getDate();
    for (int i = 0; i < allChangesets.length; i++) {
        if (isLineInPeriod(allChangesets[i].getDate(), referenceDate)) {
            lines.add(i + 1);
        }
    }
    return Optional.of(lines);
}
Also used : ScmInfo(org.sonar.ce.task.projectanalysis.scm.ScmInfo) Changeset(org.sonar.ce.task.projectanalysis.scm.Changeset) HashSet(java.util.HashSet)

Example 2 with ScmInfo

use of org.sonar.ce.task.projectanalysis.scm.ScmInfo in project sonarqube by SonarSource.

the class LastCommitVisitor method visitFile.

@Override
public void visitFile(Component file, Path<LastCommit> path) {
    // load SCM blame information from report. It can be absent when the file was not touched
    // 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.
    Optional<ScmInfo> scmInfoOptional = scmInfoRepository.getScmInfo(file);
    if (scmInfoOptional.isPresent()) {
        ScmInfo scmInfo = scmInfoOptional.get();
        path.current().addDate(scmInfo.getLatestChangeset().getDate());
    }
    saveAndAggregate(file, path);
}
Also used : ScmInfo(org.sonar.ce.task.projectanalysis.scm.ScmInfo)

Example 3 with ScmInfo

use of org.sonar.ce.task.projectanalysis.scm.ScmInfo in project sonarqube by SonarSource.

the class ScmLineReaderTest method set_scm.

@Test
public void set_scm() {
    ScmInfo scmInfo = new ScmInfoImpl(new Changeset[] { Changeset.newChangesetBuilder().setAuthor("john").setDate(123_456_789L).setRevision("rev-1").build() });
    ScmLineReader lineScm = new ScmLineReader(scmInfo);
    DbFileSources.Line.Builder lineBuilder = DbFileSources.Data.newBuilder().addLinesBuilder().setLine(1);
    assertThat(lineScm.read(lineBuilder)).isEmpty();
    assertThat(lineBuilder.getScmAuthor()).isEqualTo("john");
    assertThat(lineBuilder.getScmDate()).isEqualTo(123_456_789L);
    assertThat(lineBuilder.getScmRevision()).isEqualTo("rev-1");
}
Also used : ScmInfoImpl(org.sonar.ce.task.projectanalysis.scm.ScmInfoImpl) ScmInfo(org.sonar.ce.task.projectanalysis.scm.ScmInfo) Test(org.junit.Test)

Example 4 with ScmInfo

use of org.sonar.ce.task.projectanalysis.scm.ScmInfo in project sonarqube by SonarSource.

the class ScmLineReaderTest method set_scm_with_minim_fields.

@Test
public void set_scm_with_minim_fields() {
    ScmInfo scmInfo = new ScmInfoImpl(new Changeset[] { Changeset.newChangesetBuilder().setDate(123456789L).build() });
    ScmLineReader lineScm = new ScmLineReader(scmInfo);
    DbFileSources.Line.Builder lineBuilder = DbFileSources.Data.newBuilder().addLinesBuilder().setLine(1);
    assertThat(lineScm.read(lineBuilder)).isEmpty();
    assertThat(lineBuilder.hasScmAuthor()).isFalse();
    assertThat(lineBuilder.getScmDate()).isEqualTo(123456789L);
    assertThat(lineBuilder.hasScmRevision()).isFalse();
}
Also used : ScmInfoImpl(org.sonar.ce.task.projectanalysis.scm.ScmInfoImpl) ScmInfo(org.sonar.ce.task.projectanalysis.scm.ScmInfo) Test(org.junit.Test)

Example 5 with ScmInfo

use of org.sonar.ce.task.projectanalysis.scm.ScmInfo in project sonarqube by SonarSource.

the class ScmLineReaderTest method getLatestChange_returns_changeset_with_highest_date_of_read_lines.

@Test
public void getLatestChange_returns_changeset_with_highest_date_of_read_lines() {
    long refDate = 123_456_789L;
    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).build();
    ScmInfo scmInfo = new ScmInfoImpl(setup8LinesChangeset(changeset0, changeset1, changeset2));
    ScmLineReader lineScm = new ScmLineReader(scmInfo);
    // before any line is read, the latest changes are null
    assertThat(lineScm.getLatestChange()).isNull();
    assertThat(lineScm.getLatestChangeWithRevision()).isNull();
    // read line 1, only one changeset => 0
    readLineAndAssertLatestChanges(lineScm, 1, changeset0, changeset0);
    // read line 2, latest changeset is 1
    readLineAndAssertLatestChanges(lineScm, 2, changeset1, changeset1);
    // read line 3, latest changeset is still 1
    readLineAndAssertLatestChanges(lineScm, 3, changeset1, changeset1);
    // read line 4, latest changeset is now 2
    readLineAndAssertLatestChanges(lineScm, 4, changeset2, changeset1);
    // read line 5 to 8, there will never be any changeset more recent than 2
    readLineAndAssertLatestChanges(lineScm, 5, changeset2, changeset1);
    readLineAndAssertLatestChanges(lineScm, 6, changeset2, changeset1);
    readLineAndAssertLatestChanges(lineScm, 7, changeset2, changeset1);
    readLineAndAssertLatestChanges(lineScm, 8, changeset2, changeset1);
}
Also used : ScmInfoImpl(org.sonar.ce.task.projectanalysis.scm.ScmInfoImpl) ScmInfo(org.sonar.ce.task.projectanalysis.scm.ScmInfo) Changeset(org.sonar.ce.task.projectanalysis.scm.Changeset) Test(org.junit.Test)

Aggregations

ScmInfo (org.sonar.ce.task.projectanalysis.scm.ScmInfo)6 Test (org.junit.Test)3 ScmInfoImpl (org.sonar.ce.task.projectanalysis.scm.ScmInfoImpl)3 Changeset (org.sonar.ce.task.projectanalysis.scm.Changeset)2 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 CoverageLineReader (org.sonar.ce.task.projectanalysis.source.linereader.CoverageLineReader)1 DuplicationLineReader (org.sonar.ce.task.projectanalysis.source.linereader.DuplicationLineReader)1 HighlightingLineReader (org.sonar.ce.task.projectanalysis.source.linereader.HighlightingLineReader)1 IsNewLineReader (org.sonar.ce.task.projectanalysis.source.linereader.IsNewLineReader)1 LineReader (org.sonar.ce.task.projectanalysis.source.linereader.LineReader)1 RangeOffsetConverter (org.sonar.ce.task.projectanalysis.source.linereader.RangeOffsetConverter)1 ScmLineReader (org.sonar.ce.task.projectanalysis.source.linereader.ScmLineReader)1 SymbolsLineReader (org.sonar.ce.task.projectanalysis.source.linereader.SymbolsLineReader)1 CloseableIterator (org.sonar.core.util.CloseableIterator)1