This repository has been archived by the owner on Apr 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.ts
80 lines (62 loc) · 2.16 KB
/
index.ts
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
import * as core from "@actions/core";
import * as github from "@actions/github";
import axios from "axios";
import { promises as fs } from "fs";
const main = async () => {
try {
const action = github.context.payload.action;
const payload_pull_request = github.context.payload.pull_request;
const reviewers = payload_pull_request?.requested_reviewers;
const title = payload_pull_request?.title;
const html_url = payload_pull_request?.html_url;
const number = payload_pull_request?.number;
const url = process.env.PR_MESSAGE_SLACK_WEBHOOK_URL; // https://hooks.slack.com/...
if (!url) {
throw new Error("PR_MESSAGE_SLACK_WEBHOOK_URL is not set.");
}
if (!html_url) {
throw new Error("Could not retrieve PR URL.");
}
if (action !== "review_requested") {
console.log("'types' only supports 'review_requested'.");
return;
}
if (!Array.isArray(reviewers) || reviewers.length === 0) {
console.log("Could not retrieve 'reviewers'.");
return;
}
let github_slack_id_map: { [github_username: string]: unknown } = {};
try {
github_slack_id_map = JSON.parse(
await fs.readFile(".github/slack-id.json", "utf8")
);
} catch (e) {
// TODO: Add error handling other than ENOENT.
}
let text = "";
reviewers.forEach((reviewer) => {
const github_username = reviewer.login;
if (typeof github_username !== "string") {
return;
}
let slack_id = github_slack_id_map[github_username];
if (typeof slack_id !== "string") {
// If Slack member ID is not specified, use GitHub username instead.
slack_id = github_username;
}
text = text.concat(`<@${slack_id}> `);
});
const number_with_hash = number !== undefined ? `#${number}` : "";
if (typeof title === "string") {
text = text.concat(
`\n*Review requested: * <${html_url}|${title}${number_with_hash}>`
);
} else {
text = text.concat(`\n*Review requested: * ${html_url}`);
}
axios.post(url, { text });
} catch (e) {
core.setFailed(e instanceof Error ? e.message : JSON.stringify(e));
}
};
main();