-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
126 lines (111 loc) · 3.18 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
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
'use strict';
var Hapi = require('hapi');
var Boom = require('boom');
var req = require('request');
var Good = require('good');
var async = require('async');
var fs = require('fs');
var _ = require('lodash');
var server = new Hapi.Server();
server.connection({ port: process.env.PORT || 3000 });
var parseStatus = function (data) {
// If we can parse a GH state, return a state ID
// Otherwise return null
var state;
switch (data.state) {
case 'success':
state = 1;
break;
case 'pending':
state = 2;
break;
case 'failure':
state = 3;
break;
case 'error':
state = 99;
break;
}
return state;
};
server.route({
method: 'GET',
path: '/',
handler: function (request, reply) {
try {
// Load the config every time, so changes are picked up on the fly
var repoConfig = fs.readFileSync('./repos.json');
} catch (e) {
var err = 'Can\'t find repos.json. Make sure there is one.';
return reply(Boom.badImplementation(err));
}
try {
var repos = JSON.parse(repoConfig);
} catch (e) {
err = 'repos.json is not a valid JSON.';
return reply(Boom.badImplementation(err));
}
var tasks = [];
var token = 'token ' + process.env.GH_TOKEN;
_.forEach(repos, function (r) {
var task = function (cb) {
// Branch defaults to master if none is defined.
var branch = (r.branch || 'master');
var options = {
url: 'https://api.github.com/repos/' + r.repo + '/commits/' + branch + '/status',
method: 'GET',
headers: {
'User-Agent': 'node.js',
'Accept': 'application/vnd.github.v3+json',
'Authorization': token
}
};
req(options, function (err, res, body) {
body = JSON.parse(body);
var error;
// Turn the Github error code into something more meaningful.
if (res.statusCode === 401) {
// This leads to a single 500 error.
error = 'Can\'t authenticate. Are you using a valid GH token?';
return cb(error);
} else if (res.statusCode === 404) {
// This shouldn't lead to an error on the API side. It will just return null
error = 'Can\'t access the \'' + branch + '\' branch of ' + r.repo + '. Either the branch or repo doesn\'t exist, or the app doesn\'t have the right access token.';
}
if (err || res.statusCode !== 200) {
console.error(error || err || res);
}
var repoState = parseStatus(body);
cb(null, repoState);
});
};
tasks.push(task);
});
async.parallel(tasks, function (err, results) {
if (err) {
reply(Boom.badImplementation(err));
} else {
reply(null, results);
}
});
}
});
server.register({
register: Good,
options: {
reporters: [{
reporter: require('good-console'),
events: {
response: '*',
log: '*'
}
}]
}
}, (err) => {
if (err) {
throw err; // something bad happened loading the plugin
}
server.start(() => {
server.log('info', 'Server running at: ' + server.info.uri);
});
});