]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-18195 Missing SCM data might cause a NPE in the Compute Engine
authorDuarte Meneses <duarte.meneses@sonarsource.com>
Fri, 13 Jan 2023 08:25:34 +0000 (09:25 +0100)
committersonartech <sonartech@sonarsource.com>
Fri, 13 Jan 2023 20:02:46 +0000 (20:02 +0000)
server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/source/NewLinesRepository.java
server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/source/NewLinesRepositoryTest.java

index f0fc255cd388843ecf3463b43cdd87dcb7bdc6f8..3d6418fc752ab99adf4be914b55be7caf1d22657 100644 (file)
@@ -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);
       }
     }
index a0a33ca30be419a9e6cdd3acbf0d9ee6a6de53ce..9655320f62961b1fa785eed717d155c6b463b8c5 100644 (file)
@@ -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<Set<Integer>> 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);
   }