Skip to content

Commit b54189d

Browse files
committed
Merge pull request #8 from williamkapke/evented-approach
emit github events
2 parents 8824234 + da57626 commit b54189d

File tree

5 files changed

+82
-29
lines changed

5 files changed

+82
-29
lines changed

Procfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
web: node server

lib/github-events.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
const crypto = require('crypto')
2+
const debug = require('debug')('github')
3+
4+
const secret = process.env.GITHUB_WEBHOOK_SECRET || 'hush-hush'
5+
6+
const sign = (secret, data) => {
7+
const buffer = new Buffer(data, 'utf8')
8+
return 'sha1=' + crypto.createHmac('sha1', secret).update(buffer).digest('hex')
9+
}
10+
11+
module.exports = (app) => {
12+
app.post('/hooks/github', (req, res) => {
13+
const event = req.headers['x-github-event']
14+
if (!event) {
15+
res.writeHead(400, 'Event Header Missing')
16+
return res.end()
17+
}
18+
19+
const signature = req.headers['x-hub-signature']
20+
if (!signature || signature !== sign(secret, req.raw)) {
21+
res.writeHead(401, 'Invalid Signature')
22+
return res.end()
23+
}
24+
25+
res.end()
26+
27+
const data = req.body
28+
data.action = data.action ? event + '.' + data.action : event
29+
30+
var source = data.repository ? data.repository.full_name : data.organization.login
31+
console.log('event@%s: %s', source, data.action)
32+
33+
if (debug.enabled) {
34+
debug(JSON.stringify(data, null, 2))
35+
}
36+
37+
app.emit(data.action, data)
38+
})
39+
}

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
"license": "MIT",
1414
"dependencies": {
1515
"body-parser": "^1.15.0",
16+
"debug": "^2.2.0",
17+
"dotenv": "^2.0.0",
1618
"express": "^4.13.4",
1719
"github": "^0.2.4",
1820
"travis-ci": "^2.1.0"

scripts/display-travis-status.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
'use strict'
2+
3+
const debug = require('debug')('display_travis_status')
4+
const pollTravis = require('../lib/pollTravis')
5+
const enabledRepos = ['citgm', 'readable-stream', 'nodejs.org']
6+
7+
module.exports = function (app) {
8+
app.on('pull_request.opened', (event) => {
9+
const owner = event.repository.owner.login
10+
const repo = event.repository.name
11+
if (!~enabledRepos.indexOf(repo)) return
12+
13+
debug(`/${owner}/${repo}/pull/${event.number} opened`)
14+
pollTravis.pollThenComment(owner, repo, event.number)
15+
})
16+
17+
// to trigger polling manually
18+
app.get('/pr/:owner/:repo/:id', (req, res) => {
19+
const owner = req.params.owner
20+
const repo = req.params.repo
21+
const id = req.params.id
22+
if (~enabledRepos.indexOf(repo)) {
23+
pollTravis.pollThenComment(owner, repo, parseInt(id, 10))
24+
}
25+
res.end()
26+
})
27+
}

server.js

Lines changed: 13 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,23 @@
11
'use strict'
22

3+
require('dotenv').load({ silent: true })
4+
35
const express = require('express')
46
const bodyParser = require('body-parser')
5-
6-
const pollTravis = require('./lib/pollTravis')
7+
const captureRaw = (req, res, buffer) => { req.raw = buffer }
78

89
const app = express()
9-
10-
const port = process.env.PORT || 3000
11-
12-
app.use(bodyParser.json())
13-
14-
app.all('/hooks/github', (req, res) => {
15-
if (wasPullRequestOpened(req)) {
16-
const repo = req.body.repository
17-
18-
console.log(`* ${repo.owner.login}/${repo.name}/#${req.body.number} Opened, starting build checks!`)
19-
pollTravis.pollThenComment(repo.owner.login, repo.name, parseInt(req.body.number))
20-
}
21-
22-
res.end()
23-
})
24-
25-
// to trigger polling manually
26-
app.get('/pr/:owner/:repo/:prId', (req, res) => {
27-
pollTravis.pollThenComment(req.params.owner, req.params.repo, parseInt(req.params.prId))
28-
res.end()
10+
app.use(bodyParser.json({ verify: captureRaw }))
11+
require('./lib/github-events.js')(app)
12+
13+
// load all the files in the scripts folder
14+
require('fs').readdirSync('./scripts').forEach((file) => {
15+
file = './scripts/' + file
16+
console.log('loading:', file)
17+
require(file)(app)
2918
})
3019

31-
app.listen(process.env.PORT || 3000, () => {
20+
const port = process.env.PORT || 3000
21+
app.listen(port, () => {
3222
console.log('Example app listening on port', port)
3323
})
34-
35-
function wasPullRequestOpened (req) {
36-
const githubEvent = req.headers['x-github-event'] || ''
37-
const githubAction = req.body.action || ''
38-
return githubEvent === 'pull_request' && githubAction === 'opened'
39-
}

0 commit comments

Comments
 (0)