/
PullRequestBot.java
269 lines (230 loc) · 9.31 KB
/
PullRequestBot.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/*
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package org.openjdk.skara.bots.pr;
import org.openjdk.skara.bot.*;
import org.openjdk.skara.census.Contributor;
import org.openjdk.skara.forge.*;
import org.openjdk.skara.issuetracker.IssueProject;
import org.openjdk.skara.json.JSONValue;
import org.openjdk.skara.vcs.Hash;
import java.net.URI;
import java.nio.file.Path;
import java.time.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.logging.Logger;
import java.util.regex.Pattern;
class PullRequestBot implements Bot {
private final HostedRepository remoteRepo;
private final HostedRepository censusRepo;
private final String censusRef;
private final LabelConfiguration labelConfiguration;
private final Map<String, String> externalCommands;
private final Map<String, String> blockingCheckLabels;
private final Set<String> readyLabels;
private final Set<String> twoReviewersLabels;
private final Set<String> twentyFourHoursLabels;
private final Map<String, Pattern> readyComments;
private final IssueProject issueProject;
private final boolean ignoreStaleReviews;
private final Set<String> allowedIssueTypes;
private final Pattern allowedTargetBranches;
private final Path seedStorage;
private final HostedRepository confOverrideRepo;
private final String confOverrideName;
private final String confOverrideRef;
private final String censusLink;
private final ConcurrentMap<Hash, Boolean> currentLabels;
private final PullRequestUpdateCache updateCache;
private final Logger log = Logger.getLogger("org.openjdk.skara.bots.pr");
private Instant lastFullUpdate;
PullRequestBot(HostedRepository repo, HostedRepository censusRepo, String censusRef,
LabelConfiguration labelConfiguration, Map<String, String> externalCommands,
Map<String, String> blockingCheckLabels, Set<String> readyLabels,
Set<String> twoReviewersLabels, Set<String> twentyFourHoursLabels,
Map<String, Pattern> readyComments, IssueProject issueProject,
boolean ignoreStaleReviews, Set<String> allowedIssueTypes, Pattern allowedTargetBranches,
Path seedStorage, HostedRepository confOverrideRepo, String confOverrideName,
String confOverrideRef, String censusLink) {
remoteRepo = repo;
this.censusRepo = censusRepo;
this.censusRef = censusRef;
this.labelConfiguration = labelConfiguration;
this.externalCommands = externalCommands;
this.blockingCheckLabels = blockingCheckLabels;
this.readyLabels = readyLabels;
this.twoReviewersLabels = twoReviewersLabels;
this.twentyFourHoursLabels = twentyFourHoursLabels;
this.issueProject = issueProject;
this.readyComments = readyComments;
this.ignoreStaleReviews = ignoreStaleReviews;
this.allowedIssueTypes = allowedIssueTypes;
this.allowedTargetBranches = allowedTargetBranches;
this.seedStorage = seedStorage;
this.confOverrideRepo = confOverrideRepo;
this.confOverrideName = confOverrideName;
this.confOverrideRef = confOverrideRef;
this.censusLink = censusLink;
currentLabels = new ConcurrentHashMap<>();
updateCache = new PullRequestUpdateCache();
// Only check recently updated when starting up to avoid congestion
lastFullUpdate = Instant.now();
}
static PullRequestBotBuilder newBuilder() {
return new PullRequestBotBuilder();
}
private boolean isReady(PullRequest pr) {
var labels = new HashSet<>(pr.labels());
for (var readyLabel : readyLabels) {
if (!labels.contains(readyLabel)) {
log.fine("PR is not yet ready - missing label '" + readyLabel + "'");
return false;
}
}
var comments = pr.comments();
for (var readyComment : readyComments.entrySet()) {
var commentFound = false;
for (var comment : comments) {
if (comment.author().userName().equals(readyComment.getKey())) {
var matcher = readyComment.getValue().matcher(comment.body());
if (matcher.find()) {
commentFound = true;
break;
}
}
}
if (!commentFound) {
log.fine("PR is not yet ready - missing ready comment from '" + readyComment.getKey() +
"containing '" + readyComment.getValue().pattern() + "'");
return false;
}
}
return true;
}
private boolean checkHasExpired(PullRequest pr) {
var hash = pr.headHash();
var currentChecks = pr.checks(hash);
if (currentChecks.containsKey("jcheck")) {
var check = currentChecks.get("jcheck");
if (check.metadata().isPresent()) {
var metadata = check.metadata().get();
if (metadata.contains(":")) {
var expirationString = metadata.substring(metadata.lastIndexOf(":") + 1);
var expiresAt = Instant.ofEpochSecond(Long.parseLong(expirationString));
if (expiresAt.isBefore(Instant.now())) {
log.info("Check metadata has expired (expired at: " + expiresAt + ")");
return true;
}
}
}
}
return false;
}
private List<WorkItem> getWorkItems(List<PullRequest> pullRequests) {
var ret = new LinkedList<WorkItem>();
for (var pr : pullRequests) {
if (updateCache.needsUpdate(pr, Duration.ofMinutes(5)) || checkHasExpired(pr)) {
if (!isReady(pr)) {
continue;
}
ret.add(new CheckWorkItem(this, pr, e -> updateCache.invalidate(pr)));
}
}
return ret;
}
@Override
public List<WorkItem> getPeriodicItems() {
List<PullRequest> prs;
if (lastFullUpdate == null || lastFullUpdate.isBefore(Instant.now().minus(Duration.ofMinutes(10)))) {
lastFullUpdate = Instant.now();
log.info("Fetching all open pull requests");
prs = remoteRepo.pullRequests();
} else {
log.info("Fetching recently updated pull requests (open and closed)");
prs = remoteRepo.pullRequests(ZonedDateTime.now().minus(Duration.ofDays(1)));
}
return getWorkItems(prs);
}
@Override
public List<WorkItem> processWebHook(JSONValue body) {
var webHook = remoteRepo.parseWebHook(body);
if (webHook.isEmpty()) {
return new ArrayList<>();
}
return getWorkItems(webHook.get().updatedPullRequests());
}
HostedRepository censusRepo() {
return censusRepo;
}
String censusRef() {
return censusRef;
}
LabelConfiguration labelConfiguration() {
return labelConfiguration;
}
Map<String, String> externalCommands() {
return externalCommands;
}
Map<String, String> blockingCheckLabels() {
return blockingCheckLabels;
}
Set<String> twoReviewersLabels() {
return twoReviewersLabels;
}
Set<String> twentyFourHoursLabels() {
return twentyFourHoursLabels;
}
IssueProject issueProject() {
return issueProject;
}
ConcurrentMap<Hash, Boolean> currentLabels() {
return currentLabels;
}
boolean ignoreStaleReviews() {
return ignoreStaleReviews;
}
Set<String> allowedIssueTypes() {
return allowedIssueTypes;
}
Pattern allowedTargetBranches() {
return allowedTargetBranches;
}
Optional<Path> seedStorage() {
return Optional.ofNullable(seedStorage);
}
Optional<HostedRepository> confOverrideRepository() {
return Optional.ofNullable(confOverrideRepo);
}
String confOverrideName() {
return confOverrideName;
}
String confOverrideRef() {
return confOverrideRef;
}
Optional<URI> censusLink(Contributor contributor) {
if (censusLink == null) {
return Optional.empty();
}
return Optional.of(URI.create(censusLink.replace("{{contributor}}", contributor.username())));
}
}