1- const createProbot = require ( 'probot' ) ;
1+ const { Application } = require ( 'probot' )
22const { resolve } = require ( 'probot/lib/resolver' )
33const { findPrivateKey } = require ( 'probot/lib/private-key' )
44const { template } = require ( './views/probot' )
55
6+ let app
7+ let probot
8+
69const loadProbot = ( plugin ) => {
7- const probot = createProbot ( {
10+ app = app || new Application ( {
811 id : process . env . APP_ID ,
912 secret : process . env . WEBHOOK_SECRET ,
1013 cert : findPrivateKey ( )
@@ -14,16 +17,13 @@ const loadProbot = (plugin) => {
1417 plugin = resolve ( plugin )
1518 }
1619
17- probot . load ( plugin )
20+ app . load ( plugin )
1821
19- return probot
22+ return app
2023}
2124
22-
2325module . exports . serverless = ( plugin ) => {
24-
2526 return async ( event , context ) => {
26-
2727 // 🤖 A friendly homepage if there isn't a payload
2828 if ( event . httpMethod === 'GET' && event . path === '/probot' ) {
2929 const res = {
@@ -37,7 +37,7 @@ module.exports.serverless = (plugin) => {
3737 }
3838
3939 // Otherwise let's listen handle the payload
40- const probot = loadProbot ( plugin )
40+ probot = probot || loadProbot ( plugin )
4141
4242 // Ends function immediately after callback
4343 context . callbackWaitsForEmptyEventLoop = false
@@ -53,25 +53,31 @@ module.exports.serverless = (plugin) => {
5353 console . log ( `Received event ${ e } ${ event . body . action ? ( '.' + event . body . action ) : '' } ` )
5454 if ( event ) {
5555 try {
56- await probot . receive ( {
56+ await app . receive ( {
5757 event : e ,
5858 payload : event . body
5959 } )
6060 const res = {
6161 statusCode : 200 ,
6262 body : JSON . stringify ( {
63- message : 'Hi Node8!'
63+ message : `Received ${ e } . ${ event . body . action } `
6464 } )
6565 }
6666 return context . done ( null , res )
6767 } catch ( err ) {
6868 console . error ( err )
69- return err
69+ return context . done ( null , {
70+ statusCode : 500 ,
71+ body : JSON . stringify ( err )
72+ } )
7073 }
7174 } else {
7275 console . error ( { event, context } )
73- callback ( 'unknown error' )
76+ context . done ( null , 'unknown error' )
7477 }
78+ return context . done ( null , {
79+ statusCode : 200 ,
80+ body : 'Nothing to do.'
81+ } )
7582 }
76-
7783}
0 commit comments