-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathapp.d
326 lines (270 loc) · 10.3 KB
/
app.d
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
module dlangbot.app;
import dlangbot.bugzilla;
import dlangbot.cron;
import dlangbot.github;
import dlangbot.trello;
import dlangbot.utils;
public import dlangbot.bugzilla : bugzillaURL;
public import dlangbot.github_api : githubAPIURL, githubAuth, hookSecret;
public import dlangbot.trello : trelloAPIURL, trelloAuth, trelloSecret;
import std.datetime : Clock, days, Duration, minutes, seconds, SysTime;
import vibe.core.args;
import vibe.core.core;
import vibe.core.log;
import vibe.data.json;
import vibe.http.client : HTTPClient;
import vibe.http.common : enforceBadRequest, enforceHTTP, HTTPMethod, HTTPStatus;
import vibe.http.router : URLRouter;
import vibe.http.server : HTTPServerRequest, HTTPServerResponse, HTTPServerSettings;
import vibe.stream.operations : readAllUTF8;
bool runAsync = true;
bool runTrello = true;
Duration timeBetweenFullPRChecks = 1.minutes; // this should never be larger 30 mins on heroku
Throttler!(typeof(&searchForAutoMergePrs)) prThrottler;
enum trelloHookURL = "https://dlang-bot.herokuapp.com/trello_hook";
void startServer(HTTPServerSettings settings)
{
import vibe.core.core : vibeVersionString;
import vibe.http.fileserver : serveStaticFiles;
import vibe.http.server : HTTPServerOption, listenHTTP, render;
settings.bindAddresses = ["0.0.0.0"];
auto router = new URLRouter;
router
.get("/", (req, res) => res.render!"index.dt")
.get("*", serveStaticFiles("public"))
.post("/github_hook", &githubHook)
.match(HTTPMethod.HEAD, "/trello_hook", (HTTPServerRequest req, HTTPServerResponse res) => res.writeVoidBody)
.post("/trello_hook", &trelloHook)
.post("/codecov_hook", &codecovHook)
;
HTTPClient.setUserAgentString("dlang-bot vibe.d/"~vibeVersionString);
prThrottler = typeof(prThrottler)(&searchForAutoMergePrs, timeBetweenFullPRChecks);
listenHTTP(settings, router);
}
//==============================================================================
// Github hook
//==============================================================================
void trelloHook(HTTPServerRequest req, HTTPServerResponse res)
{
import std.array : array;
import dlangbot.trello : verifyRequest;
auto json = verifyRequest(req.headers["X-Trello-Webhook"], req.bodyReader.readAllUTF8, trelloHookURL);
logDebug("trelloHook: %s", json);
auto action = json["action"]["type"].get!string;
switch (action)
{
case "createCard", "updateCard":
auto refs = matchIssueRefs(json["action"]["data"]["card"]["name"].get!string).array;
auto descs = getDescriptions(refs);
updateTrelloCard(json["action"]["data"]["card"]["id"].get!string, refs, descs);
return res.writeBody("handled");
default:
return res.writeBody("ignored");
}
}
void githubHook(HTTPServerRequest req, HTTPServerResponse res)
{
import std.functional : toDelegate;
import dlangbot.github : verifyRequest;
auto json = verifyRequest(req.headers["X-Hub-Signature"], req.bodyReader.readAllUTF8);
logDebug("githubHook: %s", json);
switch (req.headers["X-GitHub-Event"])
{
case "ping":
return res.writeBody("pong");
case "status":
auto state = json["state"].get!string;
auto repoSlug = json["name"].get!string;
logDebug("[github/pull_request](%s): state=%s, sha=%s, url=%s", repoSlug, state, json["sha"], json["target_url"]);
// no need to trigger the checker for failure/pending
if (state == "success")
prThrottler(repoSlug);
return res.writeBody("handled");
case "pull_request":
auto action = json["action"].get!string;
auto repoSlug = json["repository"]["full_name"].get!string;
auto pullRequest = json["pull_request"].deserializeJson!PullRequest;
logInfo("[github/pull_request](%s#%s): action=%s", repoSlug, pullRequest.number, action);
switch (action)
{
case "unlabeled":
// for now unlabel events are ignored
return res.writeBody("ignored");
case "closed":
if (json["pull_request"]["merged"].get!bool)
action = "merged";
goto case;
case "opened", "reopened", "synchronize", "labeled", "edited":
if (action == "labeled")
{
if (json["label"]["name"].get!string == "bot-rebase")
{
import dlangbot.git : rebase;
runTaskHelper(&rebase, &pullRequest);
return res.writeBody("handled");
}
}
runTaskHelper(&handlePR, action, &pullRequest);
return res.writeBody("handled");
default:
return res.writeBody("ignored");
}
case "pull_request_review":
auto state = json["review"]["state"].get!string;
auto repoSlug = json["repository"]["full_name"].get!string;
auto pullRequest = json["pull_request"].deserializeJson!PullRequest;
logInfo("[github/pull_request_review](%s#%s): state=%s", repoSlug, pullRequest.number, state);
runTaskHelper(&handleReview, state, &pullRequest);
return res.writeBody("handled");
default:
return res.writeVoidBody();
}
}
//==============================================================================
void cronDaily(string[] repositories, CronConfig config)
{
auto actions = [
&detectStalledPR,
&detectInactiveStablePR,
&detectPRWithMergeConflicts,
&detectPRWithPersistentCIFailures,
];
foreach (repo; repositories)
{
logInfo("[cron-daily/%s]: starting", repo);
walkPRs(repo, actions, config);
}
}
//==============================================================================
void handlePR(string action, PullRequest* _pr)
{
import std.algorithm : among, any;
import vibe.core.core : setTimer;
import dlangbot.warnings : checkForWarnings, UserMessage;
const PullRequest pr = *_pr;
Json[] commits;
if (action == "labeled" || action == "synchronize")
{
auto labels = pr.labels;
logDebug("[github/handlePR](%s): labels", labels);
if (action == "labeled")
{
if (auto method = labels.autoMergeMethod)
commits = pr.tryMerge(method);
return;
}
if (action == "synchronize")
{
logDebug("[github/handlePR](%s): checkAndRemoveLabels", _pr.pid);
enum toRemoveLabels = ["auto-merge", "auto-merge-squash",
"needs rebase", "needs work", "stalled", "stable-stalled"];
checkAndRemoveLabels(labels, pr, toRemoveLabels);
}
}
if (action == "opened" || action == "edited")
checkTitleForLabels(pr);
// we only query the commits once
if (commits is null)
commits = ghGetRequest(pr.commitsURL).readJson[];
auto refs = getIssueRefs(commits);
auto descs = getDescriptions(refs);
auto comment = pr.getBotComment;
UserMessage[] msgs;
if (action == "opened" || action == "synchronize")
{
msgs = pr.checkForWarnings(descs, refs);
}
if (pr.base.repo.owner.login.among("dlang", "dlang-bots"))
pr.updateGithubComment(comment, action, refs, descs, msgs);
if (refs.any!(r => r.fixed))
{
import std.algorithm : canFind, filter, map, sort, uniq;
import std.array : array;
// references are already sorted by id
auto bugzillaIds = refs.map!(r => r.id).uniq;
auto bugzillSeverities = descs
.filter!(d => bugzillaIds.canFind(d.id))
.map!(i => i.severity);
logDebug("[github/handlePR](%s): trying to add bug fix label", _pr.pid);
string[] labels;
if (bugzillSeverities.canFind("enhancement"))
labels ~= "Enhancement";
else
labels ~= "Bug Fix";
pr.addLabels(labels);
}
if (runTrello)
{
logDebug("[github/handlePR](%s): updating trello card", _pr.pid);
updateTrelloCard(action, pr.htmlURL, refs, descs);
}
}
void handleReview(string action, PullRequest* _pr)
{
import std.algorithm : among, filter;
const PullRequest pr = *_pr;
auto labels = ghGetRequest(pr.labelsURL)
.readJson
.deserializeJson!(GHLabel[]);
if (auto method = autoMergeMethod(labels))
pr.tryMerge(method);
}
void setBotTimer(C)(Duration dur, C callback)
{
if (runAsync)
setTimer(dur, callback);
else
callback();
}
//==============================================================================
void codecovHook(HTTPServerRequest req, HTTPServerResponse res)
{
logDebug("codecovHook: %s", req.bodyReader.readAllUTF8);
return res.writeBody("OK");
}
//==============================================================================
version (unittest) {}
else void main(string[] args)
{
import std.array : array;
import std.algorithm.iteration : map;
import std.process : environment;
import vibe.core.args : readOption;
githubAuth = "token "~environment["GH_TOKEN"];
trelloSecret = environment["TRELLO_SECRET"];
trelloAuth = "key="~environment["TRELLO_KEY"]~"&token="~environment["TRELLO_TOKEN"];
hookSecret = environment["GH_HOOK_SECRET"];
// workaround for stupid openssl.conf on Heroku
if (environment.get("DYNO") !is null)
{
HTTPClient.setTLSSetupCallback((ctx) {
ctx.useTrustedCertificateFile("/etc/ssl/certs/ca-certificates.crt");
});
}
bool runDailyCron, runDailyCronSimulation;
auto settings = new HTTPServerSettings;
settings.port = 8080;
readOption("port|p", &settings.port, "Sets the port used for serving.");
readOption("simulate-cron-daily", &runDailyCronSimulation, "Sets the port used for serving.");
readOption("cron-daily", &runDailyCron, "Run daily cron tasks.");
if (!finalizeCommandLineOptions())
return;
string[] cronRepositories;
if (runDailyCron)
{
cronRepositories = ["dmd", "druntime", "phobos", "dlang.org", "tools", "installer"]
.map!(r => "dlang/" ~ r).array;
}
else if (runDailyCronSimulation)
{
cronRepositories = ["dlang/phobos"];
}
if (cronRepositories)
{
CronConfig config = {simulate: runDailyCronSimulation};
return cronDaily(cronRepositories, config);
}
startServer(settings);
lowerPrivileges();
runEventLoop();
}