-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
89 lines (79 loc) · 2.07 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
import core from '@actions/core';
import axios from 'axios';
const webhookUrl = core.getInput('webhook', {required: true});
const status = core.getInput('status');
// 1. message text
let text = core.getInput('text');
if (text === '') {
if (status === 'success') {
text = ':rocket: Action ran successfully.';
} else if (status === 'failure') {
text = ':x: Action failed.';
} else if (status === 'cancelled') {
text = ':heavy_multiplication_x: Step was cancelled.';
} else {
core.warning('cannot create message: got an invalid status string');
}
}
// 2. attachments color
let color = core.getInput('color');
if (color === '') {
if (status === 'success') {
color = 'good';
} else if (status === 'failure') {
color = 'danger';
} else if (status === 'cancelled') {
color = '#808080';
} else {
core.warning('assigning default color: got an invalid status string');
color = 'good';
}
}
// 3. build message
const actor = process.env.GITHUB_ACTOR;
const server = process.env.GITHUB_SERVER_URL;
const repo = process.env.GITHUB_REPOSITORY;
const runId = process.env.GITHUB_RUN_ID;
const workflow = process.env.GITHUB_WORKFLOW;
const commitSha = process.env.GITHUB_SHA;
const commitShaShort = commitSha.slice(0, 7);
const fields = [
{
title: 'Ref',
value: process.env.GITHUB_REF,
short: true,
},
{
title: 'Event',
value: process.env.GITHUB_EVENT_NAME,
short: true,
},
{
title: 'Actions URL',
value: `<${server}/${repo}/actions/runs/${runId}|${workflow}>`,
short: true,
},
{
title: 'Commit',
value: `<${server}/${repo}/commit/${commitSha}|${commitShaShort}>`,
short: true,
},
];
const customFields = JSON.parse(core.getInput('custom-fields'));
const message = {
username: core.getInput('username'),
icon_url: core.getInput('icon-url'),
icon_emoji: core.getInput('icon-emoji'),
text: text,
attachments: [{
color: color,
author_name: actor,
author_link: `${server}/${actor}`,
author_icon: `${server}/${actor}.png?size=32`,
fields: fields.concat(customFields),
}],
};
// 4. send message
(async () => {
await axios.post(webhookUrl, message);
})();