-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
301 lines (257 loc) · 8 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
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
const encryptedTokenName = process.env.encryptedTokenName;
const dynamoDBTableSlackCache = process.env.dynamoDBTableSlackCache;
const request = require('request');
const rp = require('request-promise-native');
const AWS = require('aws-sdk');
const moment = require('moment');
class Config {
constructor() {
this.map = new Map;
let config = require('./config/config.json');
const topLevelKeyList = ['account_map', 'ignore_event_map', 'repository_map'];
for (let i = 0, len = topLevelKeyList.length; i < len; i++) {
this.map.set(topLevelKeyList[i],config[topLevelKeyList[i]] || {});
}
}
set(key, value) {
this.map.set(key, value);
}
get(key) {
return this.map.get(key);
}
}
const config = new Config();
// promise functions
const getItemPromise = function(table) {
const dynamodb = new AWS.DynamoDB({apiVersion: '2012-08-10'});
return dynamodb.getItem({
Key: {
'entrypoint': {
S: 'users.list'
}
},
TableName: table,
ConsistentRead: true,
ReturnConsumedCapacity: 'TOTAL'
}).promise();
};
const requestPromise = function(token) {
return rp({
url: 'https://slack.com/api/users.list',
method: 'GET',
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
json: true,
qs: {
token: token
}
});
};
const getParameterPromise = function(token) {
const ssm = new AWS.SSM({apiVersion: '2014-11-06'});
return ssm.getParameter({
Name: token,
WithDecryption: true
}).promise();
};
const updateItemPromise = function(table, name_id_pair) {
const dynamodb = new AWS.DynamoDB({apiVersion: '2012-08-10'});
return dynamodb.updateItem({
Key: {
'entrypoint': {
S: 'users.list'
}
},
UpdateExpression: 'SET #E=:e, #R=:r',
ExpressionAttributeNames: {
'#E': 'expired_at',
'#R': 'response'
},
ExpressionAttributeValues: {
':e': {
N: moment().add('days', 1).unix().toString()
},
':r': {
S: JSON.stringify(name_id_pair)
}
},
ReturnValues: 'ALL_NEW',
TableName: table,
ReturnConsumedCapacity: 'TOTAL'
}).promise();
};
// utilities
const link = function (url, text) {
return '<' + url + '|' + text + '>';
};
const name_id_pair = function (members) {
// [NOTE]
// return an object like { 'Tatsuro Mitsuno': '<slack user id>', }
const active_members = members.filter(function(member){
return (member['deleted'] === false && member['is_bot'] === false);
});
let pair = {};
for(let i = 0; i < active_members.length; i++) {
let name = '';
if (active_members[i]['profile']['display_name_normalized'] === '') {
name = active_members[i]['profile']['real_name_normalized'];
} else {
name = active_members[i]['profile']['display_name_normalized'];
}
pair[name] = active_members[i]['id'];
}
return pair;
};
// slack real name to slack user id
const r2i = function (members) {
let account_map = {};
// [NOTE]
// config.get('account_map') = { 'github account': 'slack real name' }
// members = { 'slack real name': 'slack user id'}
// => account_map = { 'github account': 'slack user id' }
Object.keys(config.get('account_map')).forEach(function (key) {
account_map[key] = members[config.get('account_map')[key]] || config.get('account_map')[key];
});
return account_map;
};
// github 2 slack
const g2s = function (user) {
return config.get('account_map')[user] || user;
};
// repository to channel
const r2c = function(repository) {
const name = repository['name'];
let channel = null;
for ( let rule in config.get('repository_map') ) {
let re = new RegExp(rule, 'i');
if (re.test(name)) {
channel = config.get('repository_map')[rule];
break;
}
}
return channel;
};
const isIgnoreEvent = function (event, action) {
const index = (config.get('ignore_event_map')[event] || []).indexOf(action);
return index !== -1;
};
const userList = function (obj) {
return obj.map(function(x){
return '<@' + g2s(x.login) + '>';
}).join(' ');
};
const replaceUser = function (text) {
// [NOTE] GitHub Webhook replace space to plus (+) mark.
// There is no way to distinguish between + in the text and + in the blank.
return text.replace(/\+/g,' ').replace(/@([a-zA-Z0-9_\-]+)/g, function(match, p1) {
return '<@' + g2s(p1) + '>';
});
};
exports.handler = (event, context, callback) => {
// response to GitHub
const response = {
statusCode: 200,
headers: {},
body: JSON.stringify({ 'message': 'gomashio received' })
};
const githubEvent = event.headers['X-GitHub-Event'];
const payloadText = decodeURIComponent(event.body.replace(/^payload=/,''));
const payload = JSON.parse(payloadText);
const action = payload.action || '';
const repository = payload.repository || {};
// [NOTE] CloudWatch Logs will display cleanly for text format than json format.
console.info(githubEvent);
console.info(payloadText);
console.info(action);
console.info(repository);
if (isIgnoreEvent(githubEvent, action)) {
console.info('ignore event. nothing to do.');
context.succeed(response);
}
if (Object.keys(repository).length === 0) {
console.info('repository is empty. nothing to do.');
context.succeed(response);
}
const channel = r2c(repository);
if (channel === null) {
console.info('repository is not eligible for notification. nothing to do.');
context.succeed(response);
}
getParameterPromise(encryptedTokenName).then(function(res) {
config.set('slackToken', res.Parameter.Value);
return getItemPromise(dynamoDBTableSlackCache);
}).then(function(res) {
const item = res.Item || {};
if (Object.keys(item).length === 0) {
console.info('cache unavailable');
return requestPromise(config.get('slackToken')).then(function (res) {
if (res['ok'] === false) {
throw new Error(res['error']);
}
return updateItemPromise(dynamoDBTableSlackCache, name_id_pair(res['members']));
}).then(function (res) {
return res.Attributes.response['S'];
}).catch(function (err) {
console.info(err);
context.succeed(response);
});
} else {
console.info('cache available');
return item.response['S'];
}
}).then(function(members) {
config.set('account_map', r2i(JSON.parse(members)));
let text='';
switch (githubEvent){
case 'issue_comment':
case 'pull_request_review_comment':
const comment = payload.comment;
text += comment.user.login + ': \n';
text += replaceUser(comment.body) + '\n';
text += comment.html_url;
break;
case 'issues':
const issue = payload.issue;
if (action == 'assigned') {
text += 'Issue ' + action + '\n';
text += 'Assignees: ' + userList(issue.assignees) + '\n';
text += link(issue.html_url, issue.title);
}
break;
case 'pull_request':
const pull_request = payload.pull_request;
if (action == 'assigned') {
text += 'Pull Request ' + action + '\n';
text += pull_request.title + '\n';
text += 'Reviewers: ' + userList(pull_request.requested_reviewers) + '\n';
text += 'Assignees: ' + userList(pull_request.assignees) + '\n';
text += link(pull_request.html_url, pull_request.title);
}
break;
default:
break;
}
if (text === '') {
console.info('text is empty. nothing to do.');
context.succeed(response);
}
request({
url: 'https://slack.com/api/chat.postMessage',
method: 'POST',
headers: {
'Content-Type': 'application/json; charset=utf-8',
'Authorization': 'Bearer ' + config.get('slackToken')
},
json: {
text: text,
link_names: 1,
channel: channel
}
}, function (error, res, body) {
console.info(body);
context.succeed(response);
});
}).catch(function (err) {
console.info(err);
context.succeed(response);
});
};