-
Notifications
You must be signed in to change notification settings - Fork 0
/
webhook-server.js
84 lines (66 loc) · 1.85 KB
/
webhook-server.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
'use strict'
var http = require('http')
var createHandler = require('github-webhook-handler')
var handler = createHandler({ path: '/webhook', secret: process.env.WEBHOOK_SECRET })
var port = process.env.PORT || 7000
var data = require('./data')
http.createServer(function (req, res) {
handler(req, res, function (err) {
res.statusCode = 404
res.end('no such location')
})
}).listen(port)
handler.on('error', function (err) {
console.error('Error:', err.message)
})
// ----
// code events
handler.on('push', function (event) {
data.recordPush()
})
// ----
// communication events
var recentIssues = {}
handler.on('commit_comment', function (event) {
data.recordComm()
})
handler.on('pull_request', function (event) {
data.recordComm()
})
handler.on('pull_request_review_comment', function (event) {
data.recordComm()
})
handler.on('issues', function (event) {
var issue = event.payload.issue
recentIssues[issue.number] = issue
data.recordComm()
})
handler.on('issue_comment', function (event) {
var issue = event.payload.issue
recentIssues[issue.number] = issue
data.recordComm()
})
var sendToSlack = require('./send-to-slack')
var lastState
setInterval(function () {
var commChart = data.renderCommChart()
var pushChart = data.renderPushChart()
// don't keep sending the chart if nothing has changed
var state = commChart + pushChart
if (lastState === state) { return }
lastState = state
var info = {
repoUrl: process.env.REPO_URL,
commChart: commChart,
pushChart: pushChart,
recentIssues: []
}
// format recent issues list
Object.keys(recentIssues).forEach(function (key) {
var issue = recentIssues[key]
info.recentIssues.push(issue.html_url)
})
// clear the recent issues list
recentIssues = {}
sendToSlack('_Communication:_ `' + commChart + '` _Pushes_: `' + pushChart + '`', info)
}, 5000)