-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathgithub.d
373 lines (317 loc) · 11.7 KB
/
github.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
module dlangbot.github;
import dlangbot.bugzilla : bugzillaURL, bugzillaProjectSlugs, Issue, IssueRef;
import dlangbot.warnings : printMessages, UserMessage;
import std.algorithm, std.range;
import std.datetime;
import std.format : format, formattedWrite;
import std.typecons : Tuple;
import vibe.core.log;
import vibe.data.json;
public import dlangbot.github_api;
//==============================================================================
// Github comments
//==============================================================================
void printBugList(W)(W app, in IssueRef[] refs, in Issue[] descs)
{
auto combined = zip(refs.map!(r => r.id), refs.map!(r => r.fixed),
descs.map!(d => d.severity), descs.map!(d => d.desc));
app.put("Auto-close | Bugzilla | Severity | Description\n");
app.put("--- | --- | --- | ---\n");
foreach (num, closed, severity, desc; combined)
{
const escapedDesc = markdownEscape(desc);
app.formattedWrite(
"%1$s | [%2$s](%5$s/show_bug.cgi?id=%2$s) | %3$s | %4$s\n",
closed ? "✓" : "✗", num, severity, escapedDesc, bugzillaURL);
}
}
string markdownEscape(string desc)
{
import std.array : appender;
auto app = appender!string;
foreach (c; desc)
{
if (c == '|') app ~= "|";
else if (c.among('\\', '*', '_', '{', '}', '[', ']', '(', ')', '#',
'`', '+', '-', '.', '!'))
{
app ~= '\\';
app ~= c;
}
else
app ~= c;
}
return app.data;
}
string formatComment(in ref PullRequest pr, in IssueRef[] refs, in Issue[] descs, in UserMessage[] msgs)
{
import std.array : appender;
auto app = appender!string;
bool isMember = ghGetRequest(pr.membersURL ~ "?per_page=100")
.body
.deserializeJson!(GHUser[])
.canFind!(l => l.login == pr.user.login);
// Avoid actually mentioning users when referring to them
auto userRef = `**@<!-- -->%s**`.format(pr.user.login);
if (isMember)
{
app.formattedWrite(
`Thanks for your pull request, %s!
`, userRef, pr.repoSlug);
}
else
{
app.formattedWrite(
"Thanks for your pull request and interest in making D better, %s! We are looking forward to reviewing it, and you should be hearing from a maintainer soon.
Please verify that your PR follows this checklist:
- My PR is fully covered with tests (you can see the coverage diff by visiting the _details_ link of the codecov check)
- My PR is as minimal as possible (smaller, focused PRs are easier to review than big ones)
- I have provided a detailed rationale explaining my changes
- New or modified functions have Ddoc comments (with `Params:` and `Returns:`)
Please see [CONTRIBUTING.md](https://github.com/%s/blob/master/CONTRIBUTING.md) for more information.
---
If you have addressed all reviews or aren't sure how to proceed, don't hesitate to ping us with a simple comment.",
userRef, pr.repoSlug);
}
// markdown doesn't support breaking of long lines
if (bugzillaProjectSlugs.canFind(pr.repoSlug))
{
app ~= "\n### Bugzilla references\n\n";
if (refs.length)
app.printBugList(refs, descs);
else
app.formattedWrite(
`Your PR doesn't reference any Bugzilla issue.
If your PR contains non-trivial changes, please [reference a Bugzilla issue](https://github.com/dlang-bots/dlang-bot#automated-references) or create a [manual changelog](https://github.com/%s/blob/master/%s).
`, pr.repoSlug, pr.repoSlug == "dlang/dlang.org" ? "language-changelog" : "changelog");
}
if (msgs.length)
{
if (refs.length)
app ~= "\n";
app ~= "### ⚠️⚠️⚠️ Warnings ⚠️⚠️⚠️\n\n";
app.printMessages(msgs);
}
if (pr.repoSlug.among("dlang/dmd", "dlang/druntime", "dlang/phobos", "dlang/tools"))
{
app.put("
### Testing this PR locally
If you don't have a [local development environment setup](https://wiki.dlang.org/Starting_as_a_Contributor), you can use [Digger](https://github.com/CyberShadow/Digger) to test this PR:
```sh
dub run digger -- build \"%s + %s#%d\"
```
".format(pr.base.ref_, pr.base.repo.get().name, pr.number));
}
return app.data;
}
GHComment getBotComment(in ref PullRequest pr)
{
// the bot may post multiple comments (mention-bot & bugzilla links)
auto res = ghGetRequest(pr.commentsURL)
.body[]
.find!(c => c["user"]["login"] == "dlang-bot");
if (res.length)
return deserializeJson!GHComment(res[0]);
return GHComment();
}
void updateGithubComment(in ref PullRequest pr, in ref GHComment comment,
string action, IssueRef[] refs, Issue[] descs, UserMessage[] msgs)
{
// The history should be preserved and modifications after a merge/closed event are seldomly seen
if (pr.state == GHState.closed)
return;
logDebug("[github/updateGithubComment](%s): %s", pr.pid, refs);
logDebug("%s", descs);
assert(refs.map!(r => r.id).equal(descs.map!(d => d.id)));
auto msg = pr.formatComment(refs, descs, msgs);
logDebug("%s", msg);
if (msg != comment.body_)
{
if (comment.url.length)
comment.update(msg);
else if (action != "closed" && action != "merged")
comment.post(pr, msg);
}
}
//==============================================================================
// Github Auto-merge
//==============================================================================
string labelName(GHMerge.MergeMethod method)
{
final switch (method) with (GHMerge.MergeMethod)
{
case none: return null;
case merge: return "auto-merge";
case squash: return "auto-merge-squash";
case rebase: return "auto-merge-rebase";
}
}
GHMerge.MergeMethod autoMergeMethod(GHLabel[] labels)
{
with (GHMerge.MergeMethod)
{
auto labelNames = labels.map!(l => l.name);
if (labelNames.canFind!(l => (l == "auto-merge" || l == "Merge:auto-merge")))
return merge;
else if (labelNames.canFind!(l => (l == "auto-merge-squash" || l == "Merge:auto-merge-squash")))
return squash;
else if (labelNames.canFind!(l => (l == "auto-merge-rebase" || l == "Merge:auto-merge-rebase")))
return rebase;
return none;
}
}
Json[] tryMerge(in ref PullRequest pr, GHMerge.MergeMethod method)
{
import std.conv : to;
import ae.utils.aa : OrderedSet;
const status = pr.combinedStatus;
if (status.state != CIState.success)
{
logInfo("Can't auto-merge PR %s#%d with combined CI state: %s", pr.repoSlug, pr.number, status.state);
return null;
}
auto commits = ghGetRequest(pr.commitsURL).body[];
if (!pr.isOpen)
{
logWarn("Can't auto-merge PR %s#%d - it is already closed", pr.repoSlug, pr.number);
return commits;
}
if (commits.length == 0)
{
logWarn("Can't auto-merge PR %s#%d has no commits attached", pr.repoSlug, pr.number);
return commits;
}
const labelName = method.labelName;
const mergeLabelName = "Merge:" ~ labelName;
if (commits.length == 1)
method = GHMerge.MergeMethod.rebase;
auto events = ghGetRequest(pr.eventsURL).body[]
.retro
.filter!(e => e["event"] == "labeled" && e["label"]["name"].among(labelName, mergeLabelName));
string author = "unknown";
if (!events.empty)
{
logDebug("[github/tryMerge/author](%s): %s", pr.pid, events.front["actor"]);
author = getUserEmail(events.front["actor"]["login"].get!string);
}
OrderedSet!string reviewers;
foreach (ref review; pr.reviews)
{
if (review.authorAssociation >= CommentAuthorAssociation.COLLABORATOR &&
review.state == GHReview.State.APPROVED)
{
reviewers.add(getUserEmail(review.user.login));
}
}
logDebug("[github/tryMerge/commits](%s): %s", pr.pid, commits);
logDebug("[github/tryMerge/commitsURL](%s): %s", pr.pid, pr.commitsURL);
logDebug("[github/tryMerge/commits](%s): %s", pr.pid, commits[$ - 1]);
GHMerge mergeInput = {
commitMessage: "%s\n\n%-(Signed-off-by: %s\n%|%)Merged-on-behalf-of: %s".format(
pr.title,
reviewers.keys,
author),
sha: commits[$ - 1]["sha"].get!string,
mergeMethod: method
};
pr.postMerge(mergeInput);
return commits;
}
void checkAndRemoveLabels(GHLabel[] labels, in ref PullRequest pr, in string[] toRemoveLabels)
{
import std.uni : sicmp;
labels
.map!(l => l.name)
.filter!(n => toRemoveLabels.canFind!((a, b) => sicmp(a,b) == 0)(n))
.each!(l => pr.removeLabel(l));
}
void addLabels(in ref PullRequest pr, in string[] newLabels)
{
import std.uni : icmp;
auto existingLabels = ghGetRequest(pr.labelsURL)
.body[]
.map!(l => l["name"].get!string);
auto toBeAdded = newLabels
.filter!(l => existingLabels.filter!(a => a.icmp(l) == 0).empty);
if (!toBeAdded.empty)
{
logInfo("[github/handlePR](%s): adding labels: %s", pr.pid, toBeAdded);
ghSendRequest(HTTPMethod.POST, pr.labelsURL, toBeAdded.array);
}
}
void removeLabel(in ref PullRequest pr, string label)
{
import std.uri : encodeComponent;
ghSendRequest(HTTPMethod.DELETE, pr.labelsURL ~ "/" ~ label.encodeComponent);
}
void replaceLabels(in ref PullRequest pr, string[] labels)
{
ghSendRequest(HTTPMethod.PUT, pr.labelsURL, labels);
}
string getUserEmail(string login)
{
auto user = ghGetRequest("%s/users/%s".format(githubAPIURL, login)).body;
auto name = user["name"].opt!string(login);
auto email = user["email"].opt!string(login ~ "@users.noreply.github.com");
return "%s <%s>".format(name, email);
}
GHIssue[] getIssuesForLabel(string repoSlug, string label)
{
return ghGetRequest("%s/repos/%s/issues?state=open&labels=%s"
.format(githubAPIURL, repoSlug, label))
.body
.deserializeJson!(GHIssue[]);
}
auto getIssuesForLabels(string repoSlug, const string[] labels)
{
// the GitHub API doesn't allow a logical OR
GHIssue[] issues;
foreach (label; labels)
issues ~= getIssuesForLabel(repoSlug, label);
issues.sort!((a, b) => a.number < b.number);
return issues.uniq!((a, b) => a.number == b.number);
}
void searchForAutoMergePrs(string repoSlug)
{
static immutable labels = ["auto-merge", "auto-merge-squash"];
foreach (issue; getIssuesForLabels(repoSlug, labels))
{
if (!issue.isPullRequest) // TODO: query is:pr
continue;
auto pr = issue.pullRequest;
if (auto method = autoMergeMethod(issue.labels))
pr.tryMerge(method);
}
}
/**
Allows contributors to use [<label>] messages in the title.
If they are part of a pre-defined, allowed list, the bot will add the
respective label.
*/
void checkTitleForLabels(in ref PullRequest pr)
{
import std.algorithm.iteration : splitter;
import std.regex;
import std.string : strip, toLower;
static labelRe = regex(`\[(.*)\]`);
string[] userLabels;
foreach (m; pr.title.matchAll(labelRe))
{
foreach (el; m[1].splitter(","))
userLabels ~= el;
}
const string[string] userLabelsMap = [
"trivial": "trivial",
"wip": "WIP"
];
auto mappedLabels = userLabels
.sort()
.uniq
.map!strip
.map!toLower
.filter!(l => l in userLabelsMap)
.map!(l => userLabelsMap[l])
.array;
if (mappedLabels.length)
pr.addLabels(mappedLabels);
}