44import io .quarkus .test .junit .QuarkusTest ;
55import org .junit .jupiter .api .BeforeAll ;
66import org .junit .jupiter .api .Test ;
7+ import org .kohsuke .github .GHCommit ;
8+ import org .kohsuke .github .GHCommitQueryBuilder ;
9+ import org .kohsuke .github .GHPullRequestCommitDetail ;
10+ import org .kohsuke .github .PagedIterable ;
11+ import org .kohsuke .github .PagedIterator ;
712import org .wildfly .bot .utils .TestConstants ;
813import org .wildfly .bot .utils .WildflyGitHubBotTesting ;
914import org .wildfly .bot .utils .mocking .Mockable ;
1015import org .wildfly .bot .utils .mocking .MockedGHPullRequest ;
1116import org .wildfly .bot .utils .testing .PullRequestJson ;
1217import org .wildfly .bot .utils .testing .internal .TestModel ;
1318
19+ import java .util .Set ;
20+
21+ import static org .mockito .ArgumentMatchers .nullable ;
22+ import static org .mockito .Mockito .any ;
23+ import static org .mockito .Mockito .anyInt ;
24+ import static org .mockito .Mockito .eq ;
25+ import static org .mockito .Mockito .mock ;
26+ import static org .mockito .Mockito .never ;
1427import static org .mockito .Mockito .times ;
1528import static org .mockito .Mockito .verify ;
1629import static org .mockito .Mockito .verifyNoMoreInteractions ;
30+ import static org .mockito .Mockito .when ;
1731
1832@ QuarkusTest
1933@ GitHubAppTest
@@ -81,4 +95,49 @@ void testSkippingFormatCheckOnDraft() throws Throwable {
8195 verifyNoMoreInteractions (mocks .pullRequest (pullRequestJson .id ()));
8296 });
8397 }
84- }
98+
99+ @ Test
100+ void testSkippingRulesOnIncorrectRebase () throws Throwable {
101+ final String duplicateSHA = "sha1" ;
102+ final String baseBranch = "main" ;
103+ pullRequestJson = TestModel .setPullRequestJsonBuilder (pullRequestJsonBuilder -> pullRequestJsonBuilder );
104+ mockedContext = MockedGHPullRequest .builder (pullRequestJson .id ());
105+ TestModel .given (
106+ mocks -> {
107+ WildflyGitHubBotTesting .mockRepo (mocks , wildflyConfigFile , pullRequestJson , mockedContext );
108+ GHCommit commit1 = mock (GHCommit .class );
109+ GHCommit commit2 = mock (GHCommit .class );
110+
111+ when (commit1 .getSHA1 ()).thenReturn (duplicateSHA );
112+ when (commit2 .getSHA1 ()).thenReturn ("sha2" );
113+
114+ // Create a fake PagedIterable for repository commits.
115+ Set <GHCommit > fakeRepoCommitSet = Set .of (commit1 , commit2 );
116+ PagedIterable <GHCommit > fakeRepoCommits = mock (PagedIterable .class );
117+ GHCommitQueryBuilder mockedQueryBuilder = mock (GHCommitQueryBuilder .class );
118+
119+ when (mocks .repository (TestConstants .TEST_REPO ).queryCommits ()).thenReturn (mockedQueryBuilder );
120+ when (mockedQueryBuilder .from (eq (baseBranch ))).thenReturn (mockedQueryBuilder );
121+ when (mockedQueryBuilder .pageSize (anyInt ())).thenReturn (mockedQueryBuilder );
122+ when (mockedQueryBuilder .list ()).thenReturn (fakeRepoCommits );
123+ when (fakeRepoCommits .toSet ()).thenReturn (fakeRepoCommitSet );
124+
125+ GHPullRequestCommitDetail fakePRCommit = mock (GHPullRequestCommitDetail .class );
126+ PagedIterator <GHPullRequestCommitDetail > mockIterator = mock (PagedIterator .class );
127+ PagedIterable <GHPullRequestCommitDetail > fakePRCommits = mock (PagedIterable .class );
128+
129+ when (fakePRCommit .getSha ()).thenReturn (duplicateSHA );
130+ when (mockIterator .hasNext ()).thenReturn (true , false );
131+ when (mockIterator .next ()).thenReturn (fakePRCommit );
132+ when (mocks .pullRequest (pullRequestJson .id ()).listCommits ()).thenReturn (fakePRCommits );
133+ when (fakePRCommits ._iterator (anyInt ())).thenReturn (mockIterator );
134+ })
135+ .pullRequestEvent (pullRequestJson )
136+ .then (mocks -> {
137+ verify (mocks .pullRequest (pullRequestJson .id ())).getBase ();
138+ verify (mocks .repository (TestConstants .TEST_REPO )).queryCommits ();
139+ verify (mocks .pullRequest (pullRequestJson .id ()), never ()).addLabels (nullable (String [].class ));
140+ verify (mocks .pullRequest (pullRequestJson .id ()), never ()).requestReviewers (any ());
141+ });
142+ }
143+ }
0 commit comments