-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
189 lines (170 loc) · 5.74 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
var https = require('https');
const API_TOKEN = process.env['API_TOKEN'];
const settings = require('./settings.json');
exports.handler = async (event) => {
const gitHubBody = JSON.parse(event.body);
const gitHub = {
event: event.headers['X-GitHub-Event'],
action: gitHubBody.action,
repository: gitHubBody.repository.full_name,
pullRequest: gitHubBody.pull_request,
issue: gitHubBody.issue,
comment: gitHubBody.comment,
senderName: gitHubBody.sender.login,
senderUrl: gitHubBody.sender.html_url,
senderIconUrl: gitHubBody.sender.avatar_url,
};
const message = {
title: null,
url: null,
body: null,
}
// Pull request の description
if (gitHub.event === 'pull_request' && (gitHub.action === 'opened' || gitHub.action === 'edited')) {
message.title = `#${gitHub.pullRequest.number} ${gitHub.pullRequest.title}`;
message.url = gitHub.pullRequest.html_url;
message.body = gitHub.pullRequest.body;
}
// Issue の description
else if (gitHub.event === 'issues' && (gitHub.action === 'opened' || gitHub.action === 'edited')) {
message.title = `#${gitHub.issue.number} ${gitHub.issue.title}`;
message.url = gitHub.issue.html_url;
message.body = gitHub.issue.body;
}
// Pull request または Issue のコメント
else if (gitHub.event === 'issue_comment' && (gitHub.action === 'created' || gitHub.action === 'edited')) {
message.title = `Comment on #${gitHub.issue.number} ${gitHub.issue.title}`;
message.url = gitHub.comment.html_url;
message.body = gitHub.comment.body;
}
// Pull request のレビューのコメント
else if (gitHub.event === 'pull_request_review_comment' && (gitHub.action === 'created' || gitHub.action === 'edited')) {
message.title = `Review on #${gitHub.pullRequest.number} ${gitHub.pullRequest.title}`;
message.url = gitHub.comment.html_url;
message.body = gitHub.comment.body;
} else {
// どのイベントでもない場合は終了
return {
statusCode: 200,
body: 'Process has been passed through.',
};
}
// メッセージが空の場合は終了
if (!message.body) {
return {
statusCode: 200,
body: 'Process has been passed through.',
};
}
const slackChannels = []; // 通知先 Slack チャンネル
message.body = message.body.replace(/@[A-Za-z0-9-/]+/g, (match) => {
// リポジトリが一致するものを検索
const settingRepo = settings
.find((element) => {
return element.github_repository === gitHub.repository && element.github_id === match;
});
// リポジトリが*設定のものを検索
const settingAll = settings
.find((element) => {
return element.github_repository === '*' && element.github_id === match;
});
let setting;
if (settingRepo) {
setting = settingRepo; // リポジトリが一致するものが優先
} else if (settingAll) {
setting = settingAll;
} else {
return match; // 置換せずそのまま返す
}
if (setting.slack_channel !== '-' && isValidString(setting.slack_channel)) {
slackChannels.push(setting.slack_channel); // 通知先 Slack チャンネルのリストへ追加
}
if (setting.slack_id === '-' || !isValidString(setting.slack_id)) {
return match;
}
// Slack の ID へ置換
return setting.slack_id;
});
// 通知先が0件の場合は終了
if (!slackChannels.length) {
return {
statusCode: 200,
body: 'Process has been passed through.',
};
}
// 通知先の重複削除
const distinctSlackChannels = slackChannels
.filter((element, index, array) => array.indexOf(element) === index);
// Slack へ投稿
const results = await Promise.all(distinctSlackChannels.map(channel => {
return post(channel, message, gitHub);
}));
// 結果にエラーが含まれる場合
for (let r of results) {
if (r instanceof Error) {
return {
statusCode: 500,
body: 'Some were not posted.',
};
}
}
return {
statusCode: 200,
body: `All posts to slack.`,
};
};
function post(channel, message, gitHub) {
return new Promise((resolve, reject) => {
const data = {
username: "GitHub",
icon_url: "https://github.githubassets.com/images/modules/logos_page/GitHub-Mark.png",
channel: channel,
attachments: [
{
fallback: message.body,
color: "#ffb432",
author_name: gitHub.senderName,
author_link: gitHub.senderUrl,
author_icon: gitHub.senderIconUrl,
title: message.title,
title_link: message.url,
text: message.body,
footer: gitHub.repository,
footer_icon: "https://github.githubassets.com/images/modules/logos_page/GitHub-Mark.png",
ts: Math.floor(new Date().getTime() / 1000),
}
],
};
const options = {
host: 'slack.com',
port: '443',
path: '/api/chat.postMessage',
method: 'POST',
headers: {
'Authorization': 'Bearer ' + API_TOKEN,
'Content-Type': 'application/json',
},
};
const req = https.request(options, res => {
res.setEncoding('utf8');
res.on('data', (chunk) => {
const result = JSON.parse(chunk);
if (result.ok) {
resolve(chunk);
} else {
resolve(new Error());
}
});
});
req.on('error', e => {
// Lambda を正常に終了する為には全ての並列リクエストが完了するのを
// 待ってからエラー処理する必要があるのでここでは reject しない
resolve(new Error());
});
req.write(JSON.stringify(data));
req.end();
});
}
function isValidString(arg) {
return typeof arg === 'string' && arg !== '';
}