Skip to content

Commit 09d8346

Browse files
authored
feat: add sentry support (#23)
1 parent daf6384 commit 09d8346

File tree

5 files changed

+108
-5
lines changed

5 files changed

+108
-5
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ node_modules
44

55
.env
66
*.pem
7+
test-payload.json

CONTRIBUTING.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,19 @@ Ideas not as issues yet:
2020
- If the bot has an open pull request, don't open another one?
2121
- How do we handle failure modes?
2222
- Should we switch to typescript?
23+
24+
25+
26+
### Testing serverless locally
27+
`yarn add serverless-dotenv-plugin`
28+
29+
in `serverless.yml` plugins add:
30+
`- serverless-dotenv-plugin`
31+
32+
33+
Create file test-file.json with payload
34+
```
35+
curl -vX POST http://localhost:3000/ -d @test-payload.json \
36+
--header "Content-Type: application/json" \
37+
--header "X-GitHub-Event: issue_comment"
38+
```

serverless.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ plugins:
77
custom:
88
stage: ${opt:stage, self:provider.stage}
99
appId:
10-
dev: '23178'
10+
dev: '23544'
1111
prod: '23186'
1212
botName:
13-
dev: 'AllContributorsDev'
13+
dev: 'AllContributorsDevBot'
1414
prod: 'AllContributorsBot'
1515
logLevel:
1616
dev: debug
17-
prod: debug # TODO change to info soon
17+
prod: info
1818
sentryDsn:
1919
dev: ''
2020
prod: 'https://[email protected]/1366866'
@@ -28,7 +28,7 @@ provider:
2828
environment:
2929
BOT_NAME: ${self:custom.botName.${self:custom.stage}}
3030
APP_ID: ${self:custom.appId.${self:custom.stage}}
31-
DEBUG_LEVEL: ${self:custom.logLevel.${self:custom.stage}}
31+
LOG_LEVEL: ${self:custom.logLevel.${self:custom.stage}}
3232
SENTRY_DSN: ${self:custom.sentryDsn.${self:custom.stage}}
3333
DISABLE_STATS: true
3434
INSTALLATION_TOKEN_TTL: 3540

src/handler.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1-
const { serverless } = require('@probot/serverless-lambda')
1+
// TODO: put back on release of: https://github.com/probot/serverless-lambda/pull/13/files
2+
// const { serverless } = require('@probot/serverless-lambda')
3+
const { serverless } = require('serverless-lambda')
24
const appFn = require('./')
35
module.exports.probot = serverless(appFn)

src/serverless-lambda.js

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
// TODO: get off of fork once https://github.com/probot/serverless-lambda/pull/13/files is released
2+
3+
const { createProbot } = require('probot')
4+
const { resolve } = require('probot/lib/resolver')
5+
const { findPrivateKey } = require('probot/lib/private-key')
6+
7+
let probot
8+
9+
const loadProbot = appFn => {
10+
probot =
11+
probot ||
12+
createProbot({
13+
id: process.env.APP_ID,
14+
secret: process.env.WEBHOOK_SECRET,
15+
cert: findPrivateKey(),
16+
})
17+
18+
if (typeof appFn === 'string') {
19+
appFn = resolve(appFn)
20+
}
21+
22+
if (process.env.SENTRY_DSN) {
23+
probot.load(require('probot/lib/apps/sentry'))
24+
}
25+
probot.load(appFn)
26+
27+
return probot
28+
}
29+
30+
module.exports.serverless = appFn => {
31+
return async (event, context) => {
32+
// Otherwise let's listen handle the payload
33+
probot = probot || loadProbot(appFn)
34+
35+
// Ends function immediately after callback
36+
context.callbackWaitsForEmptyEventLoop = false
37+
38+
// Determine incoming webhook event type
39+
const e =
40+
event.headers['x-github-event'] || event.headers['X-GitHub-Event']
41+
42+
// Convert the payload to an Object if API Gateway stringifies it
43+
event.body =
44+
typeof event.body === 'string' ? JSON.parse(event.body) : event.body
45+
46+
const { info: logInfo, error: logError } = probot.apps[0].log
47+
48+
// Do the thing
49+
logInfo(
50+
`Received event ${e}${
51+
event.body.action ? `.${event.body.action}` : ''
52+
}`,
53+
)
54+
if (event) {
55+
try {
56+
await probot.receive({
57+
name: e,
58+
payload: event.body,
59+
})
60+
const res = {
61+
statusCode: 200,
62+
body: JSON.stringify({
63+
message: `Received ${e}.${event.body.action}`,
64+
}),
65+
}
66+
return context.done(null, res)
67+
} catch (err) {
68+
logError(err)
69+
return context.done(null, {
70+
statusCode: 500,
71+
body: JSON.stringify(err),
72+
})
73+
}
74+
} else {
75+
logError({ event, context })
76+
context.done(null, 'unknown error')
77+
}
78+
logInfo('Nothing to do.')
79+
return context.done(null, {
80+
statusCode: 200,
81+
body: 'Nothing to do.',
82+
})
83+
}
84+
}

0 commit comments

Comments
 (0)