Skip to content

Commit

Permalink
#909: Use default branch as Pull Request base when target does not exist
Browse files Browse the repository at this point in the history
The resolution of Pull Request details currently uses the project
default branch when a pull request is analysed without specifying a
target branch. However, when a target branch is specified, the plugin
expects that branch to exist and throws an exception if the target
branch is not found. To overcome this, the target branch is being
resolved to the default branch where no target branch is specified or
the requested target branch does not exist. As the API used to gather
branch information does not return PULL_REQUEST details, the result of
this searching is that a pull request will either target a branch from
the main branch, or target the main branch, there is no further nesting
or pull requests and branches.
  • Loading branch information
mc1arke committed Aug 18, 2024
1 parent c9ff809 commit 69504e6
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 69504e6

Please sign in to comment.