Files
probot/server.js
Brandon Keepers 9f1ec510f4 Merge remote-tracking branch 'origin/master' into integration
* origin/master:
  Tweak debugging
  Avoid using global
  Log stuff
  use global instead of GLOBAL
  Make debug global
  Add package version for debug
  Update CONTRIBUTING.md
  Address @bkeepers feedback
  Tweak formatting
  Add testing section header
  Actually fix formatting
  Fix code block formatting
  Add note on enabling debug mode for github client
  Change title to Setup
  Update contributing.md
  Use debug
  Add log for react
  Turn off GitHub client debug
2016-10-15 09:49:55 -05:00

49 lines
1.3 KiB
JavaScript

require('dotenv').config({silent: true});
const process = require('process');
const http = require('http');
const createHandler = require('github-webhook-handler');
const debug = require('debug')('PRobot');
const Configuration = require('./lib/configuration');
const Dispatcher = require('./lib/dispatcher');
const installations = require('./lib/installations');
const PORT = process.env.PORT || 3000;
const webhook = createHandler({path: '/', secret: process.env.WEBHOOK_SECRET || 'development'});
debug('Starting');
// Cache installations
installations.load();
// Listen for new installations
installations.listen(webhook);
http.createServer((req, res) => {
webhook(req, res, err => {
if (err) {
console.error(err);
res.statusCode = 500;
res.end('Something has gone terribly wrong.');
} else {
res.statusCode = 404;
res.end('no such location');
}
});
}).listen(PORT);
webhook.on('*', event => {
debug('webhook', event);
if (event.payload.repository) {
const account = event.payload.repository.owner.login;
installations.auth(account).then(github => {
const dispatcher = new Dispatcher(github, event);
return Configuration.load(github, event.payload.repository).then(config => {
dispatcher.call(config);
});
});
}
});
console.log('Listening on http://localhost:' + PORT);