-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
215 lines (201 loc) · 8.21 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
const debug = require('debug')('vhs-hookhub-github-nomos')
debug('Loading vhs-hookhub-github-nomos')
debug(__dirname)
const express = require('express')
const router = express.Router()
const config = require('./config.json')
const { xHubSignatureMiddleware } = require('x-hub-signature-middleware')
const smb = require('slack-message-builder')
// Perform sanity check
router.use(function (req, res, next) {
if (
req.header('X-Hub-Signature') == null ||
req.header('X-Hub-Signature').length < 40 ||
req.header('X-GitHub-Event') == undefined ||
req.header('X-GitHub-Event') == '' ||
req.rawBody == undefined
) {
res.status(412).send({
result: 'ERROR',
message: 'Missing or invalid request arguments'
})
} else {
next()
}
})
// Check X-Hub-Signature
router.use(
xHubSignatureMiddleware({
algorithm: 'sha1',
secret: config.github.secret,
require: true,
getRawBody: (req) => req.rawBody
})
)
/* Default handler. */
router.use('/', async function (req, res, next) {
debug('Handling default request')
let post_body = generateMessage(req.header('X-GitHub-Event'), req.body)
debug('post_body:', post_body)
const post_options = {
method: 'POST',
body: JSON.stringify(post_body)
}
try {
const data = await (await fetch(config.slack.url, post_options)).json()
res.send({
result: 'OK',
message: data
})
} catch (err) {
res.status(500).send({
result: 'ERROR',
message: err
})
}
})
module.exports = router
const generateMessage = function (event_type, payload) {
debug(`Generating message for ${event_type}`)
let slack_message = smb()
.username(config.slack.options.username)
.iconEmoji(config.slack.options.icon_emoji)
.channel(config.slack.options.channel)
switch (event_type) {
case 'push':
debug(`Handling push event`)
payload.commits.forEach(function (commit) {
slack_message = slack_message
.text(
"The following commit(s) got pushed to '" +
payload.repository.name +
"':"
)
.attachment()
.fallback('Required plain-text summary of the attachment.')
.color('#0000cc')
.authorName(payload.sender.login)
.authorLink(payload.sender.html_url)
.authorIcon(payload.sender.avatar_url)
.title('Commit: ' + commit.id)
.titleLink(commit.url)
.text(commit.message)
.footer('Via: vhs-hookhub-github-nomos')
.ts(Math.round(Date.parse(commit.timestamp) / 1000))
.end()
})
break
case 'issue_comment':
switch (payload.action) {
case 'created':
slack_message = slack_message
.attachment()
.fallback(
`Issue ${payload.issue.number} - ${payload.issue.title} has a new comment by ${payload.issue.user.login}`
)
.color('#0000cc')
.authorName(payload.issue.user.login)
.authorLink(payload.issue.user.html_url)
.authorIcon(payload.issue.user.avatar_url)
.title(
`Issue ${payload.issue.number} - ${payload.issue.title} has a new comment by ${payload.issue.user.login}`
)
.titleLink(payload.issue.html_url)
.text(payload.comment.body)
.footer('Via: vhs-hookhub-github-nomos')
.ts(
Math.round(
Date.parse(payload.issue.updated_at) / 1000
)
)
.end()
break
default:
slack_message = slack_message
.attachment()
.fallback(
`Issue ${payload.issue.number} - ${payload.issue.title} has a comment ${payload.action} by ${payload.issue.user.login}`
)
.color('#0000cc')
.authorName(payload.issue.user.login)
.authorLink(payload.issue.user.html_url)
.authorIcon(payload.issue.user.avatar_url)
.title(
`Issue ${payload.issue.number} - ${payload.issue.title} has a comment ${payload.action} by ${payload.issue.user.login}`
)
.titleLink(payload.issue.html_url)
.text(payload.comment.body)
.footer('Via: vhs-hookhub-github-nomos')
.ts(
Math.round(
Date.parse(payload.issue.closed_at) / 1000
)
)
.end()
break
}
break
case 'issues':
switch (payload.action) {
case 'closed':
slack_message = slack_message
.attachment()
.fallback(
`Issue ${payload.issue.number} - ${payload.issue.title} was ${payload.action} by ${payload.issue.user.login}`
)
.color('#0000cc')
.authorName(payload.issue.user.login)
.authorLink(payload.issue.user.html_url)
.authorIcon(payload.issue.user.avatar_url)
.title(
`Issue ${payload.issue.number} - ${payload.issue.title} was ${payload.action} by ${payload.issue.user.login}`
)
.titleLink(payload.issue.html_url)
.text('See issue for closing comment')
.footer('Via: vhs-hookhub-github-nomos')
.ts(
Math.round(
Date.parse(payload.issue.closed_at) / 1000
)
)
.end()
break
default:
slack_message = slack_message
.attachment()
.fallback(
`Issue ${payload.issue.number} - ${payload.issue.title} was ${payload.action} by ${payload.issue.user.login}`
)
.color('#0000cc')
.authorName(payload.issue.user.login)
.authorLink(payload.issue.user.html_url)
.authorIcon(payload.issue.user.avatar_url)
.title(
`Issue ${payload.issue.number} - ${payload.issue.title} was ${payload.action} by ${payload.issue.user.login}`
)
.titleLink(payload.issue.html_url)
.text('See issue for more info')
.footer('Via: vhs-hookhub-github-nomos')
.ts(
Math.round(
Date.parse(payload.issue.closed_at) / 1000
)
)
.end()
break
}
break
default:
debug(`Handling unknown event`)
slack_message = slack_message.text(
"We received a new '" +
event_type +
"' notification for '" +
payload.repository.name +
"', but we didn't know what to do with this event!"
)
break
}
debug(`Returning JSON payload`)
return slack_message.json()
}