Skip to content

Commit ce7c23d

Browse files
Shaoranlaospfeuffer
authored andcommitted
Fix usage of BranchResolver on Unsupported Repo Types
This fixes a problem on SVN Repos that throws an error to the client on commits (because BranchResolver is not working on SVN Repos).
1 parent e63b35e commit ce7c23d

File tree

3 files changed

+15
-4
lines changed

3 files changed

+15
-4
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
- type: fixed
2+
description: Fix failing post-commit Hook on SVN Repository, because it does not support the BranchResolver (call leads to visible error on client side)

src/main/java/com/cloudogu/scm/review/pullrequest/service/PullRequestSuggestionService.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,14 @@ public void onBranchUpdated(PostReceiveRepositoryHookEvent event) {
7070
Repository repository = event.getRepository();
7171

7272
boolean isNotSupported = !pullRequestService.supportsPullRequests(repository);
73-
boolean isSingleOrNoBranch = branchResolver.getAll(repository).size() < 2;
73+
if (isNotSupported) {
74+
return;
75+
}
7476

75-
if (isNotSupported || isSingleOrNoBranch) {
77+
// check the branches only if PullRequests support (abort before branch check)
78+
// because BranchResolver throws an error to the client if the repository type does not support branches
79+
boolean isSingleOrNoBranch = branchResolver.getAll(repository).size() < 2;
80+
if (isSingleOrNoBranch) {
7681
return;
7782
}
7883

src/test/java/com/cloudogu/scm/review/pullrequest/service/PullRequestSuggestionServiceTest.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import java.util.List;
4848

4949
import static org.assertj.core.api.Assertions.assertThat;
50+
import static org.mockito.Mockito.lenient;
5051
import static org.mockito.Mockito.when;
5152

5253
@ExtendWith({MockitoExtension.class, ShiroExtension.class})
@@ -78,10 +79,10 @@ class OnBranchPushed {
7879
@BeforeEach
7980
void setup() {
8081
when(pullRequestService.supportsPullRequests(repository)).thenReturn(true);
81-
when(hookContext.getBranchProvider().getCreatedOrModified()).thenReturn(
82+
lenient().when(hookContext.getBranchProvider().getCreatedOrModified()).thenReturn(
8283
List.of("feature")
8384
);
84-
when(branchResolver.getAll(repository)).thenReturn(List.of(
85+
lenient().when(branchResolver.getAll(repository)).thenReturn(List.of(
8586
Branch.defaultBranch(
8687
"main",
8788
"revisionOnMain",
@@ -108,7 +109,10 @@ private PostReceiveRepositoryHookEvent createPostReceiveRepositoryHookEvent() {
108109
@Test
109110
void shouldIgnorePushBecauseRepositoryDoesNotSupportPullRequests() {
110111
when(pullRequestService.supportsPullRequests(repository)).thenReturn(false);
112+
lenient().when(branchResolver.getAll(repository)).thenThrow(InternalRepositoryException.class);
113+
111114
suggestionService.onBranchUpdated(createPostReceiveRepositoryHookEvent());
115+
112116
assertThat(suggestionService.getPushEntries()).isEmpty();
113117
}
114118

0 commit comments

Comments
 (0)