-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtravis-webhook.js
84 lines (75 loc) · 2.07 KB
/
travis-webhook.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
/* Note: this mostly comes from https://github.com/chrisjaure/travisci-webhook-handler
* modified to support express */
let EventEmitter = require('events').EventEmitter;
let NodeRSA = require('node-rsa');
let statusMessages = {
success: [
'Passed',
'Fixed'
],
failure: [
'Broken',
'Failed',
'Still Failing'
],
start: [
'Pending'
]
};
function bindEmitter(obj, emitter) {
let methods = 'addListener,on,once,removeListener,removeAllListeners,setMaxListeners,listeners,emit';
methods.split(',').forEach(function (fn) {
obj[fn] = emitter[fn].bind(emitter);
});
}
function create(publicKey) {
let handler = function (req, res, next) {
console.log(req.body.payload);
console.log(req.headers);
let repoSlug = req.headers['travis-repo-slug'];
let sig = req.headers['signature'];
if (!sig) {
console.log('No Signature found on request');
return hasError('No Signature found on request');
}
if (!repoSlug) {
console.log('No repo found on request');
return hasError('No repo found on request');
}
console.log(publicKey);
function hasError(msg) {
let err = new Error(msg);
next(err);
}
//if(publicKey) {
let key = new NodeRSA(publicKey, {signingScheme: 'sha1'});
if (!key.verify(JSON.parse(req.body.payload), sig, 'base64', 'base64')) {
console.log('Signed payload does not match signature');
return hasError('Signed payload does not match signature');
}
//}
let result;
try {
result = JSON.parse(req.body.payload);
} catch (err) {
return hasError(err.message);
}
let status = null;
Object.keys(statusMessages).some(key => {
let value = statusMessages[key];
if (value.indexOf(result.status_message) !== -1) {
status = key;
return true;
}
else return false;
});
if (status === null) return console.log('An invalid status message was sent.'), hasError('An invalid status message was sent.');
res.json({ok: true});
handler.emit('*', result);
handler.emit(status, result);
};
handler.emitter = new EventEmitter();
bindEmitter(handler, handler.emitter);
return handler;
}
module.exports = create;