-
Notifications
You must be signed in to change notification settings - Fork 0
/
ci-server.js
68 lines (58 loc) · 2 KB
/
ci-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
const express = require('express');
const config = require('./pkg/config');
const SmeeClient = require('smee-client');
const bodyParser = require('body-parser');
const errorhandler = require('errorhandler');
const { StaticPool } = require("node-worker-threads-pool");
console.log('Utilizing ' + config.workers + ' workers to process jobs.')
workerPath = './worker.js';
if(config.webhook_proxy){
const smee = new SmeeClient({
source: config.webhook_proxy,
target: 'http://localhost:3000/commit',
logger: console
})
const events = smee.start();
events.addEventListener('error', (error) => {
console.log("Error occurred with Smee client: ");
console.log(error);
events = smee.start();
});
}
const pool = new StaticPool({
size: config.workers,
task: workerPath,
});
var app = express();
app.use(bodyParser.json());
app.use(errorhandler({ dumpExceptions: true, showStack: true}));
app.post('/commit', (req) => {
(async ()=> {
try{
let event = req.headers['x-github-event'];
switch(event) {
case 'push':
if( req.body.after != "0000000000000000000000000000000000000000") {
try {
const res = await pool.exec(req.body);
console.log('Job ended with status ' + res);
} catch(error) {
console.log("Job failed with " + error);
}
}
break;
case 'pull_request':
console.log('Recieved PR event');
break;
default:
console.log('Unrecognized github event: ' + event)
break;
}
} catch(error) {
console.log('While parsing incoming message: ' + error);
}
}) ();
});
app.listen(3000, () => {
console.log( `CI Server is listening on port 3000 for github webhooks`);
})