This repository has been archived by the owner on Jun 21, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
101 lines (87 loc) · 2.58 KB
/
index.js
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
const http = require('http');
const https = require('https');
const url = require('url');
const cp = require('child_process');
function c () {
var args = Array.prototype.slice.call(arguments);
var method = args.shift();
args.unshift(new Date().toISOString());
console[method].apply(this, args);
}
const logger = {
info: c.bind(this, 'log'),
error: c.bind(this, 'error')
};
function reply (res, code) {
var msg = http.STATUS_CODES[code];
logger.info(`response ${code} ${msg}`);
res.writeHead(code);
res.end(msg);
}
function validate (u, state, cb) {
logger.info('validating webhook', u, state);
const cbUrl = url.parse(u);
const req = https.request({
method: 'POST',
hostname: cbUrl.hostname,
path: cbUrl.path
}, (res) => {
cb(null, res.statusCode);
});
req.on('error', cb);
req.write(JSON.stringify({ state: state }));
req.end();
}
const server = http.createServer(function (req, res) {
logger.info(`request ${req.method} ${req.url}`);
if (req.method === 'POST') {
const parsedUrl = url.parse(req.url, true);
if (parsedUrl.pathname === '/hook' && parsedUrl.query.token === process.env.DHH_TOKEN) {
var body = '';
req.on('data', (chunk) => {
body += chunk;
});
req.on('end', () => {
try {
const payload = JSON.parse(body);
if (payload.repository.repo_name === process.env.DHH_REPO && payload.push_data.tag === process.env.DHH_TAG) {
logger.info('executing command');
cp.exec(process.env.DHH_CMD, {
cwd: process.env.DHH_CWD
}, function (err, stdout, stderr) {
var state = 'success';
var code = 200;
if (err) {
logger.error(err);
state = 'failure';
code = 500;
} else {
logger.info(stdout);
logger.error(stderr);
}
validate(payload.callback_url, state, (err, statusCode) => {
if (err) {
logger.error(err);
}
logger.info(`webhook callback response: ${statusCode}`);
reply(res, code);
});
});
} else {
throw new Error('Mismatched repo or tag in payload');
}
} catch (e) {
logger.error(e);
reply(res, 400);
}
});
} else {
reply(res, 404);
}
} else {
reply(res, 404);
}
});
server.listen(process.env.DHH_PORT, () => {
logger.info('Server listening on ', JSON.stringify(server.address()));
});