1
- const { Application } = require ( 'probot' )
1
+ const { createProbot } = 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
- const cacheManager = require ( 'cache-manager' )
6
5
7
- let app
8
6
let probot
9
- let cache
10
7
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 ( {
18
10
id : process . env . APP_ID ,
19
11
secret : process . env . WEBHOOK_SECRET ,
20
- cert : findPrivateKey ( ) ,
21
- cache
12
+ cert : findPrivateKey ( )
22
13
} )
23
14
24
- if ( typeof plugin === 'string' ) {
25
- plugin = resolve ( plugin )
15
+ if ( typeof appFn === 'string' ) {
16
+ appFn = resolve ( appFn )
26
17
}
27
18
28
- app . load ( plugin )
19
+ probot . load ( appFn )
29
20
30
- return app
21
+ return probot
31
22
}
32
23
33
- module . exports . serverless = ( plugin ) => {
24
+ module . exports . serverless = appFn => {
34
25
return async ( event , context ) => {
35
26
// 🤖 A friendly homepage if there isn't a payload
36
27
if ( event . httpMethod === 'GET' && event . path === '/probot' ) {
@@ -45,14 +36,13 @@ module.exports.serverless = (plugin) => {
45
36
}
46
37
47
38
// Otherwise let's listen handle the payload
48
- probot = probot || loadProbot ( plugin )
39
+ probot = probot || loadProbot ( appFn )
49
40
50
41
// Ends function immediately after callback
51
42
context . callbackWaitsForEmptyEventLoop = false
52
43
53
44
// Determine incoming webhook event type
54
45
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' ]
56
46
57
47
// Convert the payload to an Object if API Gateway stringifies it
58
48
event . body = ( typeof event . body === 'string' ) ? JSON . parse ( event . body ) : event . body
@@ -61,7 +51,7 @@ module.exports.serverless = (plugin) => {
61
51
console . log ( `Received event ${ e } ${ event . body . action ? ( '.' + event . body . action ) : '' } ` )
62
52
if ( event ) {
63
53
try {
64
- await app . receive ( {
54
+ await probot . receive ( {
65
55
event : e ,
66
56
payload : event . body
67
57
} )
0 commit comments