1- const { Application } = require ( 'probot' )
1+ const { createProbot } = require ( 'probot' )
22const { resolve } = require ( 'probot/lib/resolver' )
33const { findPrivateKey } = require ( 'probot/lib/private-key' )
44const { template } = require ( './views/probot' )
5- const cacheManager = require ( 'cache-manager' )
65
7- let app
86let probot
9- let cache
107
11- const loadProbot = ( plugin ) => {
12- cache = cache || cacheManager . caching ( {
13- store : 'memory' ,
14- ttl : 60 * 5 // 5 minutes
15- } )
16-
17- app = app || new Application ( {
8+ const loadProbot = appFn => {
9+ probot = probot || createProbot ( {
1810 id : process . env . APP_ID ,
1911 secret : process . env . WEBHOOK_SECRET ,
20- cert : findPrivateKey ( ) ,
21- cache
12+ cert : findPrivateKey ( )
2213 } )
2314
24- if ( typeof plugin === 'string' ) {
25- plugin = resolve ( plugin )
15+ if ( typeof appFn === 'string' ) {
16+ appFn = resolve ( appFn )
2617 }
2718
28- app . load ( plugin )
19+ probot . load ( appFn )
2920
30- return app
21+ return probot
3122}
3223
33- module . exports . serverless = ( plugin ) => {
24+ module . exports . serverless = appFn => {
3425 return async ( event , context ) => {
3526 // 🤖 A friendly homepage if there isn't a payload
3627 if ( event . httpMethod === 'GET' && event . path === '/probot' ) {
@@ -45,14 +36,13 @@ module.exports.serverless = (plugin) => {
4536 }
4637
4738 // Otherwise let's listen handle the payload
48- probot = probot || loadProbot ( plugin )
39+ probot = probot || loadProbot ( appFn )
4940
5041 // Ends function immediately after callback
5142 context . callbackWaitsForEmptyEventLoop = false
5243
5344 // Determine incoming webhook event type
5445 const e = event . headers [ 'x-github-event' ] || event . headers [ 'X-GitHub-Event' ]
55- const id = event . headers [ 'x-github-delivery' ] || event . headers [ 'X-GitHub-Delivery' ]
5646
5747 // Convert the payload to an Object if API Gateway stringifies it
5848 event . body = ( typeof event . body === 'string' ) ? JSON . parse ( event . body ) : event . body
@@ -61,7 +51,7 @@ module.exports.serverless = (plugin) => {
6151 console . log ( `Received event ${ e } ${ event . body . action ? ( '.' + event . body . action ) : '' } ` )
6252 if ( event ) {
6353 try {
64- await app . receive ( {
54+ await probot . receive ( {
6555 event : e ,
6656 payload : event . body
6757 } )
0 commit comments