-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
64 lines (53 loc) · 1.91 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
const http = require('http');
const createHandler = require('github-webhook-handler');
const handler = createHandler({ path: '/webhook', secret: process.env.WEBHOOK_SECRET_KEY });
const async = require('async');
const { buildTask, buildMasterTask } = require('./build-task');
const githubTask = require('./github-task');
const port = process.env.NODE_PORT || 7777;
const buildQueue = async.queue((task, callback) => {
if (task.action) {
buildMasterTask(task).then(() => callback()).catch(e => {
console.error(e);
callback();
});
return;
}
buildTask(task).then(() => callback()).catch(e => {
console.error(e);
callback();
});
}, 1);
const githubQueue = async.queue((task, callback) => {
githubTask(task).then(() => callback()).catch(e => {
console.error(e);
callback();
});
}, 1);
http.createServer((req, res) => {
handler(req, res, () => {
res.statusCode = 404;
res.end('no such location');
});
}).listen(port, () => console.log(`Server launched at ${port}`));
handler.on('error', err => {
console.error('Error:', err.message);
});
handler.on('push', event => {
console.log(`Received a push event for ${event.payload.ref}`);
if (event.payload.ref !== 'refs/heads/master') {
return;
}
buildQueue.push({ action: 'build-master', pull: 'master' });
});
handler.on('pull_request', event => {
const payload = event.payload;
const number = payload.pull_request.number;
console.log(`Received a pull event: PR#${number}, ${payload.action}`);
if (payload.action === 'closed') {
buildQueue.push({ action: 'delete', pull: `pull/${number}`, number, payload });
} else if (['synchronize', 'opened', 'reopened'].includes(payload.action)) {
githubQueue.push(payload);
buildQueue.push({ action: 'build', pull: `pull/${number}`, number, payload });
}
});