diff --git a/src/main/java/com/github/mc1arke/sonarqube/plugin/scanner/BranchConfigurationFactory.java b/src/main/java/com/github/mc1arke/sonarqube/plugin/scanner/BranchConfigurationFactory.java index d5ce9868..e73a4428 100644 --- a/src/main/java/com/github/mc1arke/sonarqube/plugin/scanner/BranchConfigurationFactory.java +++ b/src/main/java/com/github/mc1arke/sonarqube/plugin/scanner/BranchConfigurationFactory.java @@ -41,7 +41,7 @@ public BranchConfiguration createBranchConfiguration(String branchName, ProjectB public BranchConfiguration createPullRequestConfiguration(String pullRequestKey, String pullRequestBranch, String pullRequestBase, ProjectBranches branches) { String targetBranch = Optional.ofNullable(pullRequestBase).orElse(branches.defaultBranchName()); - String referenceBranch = findReferenceBranch(targetBranch, branches); + String referenceBranch = findReferenceBranch(Optional.ofNullable(branches.get(targetBranch)).map(BranchInfo::name).orElse(branches.defaultBranchName()), branches); return new CommunityBranchConfiguration(pullRequestBranch, BranchType.PULL_REQUEST, referenceBranch, targetBranch, pullRequestKey); } @@ -58,6 +58,7 @@ private static String findReferenceBranch(String targetBranch, ProjectBranches b throw MessageException.of(String.format("The branch '%s' of type %s does not have a target", target.name(), target.type())); } - return findReferenceBranch(targetBranchTarget, branches); + return Optional.ofNullable(branches.get(targetBranchTarget)).map(BranchInfo::name) + .orElseThrow(() -> MessageException.of(String.format("The branch '%s' of type %s has a target '%s' which does not exist", target.name(), target.type(), targetBranchTarget))); } } diff --git a/src/test/java/com/github/mc1arke/sonarqube/plugin/scanner/BranchConfigurationFactoryTest.java b/src/test/java/com/github/mc1arke/sonarqube/plugin/scanner/BranchConfigurationFactoryTest.java index 465dbf6a..165e0a87 100644 --- a/src/test/java/com/github/mc1arke/sonarqube/plugin/scanner/BranchConfigurationFactoryTest.java +++ b/src/test/java/com/github/mc1arke/sonarqube/plugin/scanner/BranchConfigurationFactoryTest.java @@ -118,6 +118,20 @@ void shouldReturnPullRequestWithTargetOfDefaultBranchIfTargetNotSpecifiedAndDefa assertThat(actual).usingRecursiveComparison().isEqualTo(new CommunityBranchConfiguration("source", BranchType.PULL_REQUEST, "defaultBranch", "defaultBranch", "key")); } + @Test + void shouldReturnPullRequestWithTargetOfDefaultBranchIfTargetDoesNotExistAndDefaultExists() { + ProjectBranches projectBranches = mock(); + when(projectBranches.isEmpty()).thenReturn(false); + when(projectBranches.defaultBranchName()).thenReturn("defaultBranch"); + BranchInfo branchInfo = new BranchInfo("defaultBranch", BranchType.BRANCH, true, null); + when(projectBranches.get("defaultBranch")).thenReturn(branchInfo); + + BranchConfigurationFactory underTest = new BranchConfigurationFactory(); + BranchConfiguration actual = underTest.createPullRequestConfiguration("key", "source", "missing", projectBranches); + + assertThat(actual).usingRecursiveComparison().isEqualTo(new CommunityBranchConfiguration("source", BranchType.PULL_REQUEST, "defaultBranch", "missing", "key")); + } + @Test void shouldReturnPullRequestWithTargetOfTargetAsReferenceIfTargetBranchExists() { ProjectBranches projectBranches = mock(ProjectBranches.class);