From 604e6f12790231546d496e6ad0da53569618078b Mon Sep 17 00:00:00 2001 From: Duarte Meneses Date: Fri, 13 Jan 2023 09:25:34 +0100 Subject: [PATCH] SONAR-18195 Missing SCM data might cause a NPE in the Compute Engine --- .../projectanalysis/source/NewLinesRepository.java | 2 +- .../source/NewLinesRepositoryTest.java | 14 +++++++++++++- 2 files changed, 14 insertions(+), 2 deletions(-) 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); } -- 2.39.5