1
- const createProbot = require ( 'probot' ) ;
1
+ const { Application } = require ( 'probot' )
2
2
const { resolve } = require ( 'probot/lib/resolver' )
3
3
const { findPrivateKey } = require ( 'probot/lib/private-key' )
4
4
const { template } = require ( './views/probot' )
5
5
6
+ let app
7
+ let probot
8
+
6
9
const loadProbot = ( plugin ) => {
7
- const probot = createProbot ( {
10
+ app = app || new Application ( {
8
11
id : process . env . APP_ID ,
9
12
secret : process . env . WEBHOOK_SECRET ,
10
13
cert : findPrivateKey ( )
@@ -14,16 +17,13 @@ const loadProbot = (plugin) => {
14
17
plugin = resolve ( plugin )
15
18
}
16
19
17
- probot . load ( plugin )
20
+ app . load ( plugin )
18
21
19
- return probot
22
+ return app
20
23
}
21
24
22
-
23
25
module . exports . serverless = ( plugin ) => {
24
-
25
26
return async ( event , context ) => {
26
-
27
27
// 🤖 A friendly homepage if there isn't a payload
28
28
if ( event . httpMethod === 'GET' && event . path === '/probot' ) {
29
29
const res = {
@@ -37,7 +37,7 @@ module.exports.serverless = (plugin) => {
37
37
}
38
38
39
39
// Otherwise let's listen handle the payload
40
- const probot = loadProbot ( plugin )
40
+ probot = probot || loadProbot ( plugin )
41
41
42
42
// Ends function immediately after callback
43
43
context . callbackWaitsForEmptyEventLoop = false
@@ -53,25 +53,31 @@ module.exports.serverless = (plugin) => {
53
53
console . log ( `Received event ${ e } ${ event . body . action ? ( '.' + event . body . action ) : '' } ` )
54
54
if ( event ) {
55
55
try {
56
- await probot . receive ( {
56
+ await app . receive ( {
57
57
event : e ,
58
58
payload : event . body
59
59
} )
60
60
const res = {
61
61
statusCode : 200 ,
62
62
body : JSON . stringify ( {
63
- message : 'Hi Node8!'
63
+ message : `Received ${ e } . ${ event . body . action } `
64
64
} )
65
65
}
66
66
return context . done ( null , res )
67
67
} catch ( err ) {
68
68
console . error ( err )
69
- return err
69
+ return context . done ( null , {
70
+ statusCode : 500 ,
71
+ body : JSON . stringify ( err )
72
+ } )
70
73
}
71
74
} else {
72
75
console . error ( { event, context } )
73
- callback ( 'unknown error' )
76
+ context . done ( null , 'unknown error' )
74
77
}
78
+ return context . done ( null , {
79
+ statusCode : 200 ,
80
+ body : 'Nothing to do.'
81
+ } )
75
82
}
76
-
77
83
}
0 commit comments