Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 29 additions & 11 deletions lib/github-events.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const crypto = require('crypto')
const debug = require('debug')('github')
const debug = require('debug')('github-events')

const secret = process.env.GITHUB_WEBHOOK_SECRET || 'hush-hush'

Expand All @@ -17,24 +17,42 @@ module.exports = (app) => {
}

const signature = req.headers['x-hub-signature']
const data = req.body
data.action = data.action ? event + '.' + data.action : event

if (!signature || signature !== sign(secret, req.raw)) {
res.writeHead(401, 'Invalid Signature')
console.error('! event@%s: %s - invalid signature, returning 401', source, data.action)
console.error('! event@%s: %s - invalid signature, returning 401', data.repository.full_name, data.action)
return res.end()
}

res.end()

const data = req.body
data.action = data.action ? event + '.' + data.action : event

var source = data.repository ? data.repository.full_name : data.organization.login
console.log('event@%s: %s', source, data.action)
emit(data)
})

if (debug.enabled) {
debug(JSON.stringify(data, null, 2))
if (process.env.SSE_RELAY) {
const EventSource = require('eventsource')
var es = new EventSource(process.env.SSE_RELAY)
es.onmessage = (e) => {
try {
const data = JSON.parse(e.data)
if (!data.action) return

emit(data)
} catch (e) {
console.error(e)
}
}
}

app.emit(data.action, data)
})
function emit (data) {
const repo = data.repository
const org = repo.owner.login || data.organization.login

console.log('event@%s/%s: %s', org, repo.name, data.action)
debug(data)

app.emit(data.action, data, org, repo.name, data.sender.login)
}
}
18 changes: 0 additions & 18 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,3 @@ const port = process.env.PORT || 3000
app.listen(port, () => {
console.log('Example app listening on port', port)
})

if (process.env.SSE_RELAY) {
const EventSource = require('eventsource')
var es = new EventSource(process.env.SSE_RELAY)
es.onmessage = (e) => {
try {
const data = JSON.parse(e.data)
if (!data.action) return

var source = data.repository ? data.repository.full_name : data.organization.login
console.log('event@%s: %s', source, data.action)

app.emit(data.action, data)
} catch (e) {
console.error(e)
}
}
}