Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion source/dlangbot/app.d
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -193,7 +194,12 @@ 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 (refs.any!(r => r.fixed) && comment.body_.length == 0)
pr.addLabels(["Bug fix"]);

if (runTrello)
updateTrelloCard(action, pr.htmlURL, refs, descs);
Expand Down
33 changes: 22 additions & 11 deletions source/dlangbot/github.d
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
{
Expand All @@ -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]);
}
}

Expand Down Expand Up @@ -232,12 +231,24 @@ 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 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"
.format(githubAPIURL, pr.repoSlug, pr.number, label);
ghSendRequest(HTTPMethod.DELETE, labelUrl);
}

string getUserEmail(string login)
Expand Down
4 changes: 3 additions & 1 deletion test/comments.d
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import utils;

import std.format : format;

// existing comment
unittest
{
setAPIExpectations(
Expand All @@ -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(
Expand All @@ -36,6 +37,7 @@ unittest
j = Json.emptyArray;
},
"/bugzilla/buglist.cgi?bug_id=8573&ctype=csv&columnlist=short_desc",
// 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);
Expand Down
16 changes: 15 additions & 1 deletion test/trello.d
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,21 @@ unittest
postTrelloHook("active_issue_16794.json");
}

// update existing dlang bot comment with related GH PRs
// no existing dlang bot comment -> create comment
unittest
{
setAPIExpectations(
"/github/repos/dlang/dmd/pulls/6359/commits",
"/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,
Expand Down Expand Up @@ -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; },
Expand Down
10 changes: 8 additions & 2 deletions test/utils.d
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might be a bit noisy, but helps a lot to identify which test failed.

payload = hookDir.buildPath("github", payload);

auto req = requestHTTP(ghTestHookURL, (scope req) {
Expand Down Expand Up @@ -228,14 +231,17 @@ 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;
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;

Expand Down