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);
}
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);
}
}
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();
}
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();
}
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();
}
Aggregations