Skip to content

Commit 31257a0

Browse files
authored
Merge pull request #39 from wilzbach/add-bugfix-label
Add bugfix label
2 parents b155530 + 2ac5ffa commit 31257a0

File tree

5 files changed

+55
-16
lines changed

5 files changed

+55
-16
lines changed

source/dlangbot/app.d

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ void githubHook(HTTPServerRequest req, HTTPServerResponse res)
169169

170170
void handlePR(string action, PullRequest pr)
171171
{
172+
import std.algorithm : any;
172173
import vibe.core.core : setTimer;
173174

174175
Json[] commits;
@@ -193,7 +194,12 @@ void handlePR(string action, PullRequest pr)
193194
auto refs = getIssueRefs(commits);
194195

195196
auto descs = getDescriptions(refs);
196-
updateGithubComment(action, refs, descs, pr.commentsURL);
197+
auto comment = pr.getBotComment;
198+
199+
pr.updateGithubComment(comment, action, refs, descs);
200+
201+
if (refs.any!(r => r.fixed) && comment.body_.length == 0)
202+
pr.addLabels(["Bug fix"]);
197203

198204
if (runTrello)
199205
updateTrelloCard(action, pr.htmlURL, refs, descs);

source/dlangbot/github.d

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@ string formatComment(R1, R2)(R1 refs, R2 descs)
4040

4141
struct Comment { string url, body_; }
4242

43-
Comment getBotComment(string commentsURL)
43+
Comment getBotComment(in ref PullRequest pr)
4444
{
4545
// the bot may post multiple comments (mention-bot & bugzilla links)
46-
auto res = ghGetRequest(commentsURL)
46+
auto res = ghGetRequest(pr.commentsURL)
4747
.readJson[]
4848
.find!(c => c["user"]["login"] == "dlang-bot" && c["body"].get!string.canFind("Bugzilla"));
4949
if (res.length)
@@ -87,9 +87,8 @@ auto ghSendRequest(T...)(HTTPMethod method, string url, T arg)
8787
}, url);
8888
}
8989

90-
void updateGithubComment(string action, IssueRef[] refs, Issue[] descs, string commentsURL)
90+
void updateGithubComment(in ref PullRequest pr, in ref Comment comment, string action, IssueRef[] refs, Issue[] descs)
9191
{
92-
auto comment = getBotComment(commentsURL);
9392
logDebug("%s", refs);
9493
if (refs.empty)
9594
{
@@ -108,7 +107,7 @@ void updateGithubComment(string action, IssueRef[] refs, Issue[] descs, string c
108107
if (comment.url.length)
109108
ghSendRequest(HTTPMethod.PATCH, comment.url, ["body" : msg]);
110109
else if (action != "closed" && action != "merged")
111-
ghSendRequest(HTTPMethod.POST, commentsURL, ["body" : msg]);
110+
ghSendRequest(HTTPMethod.POST, pr.commentsURL, ["body" : msg]);
112111
}
113112
}
114113

@@ -232,12 +231,24 @@ Json[] tryMerge(in ref PullRequest pr, MergeMethod method)
232231

233232
void checkAndRemoveMergeLabels(Json[] labels, in ref PullRequest pr)
234233
{
235-
foreach (label; labels.map!(l => l["name"].get!string).filter!(n => n.startsWith("auto-merge")))
236-
{
237-
auto labelUrl = "%s/repos/%s/issues/%d/labels/%s"
238-
.format(githubAPIURL, pr.repoSlug, pr.number, label);
239-
ghSendRequest(HTTPMethod.DELETE, labelUrl);
240-
}
234+
labels
235+
.map!(l => l["name"].get!string)
236+
.filter!(n => n.startsWith("auto-merge"))
237+
.each!(l => pr.removeLabel(l));
238+
}
239+
240+
void addLabels(in ref PullRequest pr, string[] labels)
241+
{
242+
auto labelUrl = "%s/repos/%s/issues/%d/labels"
243+
.format(githubAPIURL, pr.repoSlug, pr.number);
244+
ghSendRequest(HTTPMethod.POST, labelUrl, labels);
245+
}
246+
247+
void removeLabel(in ref PullRequest pr, string label)
248+
{
249+
auto labelUrl = "%s/repos/%s/issues/%d/labels/%s"
250+
.format(githubAPIURL, pr.repoSlug, pr.number, label);
251+
ghSendRequest(HTTPMethod.DELETE, labelUrl);
241252
}
242253

243254
string getUserEmail(string login)

test/comments.d

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import utils;
22

33
import std.format : format;
44

5+
// existing comment
56
unittest
67
{
78
setAPIExpectations(
@@ -26,7 +27,7 @@ unittest
2627
postGitHubHook("dlang_phobos_synchronize_4921.json");
2728
}
2829

29-
// no existing dlang bot comment -> create comment
30+
// no existing dlang bot comment -> create comment and add bug fix label
3031
unittest
3132
{
3233
setAPIExpectations(
@@ -36,6 +37,7 @@ unittest
3637
j = Json.emptyArray;
3738
},
3839
"/bugzilla/buglist.cgi?bug_id=8573&ctype=csv&columnlist=short_desc",
40+
// no bug fix label, since Issues are only referenced but not fixed according to commit messages
3941
"/github/repos/dlang/phobos/issues/4921/comments",
4042
(scope HTTPServerRequest req, scope HTTPServerResponse res){
4143
assert(req.method == HTTPMethod.POST);

test/trello.d

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,21 @@ unittest
4242
postTrelloHook("active_issue_16794.json");
4343
}
4444

45-
// update existing dlang bot comment with related GH PRs
45+
// no existing dlang bot comment -> create comment
4646
unittest
4747
{
4848
setAPIExpectations(
4949
"/github/repos/dlang/dmd/pulls/6359/commits",
5050
"/github/repos/dlang/dmd/issues/6359/comments",
5151
"/bugzilla/buglist.cgi?bug_id=16794&ctype=csv&columnlist=short_desc",
5252
"/github/repos/dlang/dmd/issues/6359/comments",
53+
// action: add bug fix label
54+
"/github/repos/dlang/dmd/issues/6359/labels",
55+
(scope HTTPServerRequest req, scope HTTPServerResponse res){
56+
assert(req.method == HTTPMethod.POST);
57+
assert(req.json[0].get!string == "Bug fix");
58+
res.writeVoidBody;
59+
},
5360
"/trello/1/search?query=name:%22Issue%2016794%22&"~trelloAuth,
5461
"/trello/1/cards/583f517a333add7c28e0cec7/actions?filter=commentCard&"~trelloAuth,
5562
"/trello/1/cards/583f517a333add7c28e0cec7/actions/583f517b91413ef81f1f9d34/comments?"~trelloAuth,
@@ -78,6 +85,13 @@ unittest
7885
"/github/repos/dlang/dmd/issues/6359/comments",
7986
"/bugzilla/buglist.cgi?bug_id=16794&ctype=csv&columnlist=short_desc",
8087
"/github/repos/dlang/dmd/issues/6359/comments",
88+
// action: add bug fix label
89+
"/github/repos/dlang/dmd/issues/6359/labels",
90+
(scope HTTPServerRequest req, scope HTTPServerResponse res){
91+
assert(req.method == HTTPMethod.POST);
92+
assert(req.json[0].get!string == "Bug fix");
93+
res.writeVoidBody;
94+
},
8195
"/trello/1/search?query=name:%22Issue%2016794%22&"~trelloAuth,
8296
"/trello/1/cards/583f517a333add7c28e0cec7/actions?filter=commentCard&"~trelloAuth,
8397
(ref Json j) { j = Json.emptyArray; },

test/utils.d

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,11 +191,14 @@ void setAPIExpectations(Args...)(Args args)
191191
}
192192

193193
void postGitHubHook(string payload, string eventType = "pull_request",
194-
void delegate(ref Json j, scope HTTPClientRequest req) postprocess = null)
194+
void delegate(ref Json j, scope HTTPClientRequest req) postprocess = null,
195+
int line = __LINE__, string file = __FILE__)
195196
{
196197
import std.file : readText;
197198
import std.path : buildPath;
198199

200+
logInfo("Starting test in %s:%d with payload: %s", file, line, payload);
201+
199202
payload = hookDir.buildPath("github", payload);
200203

201204
auto req = requestHTTP(ghTestHookURL, (scope req) {
@@ -228,14 +231,17 @@ void postGitHubHook(string payload, string eventType = "pull_request",
228231
}
229232

230233
void postTrelloHook(string payload,
231-
void delegate(ref Json j, scope HTTPClientRequest req) postprocess = null)
234+
void delegate(ref Json j, scope HTTPClientRequest req) postprocess = null,
235+
int line = __LINE__, string file = __FILE__)
232236
{
233237
import std.file : readText;
234238
import std.path : buildPath;
235239
import dlangbot.trello : getSignature;
236240

237241
payload = hookDir.buildPath("trello", payload);
238242

243+
logInfo("Starting test in %s:%d with payload: %s", file, line, payload);
244+
239245
auto req = requestHTTP(trelloTestHookURL, (scope req) {
240246
req.method = HTTPMethod.POST;
241247

0 commit comments

Comments
 (0)