From: Duarte Meneses Date: Fri, 13 Jan 2023 08:25:34 +0000 (+0100) Subject: SONAR-18195 Missing SCM data might cause a NPE in the Compute Engine X-Git-Tag: 9.9.0.65466~47 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=604e6f12790231546d496e6ad0da53569618078b;p=sonarqube.git SONAR-18195 Missing SCM data might cause a NPE in the Compute Engine --- diff --git a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/source/NewLinesRepository.java b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/source/NewLinesRepository.java index f0fc255cd38..3d6418fc752 100644 --- a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/source/NewLinesRepository.java +++ b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/source/NewLinesRepository.java @@ -81,7 +81,7 @@ public class NewLinesRepository { // 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)) { + if (allChangesets[i] != null && isLineInPeriod(allChangesets[i].getDate(), referenceDate)) { lines.add(i + 1); } } diff --git a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/source/NewLinesRepositoryTest.java b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/source/NewLinesRepositoryTest.java index a0a33ca30be..9655320f629 100644 --- a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/source/NewLinesRepositoryTest.java +++ b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/source/NewLinesRepositoryTest.java @@ -91,6 +91,18 @@ public class NewLinesRepositoryTest { assertThat(repository.newLinesAvailable()).isTrue(); } + @Test + public void compute_new_lines_using_scm_info_for_period_with_missing_line() { + periodHolder.setPeriod(new Period("", null, 1000L)); + scmInfoRepository.setScmInfo(FILE.getReportAttributes().getRef(), createChangesets(1100L, 900L, null, 800L)); + + Optional> newLines = repository.getNewLines(FILE); + + assertThat(newLines).isPresent(); + assertThat(newLines.get()).containsOnly(1); + assertThat(repository.newLinesAvailable()).isTrue(); + } + @Test public void compute_new_lines_using_scm_info_for_pullrequest() { periodHolder.setPeriod(null); @@ -137,7 +149,7 @@ public class NewLinesRepositoryTest { private Changeset[] createChangesets(Long... dates) { return Arrays.stream(dates) - .map(l -> Changeset.newChangesetBuilder().setDate(l).build()) + .map(l -> l == null ? null : Changeset.newChangesetBuilder().setDate(l).build()) .toArray(Changeset[]::new); }