-
Notifications
You must be signed in to change notification settings - Fork 12
/
index.js
67 lines (58 loc) · 1.72 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
const { createProbot } = require('probot')
const { resolve } = require('probot/lib/resolver')
const { findPrivateKey } = require('probot/lib/private-key')
const { template } = require('./views/probot')
let probot
const loadProbot = appFn => {
probot = probot || createProbot({
id: process.env.APP_ID,
secret: process.env.WEBHOOK_SECRET,
cert: findPrivateKey()
})
if (typeof appFn === 'string') {
appFn = resolve(appFn)
}
probot.load(appFn)
return probot
}
module.exports.serverless = appFn => {
return async (request, response) => {
// 🤖 A friendly homepage if there isn't a payload
if (request.method === 'GET' && request.path === '/probot') {
return response.send({
statusCode: 200,
headers: { 'Content-Type': 'text/html' },
body: template
})
}
// Otherwise let's listen handle the payload
probot = probot || loadProbot(appFn)
// Determine incoming webhook event type
const name = request.get('x-github-event') || request.get('X-GitHub-Event')
const id = request.get('x-github-delivery') || request.get('X-GitHub-Delivery')
// Do the thing
console.log(`Received event ${name}${request.body.action ? ('.' + request.body.action) : ''}`)
if (name) {
try {
await probot.receive({
name,
id,
payload: request.body
})
response.send({
statusCode: 200,
body: JSON.stringify({ message: 'Executed' })
})
} catch (err) {
console.error(err)
response.send({
statusCode: 500,
body: JSON.stringify({ message: err })
})
}
} else {
console.error(request)
response.sendStatus(400)
}
}
}