From da82653632ee342686bcde0474b0ab996ced2bf8 Mon Sep 17 00:00:00 2001 From: Sebastian Wilzbach Date: Sun, 25 Dec 2016 19:51:50 +0100 Subject: [PATCH 1/4] Refactor GitHub comment functions --- source/dlangbot/app.d | 4 +++- source/dlangbot/github.d | 26 +++++++++++++++----------- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/source/dlangbot/app.d b/source/dlangbot/app.d index 1717988..20ad838 100644 --- a/source/dlangbot/app.d +++ b/source/dlangbot/app.d @@ -193,7 +193,9 @@ void handlePR(string action, PullRequest pr) auto refs = getIssueRefs(commits); auto descs = getDescriptions(refs); - updateGithubComment(action, refs, descs, pr.commentsURL); + auto comment = pr.getBotComment; + + pr.updateGithubComment(comment, action, refs, descs); if (runTrello) updateTrelloCard(action, pr.htmlURL, refs, descs); diff --git a/source/dlangbot/github.d b/source/dlangbot/github.d index b54fe76..9f7768c 100644 --- a/source/dlangbot/github.d +++ b/source/dlangbot/github.d @@ -40,10 +40,10 @@ string formatComment(R1, R2)(R1 refs, R2 descs) struct Comment { string url, body_; } -Comment getBotComment(string commentsURL) +Comment getBotComment(in ref PullRequest pr) { // the bot may post multiple comments (mention-bot & bugzilla links) - auto res = ghGetRequest(commentsURL) + auto res = ghGetRequest(pr.commentsURL) .readJson[] .find!(c => c["user"]["login"] == "dlang-bot" && c["body"].get!string.canFind("Bugzilla")); if (res.length) @@ -87,9 +87,8 @@ auto ghSendRequest(T...)(HTTPMethod method, string url, T arg) }, url); } -void updateGithubComment(string action, IssueRef[] refs, Issue[] descs, string commentsURL) +void updateGithubComment(in ref PullRequest pr, in ref Comment comment, string action, IssueRef[] refs, Issue[] descs) { - auto comment = getBotComment(commentsURL); logDebug("%s", refs); if (refs.empty) { @@ -108,7 +107,7 @@ void updateGithubComment(string action, IssueRef[] refs, Issue[] descs, string c if (comment.url.length) ghSendRequest(HTTPMethod.PATCH, comment.url, ["body" : msg]); else if (action != "closed" && action != "merged") - ghSendRequest(HTTPMethod.POST, commentsURL, ["body" : msg]); + ghSendRequest(HTTPMethod.POST, pr.commentsURL, ["body" : msg]); } } @@ -232,12 +231,17 @@ Json[] tryMerge(in ref PullRequest pr, MergeMethod method) void checkAndRemoveMergeLabels(Json[] labels, in ref PullRequest pr) { - foreach (label; labels.map!(l => l["name"].get!string).filter!(n => n.startsWith("auto-merge"))) - { - auto labelUrl = "%s/repos/%s/issues/%d/labels/%s" - .format(githubAPIURL, pr.repoSlug, pr.number, label); - ghSendRequest(HTTPMethod.DELETE, labelUrl); - } + labels + .map!(l => l["name"].get!string) + .filter!(n => n.startsWith("auto-merge")) + .each!(l => pr.removeLabel(l)); +} + +void removeLabel(in ref PullRequest pr, string label) +{ + auto labelUrl = "%s/repos/%s/issues/%d/labels/%s" + .format(githubAPIURL, pr.repoSlug, pr.number, label); + ghSendRequest(HTTPMethod.DELETE, labelUrl); } string getUserEmail(string login) From 6a9094918f0080ba23b6b75b6cf624c5a8982d65 Mon Sep 17 00:00:00 2001 From: Sebastian Wilzbach Date: Sun, 25 Dec 2016 19:52:19 +0100 Subject: [PATCH 2/4] Automatically add a bug fix label - fixes #38 --- source/dlangbot/app.d | 3 +++ source/dlangbot/github.d | 7 +++++++ test/comments.d | 10 +++++++++- test/trello.d | 16 +++++++++++++++- 4 files changed, 34 insertions(+), 2 deletions(-) diff --git a/source/dlangbot/app.d b/source/dlangbot/app.d index 20ad838..34fb543 100644 --- a/source/dlangbot/app.d +++ b/source/dlangbot/app.d @@ -197,6 +197,9 @@ void handlePR(string action, PullRequest pr) pr.updateGithubComment(comment, action, refs, descs); + if (refs.length > 0 && comment.body_.length == 0) + pr.addLabels(["Bug fix"]); + if (runTrello) updateTrelloCard(action, pr.htmlURL, refs, descs); diff --git a/source/dlangbot/github.d b/source/dlangbot/github.d index 9f7768c..99d9e70 100644 --- a/source/dlangbot/github.d +++ b/source/dlangbot/github.d @@ -237,6 +237,13 @@ void checkAndRemoveMergeLabels(Json[] labels, in ref PullRequest pr) .each!(l => pr.removeLabel(l)); } +void addLabels(in ref PullRequest pr, string[] labels) +{ + auto labelUrl = "%s/repos/%s/issues/%d/labels" + .format(githubAPIURL, pr.repoSlug, pr.number); + ghSendRequest(HTTPMethod.POST, labelUrl, labels); +} + void removeLabel(in ref PullRequest pr, string label) { auto labelUrl = "%s/repos/%s/issues/%d/labels/%s" diff --git a/test/comments.d b/test/comments.d index 5f0dbb6..2fddb75 100644 --- a/test/comments.d +++ b/test/comments.d @@ -2,6 +2,7 @@ import utils; import std.format : format; +// existing comment unittest { setAPIExpectations( @@ -26,7 +27,7 @@ unittest postGitHubHook("dlang_phobos_synchronize_4921.json"); } -// no existing dlang bot comment -> create comment +// no existing dlang bot comment -> create comment and add bug fix label unittest { setAPIExpectations( @@ -36,6 +37,13 @@ unittest j = Json.emptyArray; }, "/bugzilla/buglist.cgi?bug_id=8573&ctype=csv&columnlist=short_desc", + // action: add bug fix label + "/github/repos/dlang/phobos/issues/4921/labels", + (scope HTTPServerRequest req, scope HTTPServerResponse res){ + assert(req.method == HTTPMethod.POST); + assert(req.json[0].get!string == "Bug fix"); + res.writeVoidBody; + }, "/github/repos/dlang/phobos/issues/4921/comments", (scope HTTPServerRequest req, scope HTTPServerResponse res){ assert(req.method == HTTPMethod.POST); diff --git a/test/trello.d b/test/trello.d index 0f8897b..2b9dc07 100644 --- a/test/trello.d +++ b/test/trello.d @@ -42,7 +42,7 @@ unittest postTrelloHook("active_issue_16794.json"); } -// update existing dlang bot comment with related GH PRs +// no existing dlang bot comment -> create comment unittest { setAPIExpectations( @@ -50,6 +50,13 @@ unittest "/github/repos/dlang/dmd/issues/6359/comments", "/bugzilla/buglist.cgi?bug_id=16794&ctype=csv&columnlist=short_desc", "/github/repos/dlang/dmd/issues/6359/comments", + // action: add bug fix label + "/github/repos/dlang/dmd/issues/6359/labels", + (scope HTTPServerRequest req, scope HTTPServerResponse res){ + assert(req.method == HTTPMethod.POST); + assert(req.json[0].get!string == "Bug fix"); + res.writeVoidBody; + }, "/trello/1/search?query=name:%22Issue%2016794%22&"~trelloAuth, "/trello/1/cards/583f517a333add7c28e0cec7/actions?filter=commentCard&"~trelloAuth, "/trello/1/cards/583f517a333add7c28e0cec7/actions/583f517b91413ef81f1f9d34/comments?"~trelloAuth, @@ -78,6 +85,13 @@ unittest "/github/repos/dlang/dmd/issues/6359/comments", "/bugzilla/buglist.cgi?bug_id=16794&ctype=csv&columnlist=short_desc", "/github/repos/dlang/dmd/issues/6359/comments", + // action: add bug fix label + "/github/repos/dlang/dmd/issues/6359/labels", + (scope HTTPServerRequest req, scope HTTPServerResponse res){ + assert(req.method == HTTPMethod.POST); + assert(req.json[0].get!string == "Bug fix"); + res.writeVoidBody; + }, "/trello/1/search?query=name:%22Issue%2016794%22&"~trelloAuth, "/trello/1/cards/583f517a333add7c28e0cec7/actions?filter=commentCard&"~trelloAuth, (ref Json j) { j = Json.emptyArray; }, From f293deeb52650fdd2283933d98d94defbb331686 Mon Sep 17 00:00:00 2001 From: Sebastian Wilzbach Date: Sun, 25 Dec 2016 20:03:02 +0100 Subject: [PATCH 3/4] Improve logging for tests - output test linenumbers --- test/utils.d | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/test/utils.d b/test/utils.d index 5b5b86f..4945484 100644 --- a/test/utils.d +++ b/test/utils.d @@ -191,11 +191,14 @@ void setAPIExpectations(Args...)(Args args) } void postGitHubHook(string payload, string eventType = "pull_request", - void delegate(ref Json j, scope HTTPClientRequest req) postprocess = null) + void delegate(ref Json j, scope HTTPClientRequest req) postprocess = null, + int line = __LINE__, string file = __FILE__) { import std.file : readText; import std.path : buildPath; + logInfo("Starting test in %s:%d with payload: %s", file, line, payload); + payload = hookDir.buildPath("github", payload); auto req = requestHTTP(ghTestHookURL, (scope req) { @@ -228,7 +231,8 @@ void postGitHubHook(string payload, string eventType = "pull_request", } void postTrelloHook(string payload, - void delegate(ref Json j, scope HTTPClientRequest req) postprocess = null) + void delegate(ref Json j, scope HTTPClientRequest req) postprocess = null, + int line = __LINE__, string file = __FILE__) { import std.file : readText; import std.path : buildPath; @@ -236,6 +240,8 @@ void postTrelloHook(string payload, 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; From 2ac5ffab805d3d77d2713a26aa6155ce6b421488 Mon Sep 17 00:00:00 2001 From: Martin Nowak Date: Sat, 11 Feb 2017 23:23:15 +0100 Subject: [PATCH 4/4] only tag as bug fix when the referenced issues git fixed --- source/dlangbot/app.d | 3 ++- test/comments.d | 8 +------- 2 files changed, 3 insertions(+), 8 deletions(-) diff --git a/source/dlangbot/app.d b/source/dlangbot/app.d index 34fb543..c5c8b9a 100644 --- a/source/dlangbot/app.d +++ b/source/dlangbot/app.d @@ -169,6 +169,7 @@ void githubHook(HTTPServerRequest req, HTTPServerResponse res) void handlePR(string action, PullRequest pr) { + import std.algorithm : any; import vibe.core.core : setTimer; Json[] commits; @@ -197,7 +198,7 @@ void handlePR(string action, PullRequest pr) pr.updateGithubComment(comment, action, refs, descs); - if (refs.length > 0 && comment.body_.length == 0) + if (refs.any!(r => r.fixed) && comment.body_.length == 0) pr.addLabels(["Bug fix"]); if (runTrello) diff --git a/test/comments.d b/test/comments.d index 2fddb75..682b4f0 100644 --- a/test/comments.d +++ b/test/comments.d @@ -37,13 +37,7 @@ unittest j = Json.emptyArray; }, "/bugzilla/buglist.cgi?bug_id=8573&ctype=csv&columnlist=short_desc", - // action: add bug fix label - "/github/repos/dlang/phobos/issues/4921/labels", - (scope HTTPServerRequest req, scope HTTPServerResponse res){ - assert(req.method == HTTPMethod.POST); - assert(req.json[0].get!string == "Bug fix"); - res.writeVoidBody; - }, + // no bug fix label, since Issues are only referenced but not fixed according to commit messages "/github/repos/dlang/phobos/issues/4921/comments", (scope HTTPServerRequest req, scope HTTPServerResponse res){ assert(req.method == HTTPMethod.POST);