-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebhook.js
61 lines (51 loc) · 1.5 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 crypto = require('crypto')
const checkReady = require('./checkReady')
const _update = require('./update')
const genSig = body => 'sha1=' + crypto.createHmac('sha1', process.env.WEBHOOK_SECRET).update(JSON.stringify(body)).digest('hex')
module.exports = (db, websockets) => async (req, res) => {
if (genSig(req.body) !== req.headers['x-hub-signature']) return res.code(401).type('application/json').send(new Error('Webhook secret check failed'))
res.send('OK')
console.dir(req.body, { depth: 1 })
if (req.headers['x-github-event'] !== 'pull_request') return console.log('It\'s not a pull request, ignoring.')
const {
number: id,
pull_request: {
html_url: url,
merged,
title: name,
base: { ref },
head
},
action
} = req.body
if (ref !== 'master') return console.log('It\'s not a pull request to master, ignoring.')
const existing = await db.find({ id })
const pr = {
id,
name,
url,
status: 'pending',
meta: {
sha: head.sha,
repo: {
full_name: head.repo.full_name
}
}
}
if (!existing.length) {
await db.insert(pr)
}
const update = _update(db, pr, websockets)
if (action === 'closed') {
await update(merged ? 'merged' : 'closed', null, true)
console.log('PR is being closed, ignoring.')
return
}
await update('pending')
const { success, description } = await checkReady()
if (success) {
await update('success')
return
}
await update('failure', description)
}