-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathindex.js
135 lines (122 loc) · 4.44 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
// Notify PR Review
// Copyright (c) 2023-present NAVER Corp.
// Apache-2.0
const core = require('@actions/core');
const github = require('@actions/github');
const axios = require('axios');
const ENCODE_PAIR = {
"<": "<",
">": ">"
};
const encodeText = text => text.replace(/[<>]/g, matched => ENCODE_PAIR[matched]);
const fetchUser = url => axios({
method: "get",
headers: {
Authorization: `token ${core.getInput("token")}`
},
url
}).then(res => res.data);
const D0 = "D-0";
const sendSlack = ({repoName, labels, title, url, email}) => {
const [name] = email.split("@");
const d0exists = labels.some(label => label.name === D0);
return axios({
method: "post",
headers: {
Authorization: `Bearer ${core.getInput("slackBotToken")}`,
"Content-Type": "application/json"
},
url: "https://slack.com/api/chat.postMessage",
data: {
channel: `@${name}`,
text: "리뷰 요청을 받았어요! 😊",
blocks: [
{
type: "section",
text: {
type: "mrkdwn",
text: `📬 <@${name}> 님 새로운 리뷰 요청이 도착했어요! 가능한 빠르게 리뷰에 참여해 주세요:`
}
},
{
type: "section",
text: {
type: "mrkdwn",
text: `*${repoName}:*\n<${url}|${encodeText(title)}>`
}
},
...labels.length ? [{
type: "actions",
elements: labels.map(({name}) => ({
type: "button",
text: {
type: "plain_text",
text: name
},
...name === D0 ? {style: "danger"} : {}
}))
}] : [],
...d0exists ? [{
type: "section",
text: {
type: "mrkdwn",
text: `*🚨 \`${D0}\` PR로 매우 긴급한 PR입니다! 지금 바로 리뷰에 참여해 주세요! 🚨*`
}
}] : [],
{
type: "divider"
},
{
type: "context",
elements: [
{
type: "mrkdwn",
text: "💪 코드 리뷰는 코드 품질을 향상시키고, 버그를 줄이며, 팀원 간의 지식 공유와 협업을 촉진하는 핵심 프로세스입니다.\n🙏 적극적인 참여와 의견을 부탁드립니다."
}
]
}
]
}
});
};
(async () => {
try {
const {
context: {
payload: {
pull_request: {
title,
html_url: prUrl,
labels
},
sender,
requested_reviewer: requestedReviewer,
requested_team: requestedTeam,
repository: {
full_name: repoName
}
}
}
} = github;
if (!requestedReviewer) {
core.notice(`Failed: 'requested_reviewer' does not exist. Looks like you've requested a team review which is not yet supported. The team name is '${requestedTeam.name}'.`);
return;
}
const {login, url} = requestedReviewer;
core.notice(`Sender: ${sender.login}, Receiver: ${login}, PR: ${prUrl}`);
core.info(`'${sender.login}' requests a pr review for ${title}(${prUrl})`);
core.info(`Fetching information about '${login}'...`);
const {email} = await fetchUser(url);
core.info(`Sending a slack msg to '${login}'...`);
if (!email) {
core.warning(`Failed: '${login}' has no public email.`);
core.notice(`Failed: '${login}' has no public email.`);
return;
}
await sendSlack({repoName, labels, title, url: prUrl, email});
core.info("Successfully sent");
core.notice("Successfully sent");
} catch (error) {
core.setFailed(error.message);
}
})();