-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
93 lines (80 loc) · 1.8 KB
/
main.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
const core = require('@actions/core')
const request = require('request');
process.on('unhandledRejection', handleError);
main().catch(handleError);
function handleError(err) {
console.error(err)
core.setFailed(err.message)
}
function post(url, body) {
request.post(url, {json: body}, (error, response, body) => {
if (error) {
core.error('statusCode:', response && response.statusCode);
core.error('body:', body);
core.setFailed(error);
}
});
}
function sectionBlock(text) {
return {
"type": "section",
"text": {
"type": "mrkdwn",
"text": text,
}
};
}
function contextBlock(text) {
return {
"type": "context",
"elements": [
{
"type": "mrkdwn",
"text": text
}
]
};
}
function buttonBlock(text, url) {
return {
"type": "actions",
"elements": [
{
"type": "button",
"text": {
"type": "plain_text",
"text": text,
"emoji": true
},
"url": url
}
]
};
}
function build(summary, section, context, buttonText, buttonUrl) {
blocks = []
if (section) {
blocks.push(sectionBlock(section));
}
if (context) {
blocks.push(contextBlock(context));
}
if (buttonText && buttonUrl) {
blocks.push(buttonBlock(buttonText, buttonUrl));
}
return {
"text": summary,
"blocks": blocks
}
}
async function main() {
const slackUrl = core.getInput('slack-webhook-url');
const summary = core.getInput('summary');
const message = core.getInput('message');
const context = core.getInput('context');
const buttonUrl = core.getInput('button-url');
const buttonText = core.getInput('button-text');
body = build(summary, message, context, buttonText, buttonUrl);
console.log("Posting to slack");
post(slackUrl, body);
}