-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler.js
More file actions
277 lines (235 loc) · 7.66 KB
/
handler.js
File metadata and controls
277 lines (235 loc) · 7.66 KB
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
'use strict'
const crypto = require('crypto')
const https = require('https')
const url = require('url')
const AWS = require('aws-sdk')
const kms = new AWS.KMS({ region: 'ap-northeast-1' })
const encrypted = {
github_webhook_secret: process.env.GITHUB_WEBHOOK_SECRET_ENCRYPTED,
slack_incoming_webhook_url: process.env.SLACK_INCONMING_WEBHOOK_URL_ENCRYPTED
}
let decrypted = {}
// 通知許可している github event
const allowActions = [
'issue_comment',
'pull_request',
'pull_request_review',
'pull_request_review_comment'
]
// github username : slack username
const git2slackNames = {
'kenzo0107': 'kenzo.tanaka',
}
// slack DM 拒否ユーザリスト
const denySlackDmUsers = [
'orehayamete'
]
function signRequestBody (key, body) {
return `sha1=${crypto.createHmac('sha1', key).update(body, 'utf-8').digest('hex')}`
}
function postMessage (message, callback) {
const body = JSON.stringify(message)
const options = url.parse('https://' + decrypted.slack_incoming_webhook_url)
options.method = 'POST'
options.headers = {
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(body)
}
const postReq = https.request(options, (res) => {
const chunks = []
res.setEncoding('utf8')
res.on('data', (chunk) => chunks.push(chunk))
res.on('end', () => {
if (callback) {
console.error({
body: chunks.join(''),
statusCode: res.statusCode,
statusMessage: res.statusMessage
})
}
})
return res
})
postReq.write(body)
postReq.end()
}
function sendSlackDM (channel, message, callback) {
const slackMessage = {
channel: channel,
text: `${message}`,
username: `${process.env.SLACK_USERNAME}`,
icon_emoji: `${process.env.SLACK_ICON}`
}
postMessage(slackMessage, (response) => {
if (response.statusCode < 400) {
console.info('Message posted successfully')
callback(null)
} else if (response.statusCode < 500) {
console.error(`Error posting message to Slack API: ${response.statusCode} - ${response.statusMessage}`)
callback(null)
} else {
console.error(`Server error when processing message: ${response.statusCode} - ${response.statusMessage}`)
}
})
}
function extractUsernameFromMessage (message) {
let usernames = []
let usernameCandidates = message.match(/@[a-zA-Z0-9-]+/g)
if (!usernameCandidates) {
return usernames
}
for (let i in usernameCandidates) {
var u = usernameCandidates[i].replace('@', '')
if (denySlackDmUsers.indexOf(u) > -1) {
continue
}
if (git2slackNames[u]) {
usernames.push(`@${git2slackNames[u]}`)
}
// github username と slack username に命名規則があれば、
// ここで変換 git --> slack username へ変更するも良し
}
return usernames
}
function processEvent (event, context, callback) {
console.log('event.body', event.body)
const body = JSON.parse(event.body)
if (['deleted', 'closed'].indexOf(body.action) > -1) {
console.log(`No target acction: ${body.action}`)
return
}
const headers = event.headers
const githubEvent = headers['X-GitHub-Event']
if (allowActions.indexOf(githubEvent) < 0) {
console.log(`No target githubEvent: ${githubEvent}`)
return
}
const sig = headers['X-Hub-Signature']
const id = headers['X-GitHub-Delivery']
const calculatedSig = signRequestBody(decrypted.github_webhook_secret, event.body)
if (typeof decrypted.github_webhook_secret !== 'string') {
return callback(null, {
statusCode: 401,
headers: { 'Content-Type': 'text/plain' },
body: 'Must provide a \'GITHUB_WEBHOOK_SECRET\' env variable'
})
}
if (!sig) {
return callback(null, {
statusCode: 401,
headers: { 'Content-Type': 'text/plain' },
body: 'No X-Hub-Signature found on request'
})
}
if (!githubEvent) {
return callback(null, {
statusCode: 422,
headers: { 'Content-Type': 'text/plain' },
body: 'No X-Github-Event found on request'
})
}
if (!id) {
return callback(null, {
statusCode: 401,
headers: { 'Content-Type': 'text/plain' },
body: 'No X-Github-Delivery found on request'
})
}
if (sig !== calculatedSig) {
return callback(null, {
statusCode: 401,
headers: { 'Content-Type': 'text/plain' },
body: 'X-Hub-Signature incorrect. Github webhook token doesn\'t match'
})
}
console.log(`Github-Event: "${githubEvent}" with action: "${body.action}"`)
let title
let comment
let usernames
let htmlUrl
let commentUser
let commentUsers
let message
switch (githubEvent) {
case 'issue_comment':
comment = body.comment.body || body.issue.body
usernames = extractUsernameFromMessage(comment)
if (usernames.length === 0) {
return
}
title = body.issue.title
htmlUrl = body.comment.html_url
commentUser = body.comment.user.login
// コメントしたユーザ取得
commentUsers = extractUsernameFromMessage(`@${commentUser}`)
if (commentUsers !== null && commentUsers.length > 0) {
commentUser = `\`${commentUser}\` (${commentUsers[0]})`
}
message = `:git: *${title}* ${htmlUrl} \n${commentUser}'s comment: \`\`\`${comment}\`\`\` `
for (let i in usernames) {
sendSlackDM(usernames[i], message, callback)
}
break
case 'pull_request_review_comment':
comment = body.comment.body
usernames = extractUsernameFromMessage(comment)
if (usernames.length === 0) {
return
}
title = body.pull_request.title
htmlUrl = body.comment.html_url
commentUser = body.comment.user.login
// コメントしたユーザ取得
commentUsers = extractUsernameFromMessage(`@${commentUser}`)
if (commentUsers !== null && commentUsers.length > 0) {
commentUser = `\`${commentUser}\` (${commentUsers[0]})`
}
message = `:git: *${title}* ${htmlUrl} \n${commentUser}'s comment: \`\`\`${comment}\`\`\` `
for (let i in usernames) {
sendSlackDM(usernames[i], message, callback)
}
break
case 'pull_request':
comment = body.pull_request.body
usernames = extractUsernameFromMessage(comment)
if (usernames.length === 0) {
return
}
title = body.pull_request.title
htmlUrl = body.pull_request.html_url
commentUser = body.pull_request.user.login
commentUsers = extractUsernameFromMessage(`@${commentUser}`)
if (commentUsers !== null && commentUsers.length > 0) {
commentUser = `\`${commentUser}\` (${commentUsers[0]})`
}
message = `:git: *${title}* ${htmlUrl} \n${commentUser}'s comment: \`\`\`${comment}\`\`\` `
for (let i in usernames) {
sendSlackDM(usernames[i], message, callback)
}
break
}
const response = {
statusCode: 200,
body: JSON.stringify({
input: event
})
}
return callback(null, response)
}
module.exports.githubWebhookListener = async (event, context, callback) => {
if (decrypted.github_webhook_secret && decrypted.slack_incoming_webhook_url) {
processEvent(event, context, callback)
} else {
try {
const p1 = { CiphertextBlob: new Buffer(encrypted.github_webhook_secret, 'base64') }
const p2 = { CiphertextBlob: new Buffer(encrypted.slack_incoming_webhook_url, 'base64') }
const d = [await kms.decrypt(p1).promise(), await kms.decrypt(p2).promise()]
decrypted.github_webhook_secret = d[0].Plaintext.toString('ascii')
decrypted.slack_incoming_webhook_url = d[1].Plaintext.toString('ascii')
processEvent(event, context, callback)
} catch (err) {
console.log('Decrypt error:', err)
callback(err)
}
}
}