-
Notifications
You must be signed in to change notification settings - Fork 1
/
routes.js
92 lines (74 loc) · 2.11 KB
/
routes.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
const fs = require('fs');
const app = require('express');
const utils = require('./lib/utils');
const Table = require('./lib/table');
const _object = require('lodash/object');
const collection = require('lodash/collection');
const fetchCommit = require('./lib/fetch-commit');
const processDiff = require('./lib/process-diff');
const verifySignature = require('./middleware/verify-signature');
const { get } = _object;
const { logger } = utils;
const { sortBy } = collection;
/**
* Express router.
*/
const router = app.Router();
/**
* Get API information.
*/
router.get('/', (req, res) => {
fs.readFile('./package.json', (err, buffer) => {
let string = buffer.toString();
let package = JSON.parse(string);
let { name, version, description } = package;
let repository = package.repository.url;
res.json({ name, version, description, repository });
});
});
/**
* Get all companies.
*/
router.get('/companies', (req, res) => {
let companiesTable = new Table('Companies');
companiesTable
.all()
.then(result => {
let companies = result.map(company => {
return {
name: company.fields['Name'],
website: company.fields['Website'],
location: company.fields['Location Text'],
description: company.fields['Description']
};
});
let sortedList = sortBy(companies, company => company.name.toLowerCase());
return res.json(sortedList);
})
.catch(err => {
logger.error(err);
return res.serverError();
});
});
/**
* Webhook URL for GitHub.
*/
router.post('/webhook', verifySignature, (req, res) => {
if (req.get('X-Github-Event') !== 'push') {
return res.accepted();
}
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 => {
logger.error(err);
return res.serverError();
});
return res.created();
}
return res.ok();
});
module.exports = router;