2016-09-17 00:38:36 +08:00
|
|
|
let process = require('process');
|
|
|
|
let http = require('http');
|
|
|
|
let createHandler = require('github-webhook-handler');
|
2016-09-17 01:02:43 +08:00
|
|
|
let GitHubApi = require('github');
|
2016-09-17 02:45:35 +08:00
|
|
|
|
2016-09-17 23:48:16 +08:00
|
|
|
let webhook = createHandler({path: '/', secret: process.env.WEBHOOK_SECRET || 'secret'});
|
2016-09-17 00:38:36 +08:00
|
|
|
let github = new GitHubApi();
|
|
|
|
|
2016-09-18 01:27:58 +08:00
|
|
|
let PORT = process.env.PORT || 3000;
|
|
|
|
|
2016-09-17 00:38:36 +08:00
|
|
|
github.authenticate({
|
2016-09-17 01:02:43 +08:00
|
|
|
type: 'oauth',
|
2016-09-17 02:45:35 +08:00
|
|
|
token: process.env.GITHUB_TOKEN
|
2016-09-17 00:38:36 +08:00
|
|
|
});
|
2016-09-17 00:10:30 +08:00
|
|
|
|
|
|
|
http.createServer(function (req, res) {
|
2016-09-17 00:38:36 +08:00
|
|
|
webhook(req, res, function (err) {
|
2016-09-17 02:45:35 +08:00
|
|
|
if (err) {
|
|
|
|
console.log('ERROR', err);
|
|
|
|
res.statusCode = 500;
|
|
|
|
res.end('Something has gone terribly wrong.');
|
|
|
|
} else {
|
|
|
|
res.statusCode = 404;
|
|
|
|
res.end('no such location');
|
|
|
|
}
|
|
|
|
});
|
2016-09-18 01:27:58 +08:00
|
|
|
}).listen(PORT);
|
|
|
|
|
|
|
|
console.log("Listening on http://localhost:" + PORT);
|
2016-09-17 00:38:36 +08:00
|
|
|
|
2016-09-17 02:45:35 +08:00
|
|
|
function register(behavior) {
|
|
|
|
webhook.on(behavior.webhook, function (event) {
|
|
|
|
behavior.action(event, github);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
register(require('./behaviors/autoresponder.js'));
|