Skip to content

Commit e603a36

Browse files
committed
Add first iteration of a cron.daily task
1 parent df91151 commit e603a36

File tree

10 files changed

+428
-92
lines changed

10 files changed

+428
-92
lines changed

source/dlangbot/app.d

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@ public import dlangbot.github : githubAPIURL, githubAuth, hookSecret;
88
public import dlangbot.travis : travisAPIURL;
99
public import dlangbot.trello : trelloAPIURL, trelloAuth, trelloSecret;
1010

11-
import std.datetime : Clock, Duration, minutes, seconds, SysTime;
11+
string cronDailySecret;
12+
13+
import std.datetime : Clock, days, Duration, minutes, seconds, SysTime;
1214

1315
import vibe.core.log;
1416
import vibe.data.json;
1517
import vibe.http.client : HTTPClient;
16-
import vibe.http.common : HTTPMethod;
18+
import vibe.http.common : enforceBadRequest, enforceHTTP, HTTPMethod, HTTPStatus;
1719
import vibe.http.router : URLRouter;
1820
import vibe.http.server : HTTPServerRequest, HTTPServerResponse, HTTPServerSettings;
1921
import vibe.stream.operations : readAllUTF8;
@@ -24,6 +26,8 @@ bool runTrello = true;
2426
Duration timeBetweenFullPRChecks = 5.minutes; // this should never be larger 30 mins on heroku
2527
Throttler!(typeof(&searchForAutoMergePrs)) prThrottler;
2628

29+
Duration prInactivityDur = 90.days; // PRs with no activity within X days will get flagged
30+
2731
enum trelloHookURL = "https://dlang-bot.herokuapp.com/trello_hook";
2832

2933
version(unittest){} else
@@ -42,6 +46,7 @@ shared static this()
4246
trelloAuth = "key="~environment["TRELLO_KEY"]~"&token="~environment["TRELLO_TOKEN"];
4347
hookSecret = environment["GH_HOOK_SECRET"];
4448
travisAuth = "token " ~ environment["TRAVIS_TOKEN"];
49+
cronDailySecret = environment["CRON_DAILY_SECRET"];
4550

4651
// workaround for stupid openssl.conf on Heroku
4752
if (environment.get("DYNO") !is null)
@@ -68,6 +73,7 @@ void startServer(HTTPServerSettings settings)
6873
.post("/github_hook", &githubHook)
6974
.match(HTTPMethod.HEAD, "/trello_hook", (req, res) => res.writeVoidBody)
7075
.post("/trello_hook", &trelloHook)
76+
.get("/cron_daily", &cronDaily)
7177
;
7278

7379
HTTPClient.setUserAgentString("dlang-bot vibe.d/"~vibeVersionString);
@@ -155,7 +161,7 @@ void githubHook(HTTPServerRequest req, HTTPServerResponse res)
155161
case "opened", "reopened", "synchronize", "labeled", "edited":
156162

157163
auto pullRequest = json["pull_request"].deserializeJson!PullRequest;
158-
runTaskHelper(toDelegate(&handlePR), action, pullRequest);
164+
runTaskHelper(&handlePR, action, &pullRequest);
159165
return res.writeBody("handled");
160166
default:
161167
return res.writeBody("ignored");
@@ -167,11 +173,30 @@ void githubHook(HTTPServerRequest req, HTTPServerResponse res)
167173

168174
//==============================================================================
169175

170-
void handlePR(string action, PullRequest pr)
176+
void cronDaily(HTTPServerRequest req, HTTPServerResponse res)
177+
{
178+
enforceBadRequest(req.query.length > 0, "No repo slugs provided");
179+
enforceHTTP(req.query.get("secret") == cronDailySecret,
180+
HTTPStatus.unauthorized, "Invalid or no secret provided");
181+
182+
foreach (ref slug; req.query.getAll("repo"))
183+
{
184+
logInfo("running cron.daily for: %s", slug);
185+
runTaskHelper(&searchForInactivePrs, slug, prInactivityDur);
186+
}
187+
188+
return res.writeBody("OK");
189+
}
190+
191+
//==============================================================================
192+
193+
void handlePR(string action, PullRequest* _pr)
171194
{
172195
import std.algorithm : any;
173196
import vibe.core.core : setTimer;
174197

198+
const PullRequest pr = *_pr;
199+
175200
Json[] commits;
176201

177202
if (action == "labeled" || action == "synchronize")

0 commit comments

Comments
 (0)