|
| 1 | +package io.quarkus.bot.it; |
| 2 | + |
| 3 | +import static io.quarkiverse.githubapp.testing.GitHubAppTesting.given; |
| 4 | +import static io.quarkus.bot.it.MockHelper.mockUser; |
| 5 | +import static org.mockito.Mockito.mock; |
| 6 | +import static org.mockito.Mockito.verify; |
| 7 | +import static org.mockito.Mockito.verifyNoMoreInteractions; |
| 8 | +import static org.mockito.Mockito.when; |
| 9 | + |
| 10 | +import java.util.Set; |
| 11 | + |
| 12 | +import org.junit.jupiter.api.Test; |
| 13 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 14 | +import org.kohsuke.github.GHEvent; |
| 15 | +import org.kohsuke.github.GHOrganization; |
| 16 | +import org.kohsuke.github.GHTeam; |
| 17 | +import org.kohsuke.github.GHUser; |
| 18 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 19 | + |
| 20 | +import io.quarkiverse.githubapp.testing.GitHubAppTest; |
| 21 | +import io.quarkiverse.githubapp.testing.dsl.GitHubMockSetupContext; |
| 22 | +import io.quarkus.cache.CacheInvalidateAll; |
| 23 | +import io.quarkus.test.junit.QuarkusTest; |
| 24 | + |
| 25 | +@QuarkusTest |
| 26 | +@GitHubAppTest |
| 27 | +@ExtendWith(MockitoExtension.class) |
| 28 | +public class CheckPullRequestSimilarGitHubHandleTest { |
| 29 | + |
| 30 | + private static final String CONFIG = """ |
| 31 | + features: [ CHECK_SIMILAR_GITHUB_HANDLES ] |
| 32 | + similarGitHubHandleCheck: |
| 33 | + org: quarkusio |
| 34 | + teams: [quarkus-admin, quarkus-push] |
| 35 | + """; |
| 36 | + |
| 37 | + private static final String EXPECTED_COMMENT = """ |
| 38 | + > [!WARNING] |
| 39 | + > We noticed that your GitHub handle is very similar to that of a team member. |
| 40 | + > |
| 41 | + > To avoid any confusion, we wanted to flag this so that reviewers can double-check. |
| 42 | + > No action is needed on your part. Thank you for your understanding! |
| 43 | +
|
| 44 | + /cc @gsmet |
| 45 | +
|
| 46 | + <sub>This message is automatically generated by a bot.</sub> |
| 47 | + <!-- Quarkus-Bot-Similar-GitHub-Handle -->"""; |
| 48 | + |
| 49 | + @CacheInvalidateAll(cacheName = "team-members-cache") |
| 50 | + void setupTeamMocks(GitHubMockSetupContext mocks) throws Exception { |
| 51 | + GHOrganization org = mock(GHOrganization.class); |
| 52 | + when(mocks.installationClient(13173124L).getOrganization("quarkusio")).thenReturn(org); |
| 53 | + |
| 54 | + GHTeam adminTeam = mock(GHTeam.class); |
| 55 | + GHTeam pushTeam = mock(GHTeam.class); |
| 56 | + when(org.getTeamBySlug("quarkus-admin")).thenReturn(adminTeam); |
| 57 | + when(org.getTeamBySlug("quarkus-push")).thenReturn(pushTeam); |
| 58 | + |
| 59 | + GHUser gsmet = mockUser("gsmet"); |
| 60 | + GHUser maxandersen = mockUser("maxandersen"); |
| 61 | + when(adminTeam.getMembers()).thenReturn(Set.of(gsmet, maxandersen)); |
| 62 | + when(pushTeam.getMembers()).thenReturn(Set.of(gsmet)); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + void similarHandleShouldPostComment() throws Exception { |
| 67 | + given().github(mocks -> { |
| 68 | + mocks.configFile("quarkus-github-bot.yml").fromString(CONFIG); |
| 69 | + setupTeamMocks(mocks); |
| 70 | + }) |
| 71 | + .when().payloadFromClasspath("/pullrequest-opened-similar-github-handle.json") |
| 72 | + .event(GHEvent.PULL_REQUEST) |
| 73 | + .then().github(mocks -> { |
| 74 | + verify(mocks.pullRequest(527350930)).comment(EXPECTED_COMMENT); |
| 75 | + }); |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + void exactTeamMemberHandleShouldNotPostComment() throws Exception { |
| 80 | + given().github(mocks -> { |
| 81 | + mocks.configFile("quarkus-github-bot.yml").fromString(CONFIG); |
| 82 | + setupTeamMocks(mocks); |
| 83 | + }) |
| 84 | + .when().payloadFromClasspath("/pullrequest-opened-team-member.json") |
| 85 | + .event(GHEvent.PULL_REQUEST) |
| 86 | + .then().github(mocks -> { |
| 87 | + verifyNoMoreInteractions(mocks.ghObjects()); |
| 88 | + }); |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + void unrelatedHandleShouldNotPostComment() throws Exception { |
| 93 | + given().github(mocks -> { |
| 94 | + mocks.configFile("quarkus-github-bot.yml").fromString(CONFIG); |
| 95 | + setupTeamMocks(mocks); |
| 96 | + }) |
| 97 | + // yrodiere is unrelated to any team member |
| 98 | + .when().payloadFromClasspath("/pullrequest-opened-title-ends-with-dot.json") |
| 99 | + .event(GHEvent.PULL_REQUEST) |
| 100 | + .then().github(mocks -> { |
| 101 | + verifyNoMoreInteractions(mocks.ghObjects()); |
| 102 | + }); |
| 103 | + } |
| 104 | + |
| 105 | + @Test |
| 106 | + void featureDisabledShouldNotPostComment() throws Exception { |
| 107 | + given().github(mocks -> { |
| 108 | + mocks.configFile("quarkus-github-bot.yml") |
| 109 | + .fromString("features: [ CHECK_EDITORIAL_RULES ]\n"); |
| 110 | + }) |
| 111 | + .when().payloadFromClasspath("/pullrequest-opened-similar-github-handle.json") |
| 112 | + .event(GHEvent.PULL_REQUEST) |
| 113 | + .then().github(mocks -> { |
| 114 | + verifyNoMoreInteractions(mocks.ghObjects()); |
| 115 | + }); |
| 116 | + } |
| 117 | +} |
0 commit comments