-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathindex.js
More file actions
52 lines (38 loc) · 1.53 KB
/
index.js
File metadata and controls
52 lines (38 loc) · 1.53 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
52
'use strict';
var express = require('express'),
app = express(),
server = require('http').createServer(app),
bodyParser = require('body-parser'),
secret = process.env.DEPLOY_LISTENER_SECRET,
verifyGitHubSignature = require('./lib/verifyGitHubSignature'),
getConfig = require('./lib/getConfig'),
deployTasks = require('./lib/deployTasks');
app.use(bodyParser.urlencoded({extended: false}));
app.use(bodyParser.json());
verifyGitHubSignature.setSecret(secret);
getConfig(function (config) {
deployTasks.initConfig(config);
var branch = config.branch || 'master';
app.post(config.route, function (req, res) {
// Checking if request is authentic
if (verifyGitHubSignature.ofRequest(req)) {
// If master was updated, do stuff
if (req.body.ref && req.body.ref === `refs/heads/${branch}`) {
console.log('Valid payload! Running commands');
deployTasks.run(function () {
res.status(200).send();
});
} else {
// if other branches were updated, send 200 only to make github happy...
console.log(`Received payload unrelated to ${branch} branch`);
res.status(200).send();
}
} else {
console.warn('Received payload with an invalid secret');
res.status(403).send();
}
});
server.listen(config.port, function () {
console.log(`Listening for webhook events on port ${config.port}`);
});
});