]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-15424 Compute Engine fails with NPE when analysing PR without main branch
authorDuarte Meneses <duarte.meneses@sonarsource.com>
Thu, 23 Sep 2021 18:40:48 +0000 (13:40 -0500)
committersonartech <sonartech@sonarsource.com>
Fri, 24 Sep 2021 20:03:10 +0000 (20:03 +0000)
sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/branch/ProjectBranches.java
sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/branch/ProjectBranchesTest.java

index 2c49da589639e83b065471bade48010762ab1d33..9f53d9ad8a203f9d90927a3dded47fa29c33e80d 100644 (file)
@@ -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<String, BranchInfo> 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
index d9f07a3df163f212e75f9d4eade1a8700e30d263..42daac1339158f5fe11fea370ceae8ff102f121c 100644 (file)
@@ -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");
   }
 }