-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
177 lines (156 loc) · 5.26 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
import core from '@actions/core';
import github from '@actions/github';
import fetch from 'node-fetch';
/**
* Stylizes a markdown body into an appropriate embed message style.
* Remove Carriage Return character to reduce size
* Remove HTML comments (commonly added by 'Generate release notes' button)
* Better URL linking for common Github links: PRs, Issues, Compare
* Redundant whitespace and newlines removed, keeping at max 2 to provide space between paragraphs
* Trim leading/trailing whitespace
* If reduce_headings:
* H3s converted to bold and underlined
* H2s converted to bold
* @param {string} description
*/
const formatDescription = (description) => {
let edit = description
.replace(/\r/g, '')
.replace(/<!--.*?-->/gs, '')
.replace(
new RegExp(
"https://github.com/(.+)/(.+)/(issues|pull|commit|compare)/(\\S+)",
"g"
),
(match, user, repo, type, id) => {
return `[${getTypePrefix(type) + id}](${match})`
}
)
.replace(/\n\s*\n/g, (ws) => {
const nlCount = (ws.match(/\n/g) || []).length
return nlCount >= 2 ? '\n\n' : '\n'
})
.trim()
if (core.getBooleanInput('reduce_headings')) {
edit = edit
.replace(/^###\s+(.+)$/gm, '**__$1__**')
.replace(/^##\s+(.+)$/gm, '**$1**')
}
return edit
}
/**
* Get a prefix to use for Github link display
* @param {'issues' | 'pull' | 'commit' | 'compare'} type
*/
function getTypePrefix (type) {
switch (type) {
case 'issues':
return 'Issue #'
case 'pull':
return 'PR #'
case 'commit':
return 'Commit #'
case 'compare':
return ''
default:
return '#'
}
}
/**
* Gets the max description length if set to a valid number,
* otherwise the default of 4096
*/
function getMaxDescription () {
try {
const max = core.getInput('max_description')
if (typeof max === 'string' && max.length) {
// 4096 is max for Embed Description
// https://discord.com/developers/docs/resources/channel#embed-object-embed-limits
return Math.min(parseInt(max, 10), 4096)
}
} catch (err) {
core.warning(`max_description not a valid number: ${err}`)
}
return 4096
}
/**
* Get the context of the action, returns a GitHub Release payload.
*/
function getContext () {
const payload = github.context.payload;
return {
body: payload.release.body,
name: payload.release.name,
html_url: payload.release.html_url
}
}
/**
*
* @param {string} str
* @param {number} maxLength
* @param {string} [url]
* @param {boolean} [clipAtLine=false]
*/
function limit(str, maxLength, url, clipAtLine) {
clipAtLine ??= false
if (str.length <= maxLength)
return str
let replacement = clipAtLine ? '\n…' : '…'
if (url) {
replacement = `${clipAtLine ? '\n' : ''}([…](${url}))`
}
maxLength = maxLength - replacement.length
str = str.substring(0, maxLength)
const lastNewline = str.search(new RegExp(`[^${clipAtLine ? '\n' : '\s'}]*$`))
if (lastNewline > -1) {
str = str.substring(0, lastNewline)
}
return str + replacement
}
/**
* Handles the action.
* Get inputs, creates a stylized response webhook, and sends it to the channel.
*/
async function run () {
const webhookUrl = core.getInput('webhook_url');
const color = core.getInput('color');
const username = core.getInput('username');
const avatarUrl = core.getInput('avatar_url');
const content = core.getInput('content');
const footerTitle = core.getInput('footer_title');
const footerIconUrl = core.getInput('footer_icon_url');
const footerTimestamp = core.getInput('footer_timestamp');
if (!webhookUrl) return core.setFailed('webhook_url not set. Please set it.');
const {body, html_url, name} = getContext();
const description = formatDescription(body);
let embedMsg = {
title: limit(name, 256),
url: html_url,
color: color,
description: description,
footer: {}
}
if (footerTitle != '') embedMsg.footer.text = limit(footerTitle, 2048);
if (footerIconUrl != '') embedMsg.footer.icon_url = footerIconUrl;
if (footerTimestamp == 'true') embedMsg.timestamp = new Date().toISOString();
let embedSize = embedMsg.title.length + (embedMsg.footer?.text?.length ?? 0)
embedMsg.description = limit(embedMsg.description, Math.min(getMaxDescription(), 6000 - embedSize), embedMsg.url, true)
let requestBody = {
embeds: [embedMsg]
}
if (username != '') requestBody.username = username;
if (avatarUrl != '') requestBody.avatar_url = avatarUrl;
if (content != '') requestBody.content = content;
const url = `${webhookUrl}?wait=true`;
fetch(url, {
method: 'post',
body: JSON.stringify(requestBody),
headers: { 'Content-Type': 'application/json' }
})
.then(res => res.json())
.then(data => core.info(JSON.stringify(data)))
.catch(err => core.info(err))
}
run()
.then(() => {core.info('Action completed successfully')})
.catch(err => {core.setFailed(err.message)})