-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
38 lines (33 loc) · 1.14 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
const bodyParser = require('body-parser');
const express = require('express');
const winston = require('winston');
const _object = require('lodash/object');
const { get } = _object;
const verifySignatureMiddleware = require('./middleware/verify-signature');
const fetchCommit = require('./lib/fetch-commit');
const processDiff = require('./lib/process-diff');
const app = express();
app.use(bodyParser.json());
app.set('port', (process.env.PORT || 5000));
app.post('/webhook', verifySignatureMiddleware, (req, res) => {
if (req.get('X-Github-Event') !== 'push') {
return res.sendStatus(200);
}
let { after: newSha, ref } = req.body;
let isMaster = ref === 'refs/heads/master';
if (isMaster && newSha) {
let commitsUrl = get(req, 'body.repository.commits_url', '').replace('{/sha}', `/${newSha}`);
fetchCommit(commitsUrl)
.then(processDiff)
.catch((err) => {
winston.error(err);
return res.sendStatus(500);
});
return res.sendStatus(201);
}
return res.sendStatus(200);
});
app.listen(app.get('port'), () => {
winston.info('Node app is running on port', app.get('port'));
});
module.exports = app;