probot/server.js

50 lines
1.3 KiB
JavaScript

let process = require('process');
let http = require('http');
let createHandler = require('github-webhook-handler');
let GitHubApi = require('github');
let webhook = createHandler({path: '/', secret: process.env.WEBHOOK_SECRET || 'secret'});
let github = new GitHubApi();
const Configuration = require('./lib/configuration');
const Dispatcher = require('./lib/dispatcher');
let PORT = process.env.PORT || 3000;
github.authenticate({
type: 'oauth',
token: process.env.GITHUB_TOKEN
});
http.createServer(function (req, res) {
webhook(req, res, function (err) {
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');
}
});
}).listen(PORT);
console.log("Listening on http://localhost:" + PORT);
function register(behavior) {
webhook.on(behavior.webhook, function (event) {
behavior.action(event, github);
});
}
webhook.on('*', function (event) {
if (event.payload.repository) {
const dispatcher = new Dispatcher(github, event);
return Configuration.load(github, event.payload.repository).then(
config => dispatcher.call(config)
);
}
});
// register(require('./behaviors/autoresponder.js'));