Skip to content

Commit 125b49d

Browse files
marko-bekhtayrodiere
authored andcommitted
Add a way to ignore a check for a GitHub team
1 parent af500c7 commit 125b49d

File tree

6 files changed

+622
-15
lines changed

6 files changed

+622
-15
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,14 @@ jira:
6969
titlePattern: ".*\\bmaven\\b.*\\bplugin\\b.*" # will ignore build dependency upgrades i.e. maven plugin version upgrades.
7070
- user: all-contributors[bot]
7171
titlePattern: ".*"
72+
# ignore check rule can also be performed against a GH team within an org.
73+
# in such case the username is ignored and only team/title are considered:
74+
- team:
75+
# A team name within the organization:
76+
name: team-name
77+
# Organization name, i.e. hibernate
78+
organization: org-name
79+
titlePattern: ".*"
7280
# To skip commits that contain only irrelevant files for JIRA-related checks (commit includes JIRA issue key),
7381
# a list of ignored files rules can be configured:
7482
ignoreFiles:

src/main/java/org/hibernate/infra/bot/CheckPullRequestContributionRules.java

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@
3232
import org.kohsuke.github.GHPullRequest;
3333
import org.kohsuke.github.GHPullRequestCommitDetail;
3434
import org.kohsuke.github.GHRepository;
35+
import org.kohsuke.github.GHTeam;
3536
import org.kohsuke.github.GHUser;
37+
import org.kohsuke.github.GitHub;
3638

3739
public class CheckPullRequestContributionRules {
3840

@@ -58,35 +60,36 @@ void pullRequestChanged(
5860
@PullRequest.Opened @PullRequest.Reopened @PullRequest.Edited @PullRequest.Synchronize
5961
GHEventPayload.PullRequest payload,
6062
@ConfigFile("hibernate-github-bot.yml") RepositoryConfig repositoryConfig,
61-
@ConfigFile("PULL_REQUEST_TEMPLATE.md") String pullRequestTemplate) throws IOException {
62-
checkPullRequestContributionRules( payload.getRepository(), repositoryConfig, pullRequestTemplate, payload.getPullRequest() );
63+
@ConfigFile("PULL_REQUEST_TEMPLATE.md") String pullRequestTemplate,
64+
GitHub gitHub) throws IOException {
65+
checkPullRequestContributionRules( payload.getRepository(), gitHub, repositoryConfig, pullRequestTemplate, payload.getPullRequest() );
6366
}
6467

6568
void checkRunRequested(@CheckRun.Rerequested GHEventPayload.CheckRun payload,
6669
@ConfigFile("hibernate-github-bot.yml") RepositoryConfig repositoryConfig,
67-
@ConfigFile("PULL_REQUEST_TEMPLATE.md") String pullRequestTemplate) throws IOException {
70+
@ConfigFile("PULL_REQUEST_TEMPLATE.md") String pullRequestTemplate,
71+
GitHub gitHub) throws IOException {
6872
for ( GHPullRequest pullRequest : payload.getCheckRun().getPullRequests() ) {
69-
checkPullRequestContributionRules( payload.getRepository(), repositoryConfig, pullRequestTemplate, pullRequest );
73+
checkPullRequestContributionRules( payload.getRepository(), gitHub, repositoryConfig, pullRequestTemplate, pullRequest );
7074
}
7175
}
7276

7377
void checkSuiteRequested(@CheckSuite.Requested @CheckSuite.Rerequested GHEventPayload.CheckSuite payload,
7478
@ConfigFile("hibernate-github-bot.yml") RepositoryConfig repositoryConfig,
75-
@ConfigFile("PULL_REQUEST_TEMPLATE.md") String pullRequestTemplate) throws IOException {
79+
@ConfigFile("PULL_REQUEST_TEMPLATE.md") String pullRequestTemplate,
80+
GitHub gitHub) throws IOException {
7681
for ( GHPullRequest pullRequest : payload.getCheckSuite().getPullRequests() ) {
77-
checkPullRequestContributionRules( payload.getRepository(), repositoryConfig, pullRequestTemplate, pullRequest );
82+
checkPullRequestContributionRules( payload.getRepository(), gitHub, repositoryConfig, pullRequestTemplate, pullRequest );
7883
}
7984
}
8085

81-
private void checkPullRequestContributionRules(GHRepository repository, RepositoryConfig repositoryConfig,
82-
String pullRequestTemplate,
83-
GHPullRequest pullRequest)
84-
throws IOException {
86+
private void checkPullRequestContributionRules(GHRepository repository, GitHub gitHub, RepositoryConfig repositoryConfig,
87+
String pullRequestTemplate, GHPullRequest pullRequest) throws IOException {
8588
if ( !shouldCheck( repository, pullRequest ) ) {
8689
return;
8790
}
8891

89-
PullRequestCheckRunContext context = new PullRequestCheckRunContext( deploymentConfig, repository, repositoryConfig, pullRequest );
92+
PullRequestCheckRunContext context = new PullRequestCheckRunContext( deploymentConfig, gitHub, repository, repositoryConfig, pullRequest );
9093
List<PullRequestCheck> checks = createChecks( repositoryConfig, pullRequestTemplate );
9194
List<PullRequestCheckRunOutput> outputs = new ArrayList<>();
9295
for ( PullRequestCheck check : checks ) {
@@ -323,8 +326,21 @@ protected boolean shouldCheckPullRequest(PullRequestCheckRunContext context) thr
323326
GHUser author = context.pullRequest.getUser();
324327
String title = context.pullRequest.getTitle();
325328
for ( RepositoryConfig.IgnoreConfiguration ignore : ignoredPRConfigurations ) {
326-
if ( ignore.getUser().equals( author.getLogin() )
327-
&& ignore.getTitlePattern().matcher( title ).matches() ) {
329+
if ( ignore.getTeam().isPresent() ) {
330+
boolean userTeamCanSkip = false;
331+
var teamToFind = ignore.getTeam().get();
332+
GHTeam team = context.gitHub.getOrganization( teamToFind.getOrganization() ).getTeamByName( teamToFind.getName() );
333+
for ( GHUser user : team.listMembers() ) {
334+
if ( user.getLogin().equals( author.getLogin() ) ) {
335+
userTeamCanSkip = true;
336+
break;
337+
}
338+
}
339+
if ( userTeamCanSkip && ignore.getTitlePattern().matcher( title ).matches() ) {
340+
return false;
341+
}
342+
}
343+
else if ( ignore.getUser().equals( author.getLogin() ) && ignore.getTitlePattern().matcher( title ).matches() ) {
328344
return false;
329345
}
330346
}

src/main/java/org/hibernate/infra/bot/config/RepositoryConfig.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ public static class IgnoreConfiguration {
7575

7676
private String user;
7777
private Pattern titlePattern;
78+
private Optional<GitHubTeam> team = Optional.empty();
7879

7980
public String getUser() {
8081
return user;
@@ -91,6 +92,35 @@ public Pattern getTitlePattern() {
9192
public void setTitlePattern(String titlePattern) {
9293
this.titlePattern = Patterns.compile( titlePattern );
9394
}
95+
96+
public Optional<GitHubTeam> getTeam() {
97+
return team;
98+
}
99+
100+
public void setTeam(GitHubTeam team) {
101+
this.team = Optional.of( team );
102+
}
103+
104+
public static class GitHubTeam {
105+
private String name;
106+
private String organization;
107+
108+
public String getName() {
109+
return name;
110+
}
111+
112+
public void setName(String name) {
113+
this.name = name;
114+
}
115+
116+
public String getOrganization() {
117+
return organization;
118+
}
119+
120+
public void setOrganization(String organization) {
121+
this.organization = organization;
122+
}
123+
}
94124
}
95125

96126
public static class Develocity {

src/main/java/org/hibernate/infra/bot/prcheck/PullRequestCheckRunContext.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,20 @@
55

66
import org.kohsuke.github.GHPullRequest;
77
import org.kohsuke.github.GHRepository;
8+
import org.kohsuke.github.GitHub;
89

910
public final class PullRequestCheckRunContext {
1011

1112
public final DeploymentConfig deploymentConfig;
13+
public final GitHub gitHub;
1214
public final GHRepository repository;
1315
public final GHPullRequest pullRequest;
1416
public final RepositoryConfig repositoryConfig;
1517

16-
public PullRequestCheckRunContext(DeploymentConfig deploymentConfig, GHRepository repository,
17-
RepositoryConfig repositoryConfig, GHPullRequest pullRequest) {
18+
public PullRequestCheckRunContext(DeploymentConfig deploymentConfig, GitHub gitHub, GHRepository repository,
19+
RepositoryConfig repositoryConfig, GHPullRequest pullRequest) {
1820
this.deploymentConfig = deploymentConfig;
21+
this.gitHub = gitHub;
1922
this.repository = repository;
2023
this.repositoryConfig = repositoryConfig;
2124
this.pullRequest = pullRequest;

src/test/java/org/hibernate/infra/bot/tests/CheckPullRequestContributionRulesLicenseTest.java

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@
22

33
import static io.quarkiverse.githubapp.testing.GitHubAppTesting.given;
44
import static org.assertj.core.api.Assertions.assertThat;
5+
import static org.hibernate.infra.bot.tests.PullRequestMockHelper.mockPagedIterable;
6+
import static org.mockito.ArgumentMatchers.anyString;
57
import static org.mockito.Mockito.mock;
68
import static org.mockito.Mockito.verify;
79
import static org.mockito.Mockito.verifyNoMoreInteractions;
810
import static org.mockito.Mockito.when;
911

1012
import java.io.IOException;
13+
import java.util.List;
1114

1215
import org.junit.jupiter.api.Test;
1316
import org.junit.jupiter.api.extension.ExtendWith;
@@ -17,9 +20,15 @@
1720
import org.kohsuke.github.GHCheckRun;
1821
import org.kohsuke.github.GHCheckRunBuilder;
1922
import org.kohsuke.github.GHEvent;
23+
import org.kohsuke.github.GHOrganization;
2024
import org.kohsuke.github.GHPullRequest;
2125
import org.kohsuke.github.GHRepository;
26+
import org.kohsuke.github.GHTeam;
27+
import org.kohsuke.github.GHUser;
28+
import org.kohsuke.github.GitHub;
29+
import org.kohsuke.github.PagedIterable;
2230
import org.mockito.ArgumentCaptor;
31+
import org.mockito.ArgumentMatchers;
2332
import org.mockito.junit.jupiter.MockitoExtension;
2433

2534
@QuarkusTest
@@ -208,4 +217,67 @@ void licenseCheckIgnored() throws IOException {
208217
} );
209218
}
210219

220+
@Test
221+
void licenseCheckIgnoredByTeam() throws IOException {
222+
long repoId = 344815557L;
223+
long prId = 585627026L;
224+
given()
225+
.github( mocks -> {
226+
mocks.configFile( "hibernate-github-bot.yml" )
227+
.fromString( """
228+
jira:
229+
projectKey: "HSEARCH"
230+
# We also ignore jira keys check as dependabot PRs won't have them anyways:
231+
ignore:
232+
- user: dependabot[bot]
233+
titlePattern: ".*\\\\bmaven\\\\b.*\\\\bplugin\\\\b.*"
234+
licenseAgreement:
235+
enabled: true
236+
ignore:
237+
- team:
238+
name: skippable
239+
organization: hibernate
240+
titlePattern: ".*+"
241+
""" );
242+
mocks.configFile( "PULL_REQUEST_TEMPLATE.md" )
243+
.fromString( """
244+
[Please describe here what your change is about]
245+
246+
<!--
247+
Please read and do not remove the following lines:
248+
-->
249+
----------------------
250+
By submitting this pull request, I confirm that my contribution is made under the terms of the [Apache 2.0 license](https://www.apache.org/licenses/LICENSE-2.0.txt)
251+
and can be relicensed under the terms of the [LGPL v2.1 license](https://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt) in the future at the maintainers' discretion.
252+
For more information on licensing, please check [here](https://github.com/hibernate/hibernate-search/blob/main/CONTRIBUTING.md#legal).
253+
254+
----------------------
255+
""" );
256+
257+
GHRepository repoMock = mocks.repository( "yrodiere/hibernate-github-bot-playground" );
258+
when( repoMock.getId() ).thenReturn( repoId );
259+
GitHub gitHubMock = mocks.installationClient( 15144501L ); // see the submitted JSON file for this value
260+
GHOrganization organization = mock( GHOrganization.class );
261+
GHTeam team = mock( GHTeam.class );
262+
GHUser user = mock( GHUser.class );
263+
when( user.getLogin() ).thenReturn( "dependabot[bot]" );
264+
PagedIterable<GHUser> page = mockPagedIterable( List.of( user ) );
265+
when( team.listMembers() ).thenReturn( page );
266+
when( organization.getTeamByName( ArgumentMatchers.eq( "skippable" ) ) ).thenReturn( team );
267+
when( gitHubMock.getOrganization( ( anyString() ) ) ).thenReturn( organization );
268+
269+
PullRequestMockHelper.start( mocks, prId, repoMock )
270+
.noComments();
271+
272+
mockCheckRuns( repoMock, "6e9f11a1e2946b207c6eb245ec942f2b5a3ea156" );
273+
} )
274+
.when()
275+
.payloadFromClasspath( "/pullrequest-opened-hsearch-1111-skippable-team-member.json" )
276+
.event( GHEvent.PULL_REQUEST )
277+
.then()
278+
.github( mocks -> {
279+
verifyNoMoreInteractions( mocks.ghObjects() );
280+
} );
281+
}
282+
211283
}

0 commit comments

Comments
 (0)