-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathutils.d
455 lines (385 loc) · 13.8 KB
/
utils.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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
module utils;
import vibe.d, std.algorithm, std.process, std.range, std.regex, std.stdio;
import std.functional, std.string;
// forward commonly needed imports
public import dlangbot.app;
public import vibe.core.log;
public import vibe.http.common : HTTPMethod, HTTPStatus;
public import vibe.http.client : HTTPClientRequest;
public import vibe.http.server : HTTPServerRequest, HTTPServerResponse;
public import std.functional : toDelegate;
public import vibe.data.json : deserializeJson, serializeToJson, Json;
public import std.datetime.systime : SysTime;
public import std.algorithm;
import std.datetime.timezone : TimeZone, UTC;
// existing dlang bot comment -> update comment
string testServerURL;
string ghTestHookURL;
string trelloTestHookURL;
string buildkiteTestHookURL;
enum payloadDir = "./data/payloads";
enum graphqlDir = payloadDir ~ "/graphql";
enum hookDir = "./data/hooks";
/// Tries to find a free port
ushort getFreePort()
{
import std.conv : to;
import std.socket : AddressFamily, InternetAddress, Socket, SocketType;
auto s = new Socket(AddressFamily.INET, SocketType.STREAM);
scope(exit) s.close;
s.bind(new InternetAddress(0));
return s.localAddress.toPortString.to!ushort;
}
version(unittest)
shared static this()
{
// overwrite environment configs
githubAuth = "GH_DUMMY_AUTH_TOKEN";
githubHookSecret = "GH_DUMMY_HOOK_SECRET";
trelloAuth = "key=01234&token=abcde";
buildkiteAuth = "Bearer abcdef";
buildkiteHookSecret = "1234567890";
hcloudAuth = "Bearer BDc2RZCKKvgdyF6Dgex1kg4NGwkScI9xzBZqGJemkR4GopohwatiH0IRD2iTg61o";
dlangbotAgentAuth = "Bearer fjSL8ITFkOxS5PF9p5lM41mox";
bugzillaLogin = "bugzilla@test.org";
bugzillaPassword = "BUGZILLA_DUMMY_PW";
// start our hook server
auto settings = new HTTPServerSettings;
settings.port = getFreePort;
startServer(settings);
startFakeAPIServer();
testServerURL = "http://" ~ settings.bindAddresses[0] ~ ":"
~ settings.port.to!string;
ghTestHookURL = testServerURL ~ "/github_hook";
trelloTestHookURL = testServerURL ~ "/trello_hook";
buildkiteTestHookURL = testServerURL ~ "/buildkite_hook";
setLogLevel(LogLevel.info);
runAsync = false;
}
void startFakeAPIServer()
{
// start a fake API server
auto fakeSettings = new HTTPServerSettings;
fakeSettings.port = getFreePort;
fakeSettings.bindAddresses = ["0.0.0.0"];
auto router = new URLRouter;
router.any("*", &payloadServer);
listenHTTP(fakeSettings, router);
auto fakeAPIServerURL = "http://" ~ fakeSettings.bindAddresses[0] ~ ":"
~ fakeSettings.port.to!string;
githubAPIURL = fakeAPIServerURL ~ "/github";
trelloAPIURL = fakeAPIServerURL ~ "/trello";
buildkiteAPIURL = fakeAPIServerURL ~ "/buildkite";
hcloudAPIURL = fakeAPIServerURL ~ "/hcloud";
bugzillaURL = fakeAPIServerURL ~ "/bugzilla";
twitterURL = fakeAPIServerURL ~ "/twitter";
}
// serves saved GitHub API payloads
auto payloadServer(scope HTTPServerRequest req, scope HTTPServerResponse res)
{
import std.path, std.file;
APIExpectation expectation = void;
// simple observer that checks whether a request is expected
auto idx = apiExpectations.map!(x => x.url).countUntil(req.requestURL);
if (idx >= 0)
{
expectation = apiExpectations[idx];
if (apiExpectations.length > 1)
apiExpectations = apiExpectations[0 .. idx] ~ apiExpectations[idx + 1 .. $];
else
apiExpectations.length = 0;
}
else
{
logError("Remaining expected URLs: %s", apiExpectations.map!(x => x.url));
logError("Request for unexpected URL received: '%s'", req.requestURL);
assert(0);
}
res.statusCode = expectation.respStatusCode;
// set failure status code exception to suppress false errors
import dlangbot.utils : _expectedStatusCode;
if (expectation.respStatusCode / 100 != 2)
_expectedStatusCode = expectation.respStatusCode;
auto requestURL = req.requestURL;
if (requestURL == "/bugzilla/jsonrpc.cgi")
{
// Bugzilla uses JSON-RPC, with parameters passed as POST data.
// This does not work for us, as we can't provide different
// payloads for different requests.
// Extract them from the POST data and apply them on the path.
assert(req.method == HTTPMethod.POST);
requestURL ~= "/" ~ req.json["method"].get!string;
if ("params" in req.json && "ids" in req.json["params"][0])
requestURL ~= "/" ~ req.json["params"][0]["ids"].get!(Json[]).map!(id => id.get!int.to!string).join(",");
}
string filePath = buildPath(payloadDir, requestURL[1 .. $].replace("/", "_"));
if (expectation.reqHandler !is null)
{
scope(failure) {
writefln("Method: %s", req.method);
writefln("Json: %s", req.json);
}
expectation.reqHandler(req, res);
if (res.headerWritten)
return;
if (!filePath.exists)
return res.writeVoidBody;
}
if (!filePath.exists)
{
logError("Please create payload: " ~ filePath);
assert(0);
}
else
{
logInfo("reading payload: %s", filePath);
auto payload = filePath.readText;
if (req.requestURL.startsWith("/github", "/trello", "/buildkite", "/hcloud"))
{
auto payloadJson = payload.parseJsonString;
replaceAPIReferences("https://api.github.com", githubAPIURL, payloadJson);
replaceAPIReferences("https://api.trello.com", trelloAPIURL, payloadJson);
replaceAPIReferences("https://api.buildkite.com/v2", buildkiteAPIURL, payloadJson);
if (expectation.jsonHandler !is null)
expectation.jsonHandler(payloadJson);
return res.writeJsonBody(payloadJson);
}
else
{
return res.writeBody(payload);
}
}
}
void replaceAPIReferences(string official, string local, ref Json json)
{
void recursiveReplace(ref Json j)
{
switch (j.type)
{
case Json.Type.array:
case Json.Type.object:
j.each!recursiveReplace;
break;
case Json.Type.string:
string v = j.get!string;
if (v.countUntil(official) >= 0)
j = v.replace(official, local);
break;
default:
break;
}
}
recursiveReplace(json);
}
struct APIExpectation
{
/// the called server url
string url;
/// implement a custom request handler
private void delegate(scope HTTPServerRequest req, scope HTTPServerResponse res) reqHandler;
/// modify the json of the payload before being served
private void delegate(ref Json j) jsonHandler;
/// respond with the given status
HTTPStatus respStatusCode = HTTPStatus.ok;
this(string url)
{
this.url = url;
}
}
__gshared APIExpectation[] apiExpectations;
void setAPIExpectations(Args...)(Args args)
{
import std.functional : toDelegate;
import std.traits : Parameters;
synchronized {
apiExpectations.length = 0;
foreach (i, arg; args)
{
static if (is(Args[i] : string))
{
apiExpectations ~= APIExpectation(arg);
}
else static if (is(Args[i] : HTTPStatus))
{
apiExpectations[$ - 1].respStatusCode = arg;
}
else
{
alias params = Parameters!arg;
static if (is(params[0] : HTTPServerRequest))
{
apiExpectations[$ - 1].reqHandler = arg.toDelegate;
}
else static if (is(params[0] : Json))
{
apiExpectations[$ - 1].jsonHandler = arg.toDelegate;
}
else
{
static assert(0, "Unknown handler type");
}
assert(apiExpectations[$ - 1].jsonHandler is null ||
apiExpectations[$ - 1].reqHandler is null, "Either provide a reqHandler or a jsonHandler");
}
}
}
}
void graphQL(string path, alias process=(ref Json){})(scope HTTPServerRequest req, scope HTTPServerResponse res)
{
import std.file : readText;
import std.path : buildPath;
assert(req.method == HTTPMethod.POST);
assert(req.json["query"].get!string.canFind("query"));
auto filePath = buildPath(graphqlDir, path);
logInfo("reading payload: %s", filePath);
auto json = filePath.readText.parseJsonString;
process(json);
res.writeJsonBody(json);
}
void checkAPIExpectations()
{
if (apiExpectations.length != 0)
logWarn("Didn't request: %s", apiExpectations.map!(x => x.url));
assert(apiExpectations.length == 0);
}
void postGitHubHook(string payload, string eventType = "pull_request",
void delegate(ref Json j, scope HTTPClientRequest req) postprocess = null,
int line = __LINE__, string file = __FILE__)
{
import std.file : readText;
import std.path : buildPath;
import dlangbot.github : getSignature;
logInfo("Starting test in %s:%d with payload: %s", file, line, payload);
payload = hookDir.buildPath("github", payload);
auto req = requestHTTP(ghTestHookURL, (scope req) {
req.method = HTTPMethod.POST;
auto payload = payload.readText.parseJsonString;
// localize accessed URLs
replaceAPIReferences("https://api.github.com", githubAPIURL, payload);
req.headers["X-GitHub-Event"] = eventType;
if (postprocess !is null)
postprocess(payload, req);
auto respStr = payload.toString;
req.headers["X-Hub-Signature"] = getSignature(respStr);
req.writeBody(cast(ubyte[]) respStr);
});
assert(req.statusCode == 200,
"Request failed with status %d. Response body:\n\n%s"
.format(req.statusCode, req.bodyReader.readAllUTF8));
assert(req.bodyReader.readAllUTF8 == "handled");
checkAPIExpectations;
}
void postTrelloHook(string payload,
void delegate(ref Json j, scope HTTPClientRequest req) postprocess = null,
int line = __LINE__, string file = __FILE__)
{
import std.file : readText;
import std.path : buildPath;
import dlangbot.trello : getSignature;
payload = hookDir.buildPath("trello", payload);
logInfo("Starting test in %s:%d with payload: %s", file, line, payload);
auto req = requestHTTP(trelloTestHookURL, (scope req) {
req.method = HTTPMethod.POST;
auto payload = payload.readText.parseJsonString;
// localize accessed URLs
replaceAPIReferences("https://api.trello.com", trelloAPIURL, payload);
if (postprocess !is null)
postprocess(payload, req);
auto respStr = payload.toString;
req.headers["X-Trello-Webhook"] = getSignature(respStr, trelloHookURL);
req.writeBody(cast(ubyte[]) respStr);
});
scope(failure) {
if (req.statusCode != 200)
writeln(req.bodyReader.readAllUTF8);
}
assert(req.statusCode == 200);
assert(req.bodyReader.readAllUTF8 == "handled");
checkAPIExpectations;
}
void postBuildkiteHook(string payload,
void delegate(ref Json j, scope HTTPClientRequest req) postprocess = null,
int line = __LINE__, string file = __FILE__)
{
import std.file : readText;
import std.path : buildPath;
import dlangbot.trello : getSignature;
payload = hookDir.buildPath("buildkite", payload);
logInfo("Starting test in %s:%d with payload: %s", file, line, payload);
auto req = requestHTTP(buildkiteTestHookURL, (scope req) {
req.method = HTTPMethod.POST;
auto payload = payload.readText.parseJsonString;
// localize accessed URLs
replaceAPIReferences("https://api.buildkite.com", buildkiteAPIURL, payload);
req.headers["X-Buildkite-Event"] = payload["event"].get!string;
if (postprocess !is null)
postprocess(payload, req);
auto respStr = payload.toString;
req.headers["X-Buildkite-Token"] = buildkiteHookSecret;
req.writeBody(cast(ubyte[]) respStr);
});
scope(failure) {
if (req.statusCode != 200)
writeln(req.bodyReader.readAllUTF8);
}
assert(req.statusCode == 200);
assert(req.bodyReader.readAllUTF8 == "handled");
checkAPIExpectations;
}
void postAgentShutdownCheck(string hostname, int line = __LINE__, string file = __FILE__)
{
import vibe.textfilter.urlencode : urlEncode;
logInfo("Starting test in %s:%d with hostname %s", file, line, hostname);
auto req = requestHTTP(testServerURL ~ "/agent_shutdown_check", (scope req) {
req.method = HTTPMethod.POST;
req.headers["Authentication"] = dlangbotAgentAuth;
req.writeFormBody(["hostname": hostname]);
});
scope(failure) {
if (req.statusCode != 200)
writeln(req.bodyReader.readAllUTF8);
}
assert(req.statusCode == 200);
checkAPIExpectations;
req.dropBody;
}
void runCronDailyTest(string[] repositories, int line = __LINE__, string file = __FILE__)
{
import dlangbot.app : cronDaily;
import dlangbot.cron : CronConfig;
logInfo("Starting cron test in %s:%d", file, line);
CronConfig config = {
simulate: false,
waitAfterMergeNullState: 1.msecs,
};
cronDaily(repositories, config);
checkAPIExpectations;
}
SysTime now(immutable TimeZone tz = UTC())
{
import std.datetime.systime : Clock;
auto now = Clock.currTime(tz);
now.fracSecs = Duration.zero;
return now;
}
/*
A convenience struct for disabling parts of the dlang-bot for a single test.
---
unittest
{
auto d = Disable!("runTrello", "runBugzillaUpdates")(0);
...
}
---
*/
struct Disable(params...)
{
this(int dummy) {
static foreach (p; params)
mixin(p ~ `= false;`);
}
~this() {
static foreach (p; params)
mixin(p ~ `= true;`);
}
}