-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
281 lines (259 loc) · 7.27 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
"use strict";
const core = require("@actions/core");
const github = require("@actions/github");
// Requirements outside of Github Actions
const okta = require("@okta/okta-sdk-nodejs");
const { WebClient } = require("@slack/web-api");
const { Octokit } = require("@octokit/rest");
const { createActionAuth } = require("@octokit/auth-action");
const oktaClient = new okta.Client();
const githubFieldName = process.env.GITHUB_FIELD_NAME;
const token = process.env.SLACK_BOT_TOKEN;
const slack = new WebClient(token);
const ellipsize = function (text, maxLen) {
if (text.length <= maxLen) {
return text;
}
const ellipsis = "...";
return text.slice(0, maxLen - ellipsis.length) + ellipsis;
};
const slackMessageTemplateNewPR = function (requestUser, pr) {
return [
{
type: "section",
text: {
type: "mrkdwn",
text: `@${requestUser} has requested a review on the following Pull Request: `,
},
},
{
type: "divider",
},
{
type: "section",
text: {
type: "mrkdwn",
text: pr.url,
},
accessory: {
type: "button",
text: {
type: "plain_text",
text: ellipsize(pr.title, 75),
},
url: pr.url,
},
},
];
};
const slackMessageTemplateReviewedPR = function (reviewUser, pr) {
const verb =
{
approved: "has approved",
request_changes: "has requested changes on",
commented: "has commented on",
}[pr.state] || "has commented on";
return [
{
type: "section",
text: {
type: "mrkdwn",
text: `${reviewUser} ${verb} your PR "${pr.title}": `,
},
},
{
type: "divider",
},
{
type: "section",
text: {
type: "mrkdwn",
text: pr.url,
},
accessory: {
type: "button",
text: {
type: "plain_text",
text: ellipsize(pr.title, 75),
},
url: pr.url,
},
},
];
};
const sendSlackMessage = async function (message, to) {
const response = await slack.chat.postMessage({
channel: to,
blocks: message,
});
return response;
};
const getOktaUser = async function (handle) {
const search = `(profile.${githubFieldName} eq "@${handle}") or (profile.${githubFieldName} eq "${handle}")`;
console.log("Searching okta", search);
const result = await oktaClient.listUsers({ search, limit: 1 }).next();
console.log("Found oktaUser", result.value.profile.Github);
return result;
};
const getUserEmailByGithub = function (oktaUser) {
return oktaUser.value.profile.email;
};
const getSlackIdByEmail = async function (email) {
const result = await slack.users.lookupByEmail({ email: email });
console.log("Using email", email, "found slack user", result);
return result.user.id;
};
const getSlackNameByEmail = async function (email) {
const result = await slack.users.lookupByEmail({ email: email });
console.log("Using email", email, "found slack user", result);
return result.user.name;
};
const getSlackNameByGithub = async function (ghHandle) {
try {
const slackName = await getOktaUser(ghHandle)
.then(getUserEmailByGithub)
.then(getSlackNameByEmail);
return slackName;
} catch (err) {
return handle;
}
};
const getSlackIdByGithub = async function (ghHandle) {
const slackId = await getOktaUser(ghHandle)
.then(getUserEmailByGithub)
.then(getSlackIdByEmail);
return slackId;
};
let _octokit;
const getOctokit = async function () {
if (!_octokit) {
const auth = createActionAuth();
const ghAuthentication = await auth();
_octokit = new Octokit({
authStrategy: createActionAuth,
auth: ghAuthentication,
});
}
return _octokit;
};
const getGithubHandlesForTeam = async function (org, team_slug) {
const octokit = await getOctokit();
const members = await octokit.teams.listMembersInOrg({
org,
team_slug,
});
return members.data;
};
const handleReviewRequested = async function (payload) {
let pullRequestData;
try {
pullRequestData = {
title: payload.pull_request.title,
url: payload.pull_request.html_url,
org: payload.organization.login,
requester: payload.pull_request.user.login,
};
if (payload.requested_team) {
const teamMembers = await getGithubHandlesForTeam(
payload.organization.login,
payload.requested_team.slug
);
pullRequestData.reviewers = teamMembers
.map((teamMember) => teamMember.login)
.filter((login) => login !== payload.pull_request.user.login);
} else {
pullRequestData.reviewers = [payload.requested_reviewer.login];
}
} catch (err) {
console.log("Unable to construct pullRequestData for payload", payload);
core.setFailed(err.message);
return;
}
console.log("pullRequestData", pullRequestData);
try {
const requestUser = await getSlackNameByGithub(pullRequestData.requester);
const results = await Promise.allSettled(
pullRequestData.reviewers.map(async (reviewer) => {
const reviewUser = await getSlackIdByGithub(reviewer);
const res = await sendSlackMessage(
slackMessageTemplateNewPR(requestUser, pullRequestData),
reviewUser
);
console.log(
"Message sent about",
requestUser,
"to",
reviewUser,
res.ts
);
})
);
const failureReasons = results
.filter((result) => result.status === "rejected")
.map((result) => result.reason);
if (failureReasons.length) {
console.log("Failed to message", failureReasons.length, "users");
throw failureReasons[0];
}
} catch (err) {
core.setFailed(err.message);
}
};
const handleReviewSubmitted = async function (payload) {
let pullRequestData;
try {
pullRequestData = {
title: payload.pull_request.title,
url: payload.pull_request.html_url,
org: payload.organization.login,
pr_owner: payload.pull_request.user.login,
reviewer: payload.review.user.login,
state: payload.review.state.toLowerCase(),
};
} catch (err) {
console.log(
"Unable to construct pullRequestData for payload with PR:",
payload.pull_request,
"and review",
payload.review
);
core.setFailed(err.message);
return;
}
console.log("pullRequestData", pullRequestData);
if (pullRequestData.pr_owner === pullRequestData.reviewer) {
return;
}
try {
const reviewUser = await getSlackNameByGithub(pullRequestData.reviewer);
const ownerUser = await getSlackIdByGithub(pullRequestData.pr_owner);
const res = await sendSlackMessage(
slackMessageTemplateReviewedPR(reviewUser, pullRequestData),
ownerUser
);
console.log(
"Message sent about review by",
reviewUser,
"to",
ownerUser,
res.ts
);
} catch (err) {
core.setFailed(err.message);
}
};
const main = function (context) {
console.log("Event received", context.eventName, context.payload.action);
if (
context.eventName === "pull_request" &&
context.payload.action === "review_requested"
) {
handleReviewRequested(context.payload);
} else if (
context.eventName === "pull_request_review" &&
context.payload.action === "submitted"
) {
handleReviewSubmitted(context.payload);
}
};
main(github.context);