Skip to content

Commit 3bda0ff

Browse files
wilzbachdlang-bot
authored andcommitted
Markdown-escape GitHub comments
1 parent bb29c92 commit 3bda0ff

File tree

3 files changed

+81
-1
lines changed

3 files changed

+81
-1
lines changed

source/dlangbot/github.d

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,33 @@ void printBugList(W)(W app, in IssueRef[] refs, in Issue[] descs)
2525
app.put("--- | --- | --- | ---\n");
2626
foreach (num, closed, severity, desc; combined)
2727
{
28+
const escapedDesc = markdownEscape(desc);
2829
app.formattedWrite(
2930
"%1$s | [%2$s](%5$s/show_bug.cgi?id=%2$s) | %3$s | %4$s\n",
30-
closed ? "" : "", num, severity, desc, bugzillaURL);
31+
closed ? "" : "", num, severity, escapedDesc, bugzillaURL);
3132
}
3233
}
3334

35+
string markdownEscape(string desc)
36+
{
37+
import std.array : appender;
38+
auto app = appender!string;
39+
foreach (c; desc)
40+
{
41+
if (c == '|') app ~= "|";
42+
else if (c == '`') app ~= "``";
43+
else if (c.among('\\', '*', '_', '{', '}', '[', ']', '(', ')', '#',
44+
'+', '-', '.', '!'))
45+
{
46+
app ~= '\\';
47+
app ~= c;
48+
}
49+
else
50+
app ~= c;
51+
}
52+
return app.data;
53+
}
54+
3455
string formatComment(in ref PullRequest pr, in IssueRef[] refs, in Issue[] descs, in UserMessage[] msgs)
3556
{
3657
import std.array : appender;

test/bugzilla.d

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,54 @@ EOF".chomp;
231231
postGitHubHook("dlang_dmd_open_6359.json");
232232
}
233233

234+
@("pr-open-notify-bugzilla-whitehole")
235+
unittest
236+
{
237+
setAPIExpectations(
238+
"/github/repos/dlang/dmd/pulls/6359/commits",
239+
(ref Json j){
240+
j[0]["commit"]["message"] = "Fix Issue 20540 - (White|Black)Hole does not work with return|scope functions";
241+
},
242+
"/github/repos/dlang/dmd/issues/6359/comments",
243+
"/bugzilla/buglist.cgi?bug_id=20540&ctype=csv&columnlist=short_desc,bug_status,resolution,bug_severity,priority,keywords",
244+
(scope HTTPServerRequest req, scope HTTPServerResponse res){
245+
res.writeBody(
246+
`bug_id,"short_desc","bug_status","resolution","bug_severity","priority","keywords"
247+
20540,"(White|Black)Hole does not work with return|scope functions","NEW","---","normal","P1","pull"`);
248+
},
249+
"/github/orgs/dlang/public_members?per_page=100",
250+
"/github/repos/dlang/dmd/issues/6359/comments",
251+
(scope HTTPServerRequest req, scope HTTPServerResponse res) {
252+
assert(req.method == HTTPMethod.POST);
253+
assert(req.json["body"].get!string.canFind(r"| \(White|Black\)Hole does not work with return|scope functions"));
254+
},
255+
"/github/repos/dlang/dmd/issues/6359/labels",
256+
"/github/repos/dlang/dmd/issues/6359/labels",
257+
(scope HTTPServerRequest req, scope HTTPServerResponse res) {},
258+
"/trello/1/search?query=name:%22Issue%2020540%22&"~trelloAuth,
259+
(scope HTTPServerRequest req, scope HTTPServerResponse res) {
260+
res.writeBody(`{"cards": []}`);
261+
},
262+
"/bugzilla/jsonrpc.cgi", // Bug.comments
263+
(scope HTTPServerRequest req, scope HTTPServerResponse res){
264+
res.writeBody(`{"error" : null, "result" : {
265+
"bugs" : {"20540" : {"comments" : []}},
266+
"comments" : {}
267+
}}`);
268+
},
269+
"/bugzilla/jsonrpc.cgi", // Bug.update
270+
(scope HTTPServerRequest req, scope HTTPServerResponse res){
271+
assert(req.method == HTTPMethod.POST);
272+
assert(req.json["method"].get!string == "Bug.update");
273+
274+
auto j = Json(["error" : Json(null), "result" : Json.emptyObject]);
275+
res.writeJsonBody(j);
276+
},
277+
);
278+
279+
postGitHubHook("dlang_dmd_open_6359.json");
280+
}
281+
234282
@("pr-open-different-org")
235283
unittest
236284
{

test/comments.d

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,3 +510,14 @@ unittest
510510

511511
postGitHubHook("dlang_phobos_synchronize_4921.json");
512512
}
513+
514+
515+
@("github-markdown-escape")
516+
unittest
517+
{
518+
import dlangbot.github : markdownEscape;
519+
assert("a|b".markdownEscape == "a|b");
520+
assert("a`b".markdownEscape == "a``b");
521+
assert("a\\b".markdownEscape == "a\\\\b");
522+
assert("a+b".markdownEscape == r"a\+b");
523+
}

0 commit comments

Comments
 (0)