forked from searchspring/asana-github-pr-webhook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebhook.js
61 lines (50 loc) · 1.83 KB
/
webhook.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
const processor = require('./src/processor')
const log = require('debug-with-levels')('agpw:webhook')
const { json, send } = require('micro')
const verifySecret = require('verify-github-webhook-secret')
module.exports = async (req, res) => {
log.trace('request')
const webhookSecret = process.env.WEBHOOK_SECRET
const asanaAccessToken = process.env.ASANA_ACCESS_TOKEN
const githubAccessToken = process.env.GITHUB_ACCESS_TOKEN
if (!webhookSecret) {
var noWebhookMessage = 'No WEBHOOK_SECRET set so wont run any thing'
log.error(noWebhookMessage)
send(res, 403, noWebhookMessage)
return
}
if (!asanaAccessToken) {
var noAsanaMessage = 'No ASANA_ACCESS_TOKEN set so wont run any thing'
log.error(noAsanaMessage)
send(res, 403, noAsanaMessage)
return
}
if (!githubAccessToken) {
var noGithubMessage = 'No GITHUB_ACCESS_TOKEN set so wont run any thing'
log.error(noGithubMessage)
send(res, 403, noGithubMessage)
return
}
log.info('starting with: ')
log.info('WEBHOOK_SECRET=: ********' + webhookSecret.slice(-4))
log.info('ASANA_ACCESS_TOKEN=: ********' + asanaAccessToken.slice(-4))
log.info('GITHUB_ACCESS_TOKEN=: ********' + githubAccessToken.slice(-4))
if (req.headers['x-github-event'] !== 'pull_request') {
var unsupportedEventMessage = 'Only supports github events of type pull request'
log.error(unsupportedEventMessage)
send(res, 403, unsupportedEventMessage)
return
}
const data = await json(req)
log.debug('data: ' + data)
const valid = await verifySecret(req, webhookSecret)
if (!valid) {
var secretMismatchError = 'Webhook secret does not match environment variable.'
log.error(secretMismatchError)
send(res, 403, secretMismatchError)
return
}
await processor.processWebhook(data)
log.trace('request complete')
res.end('worked')
}