forked from probot/probot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
104 lines (83 loc) · 2.47 KB
/
index.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
const bunyan = require('bunyan')
const bunyanFormat = require('bunyan-format')
const sentryStream = require('bunyan-sentry-stream')
const cacheManager = require('cache-manager')
const createApp = require('github-app')
const createWebhook = require('github-webhook-handler')
const Raven = require('raven')
const createRobot = require('./lib/robot')
const createServer = require('./lib/server')
const resolve = require('./lib/resolver')
const serializers = require('./lib/serializers')
const cache = cacheManager.caching({
store: 'memory',
ttl: 60 * 60 // 1 hour
})
const logger = bunyan.createLogger({
name: 'Probot',
level: process.env.LOG_LEVEL || 'debug',
stream: bunyanFormat({outputMode: process.env.LOG_FORMAT || 'short'}),
serializers
})
const defaultApps = [
require('./lib/plugins/stats'),
require('./lib/plugins/default')
]
// Log all unhandled rejections
process.on('unhandledRejection', logger.error.bind(logger))
module.exports = (options = {}) => {
const webhook = createWebhook({path: options.webhookPath || '/', secret: options.secret || 'development'})
const app = createApp({
id: options.id,
cert: options.cert,
debug: process.env.LOG_LEVEL === 'trace'
})
const server = createServer(webhook)
// Log all received webhooks
webhook.on('*', event => {
logger.trace(event, 'webhook received')
receive(event)
})
// Log all webhook errors
webhook.on('error', logger.error.bind(logger))
// If sentry is configured, report all logged errors
if (process.env.SENTRY_DSN) {
Raven.disableConsoleAlerts()
Raven.config(process.env.SENTRY_DSN, {
autoBreadcrumbs: true
}).install({})
logger.addStream(sentryStream(Raven))
}
const robots = []
function receive (event) {
return Promise.all(robots.map(robot => robot.receive(event)))
}
function load (plugin) {
if (typeof plugin === 'string') {
plugin = resolve(plugin)
}
const robot = createRobot({app, cache, logger, catchErrors: true})
// Connect the router from the robot to the server
server.use(robot.router)
// Initialize the plugin
plugin(robot)
robots.push(robot)
return robot
}
function setup (apps) {
apps.concat(defaultApps).forEach(app => load(app))
}
return {
server,
webhook,
receive,
logger,
load,
setup,
start () {
server.listen(options.port)
logger.trace('Listening on http://localhost:' + options.port)
}
}
}
module.exports.createRobot = createRobot