-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
66 lines (52 loc) · 1.58 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
'use strict'
const config = require('./config')
const Haiku = require('./lib/Haiku')
const http = require('http')
const webhookHandler = require('github-webhook-handler')({
path: config.get('webhookEndpoint'),
secret: config.get('webhookSecret')
})
http.createServer((req, res) => {
webhookHandler(req, res, (err) => {
res.statusCode = 404
res.end('Unknown endpoint')
})
}).listen(config.get('port'), () => {
console.log(`Server listening on port ${config.get('port')}`)
})
webhookHandler.on('pull_request', (data) => {
const branch = data.payload.pull_request.base.ref
const headSha = data.payload.pull_request.head.sha
if (branch !== config.get('pullRequest.branch')) return
let haiku = new Haiku({
buildpackUrl: config.get('buildpack'),
githubUsername: data.payload.repository.owner.login,
githubRepository: data.payload.repository.name,
githubApiToken: config.get('githubApiToken'),
herokuApiToken: config.get('herokuApiToken')
})
switch (data.payload.action) {
// PR created
case 'opened':
haiku.createApp().then(app => {
return haiku.buildAndUpdateStatus(headSha)
})
break
// PR updated
case 'synchronize':
let previousSha = data.payload.before
return haiku.loadApp(previousSha).then(app => {
return haiku.buildAndUpdateStatus(headSha)
})
break
// PR closed
case 'closed':
return haiku.loadApp(headSha).then(app => {
return haiku.deleteApp()
})
break
}
})
webhookHandler.on('error', (err) => {
console.error('Error:', err.message)
})