forked from hibernate/hibernate-github-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCheckPullRequestContributionRules.java
221 lines (184 loc) · 8.43 KB
/
CheckPullRequestContributionRules.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package org.hibernate.infra.bot;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.regex.Pattern;
import jakarta.inject.Inject;
import org.hibernate.infra.bot.check.Check;
import org.hibernate.infra.bot.check.CheckRunContext;
import org.hibernate.infra.bot.check.CheckRunOutput;
import org.hibernate.infra.bot.check.CheckRunRule;
import org.hibernate.infra.bot.config.DeploymentConfig;
import org.hibernate.infra.bot.config.RepositoryConfig;
import org.hibernate.infra.bot.util.CommitMessages;
import org.jboss.logging.Logger;
import io.quarkiverse.githubapp.ConfigFile;
import io.quarkiverse.githubapp.event.CheckRun;
import io.quarkiverse.githubapp.event.CheckSuite;
import io.quarkiverse.githubapp.event.PullRequest;
import org.kohsuke.github.GHEventPayload;
import org.kohsuke.github.GHIssueComment;
import org.kohsuke.github.GHIssueState;
import org.kohsuke.github.GHPullRequest;
import org.kohsuke.github.GHPullRequestCommitDetail;
import org.kohsuke.github.GHRepository;
public class CheckPullRequestContributionRules {
private static final Logger LOG = Logger.getLogger( CheckPullRequestContributionRules.class );
private static final Pattern SPACE_PATTERN = Pattern.compile( "\\s+" );
private static final String COMMENT_INTRO_PASSED = """
Thanks for your pull request!
This pull request appears to follow the contribution rules.""";
private static final String COMMENT_INTRO_FAILED = """
Thanks for your pull request!
This pull request does not follow the contribution rules. Could you have a look?
""";
private static final String COMMENT_FOOTER = "\n\n› This message was automatically generated.";
@Inject
DeploymentConfig deploymentConfig;
void pullRequestChanged(
@PullRequest.Opened @PullRequest.Reopened @PullRequest.Edited @PullRequest.Synchronize
GHEventPayload.PullRequest payload,
@ConfigFile("hibernate-github-bot.yml") RepositoryConfig repositoryConfig) throws IOException {
checkPullRequestContributionRules( payload.getRepository(), repositoryConfig,
payload.getPullRequest()
);
}
void checkRunRequested(@CheckRun.Rerequested GHEventPayload.CheckRun payload,
@ConfigFile("hibernate-github-bot.yml") RepositoryConfig repositoryConfig) throws IOException {
for ( GHPullRequest pullRequest : payload.getCheckRun().getPullRequests() ) {
checkPullRequestContributionRules( payload.getRepository(), repositoryConfig, pullRequest );
}
}
void checkSuiteRequested(@CheckSuite.Requested @CheckSuite.Rerequested GHEventPayload.CheckSuite payload,
@ConfigFile("hibernate-github-bot.yml") RepositoryConfig repositoryConfig) throws IOException {
for ( GHPullRequest pullRequest : payload.getCheckSuite().getPullRequests() ) {
checkPullRequestContributionRules( payload.getRepository(), repositoryConfig, pullRequest );
}
}
private void checkPullRequestContributionRules(GHRepository repository, RepositoryConfig repositoryConfig,
GHPullRequest pullRequest)
throws IOException {
if ( !shouldCheck( repository, pullRequest ) ) {
return;
}
CheckRunContext context = new CheckRunContext( deploymentConfig, repository, repositoryConfig, pullRequest );
List<Check> checks = createChecks( repositoryConfig );
List<CheckRunOutput> outputs = new ArrayList<>();
for ( Check check : checks ) {
outputs.add( Check.run( context, check ) );
}
boolean passed = outputs.stream().allMatch( CheckRunOutput::passed );
GHIssueComment existingComment = findExistingComment( pullRequest );
// Avoid creating noisy comments for no reason, in particular if checks passed
// or if the pull request was already closed.
if ( existingComment == null && ( passed || GHIssueState.CLOSED.equals( pullRequest.getState() ) ) ) {
return;
}
StringBuilder message = new StringBuilder( passed ? COMMENT_INTRO_PASSED : COMMENT_INTRO_FAILED );
outputs.forEach( output -> output.appendFailingRules( message ) );
message.append( COMMENT_FOOTER );
if ( !deploymentConfig.isDryRun() ) {
if ( existingComment == null ) {
pullRequest.comment( message.toString() );
}
else {
existingComment.update( message.toString() );
}
}
else {
LOG.info( "Pull request #" + pullRequest.getNumber() + " - Add comment " + message.toString() );
}
}
// GitHub sometimes mentions pull requests in the payload that are definitely not related to the changes,
// such as very old pull requests on the branch that just got updated,
// or pull requests on different repositories.
// We have to ignore those, otherwise we'll end up creating comments on old pull requests.
private boolean shouldCheck(GHRepository repository, GHPullRequest pullRequest) {
return !GHIssueState.CLOSED.equals( pullRequest.getState() )
&& repository.getId() == pullRequest.getBase().getRepository().getId();
}
private GHIssueComment findExistingComment(GHPullRequest pullRequest) throws IOException {
for ( GHIssueComment comment : pullRequest.listComments() ) {
if ( comment.getBody().startsWith( COMMENT_INTRO_PASSED )
|| comment.getBody().startsWith( COMMENT_INTRO_FAILED ) ) {
return comment;
}
}
return null;
}
private List<Check> createChecks(RepositoryConfig repositoryConfig) {
List<Check> checks = new ArrayList<>();
checks.add( new TitleCheck() );
if ( repositoryConfig != null && repositoryConfig.jira != null ) {
final boolean checkMentions = repositoryConfig.jira.getInsertLinksInPullRequests().isEmpty()
|| repositoryConfig.jira.getInsertLinksInPullRequests().get().equals( Boolean.FALSE );
repositoryConfig.jira.getIssueKeyPattern()
.ifPresent( issueKeyPattern -> checks.add( new JiraIssuesCheck( issueKeyPattern, checkMentions ) ) );
}
return checks;
}
static class TitleCheck extends Check {
TitleCheck() {
super( "Contribution — Title" );
}
@Override
public void perform(CheckRunContext context, CheckRunOutput output) {
String title = context.pullRequest.getTitle();
output.rule( "The pull request title should contain at least 2 words to describe the change properly" )
.result( title != null && SPACE_PATTERN.split( title.trim() ).length >= 2 );
output.rule( "The pull request title should not end with an ellipsis (make sure the title is complete)" )
.result( title == null || !title.endsWith( "…" ) );
}
}
static class JiraIssuesCheck extends Check {
private final Pattern issueKeyPattern;
private final boolean checkMentions;
JiraIssuesCheck(Pattern issueKeyPattern, boolean checkMentions) {
super( "Contribution — JIRA issues" );
this.issueKeyPattern = issueKeyPattern;
this.checkMentions = checkMentions;
}
@Override
public void perform(CheckRunContext context, CheckRunOutput output) throws IOException {
String title = context.pullRequest.getTitle();
String body = context.pullRequest.getBody();
Set<String> issueKeys = new LinkedHashSet<>();
Set<String> commitsWithMessageNotStartingWithIssueKey = new LinkedHashSet<>();
for ( GHPullRequestCommitDetail commitDetails : context.pullRequest.listCommits() ) {
GHPullRequestCommitDetail.Commit commit = commitDetails.getCommit();
List<String> commitIssueKeys = CommitMessages.extractIssueKeys( issueKeyPattern, commit.getMessage() );
if ( commitIssueKeys.isEmpty() ) {
commitsWithMessageNotStartingWithIssueKey.add( commitDetails.getSha() );
}
else {
issueKeys.addAll( commitIssueKeys );
}
}
CheckRunRule commitRule =
output.rule( "All commit messages should start with a JIRA issue key matching pattern `"
+ issueKeyPattern + "`" );
if ( commitsWithMessageNotStartingWithIssueKey.isEmpty() ) {
commitRule.passed();
}
else {
commitRule.failed( "Offending commits: " + commitsWithMessageNotStartingWithIssueKey );
}
if ( checkMentions ) {
// We only need to check mentions if automatic body editing is disabled
CheckRunRule pullRequestRule = output.rule(
"The PR title or body should list the keys of all JIRA issues mentioned in the commits");
List<String> issueKeysNotMentionedInPullRequest = issueKeys.stream()
.filter(issueKey -> (title == null || !title.contains(issueKey))
&& (body == null || !body.contains(issueKey)))
.toList();
if (issueKeysNotMentionedInPullRequest.isEmpty()) {
pullRequestRule.passed();
} else {
pullRequestRule.failed("Issue keys mentioned in commits but missing from the PR title or body: " + issueKeysNotMentionedInPullRequest);
}
}
}
}
}