-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
185 lines (169 loc) · 5.82 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
'use strict';
//requires
const Slack = require('slack-node');
const slack = new Slack();
// settings (lambda環境変数)
var BOTNAME = process.env['BOTNAME'];
var BASEURL = process.env['BASEURL'];
var WEBHOOKURI = process.env['WEBHOOKURI'];
slack.setWebhook(WEBHOOKURI);
// Format chat message
function makeChatMessage(body) {
var msgObj = new Object();
var label = "";
var bl_key = "";
var bl_summary = "";
var bl_comment = "";
var bl_url = "";
switch (body.type) {
case 1:
label = "追加";
bl_key = "[" + body.project.projectKey + "-" + body.content.key_id + "]";
bl_summary = "「" + body.content.summary + "」";
bl_url = BASEURL + "view/" + body.project.projectKey + "-" + body.content.key_id;
bl_comment = body.content.description;
break;
case 2:
label = "更新";
bl_key = "[" + body.project.projectKey + "-" + body.content.key_id + "]";
bl_summary = "「" + body.content.summary + "」";
bl_url = BASEURL + "view/" + body.project.projectKey + "-" + body.content.key_id;
bl_comment = body.content.description;
break;
case 3:
label = "コメント";
bl_key = "[" + body.project.projectKey + "-" + body.content.key_id + "]";
bl_summary = "「" + body.content.summary + "」";
bl_url = BASEURL + "view/" + body.project.projectKey + "-" + body.content.key_id + "#comment-" + body.content.comment.id;
bl_comment = body.content.comment.content;
break;
case 14:
label = "課題まとめて更新";
bl_key = "";
bl_summary = "";
bl_url = BASEURL + "projects/" + body.project.projectKey;
bl_comment = body.createdUser.name + "さんが課題をまとめて操作しました。";
break;
case 5:
label = "Wiki追加";
bl_key = "";
bl_summary = "「" + body.content.name + "」";
bl_url = BASEURL + "alias/wiki/" + body.content.id;
bl_comment = body.createdUser.name + "さんがWikiページを追加しました。";
break;
case 6:
label = "Wiki更新";
bl_key = "";
bl_summary = "「" + body.content.name + "」";
bl_url = BASEURL + "alias/wiki/" + body.content.id;
bl_comment = body.createdUser.name + "さんがWikiページを更新しました。";
break;
case 11:
label = "SVNコミット";
bl_key = "[r" + body.content.rev + "]";
bl_summary = "";
bl_url = BASEURL + "rev/" + body.project.projectKey + "/" + body.content.rev;
bl_comment = body.content.comment;
break;
case 12:
label = "Gitプッシュ";
var git_rev = body.content.revisions[0].rev;
git_rev = git_rev.substr(0, 10);
bl_key = "[" + git_rev + "]";
bl_summary = "";
bl_url = BASEURL + "git/" + body.project.projectKey + "/" + body.content.repository.name + "/" + body.content.revision_type + "/" + body.content.revisions[0].rev;
bl_comment = body.content.revisions[0].comment;
break;
case 18:
label = "プルリクエスト追加";
bl_key = "( 担当:" + body.content.assignee.name + " )";
bl_summary = "「" + body.content.summary + "」";
bl_url = BASEURL + "git/" + body.project.projectKey + "/" + body.content.repository.name + "/pullRequests/" + body.content.number;
bl_comment = body.content.description;
break;
case 19:
label = "プルリクエスト更新";
bl_key = "( 担当:" + body.content.assignee.name + " )";
bl_summary = "「" + body.content.summary + "」";
bl_url = BASEURL + "git/" + body.project.projectKey + "/" + body.content.repository.name + "/pullRequests/" + body.content.number;
bl_comment = body.content.description;
break;
case 20:
label = "プルリクエストコメント";
bl_key = "( 担当:" + body.content.assignee.name + " )";
bl_summary = "";
bl_url = BASEURL + "git/" + body.project.projectKey + "/" + body.content.repository.name + "/pullRequests/" + body.content.number + "#comment-" + body.content.comment.id;
bl_comment = body.content.comment.content;
break;
default:
return;
}
console.log(label);
if (label) {
msgObj['message'] = bl_key + " " +
label +
bl_summary +
" by " + body.createdUser.name +
"\n " + bl_url;
// 長いコメントは後ろカット
if (bl_comment.length > 200) {
bl_comment = bl_comment.substr(0, 200) + "...";
}
msgObj['comment'] = bl_comment;
}
return msgObj;
}
// POST Slack
function postSlack(channel, message, comment) {
// 引用コメント部分整形
var attachments_opts = "";
if (comment) {
attachments_opts = {
"color": "#42ce9f",
"fields": [
{
"value": comment,
"short": false
}
]
};
}
return new Promise(function (resolve, reject) {
slack.webhook({
channel: channel,
username: BOTNAME,
text: message,
attachments: [attachments_opts]
}, function (err, response) {
if (err) {
console.log(response);
reject(new Error('Error'));
return;
} else {
resolve(response);
}
});
});
}
// Main Handler
exports.handler = function (event, context, callback) {
var room;
var body;
var slackObj = new Object();
console.log('event:', JSON.stringify(event, null, 4));
if (event.pathParameters.room && event.body) {
// 通知先チャンネル取得
room = event.pathParameters.room;
console.log('room:', room);
// json整形・メッセージ作成
body = JSON.parse(event.body);
slackObj = makeChatMessage(body);
// Slack投稿
if (slackObj) {
postSlack(room, slackObj['message'], slackObj['comment']);
}
} else {
console.log('対象チャンネルが指定されていないか、データが取得できません。');
}
callback(null, 'Done.');
};