-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
74 lines (63 loc) · 1.77 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
/**
Webhook listener.
*/
"use strict";
const GitHubApi = require("github");
const program = require("commander");
const http = require('http');
const main = require('./pull_request');
const seqqueue = require('seq-queue');
const config = require('./config');
// Pull request actions to process
const pullRequestActions = ['opened', 'synchronize', 'reopened'];
const taskQueue = seqqueue.createQueue(30000);
program
.version('0.0.0')
.option('--port <port>', 'Port to listen on.')
.option('--token <token>', 'Github user token')
.option('--secret <secret>', 'Github hook secret')
.parse(process.argv);
const port = (program.port || 3000);
const github = new GitHubApi({
version: "3.0.0",
headers: {
"user-agent": "80x40-client"
}
});
github.authenticate({
type: "oauth",
token: program.token
});
const webhookHandler = require('github-webhook-handler')({
path: '/',
secret: program.secret
});
webhookHandler.on('pull_request', (event) => {
const action = event.payload.action;
if (pullRequestActions.indexOf(action) === -1) {
console.log('ignoring action', action);
return;
}
taskQueue.push((task) => {
main.handlePullRequest(github, config.user, config.repo,
event.payload.number,
(err) => {
if (err) {
console.error("ERROR", err);
} else {
console.log("OK");
}
task.done();
})
},
() => {
console.error("Timeout");
});
});
console.log("Listening for webhook events on port", port);
http.createServer(function (req, res) {
webhookHandler(req, res, (err) => {
res.statusCode = 404
res.end('');
});
}).listen(port);