Search in sources :

Example 1 with Changeset

use of org.sonar.ce.task.projectanalysis.scm.Changeset 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 Changeset

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

the class FileSourceDataComputer method compute.

public Data compute(Component file, FileSourceDataWarnings fileSourceDataWarnings) {
    try (CloseableIterator<String> linesIterator = sourceLinesRepository.readLines(file);
        SourceLineReadersFactory.LineReaders lineReaders = sourceLineReadersFactory.getLineReaders(file)) {
        SourceLinesHashRepositoryImpl.LineHashesComputer lineHashesComputer = sourceLinesHash.getLineHashesComputerToPersist(file);
        DbFileSources.Data.Builder fileSourceBuilder = DbFileSources.Data.newBuilder();
        int currentLine = 0;
        while (linesIterator.hasNext()) {
            currentLine++;
            String lineSource = linesIterator.next();
            boolean hasNextLine = linesIterator.hasNext();
            sourceHashComputer.addLine(lineSource, hasNextLine);
            lineHashesComputer.addLine(lineSource);
            DbFileSources.Line.Builder lineBuilder = fileSourceBuilder.addLinesBuilder().setSource(lineSource).setLine(currentLine);
            lineReaders.read(lineBuilder, readError -> fileSourceDataWarnings.addWarning(file, readError));
        }
        Changeset latestChangeWithRevision = lineReaders.getLatestChangeWithRevision();
        return new Data(fileSourceBuilder.build(), lineHashesComputer.getResult(), sourceHashComputer.getHash(), latestChangeWithRevision);
    }
}
Also used : Changeset(org.sonar.ce.task.projectanalysis.scm.Changeset)

Example 3 with Changeset

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

the class ScmLineReader method read.

@Override
public Optional<ReadError> read(DbFileSources.Line.Builder lineBuilder) {
    if (scmReport.hasChangesetForLine(lineBuilder.getLine())) {
        Changeset changeset = scmReport.getChangesetForLine(lineBuilder.getLine());
        String author = changeset.getAuthor();
        if (author != null) {
            lineBuilder.setScmAuthor(author);
        }
        String revision = changeset.getRevision();
        if (revision != null) {
            lineBuilder.setScmRevision(revision);
        }
        lineBuilder.setScmDate(changeset.getDate());
        updateLatestChange(changeset);
        if (revision != null) {
            updateLatestChangeWithRevision(changeset);
        }
    }
    return Optional.empty();
}
Also used : Changeset(org.sonar.ce.task.projectanalysis.scm.Changeset)

Example 4 with Changeset

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

the class PersistFileSourcesStepTest method not_update_sources_when_nothing_has_changed.

@Test
public void not_update_sources_when_nothing_has_changed() {
    dbClient.fileSourceDao().insert(dbTester.getSession(), createDto());
    dbTester.getSession().commit();
    Changeset changeset = Changeset.newChangesetBuilder().setDate(1L).setRevision("rev-1").build();
    setComputedData(DbFileSources.Data.newBuilder().build(), Collections.singletonList("lineHash"), "sourceHash", changeset);
    underTest.execute(new TestComputationStepContext());
    assertThat(dbTester.countRowsOfTable("file_sources")).isOne();
    FileSourceDto fileSourceDto = dbClient.fileSourceDao().selectByFileUuid(session, FILE1_UUID);
    assertThat(fileSourceDto.getSrcHash()).isEqualTo("sourceHash");
    assertThat(fileSourceDto.getLineHashes()).isEqualTo(Collections.singletonList("lineHash"));
    assertThat(fileSourceDto.getCreatedAt()).isEqualTo(PAST);
    assertThat(fileSourceDto.getUpdatedAt()).isEqualTo(PAST);
    verify(fileSourceDataWarnings).commitWarnings();
}
Also used : FileSourceDto(org.sonar.db.source.FileSourceDto) TestComputationStepContext(org.sonar.ce.task.step.TestComputationStepContext) Changeset(org.sonar.ce.task.projectanalysis.scm.Changeset) BaseStepTest(org.sonar.ce.task.projectanalysis.step.BaseStepTest) Test(org.junit.Test)

Example 5 with Changeset

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

the class PersistFileSourcesStepTest method save_revision.

@Test
public void save_revision() {
    Changeset latest = Changeset.newChangesetBuilder().setDate(0L).setRevision("rev-1").build();
    setComputedData(DbFileSources.Data.newBuilder().build(), Collections.singletonList("lineHashes"), "srcHash", latest);
    underTest.execute(new TestComputationStepContext());
    FileSourceDto fileSourceDto = dbClient.fileSourceDao().selectByFileUuid(session, FILE1_UUID);
    assertThat(fileSourceDto.getRevision()).isEqualTo("rev-1");
    verify(fileSourceDataWarnings).commitWarnings();
}
Also used : FileSourceDto(org.sonar.db.source.FileSourceDto) TestComputationStepContext(org.sonar.ce.task.step.TestComputationStepContext) Changeset(org.sonar.ce.task.projectanalysis.scm.Changeset) BaseStepTest(org.sonar.ce.task.projectanalysis.step.BaseStepTest) Test(org.junit.Test)

Aggregations

Changeset (org.sonar.ce.task.projectanalysis.scm.Changeset)15 Test (org.junit.Test)10 BaseStepTest (org.sonar.ce.task.projectanalysis.step.BaseStepTest)4 TestComputationStepContext (org.sonar.ce.task.step.TestComputationStepContext)4 FileSourceDto (org.sonar.db.source.FileSourceDto)4 DefaultIssue (org.sonar.core.issue.DefaultIssue)3 DbFileSources (org.sonar.db.protobuf.DbFileSources)3 List (java.util.List)2 Consumer (java.util.function.Consumer)2 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)2 Before (org.junit.Before)2 Mockito.mock (org.mockito.Mockito.mock)2 Mockito.verify (org.mockito.Mockito.verify)2 Mockito.when (org.mockito.Mockito.when)2 Component (org.sonar.ce.task.projectanalysis.component.Component)2 ReportComponent (org.sonar.ce.task.projectanalysis.component.ReportComponent)2 ScmInfo (org.sonar.ce.task.projectanalysis.scm.ScmInfo)2 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 HashSet (java.util.HashSet)1