From 5377194709960576b1367a220c6ba20ce6f41ef0 Mon Sep 17 00:00:00 2001 From: Duarte Meneses Date: Thu, 23 Sep 2021 13:40:48 -0500 Subject: [PATCH] SONAR-15424 Compute Engine fails with NPE when analysing PR without main branch --- .../org/sonar/scanner/scan/branch/ProjectBranches.java | 7 ++++++- .../org/sonar/scanner/scan/branch/ProjectBranchesTest.java | 1 + 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/branch/ProjectBranches.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/branch/ProjectBranches.java index 2c49da58963..9f53d9ad8a2 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/branch/ProjectBranches.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/branch/ProjectBranches.java @@ -22,6 +22,7 @@ package org.sonar.scanner.scan.branch; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Objects; import javax.annotation.CheckForNull; import javax.annotation.concurrent.Immutable; @@ -30,6 +31,9 @@ import javax.annotation.concurrent.Immutable; */ @Immutable public class ProjectBranches { + // matches server-side default when creating a project. See BranchDto#DEFAULT_MAIN_BRANCH_NAME + private static final String DEFAULT_MAIN_BRANCH_NAME = "master"; + private final Map branches; private final String defaultBranchName; @@ -43,7 +47,8 @@ public class ProjectBranches { mainBranchName = branchName; } } - this.defaultBranchName = mainBranchName; + + this.defaultBranchName = Objects.requireNonNullElse(mainBranchName, DEFAULT_MAIN_BRANCH_NAME); } @CheckForNull diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/branch/ProjectBranchesTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/branch/ProjectBranchesTest.java index d9f07a3df16..42daac13391 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/branch/ProjectBranchesTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/branch/ProjectBranchesTest.java @@ -70,5 +70,6 @@ public class ProjectBranchesTest { public void isEmpty() { assertThat(underTest.isEmpty()).isFalse(); assertThat(new ProjectBranches(Collections.emptyList()).isEmpty()).isTrue(); + assertThat(new ProjectBranches(Collections.emptyList()).defaultBranchName()).isEqualTo("master"); } } -- 2.39.5