Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#909: Use default branch as Pull Request base when target does not exist #15

Merged
merged 1 commit into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading