-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.js
49 lines (39 loc) · 1.41 KB
/
server.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
var express = require('express');
const rp = require('request-promise');
const Promise = require('bluebird');
var DEFAULT_PORT = 8787;
var app = express();
// serve static files from the "public" dir
app.use(express.static('public'));
// read in the config file
var config = require('./config.json');
if ((!config.accessToken) || (config.accessToken === '')) {
throw new Error('No "accessToken" supplied in config.json!');
}
var port = config.port || DEFAULT_PORT;
app.get('/prs', function getPrs(req, res) {
const getPrsRequests = config.repositories.reduce(getPullRequestsFromGithub, {});
Promise.props(getPrsRequests)
.then((pullRequests) => res.status(200).json(pullRequests))
.catch((err) => {
console.error('Github API error', err);
res.status(500).end();
});
});
function getPullRequestsFromGithub(acc, repository) {
const options = {
method: 'GET',
uri: `https://api.github.com/repos/${repository}/pulls`,
headers: {
'Authorization': 'token ' + config.accessToken,
'User-Agent': 'PRadiator app', // https://developer.github.com/v3/#user-agent-required
'Accept': 'application/vnd.github.v3+json'
},
json: true,
};
acc[repository] = rp(options);
return acc;
}
app.listen(port, function() {
console.log('pradiator server started at http://localhost:' + port + '/');
});