Skip to content

Commit 8153e4d

Browse files
authored
Merge pull request #76 from MartinNowak/scheduler
use command line argument to run daily cron tasks merged-on-behalf-of: Sebastian Wilzbach <sebi.wilzbach@gmail.com>
2 parents 9c60f50 + ba7b247 commit 8153e4d

File tree

6 files changed

+59
-47
lines changed

6 files changed

+59
-47
lines changed

.travis.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,15 @@ os:
33
- linux
44
language: d
55
d:
6+
- dmd-nightly
67
- dmd-beta
78
- dmd
9+
- ldc-beta
810
- ldc
911

1012
matrix:
13+
allow_failures:
14+
- d: ldc
1115
include:
1216
- d: dmd-2.070.2 # the deployment compiler
1317
env: COVERAGE=true

dub.sdl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ copyright "Copyright © 2015, Martin Nowak"
44
authors "Martin Nowak"
55
dependency "vibe-d" version="~>0.7.30"
66
configuration "default" {
7-
versions "VibeDefaultMain"
7+
versions "VibeCustomMain"
88
targetType "executable"
99
}
1010
configuration "unittest" {

source/dlangbot/app.d

Lines changed: 42 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ public import dlangbot.bugzilla : bugzillaURL;
77
public import dlangbot.github : githubAPIURL, githubAuth, hookSecret;
88
public import dlangbot.trello : trelloAPIURL, trelloAuth, trelloSecret;
99

10-
string cronDailySecret;
11-
1210
import std.datetime : Clock, days, Duration, minutes, seconds, SysTime;
1311

12+
import vibe.core.args;
13+
import vibe.core.core;
1414
import vibe.core.log;
1515
import vibe.data.json;
1616
import vibe.http.client : HTTPClient;
@@ -29,32 +29,6 @@ Duration prInactivityDur = 90.days; // PRs with no activity within X days will g
2929

3030
enum trelloHookURL = "https://dlang-bot.herokuapp.com/trello_hook";
3131

32-
version(unittest){} else
33-
shared static this()
34-
{
35-
import std.process : environment;
36-
import vibe.core.args : readOption;
37-
38-
auto settings = new HTTPServerSettings;
39-
settings.port = 8080;
40-
readOption("port|p", &settings.port, "Sets the port used for serving.");
41-
startServer(settings);
42-
43-
githubAuth = "token "~environment["GH_TOKEN"];
44-
trelloSecret = environment["TRELLO_SECRET"];
45-
trelloAuth = "key="~environment["TRELLO_KEY"]~"&token="~environment["TRELLO_TOKEN"];
46-
hookSecret = environment["GH_HOOK_SECRET"];
47-
cronDailySecret = environment["CRON_DAILY_SECRET"];
48-
49-
// workaround for stupid openssl.conf on Heroku
50-
if (environment.get("DYNO") !is null)
51-
{
52-
HTTPClient.setTLSSetupCallback((ctx) {
53-
ctx.useTrustedCertificateFile("/etc/ssl/certs/ca-certificates.crt");
54-
});
55-
}
56-
}
57-
5832
void startServer(HTTPServerSettings settings)
5933
{
6034
import vibe.core.core : vibeVersionString;
@@ -71,7 +45,6 @@ void startServer(HTTPServerSettings settings)
7145
.post("/github_hook", &githubHook)
7246
.match(HTTPMethod.HEAD, "/trello_hook", (req, res) => res.writeVoidBody)
7347
.post("/trello_hook", &trelloHook)
74-
.get("/cron_daily", &cronDaily)
7548
;
7649

7750
HTTPClient.setUserAgentString("dlang-bot vibe.d/"~vibeVersionString);
@@ -152,19 +125,13 @@ void githubHook(HTTPServerRequest req, HTTPServerResponse res)
152125

153126
//==============================================================================
154127

155-
void cronDaily(HTTPServerRequest req, HTTPServerResponse res)
128+
void cronDaily()
156129
{
157-
enforceBadRequest(req.query.length > 0, "No repo slugs provided");
158-
enforceHTTP(req.query.get("secret") == cronDailySecret,
159-
HTTPStatus.unauthorized, "Invalid or no secret provided");
160-
161-
foreach (ref slug; req.query.getAll("repo"))
130+
foreach (repo; ["dlang/phobos"])
162131
{
163-
logInfo("running cron.daily for: %s", slug);
164-
runTaskHelper(&searchForInactivePrs, slug, prInactivityDur);
132+
logInfo("running cron.daily for: %s", repo);
133+
searchForInactivePrs(repo, prInactivityDur);
165134
}
166-
167-
return res.writeBody("OK");
168135
}
169136

170137
//==============================================================================
@@ -213,3 +180,39 @@ void handlePR(string action, PullRequest* _pr)
213180
if (runTrello)
214181
updateTrelloCard(action, pr.htmlURL, refs, descs);
215182
}
183+
184+
//==============================================================================
185+
186+
version (unittest) {}
187+
else void main(string[] args)
188+
{
189+
import std.process : environment;
190+
import vibe.core.args : readOption;
191+
192+
githubAuth = "token "~environment["GH_TOKEN"];
193+
trelloSecret = environment["TRELLO_SECRET"];
194+
trelloAuth = "key="~environment["TRELLO_KEY"]~"&token="~environment["TRELLO_TOKEN"];
195+
hookSecret = environment["GH_HOOK_SECRET"];
196+
197+
// workaround for stupid openssl.conf on Heroku
198+
if (environment.get("DYNO") !is null)
199+
{
200+
HTTPClient.setTLSSetupCallback((ctx) {
201+
ctx.useTrustedCertificateFile("/etc/ssl/certs/ca-certificates.crt");
202+
});
203+
}
204+
205+
bool runDailyCron;
206+
auto settings = new HTTPServerSettings;
207+
settings.port = 8080;
208+
readOption("port|p", &settings.port, "Sets the port used for serving.");
209+
readOption("cron-daily", &runDailyCron, "Run daily cron tasks.");
210+
if (!finalizeCommandLineOptions())
211+
return;
212+
if (runDailyCron)
213+
return cronDaily();
214+
215+
startServer(settings);
216+
lowerPrivileges();
217+
runEventLoop();
218+
}

source/dlangbot/github.d

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,7 @@ struct PullRequest
420420
}
421421
Branch base, head;
422422
enum State { open, closed }
423-
enum MergeableState { clean, dirty, unstable, unknown }
423+
enum MergeableState { clean, dirty, unstable, blocked, unknown }
424424
@byName State state;
425425
uint number;
426426
string title;

test/cronjob.d

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,6 @@ unittest
4747
},
4848
);
4949

50-
openUrl("/cron_daily?repo=dlang/phobos&secret=" ~ cronDailySecret, "OK");
50+
import dlangbot.app : cronDaily;
51+
cronDaily();
5152
}

test/utils.d

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ shared static this()
2929
githubAuth = "GH_DUMMY_AUTH_TOKEN";
3030
hookSecret = "GH_DUMMY_HOOK_SECRET";
3131
trelloAuth = "key=01234&token=abcde";
32-
cronDailySecret = "dummyCronSecret";
3332

3433
// start our hook server
3534
auto settings = new HTTPServerSettings;
@@ -124,9 +123,12 @@ auto payloadServer(scope HTTPServerRequest req, scope HTTPServerResponse res)
124123
if (expectation.jsonHandler !is null)
125124
expectation.jsonHandler(payloadJson);
126125

127-
payload = payloadJson.toString;
126+
return res.writeJsonBody(payloadJson);
127+
}
128+
else
129+
{
130+
return res.writeBody(payload);
128131
}
129-
return res.writeBody(payload);
130132
}
131133
}
132134

@@ -168,18 +170,19 @@ struct APIExpectation
168170
}
169171
}
170172

171-
APIExpectation[] apiExpectations;
173+
__gshared APIExpectation[] apiExpectations;
172174

173175
void setAPIExpectations(Args...)(Args args)
174176
{
175177
import std.functional : toDelegate;
176178
import std.traits : Parameters;
179+
synchronized {
177180
apiExpectations.length = 0;
178181
foreach (i, arg; args)
179182
{
180183
static if (is(Args[i] : string))
181184
{
182-
apiExpectations ~= APIExpectation(arg);
185+
apiExpectations ~= APIExpectation(arg);
183186
}
184187
else
185188
{
@@ -200,6 +203,7 @@ void setAPIExpectations(Args...)(Args args)
200203
apiExpectations[$ - 1].reqHandler is null, "Either provide a reqHandler or a jsonHandler");
201204
}
202205
}
206+
}
203207
}
204208

205209
void postGitHubHook(string payload, string eventType = "pull_request",

0 commit comments

Comments
 (0)