This repository has been archived by the owner on Jan 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
server.js
329 lines (289 loc) · 10.9 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
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/**
*
*
* pdf.js bot server
*
* Copyright (c) 2011 Mozilla Foundation
* Please see LICENSE file for license information.
*
*
**/
// Modules
var fs = require('fs'),
net = require('net'),
express = require('express'),
app = express.createServer(),
path = require('path'),
request = require('request'),
exec = require('child_process').exec,
github = require('./lib/github'),
queue = require('./lib/queue');
var config = {},
configFile = 'config.json';
//
// Environment vars
//
if (!process.env.GITHUB_CREDENTIALS) {
console.log('Environment variable GITHUB_CREDENTIALS not configured');
console.log('Example: GITHUB_CREDENTIALS=pdfjsbot:password123\n');
process.exit();
}
if (process.env.PDFJSBOT_STAGING === 'yes') {
configFile = 'config_staging.json';
}
console.log((new Date())+': reading configurations from file "'+configFile+'"');
config = JSON.parse( fs.readFileSync(configFile).toString() );
config.github_creds = process.env.GITHUB_CREDENTIALS;
//
// Main loop
//
setupServer(function(){
processNewCommands(); // first call
setInterval(function(){
processNewCommands();
}, config.check_interval_secs*1000);
});
//
// Set up server, etc
//
function setupServer(callback){
// HTTP server
app.use(express.bodyParser());
app.use(app.router);
app.use(express.static(config.tmp_path));
app.listen(config.server_port);
// POST /newmerge handler
app.post('/newmerge', function(req, res){
var payload = JSON.parse(req.body.payload);
console.log((new Date())+': received POST /newmerge');
if (payload.ref !== 'refs/heads/master') {
console.log((new Date())+': /newmerge not on master branch. skipping update of gh-pages');
return;
}
queue.push(function(){
console.log((new Date())+': processing POST /newmerge');
runScript(
{
script: 'web',
main_url: 'git://github.com/'+config.main_repo+'.git',
pull_url: '',
pull_sha: '',
ref_url: '',
tmp_path: config.tmp_path,
timeout: config.process_timeout_mins*60*1000,
},
function(output){
console.log((new Date())+': done POST /newmerge');
queue.next();
}
);
}); // queue push
}); // POST /newmerge
getPublicIP(function(){
config.server_url = 'http://'+config.server_host+':'+config.server_port;
console.log((new Date())+': Server listening at '+config.server_url+', serving dir '+config.tmp_path);
// Github-specific
github.setConfig(config);
github.buildWhitelist(function(){
console.log((new Date())+': found '+config.whitelist.length+' whitelisted users');
if (callback) callback();
}); // buildWhitelist
}); // getPublicIP
}; // setupServer
//
// getPublicIP()
//
function getPublicIP(callback){
request.get('http://ifconfig.me/ip', function(error, response, body){
config.server_host = body.replace(/\s|\n/g, '');
if (callback) callback();
});
};
//
// processNewCommands()
// Scan Github for commands in open pull requests and process them
//
function processNewCommands(){
// Loop over every new command in all monitored pull requests
github.forEachNewCommand(function(cmd){
var t1 = new Date(),
liveOutputFile = cmd.id+'.txt';
console.log((new Date())+': processing new command "'+cmd.command+'" in Pull #'+cmd.pull_number+' from @'+cmd.user+' (id:'+cmd.id+'), queue size: '+queue.size());
github.postStartMessage(cmd, 'Processing command **'+(cmd.command||'(empty)')+'** by user _'+cmd.user+'_. Queue size: '+queue.size()+'\n\n'+
'Live script output is available (after queueing is done) at: '+config.server_url+'/'+liveOutputFile);
//
// Process each command in queue
// *** DON'T forget to add queue.next() after the innermost callback of each command/script!
//
queue.push(function(){
var endMessage = '';
switch (cmd.command) {
//
// Process 'test' command
//
case 'test':
runScript(
{
script: cmd.command,
main_url: 'git://github.com/'+config.main_repo+'.git',
pull_url: cmd.pull_url,
pull_sha: cmd.pull_sha,
ref_url: config.ref_repo,
tmp_path: config.tmp_path,
timeout: config.process_timeout_mins*60*1000,
output_file: liveOutputFile
},
function(output){
var msg = '';
output = '\n'+output; // hack to get first line into code below
output = output.replace(/\n/g, '\n '); // reformat output as Github/Markdown code
// Tests passed?
if (output.search("All tests passed") > -1 && // 'make test'
output.search("files checked, no errors found") > -1) { // 'make lint'
if (output.search("WARNING") < 0) {
msg = '**All tests passed.**';
}
else {
msg = '# WARNING(s) found';
msg += '\n\n**All tests passed,** but with **WARNING(s)**.';
msg += '\n\nMake sure to _read them!_ :).';
}
}
// Tests DID NOT pass
else {
msg = '# ERROR(s) found';
if (path.existsSync(config.tmp_path+'/tests/'+cmd.pull_sha+'/eq.log')) {
var url = config.server_url+'/tests/'+cmd.pull_sha+'/reftest-analyzer.xhtml'+
'#web=/tests/'+cmd.pull_sha+'/eq.log';
msg += '\n\n**ATTENTION:** There was a _snapshot difference:_\n'+url;
}
} // if !tests passed
msg += '\n\nOutput:\n\n'+output;
github.postEndMessage(cmd, (new Date())-t1, msg);
console.log((new Date())+': done processing command "'+cmd.command+'" in Pull #'+cmd.pull_number+' from @'+cmd.user+' (id:'+cmd.id+')');
queue.next();
}
); // test
break;
//
// Process 'lint' command
//
case 'lint':
runScript(
{
script: cmd.command,
main_url: 'git://github.com/'+config.main_repo+'.git',
pull_url: cmd.pull_url,
pull_sha: cmd.pull_sha,
ref_url: config.ref_repo,
tmp_path: config.tmp_path,
timeout: config.process_timeout_mins*60*1000,
output_file: liveOutputFile
},
function(output){
var msg = '';
output = '\n'+output; // hack to get first line into code below
output = output.replace(/\n/g, '\n '); // reformat output as Github/Markdown code
// Tests passed?
if (output.search("files checked, no errors found") > -1) { // 'make lint'
if (output.search("WARNING") < 0) {
msg = '**All tests passed.**';
}
else {
msg = '# WARNING(s) found';
msg += '\n\n**All tests passed,** but with **WARNING(s)**.';
msg += '\n\nMake sure to _read them!_ :).';
}
}
// Tests DID NOT pass
else {
msg = '# ERROR(s) found';
} // if !lint passed
msg += '\n\nOutput:\n\n'+output;
github.postEndMessage(cmd, (new Date())-t1, msg);
console.log((new Date())+': done processing command "'+cmd.command+'" in Pull #'+cmd.pull_number+' from @'+cmd.user+' (id:'+cmd.id+')');
queue.next();
}
); // lint
break;
//
// Process 'makeref' command
//
case 'makeref':
runScript(
{
script: cmd.command,
main_url: 'git://github.com/'+config.main_repo+'.git',
pull_url: cmd.pull_url,
pull_sha: cmd.pull_sha,
ref_url: config.ref_repo,
tmp_path: config.tmp_path,
timeout: config.process_timeout_mins*60*1000,
output_file: liveOutputFile
},
function(output){
var msg = '';
output = '\n'+output; // hack to get first line into code below
output = output.replace(/\n/g, '\n '); // reformat output as Github/Markdown code
// Makeref OK?
if (output.search("All tests passed") > -1 && // 'make test'
output.search("files checked, no errors found") > -1) { // 'make lint'
msg = '**References generated**';
msg += '\n\nImages pushed to `'+config.ref_repo+'`';
if (output.search("WARNING") > -1)
msg += '\n\n**WARNING:** Some WARNING messages found! Make sure to _read them_ :)';
}
// Makeref NOT ok
else {
msg = '**ERROR(s) found!**';
}; // if tests !passed
msg += '\n\nOutput:\n\n'+output;
github.postEndMessage(cmd, (new Date())-t1, msg);
console.log((new Date())+': done processing command "'+cmd.command+'" in Pull #'+cmd.pull_number+' from @'+cmd.user+' (id:'+cmd.id+')');
queue.next();
}
); // makeref
break;
//
// Process unknown command
//
default:
// Timeout hack to ensure Github response comes after "Processing" message
setTimeout(function(){
github.postEndMessage(cmd, (new Date())-t1, 'Unknown command: ' + (cmd.command || '(empty)'));
queue.next();
}, 2000);
}; // switch
}); // queue.push
}); // forEachNewCommand
}; // checkNewMessages()
//
// runScript()
//
function runScript(args, callback){
var child,
outputFile = args.output_file ? args.tmp_path+'/'+args.output_file : undefined,
script = './run-'+args.script,
cmd = 'mkdir -p '+args.tmp_path+'; '+
script+' '+args.main_url+' '+args.pull_url+' '+args.pull_sha+' '+args.ref_url+' '+args.tmp_path+' 2>&1'+
(outputFile ? ' | tee '+outputFile : '');
console.log((new Date())+': running: '+cmd);
//
// Launch process
//
child = exec(cmd,
{
timeout: args.timeout || undefined
},
function(error, stdout, stderr){
//
// Script done
//
if (error && error.killed) {
exec('kill -TERM '+child.pid); // just in case Node messed up
stdout += '\n\n***\nProcess killed (timeout).\n';
}
if (callback) callback(stdout+'\n\n_____________________________ stderr:\n\n'+stderr);
}
);
}; // runScript()