Skip to content

Commit c854fb1

Browse files
committed
Add ignore configuration to the license and task checks
similar to the one we currently have for the Jira check. Dependabot PRs won't have a license agreement, but then they do not need it. Same with the tasks. If we'd need tasks for dependabot PRs the list will be most likely different from the "default" one.
1 parent 027230c commit c854fb1

File tree

5 files changed

+185
-38
lines changed

5 files changed

+185
-38
lines changed

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

+50-36
Original file line numberDiff line numberDiff line change
@@ -158,9 +158,8 @@ private List<PullRequestCheck> createChecks(RepositoryConfig repositoryConfig, S
158158
&& repositoryConfig.licenseAgreement != null
159159
&& repositoryConfig.licenseAgreement.getEnabled().orElse( Boolean.FALSE ) ) {
160160
Matcher matcher = repositoryConfig.licenseAgreement.getPullRequestTemplatePattern().matcher( pullRequestTemplate );
161-
if ( matcher.matches()
162-
&& matcher.groupCount() == 1 ) {
163-
checks.add( new LicenseCheck( matcher.group( 1 ).trim() ) );
161+
if ( matcher.matches() && matcher.groupCount() == 1 ) {
162+
checks.add( new LicenseCheck( matcher.group( 1 ).trim(), repositoryConfig.licenseAgreement.getIgnore() ) );
164163
}
165164
else {
166165
throw new IllegalArgumentException( "Misconfigured license agreement check. Pattern should contain exactly 1 match group. Pattern: %s. Fetched Pull Request template: %s".formatted( repositoryConfig.licenseAgreement.getPullRequestTemplatePattern(), pullRequestTemplate ) );
@@ -169,7 +168,7 @@ private List<PullRequestCheck> createChecks(RepositoryConfig repositoryConfig, S
169168

170169
if ( repositoryConfig != null && repositoryConfig.pullRequestTasks != null
171170
&& repositoryConfig.pullRequestTasks.getEnabled().orElse( Boolean.FALSE ) ) {
172-
checks.add( new TasksCompletedCheck() );
171+
checks.add( new TasksCompletedCheck(repositoryConfig.pullRequestTasks.getIgnore() ) );
173172
}
174173

175174
return checks;
@@ -192,33 +191,25 @@ public void perform(PullRequestCheckRunContext context, PullRequestCheckRunOutpu
192191
}
193192
}
194193

195-
static class JiraIssuesCheck extends PullRequestCheck {
194+
static class JiraIssuesCheck extends IgnorablePullRequestCheck {
196195

197196
private final Pattern issueKeyPattern;
198197

199198
private final Integer issueLinksLimit;
200199

201-
private final List<RepositoryConfig.IgnoreConfiguration> ignoredPRConfigurations;
202200
private final GlobMatcher ignoredFilesMatcher;
203201

204202
JiraIssuesCheck(Pattern issueKeyPattern, Integer issueLinksLimit,
205203
List<RepositoryConfig.IgnoreConfiguration> ignoredPRConfigurations,
206204
List<String> ignoreFilePatterns) {
207-
super( "Contribution — JIRA issues" );
205+
super( "Contribution — JIRA issues", ignoredPRConfigurations );
208206
this.issueKeyPattern = issueKeyPattern;
209207
this.issueLinksLimit = issueLinksLimit;
210-
this.ignoredPRConfigurations = ignoredPRConfigurations;
211208
this.ignoredFilesMatcher = new GlobMatcher( ignoreFilePatterns );
212209
}
213210

214211
@Override
215-
public void perform(PullRequestCheckRunContext context, PullRequestCheckRunOutput output) throws IOException {
216-
if ( !shouldCheckPullRequest( context ) ) {
217-
// Means we have an ignore rule configured that matches our pull request.
218-
// No need to check anything else.
219-
return;
220-
}
221-
212+
public void doPerform(PullRequestCheckRunContext context, PullRequestCheckRunOutput output) throws IOException {
222213
String title = context.pullRequest.getTitle();
223214
String body = context.pullRequest.getBody();
224215

@@ -263,32 +254,19 @@ public void perform(PullRequestCheckRunContext context, PullRequestCheckRunOutpu
263254
}
264255
}
265256
}
266-
267-
private boolean shouldCheckPullRequest(PullRequestCheckRunContext context) throws IOException {
268-
GHUser author = context.pullRequest.getUser();
269-
String title = context.pullRequest.getTitle();
270-
for ( RepositoryConfig.IgnoreConfiguration ignore : ignoredPRConfigurations ) {
271-
if ( ignore.getUser().equals( author.getLogin() )
272-
&& ignore.getTitlePattern().matcher( title ).matches() ) {
273-
return false;
274-
}
275-
}
276-
277-
return true;
278-
}
279257
}
280258

281-
static class LicenseCheck extends PullRequestCheck {
259+
static class LicenseCheck extends IgnorablePullRequestCheck {
282260

283261
private final String agreementText;
284262

285-
protected LicenseCheck(String agreementText) {
286-
super( "Contribution — License agreement" );
263+
protected LicenseCheck(String agreementText, List<RepositoryConfig.IgnoreConfiguration> ignoredPRConfigurations) {
264+
super( "Contribution — License agreement", ignoredPRConfigurations );
287265
this.agreementText = Patterns.sanitizeNewLines( agreementText );
288266
}
289267

290268
@Override
291-
public void perform(PullRequestCheckRunContext context, PullRequestCheckRunOutput output) {
269+
public void doPerform(PullRequestCheckRunContext context, PullRequestCheckRunOutput output) {
292270
String body = Patterns.sanitizeNewLines( context.pullRequest.getBody() );
293271
PullRequestCheckRunRule rule = output.rule( "The pull request description must contain the license agreement text." );
294272
if ( body != null && body.contains( agreementText ) ) {
@@ -305,18 +283,54 @@ public void perform(PullRequestCheckRunContext context, PullRequestCheckRunOutpu
305283
}
306284
}
307285

308-
static class TasksCompletedCheck extends PullRequestCheck {
286+
static class TasksCompletedCheck extends IgnorablePullRequestCheck {
309287

310-
protected TasksCompletedCheck() {
311-
super( "Contribution — Review tasks" );
288+
protected TasksCompletedCheck(List<RepositoryConfig.IgnoreConfiguration> ignoredPRConfigurations) {
289+
super( "Contribution — Review tasks", ignoredPRConfigurations );
312290
}
313291

314292
@Override
315-
public void perform(PullRequestCheckRunContext context, PullRequestCheckRunOutput output) {
293+
public void doPerform(PullRequestCheckRunContext context, PullRequestCheckRunOutput output) {
316294
String body = context.pullRequest.getBody();
317295
output.rule( "All pull request tasks should be completed." )
318296
.result( !EditPullRequestBodyAddTaskList.containsUnfinishedTasks( body ) );
319297
}
320298
}
321299

300+
static abstract class IgnorablePullRequestCheck extends PullRequestCheck {
301+
302+
private final List<RepositoryConfig.IgnoreConfiguration> ignoredPRConfigurations;
303+
304+
protected IgnorablePullRequestCheck(String title, List<RepositoryConfig.IgnoreConfiguration> ignoredPRConfigurations) {
305+
super( title );
306+
this.ignoredPRConfigurations = ignoredPRConfigurations;
307+
}
308+
309+
@Override
310+
public final void perform(PullRequestCheckRunContext context, PullRequestCheckRunOutput output) throws IOException {
311+
if ( !shouldCheckPullRequest( context ) ) {
312+
// Means we have an ignore rule configured that matches our pull request.
313+
// No need to check anything else.
314+
return;
315+
}
316+
317+
doPerform( context, output );
318+
}
319+
320+
abstract void doPerform(PullRequestCheckRunContext context, PullRequestCheckRunOutput output) throws IOException;
321+
322+
protected boolean shouldCheckPullRequest(PullRequestCheckRunContext context) throws IOException {
323+
GHUser author = context.pullRequest.getUser();
324+
String title = context.pullRequest.getTitle();
325+
for ( RepositoryConfig.IgnoreConfiguration ignore : ignoredPRConfigurations ) {
326+
if ( ignore.getUser().equals( author.getLogin() )
327+
&& ignore.getTitlePattern().matcher( title ).matches() ) {
328+
return false;
329+
}
330+
}
331+
332+
return true;
333+
}
334+
}
335+
322336
}

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

+11-2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.kohsuke.github.GHPullRequest;
2828
import org.kohsuke.github.GHPullRequestCommitDetail;
2929
import org.kohsuke.github.GHRepository;
30+
import org.kohsuke.github.GHUser;
3031

3132
public class EditPullRequestBodyAddTaskList {
3233
private static final Logger LOG = Logger.getLogger( EditPullRequestBodyAddTaskList.class );
@@ -58,7 +59,7 @@ private void addUpdateTaskList(
5859
return;
5960
}
6061

61-
if ( !shouldCheck( repository, pullRequest ) ) {
62+
if ( !shouldCheck( repository, pullRequest, repositoryConfig.pullRequestTasks.getIgnore() ) ) {
6263
return;
6364
}
6465

@@ -175,7 +176,15 @@ private static String currentTaskBody(String originalBody) {
175176
}
176177
}
177178

178-
private boolean shouldCheck(GHRepository repository, GHPullRequest pullRequest) {
179+
private boolean shouldCheck(GHRepository repository, GHPullRequest pullRequest, List<RepositoryConfig.IgnoreConfiguration> ignoredPRConfigurations) throws IOException {
180+
GHUser author = pullRequest.getUser();
181+
String title = pullRequest.getTitle();
182+
for ( RepositoryConfig.IgnoreConfiguration ignore : ignoredPRConfigurations ) {
183+
if ( ignore.getUser().equals( author.getLogin() )
184+
&& ignore.getTitlePattern().matcher( title ).matches() ) {
185+
return false;
186+
}
187+
}
179188
return !GHIssueState.CLOSED.equals( pullRequest.getState() )
180189
&& repository.getId() == pullRequest.getBase().getRepository().getId();
181190
}

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

+18
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ public void setPattern(String pattern) {
138138
public static class LicenseAgreement {
139139
private Optional<Boolean> enabled = Optional.empty();
140140
private Pattern pullRequestTemplatePattern = Patterns.compile( ".+([-]{22}.+[-]{22}).++" );
141+
private List<IgnoreConfiguration> ignore = Collections.emptyList();
141142

142143
public Optional<Boolean> getEnabled() {
143144
return enabled;
@@ -154,12 +155,21 @@ public Pattern getPullRequestTemplatePattern() {
154155
public void setPullRequestTemplatePattern(String pullRequestTemplatePattern) {
155156
this.pullRequestTemplatePattern = Patterns.compile( pullRequestTemplatePattern );
156157
}
158+
159+
public List<IgnoreConfiguration> getIgnore() {
160+
return ignore;
161+
}
162+
163+
public void setIgnore(List<IgnoreConfiguration> ignore) {
164+
this.ignore = ignore;
165+
}
157166
}
158167

159168
public static class TaskList {
160169
private static final String DEFAULT_TASKS_CATEGORY = "default";
161170
private Optional<Boolean> enabled = Optional.empty();
162171
private Map<String, List<String>> tasks = new HashMap<>();
172+
private List<IgnoreConfiguration> ignore = Collections.emptyList();
163173

164174
public Optional<Boolean> getEnabled() {
165175
return enabled;
@@ -180,5 +190,13 @@ public void setTasks(Map<String, List<String>> tasks) {
180190
public List<String> defaultTasks() {
181191
return tasks.getOrDefault( DEFAULT_TASKS_CATEGORY, List.of() );
182192
}
193+
194+
public List<IgnoreConfiguration> getIgnore() {
195+
return ignore;
196+
}
197+
198+
public void setIgnore(List<IgnoreConfiguration> ignore) {
199+
this.ignore = ignore;
200+
}
183201
}
184202
}

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

+52
Original file line numberDiff line numberDiff line change
@@ -156,4 +156,56 @@ void bodyContainsLicenseAgreement() throws IOException {
156156
} );
157157
}
158158

159+
@Test
160+
void licenseCheckIgnored() throws IOException {
161+
long repoId = 344815557L;
162+
long prId = 585627026L;
163+
given()
164+
.github( mocks -> {
165+
mocks.configFile("hibernate-github-bot.yml")
166+
.fromString( """
167+
jira:
168+
projectKey: "HSEARCH"
169+
# We also ignore jira keys check as dependabot PRs won't have them anyways:
170+
ignore:
171+
- user: dependabot[bot]
172+
titlePattern: ".*\\\\bmaven\\\\b.*\\\\bplugin\\\\b.*"
173+
licenseAgreement:
174+
enabled: true
175+
ignore:
176+
- user: dependabot[bot]
177+
titlePattern: ".*\\\\bmaven\\\\b.*\\\\bplugin\\\\b.*"
178+
""" );
179+
mocks.configFile("PULL_REQUEST_TEMPLATE.md")
180+
.fromString( """
181+
[Please describe here what your change is about]
182+
183+
<!--
184+
Please read and do not remove the following lines:
185+
-->
186+
----------------------
187+
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)
188+
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.
189+
For more information on licensing, please check [here](https://github.com/hibernate/hibernate-search/blob/main/CONTRIBUTING.md#legal).
190+
191+
----------------------
192+
""" );
193+
194+
GHRepository repoMock = mocks.repository( "yrodiere/hibernate-github-bot-playground" );
195+
when( repoMock.getId() ).thenReturn( repoId );
196+
197+
PullRequestMockHelper.start( mocks, prId, repoMock )
198+
.noComments();
199+
200+
mockCheckRuns( repoMock, "6e9f11a1e2946b207c6eb245ec942f2b5a3ea156" );
201+
} )
202+
.when()
203+
.payloadFromClasspath( "/pullrequest-opened-hsearch-1111-dependabot-upgrades-build-dependencies.json" )
204+
.event( GHEvent.PULL_REQUEST )
205+
.then()
206+
.github( mocks -> {
207+
verifyNoMoreInteractions( mocks.ghObjects() );
208+
} );
209+
}
210+
159211
}

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

+54
Original file line numberDiff line numberDiff line change
@@ -213,4 +213,58 @@ void bodyContainsTasks() throws IOException {
213213
} );
214214
}
215215

216+
@Test
217+
void tasksCheckIgnored() throws IOException {
218+
long repoId = 344815557L;
219+
long prId = 585627026L;
220+
given()
221+
.github( mocks -> {
222+
mocks.configFile("hibernate-github-bot.yml")
223+
.fromString( """
224+
jira:
225+
projectKey: "HSEARCH"
226+
# We also ignore jira keys check as dependabot PRs won't have them anyways:
227+
ignore:
228+
- user: dependabot[bot]
229+
titlePattern: ".*\\\\bmaven\\\\b.*\\\\bplugin\\\\b.*"
230+
pullRequestTasks:
231+
enabled: true
232+
tasks:
233+
default:
234+
- task1
235+
- task2
236+
- task3
237+
- task4
238+
bug:
239+
- bug task1
240+
- bug task2
241+
- bug task3
242+
- bug task4
243+
improvement:
244+
- improvement task1
245+
- improvement task2
246+
- improvement task3
247+
- improvement task4
248+
ignore:
249+
- user: dependabot[bot]
250+
titlePattern: ".*\\\\bmaven\\\\b.*\\\\bplugin\\\\b.*"
251+
""" );
252+
253+
GHRepository repoMock = mocks.repository( "yrodiere/hibernate-github-bot-playground" );
254+
when( repoMock.getId() ).thenReturn( repoId );
255+
256+
PullRequestMockHelper.start( mocks, prId, repoMock )
257+
.noComments();
258+
259+
mockCheckRuns( repoMock, "6e9f11a1e2946b207c6eb245ec942f2b5a3ea156" );
260+
} )
261+
.when()
262+
.payloadFromClasspath( "/pullrequest-opened-hsearch-1111-dependabot-upgrades-build-dependencies.json" )
263+
.event( GHEvent.PULL_REQUEST )
264+
.then()
265+
.github( mocks -> {
266+
verifyNoMoreInteractions( mocks.ghObjects() );
267+
} );
268+
}
269+
216270
}

0 commit comments

Comments
 (0)