-
Notifications
You must be signed in to change notification settings - Fork 57
/
main.ts
167 lines (139 loc) · 4.8 KB
/
main.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
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
import { getInput, setFailed } from "@actions/core"
import { context } from "@actions/github"
import fetch from "node-fetch"
type JobStatus = "success" | "failure" | "cancelled" | "warning" | "skipped"
const actionColor = (status: JobStatus) => {
if (status === "success") return "good"
if (status === "failure") return "danger"
if (status === "cancelled") return "danger"
if (status === "skipped") return "#4a4a4a"
return "warning"
}
const actionStatus = (status: JobStatus) => {
if (status === "success") return "passed"
if (status === "failure") return "failed"
if (status === "cancelled") return "cancelled"
if (status === "skipped") return "skipped"
return "passed with warnings"
}
const actionEmoji = (status: JobStatus) => {
if (status === "success") return getInput("icon_success")
if (status === "failure") return getInput("icon_failure")
if (status === "cancelled") return getInput("icon_cancelled")
if (status === "skipped") return getInput("icon_skipped")
return getInput("icon_warnings")
}
const makeMessage = (template: string, values: Record<string, string>) => {
for (const k of Object.keys(values)) {
template = template.replaceAll(`{${k}}`, values[k])
}
return template
}
const parseList = (value: string) => {
return value
.split(",")
.map((x) => x.trim())
.filter((x) => x.length > 0)
}
const parseStatusList = (value: string) => {
return parseList(value) as JobStatus[]
}
const getMentionUsers = (status: JobStatus) => {
const mentionUsers = getInput("mention_users")
const mentionUsersWhen = getInput("mention_users_when")
const users = parseList(mentionUsers)
if (!users.length || !mentionUsersWhen.includes(status)) return ""
return users.map((x) => `<@${x}>`).join(" ")
}
const getMentionGroups = (status: JobStatus) => {
const mentionGroups = getInput("mention_groups")
const mentionGroupsWhen = getInput("mention_groups_when")
const groups = parseList(mentionGroups)
if (!groups.length || !mentionGroupsWhen.includes(status)) return ""
return groups
.map((x) => {
// useful for mentions like @channel
// to mention a channel programmatically, we need to do <!channel>
return x[0] === "!" ? `<${x}>` : `<!subteam^${x}>`
})
.join(" ")
}
const getWorkflowUrl = async (repo: string, name: string) => {
if (process.env.NODE_ENV === "test") return "test-workflow-url"
const api = context.apiUrl
const token = getInput("token")
const url = `${api}/repos/${repo}/actions/workflows`
const rep = await fetch(url, {
headers: {
Accept: "application/vnd.github.v3+json",
Authorization: `token ${token}`,
},
})
if (rep.status === 200) {
const data = await rep.json()
const workflows = data.workflows
for (const workflow of workflows) {
if (workflow.name === name) {
return workflow.html_url
}
}
}
return ""
}
export const buildPayload = async () => {
const repo = `${context.repo.owner}/${context.repo.repo}`
const repoUrl = `${context.serverUrl}/${repo}`
const jobStatus = getInput("status") as JobStatus
const patterns: Record<string, string> = {
repo,
branch: context.ref,
branch_url: `${repoUrl}/tree/${context.ref.replace("refs/heads/", "")}`,
commit_sha: context.sha.substring(0, 7),
commit_url: `${repoUrl}/commit/${context.sha}`,
repo_url: `${repoUrl}`,
run_url: `${repoUrl}/actions/runs/${context.runId}`,
job: context.job,
workflow: context.workflow,
workflow_url: await getWorkflowUrl(repo, context.workflow),
color: actionColor(jobStatus),
status_message: actionStatus(jobStatus),
emoji: actionEmoji(jobStatus),
}
const title = makeMessage(getInput("notification_title"), patterns)
const message = makeMessage(getInput("message_format"), patterns)
const footer = makeMessage(getInput("footer"), patterns)
const text = [message, getMentionUsers(jobStatus), getMentionGroups(jobStatus)]
.filter((x) => x.length > 0)
.join("\n")
const attachment = {
text: text,
fallback: title,
pretext: title,
color: patterns["color"],
mrkdwn_in: ["text"],
footer: footer,
}
const payload = { attachments: [attachment] }
return JSON.stringify(payload)
}
const notifySlack = async (payload: string) => {
const webhookUrl = process.env.SLACK_WEBHOOK_URL
if (!webhookUrl) throw new Error("No SLACK_WEBHOOK_URL provided")
fetch(webhookUrl, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: payload,
})
}
const run = async () => {
try {
const notifyWhen = parseStatusList(getInput("notify_when"))
const jobStatus = getInput("status") as JobStatus
if (!notifyWhen.includes(jobStatus)) return
const payload = await buildPayload()
await notifySlack(payload)
} catch (e) {
if (e instanceof Error) setFailed(e.message)
}
}
run()