Skip to content

Commit 101e1c7

Browse files
committed
move sse code to github-events
1 parent 77ec3fd commit 101e1c7

2 files changed

Lines changed: 25 additions & 25 deletions

File tree

‎lib/github-events.js‎

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const crypto = require('crypto')
2-
const debug = require('debug')('github')
2+
const debug = require('debug')('github-events')
33

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

@@ -28,13 +28,31 @@ module.exports = (app) => {
2828
const data = req.body
2929
data.action = data.action ? event + '.' + data.action : event
3030

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

34-
if (debug.enabled) {
35-
debug(JSON.stringify(data, null, 2))
34+
if (process.env.SSE_RELAY) {
35+
const EventSource = require('eventsource')
36+
var es = new EventSource(process.env.SSE_RELAY)
37+
es.onmessage = (e) => {
38+
try {
39+
const data = JSON.parse(e.data)
40+
if (!data.action) return
41+
42+
emit(data)
43+
} catch (e) {
44+
console.error(e)
45+
}
3646
}
47+
}
3748

38-
app.emit(data.action, data)
39-
})
49+
function emit (data) {
50+
const repo = data.repository
51+
const org = repo.owner.login || data.organization.login
52+
53+
console.log('event@%s/%s: %s', org, repo.name, data.action)
54+
debug(data)
55+
56+
app.emit(data.action, data, org, repo.name, data.sender.login)
57+
}
4058
}

‎server.js‎

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,3 @@ const port = process.env.PORT || 3000
2121
app.listen(port, () => {
2222
console.log('Example app listening on port', port)
2323
})
24-
25-
if (process.env.SSE_RELAY) {
26-
const EventSource = require('eventsource')
27-
var es = new EventSource(process.env.SSE_RELAY)
28-
es.onmessage = (e) => {
29-
try {
30-
const data = JSON.parse(e.data)
31-
if (!data.action) return
32-
33-
var source = data.repository ? data.repository.full_name : data.organization.login
34-
console.log('event@%s: %s', source, data.action)
35-
36-
app.emit(data.action, data)
37-
} catch (e) {
38-
console.error(e)
39-
}
40-
}
41-
}

0 commit comments

Comments
 (0)