@@ -5,12 +5,14 @@ const { template } = require('./views/probot')
5
5
6
6
let probot
7
7
8
- const loadProbot = appFn => {
9
- probot = probot || new Probot ( {
10
- id : process . env . APP_ID ,
11
- secret : process . env . WEBHOOK_SECRET ,
12
- privateKey : findPrivateKey ( )
13
- } )
8
+ const loadProbot = ( appFn ) => {
9
+ probot =
10
+ probot ||
11
+ new Probot ( {
12
+ id : process . env . APP_ID ,
13
+ secret : process . env . WEBHOOK_SECRET ,
14
+ privateKey : findPrivateKey ( )
15
+ } )
14
16
15
17
if ( typeof appFn === 'string' ) {
16
18
appFn = resolve ( appFn )
@@ -22,10 +24,13 @@ const loadProbot = appFn => {
22
24
}
23
25
24
26
const lowerCaseKeys = ( obj = { } ) =>
25
- Object . keys ( obj ) . reduce ( ( accumulator , key ) =>
26
- Object . assign ( accumulator , { [ key . toLocaleLowerCase ( ) ] : obj [ key ] } ) , { } )
27
+ Object . keys ( obj ) . reduce (
28
+ ( accumulator , key ) =>
29
+ Object . assign ( accumulator , { [ key . toLocaleLowerCase ( ) ] : obj [ key ] } ) ,
30
+ { }
31
+ )
27
32
28
- module . exports . serverless = appFn => {
33
+ module . exports . serverless = ( appFn ) => {
29
34
return async ( event , context ) => {
30
35
// 🤖 A friendly homepage if there isn't a payload
31
36
if ( event . httpMethod === 'GET' && event . path === '/probot' ) {
@@ -61,7 +66,8 @@ module.exports.serverless = appFn => {
61
66
}
62
67
63
68
// Convert the payload to an Object if API Gateway stringifies it
64
- event . body = ( typeof event . body === 'string' ) ? JSON . parse ( event . body ) : event . body
69
+ event . body =
70
+ typeof event . body === 'string' ? JSON . parse ( event . body ) : event . body
65
71
66
72
// Bail for null body
67
73
if ( ! event . body ) {
@@ -72,20 +78,21 @@ module.exports.serverless = appFn => {
72
78
}
73
79
74
80
// Do the thing
75
- console . log ( `Received event ${ e } ${ event . body . action ? ( '.' + event . body . action ) : '' } ` )
81
+ console . log (
82
+ `Received event ${ e } ${ event . body . action ? '.' + event . body . action : '' } `
83
+ )
76
84
if ( event ) {
77
85
try {
78
86
await probot . receive ( {
79
87
name : e ,
80
88
payload : event . body
81
89
} )
82
- const res = {
90
+ return {
83
91
statusCode : 200 ,
84
92
body : JSON . stringify ( {
85
93
message : `Received ${ e } .${ event . body . action } `
86
94
} )
87
95
}
88
- return res
89
96
} catch ( err ) {
90
97
console . error ( err )
91
98
return {
@@ -95,11 +102,7 @@ module.exports.serverless = appFn => {
95
102
}
96
103
} else {
97
104
console . error ( { event, context } )
98
- throw 'unknown error'
99
- }
100
- return {
101
- statusCode : 200 ,
102
- body : 'Nothing to do.'
105
+ throw new Error ( 'unknown error' )
103
106
}
104
107
}
105
108
}
0 commit comments