-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
222 lines (194 loc) · 5.87 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
import * as core from "@actions/core";
import { context, getOctokit } from "@actions/github";
import { IncomingWebhook } from "@slack/webhook";
async function run() {
const token = core.getInput("token");
const slackWebhook = core.getInput("slack-webhook");
// Mask values when logging
core.setSecret(token);
core.setSecret(slackWebhook);
const repoOwner = context.repo.owner;
const repo = context.repo.repo;
const wfRunId = context.runId;
// Authenticate with GitHub
const octokit = getOctokit(token);
// Fetch workflow run data
const { data: wfRun } = await octokit.rest.actions.getWorkflowRun({
owner: repoOwner,
repo: repo,
run_id: wfRunId,
});
// Fetch jobs data for the same workflow run
const { data: jobsResponse } =
await octokit.rest.actions.listJobsForWorkflowRun({
owner: repoOwner,
repo: repo,
run_id: wfRunId,
});
// Extract the data we need for creating the Slack message
const commitAuthor = wfRun.actor.login;
const avatarUrl = wfRun.actor.avatar_url;
const repositoryUrl = wfRun.repository.html_url;
const project = wfRun.repository.full_name.split("/")[1];
const branchName = wfRun.head_branch;
const branchUrl = repositoryUrl + "/tree/" + branchName;
const commitSha = wfRun.head_sha;
const shortCommit = commitSha.slice(0, 7);
const commitUrl = repositoryUrl + "/commit/" + commitSha;
const commitMessage = wfRun.display_title;
const completedJobs = jobsResponse.jobs.filter(job => job.status == "completed")
const successfulJobs = completedJobs.filter(job => job.conclusion == "success")
const skippedJobs = completedJobs.filter(job => job.conclusion == "skipped")
const failedJobs = completedJobs.filter(job => job.conclusion == "failure")
const cancelledJobs = completedJobs.filter(job => job.conclusion == "cancelled")
const jobsWithLink = jobs => jobs.map(job => `<${job.html_url}|${job.name}>`).join(", ")
const sections = []
if (failedJobs.length > 0) {
sections.push({
type: "section",
text: {
type: "mrkdwn",
text: `*Failed:* ${jobsWithLink(failedJobs)}`,
},
})
}
if (cancelledJobs.length > 0) {
sections.push({
type: "section",
text: {
type: "mrkdwn",
text: `*Cancelled:* ${jobsWithLink(cancelledJobs)}`,
},
})
}
if (successfulJobs.length > 0 && failedJobs.length == 0 && cancelledJobs.length == 0) {
const skipped = skippedJobs.length > 0 ? ` (${skippedJobs.length} skipped)` : ""
sections.push({
type: "section",
text: {
type: "mrkdwn",
text: `All jobs succeeded${skipped} ${fetchEmoji(project)}`,
},
})
}
const status = failedJobs.length > 0 ? "failed" : cancelledJobs.length > 0 ? "cancelled" : successfulJobs.length > 0 ? "success" : undefined
// Everything was skipped; don't bother mentioning it.
if (status === undefined) return
// Create Slack message
// For debugging: https://app.slack.com/block-kit-builder/
const slackMessage = {
// We use attachments to keep the color bar on the left side
attachments: [
{
fallback: `${statusMoji[status]} ${project} (${branchName}) - ${commitAuthor}: ${commitMessage}`,
color: statusColor[status],
blocks: [
// Author
{
type: "context",
elements: [
{
type: "image",
image_url: avatarUrl,
alt_text: "Cute cat",
},
{
type: "mrkdwn",
text: "*" + commitAuthor + "*",
},
],
},
// Repository + branch + commit
{
type: "section",
text: {
type: "mrkdwn",
text: `*${project}* (<${branchUrl}|${branchName}>)\n${commitMessage} (<${commitUrl}|${shortCommit}>)`,
},
},
...sections,
],
},
],
};
// For debugging on Slack Block Kit Builder:
// console.log("Slack message: ", JSON.stringify(slackMessage));
// Send message to Slack
await new IncomingWebhook(slackWebhook).send(slackMessage);
}
const fetchEmoji = (project) => {
const xmasEmojis = [
":christmas_tree:",
":gift:",
":santa:",
":mrs_claus:",
":snowman:",
":snowflake:",
":bell:",
":star:",
":santa_bas:",
":santa_bouke:",
":santa_mattijs:",
":santa_rolf:",
":xmas_emiel:",
]
const today = new Date();
const xmasStart = new Date(today.getFullYear(), 11, 19);
const xmasEnd = new Date(today.getFullYear(), 11, 26);
if (today > xmasStart && today < xmasEnd) {
return xmasEmojis[Math.floor(Math.random() * xmasEmojis.length)]
}
const successEmojis = [
":partying_face:",
":partyparrot:",
":tada:",
":rocket:",
":trophy:",
":first_place_medal:",
":balloon:",
":confetti_ball:",
":man_dancing:",
":man_in_lotus_position:",
":woman_in_lotus_position:",
":beers:",
":clinking_glasses:",
":sunny:",
":crown:",
":female_superhero:",
":superhero:",
":dancer:",
":mario_luigi_dance:",
":champagne:",
]
const customer = project.split("-")[0]
switch (customer) {
case "dienst":
case "omron":
successEmojis.push(":happy_mattijs:");
successEmojis.push(":happy_mattijs:");
break;
case "pgs":
successEmojis.push(":happy_bouke:");
successEmojis.push(":happy_bouke:");
break;
case "taxology":
successEmojis.push(":happy_rolf:");
successEmojis.push(":happy_rolf:");
break;
}
return successEmojis[Math.floor(Math.random() * successEmojis.length)];
}
const statusMoji = {
failed: "❌",
cancelled: "🚫",
success: "✅",
}
const statusColor = {
failed: "#960018",
cancelled: "#A9A9A9",
success: "#5CB589",
}
run().catch((error) => {
core.setFailed(error.message);
console.error(error.stack);
});