-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
51 lines (41 loc) · 1.46 KB
/
index.js
File metadata and controls
51 lines (41 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
'use strict';
const crypto = require('crypto');
const child_process = require('child_process');
const _ = require('lodash');
const Koa = require('koa');
const koaBody = require('koa-body');
const app = new Koa();
const config = require('./config');
app.use(koaBody());
function exec (exe) {
return new Promise(function (resolve) {
child_process.execFile(exe.file, exe.args, exe.options, function (err, res) {
if (err) {
throw err;
}
return resolve(res);
});
});
}
app.use(function *(next) {
const payload = this.request.body;
const hmac = crypto.createHmac('sha1', process.env.GITHUB_SECRET);
try {
hmac.update(JSON.stringify(payload));
} catch (e) {
this.throw(400, 'Unable to parse the request body');
}
const calculatedSignature = `sha1=${ hmac.digest('hex') }`;
this.assert(calculatedSignature === this.get('x-hub-signature'), 400, 'Signatures dows not match');
const repo = _.get(this.request.body, 'repository.full_name');
const ref = _.get(this.request.body, 'ref');
this.assert(repo && ref, 400, 'Unable to get repository full name and branch name');
const exe = _.get(config, [repo, ref]);
this.assert(exe && exe.file, 400, 'Unable to find a configuration for', repo, ref);
yield exec(exe);
this.status = 200;
yield next;
});
app.listen(process.env.PORT, function () {
console.log('listening on', process.env.PORT);
});