forked from SethCohen/github-releases-to-discord
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
340 lines (299 loc) · 11.9 KB
/
Copy pathindex.js
File metadata and controls
340 lines (299 loc) · 11.9 KB
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
import * as core from '@actions/core';
import * as github from '@actions/github';
import { fileURLToPath } from 'node:url';
/**
* Removes carriage return characters.
* @param {string} text The input text.
* @returns {string} The text without carriage return characters.
*/
const removeCarriageReturn = (text) => text.replace(/\r/g, '');
/**
* Removes HTML comments.
* @param {string} text The input text.
* @returns {string} The text without HTML comments.
*/
const removeHTMLComments = (text) => text.replace(/<!--.*?-->/gs, '');
/**
* Reduces redundant newlines and spaces.
* Keeps a maximum of 2 newlines to provide spacing between paragraphs.
* @param {string} text The input text.
* @returns {string} The text with reduced newlines.
*/
const reduceNewlines = (text) => text.replace(/\n\s*\n/g, (ws) => {
const nlCount = (ws.match(/\n/g) || []).length;
return nlCount >= 2 ? '\n\n' : '\n';
});
/**
* Converts @mentions to GitHub profile links for valid GitHub usernames.
* @param {string} text The input text.
* @returns {string} The text with valid @mentions converted to links.
*/
const convertMentionsToLinks = (text) => text.replace(
/(?<![/@\w])@((?!-)(?!.*?--)[a-zA-Z0-9](?:-?[a-zA-Z0-9]){0,37})(?![.\w/-])(?!.*\])/g,
(match, name) => `[@${name}](https://github.com/${name})`
);
/**
* Removes any GitHub PR, commit, or issue links from the text, including markdown links.
* @param {string} text The input text.
* @returns {string} The text without GitHub PR and commit links.
*/
const removeGithubReferenceLinks = (text) => text
// Remove markdown links to PRs, commits, and issues
.replace(/\[[^\]]*\]\(https:\/\/github\.com\/[^(\s)]+\/pull\/\d+\)/g, '')
.replace(/\[[^\]]*\]\(https:\/\/github\.com\/[^(\s)]+\/commit\/\w+\)/g, '')
.replace(/\[[^\]]*\]\(https:\/\/github\.com\/[^(\s)]+\/issues\/\d+\)/g, '')
// Remove bare PR, commit, and issue URLs
.replace(/https:\/\/github\.com\/[^(\s)]+\/pull\/\d+/g, '')
.replace(/https:\/\/github\.com\/[^(\s)]+\/commit\/\w+/g, '')
.replace(/https:\/\/github\.com\/[^(\s)]+\/issues\/\d+/g, '')
// Remove empty parentheses left behind
.replace(/\(\s*\)/g, '');
/**
* Reduces headings to a smaller format if 'reduce_headings' is enabled.
* Converts H3 to bold+underline, H2 to bold.
* @param {string} text The input text.
* @returns {string} The text with reduced heading sizes.
*/
const reduceHeadings = (text) => text
.split('\n')
.map((line) => {
const h3 = line.match(/^\s*###\s+(.+?)\s*#*\s*$/);
if (h3) {
return `**__${h3[1].trim()}__**`;
}
const h2 = line.match(/^\s*##\s+(.+?)\s*#*\s*$/);
if (h2) {
return `**${h2[1].trim()}**`;
}
return line;
})
.join('\n');
/**
* Converts PR, issue, and changelog links to markdown format, ignoring existing markdown links.
* - PR links: `https://github.com/OWNER/REPO/pull/1` -> `[PR #1](https://github.com/OWNER/REPO/pull/1)`
* - Issue links: `https://github.com/OWNER/REPO/issues/1` -> `[Issue #30](https://github.com/OWNER/REPO/issues/1)`
* - Changelog links: `https://github.com/OWNER/REPO/compare/v1.0.0...v1.1.0` -> `[v1.0.0...v1.1.0](https://github.com/OWNER/REPO/compare/v1.0.0...v1.1.0)`
* @param {string} text The input text.
* @returns {string} The text with links converted to markdown format.
*/
const convertLinksToMarkdown = (text) => {
// Extract existing markdown links and replace them with placeholders
const markdownLinks = [];
const textWithoutMarkdownLinks = text.replace(/\[.*?\]\(.*?\)/g, (link) => {
markdownLinks.push(link);
return `__MARKDOWN_LINK_PLACEHOLDER_${markdownLinks.length - 1}__`;
});
// Convert standalone PR, issue, and changelog URLs to markdown format
let processedText = textWithoutMarkdownLinks
.replace(/https:\/\/github\.com\/([\w-]+)\/([\w-]+)\/pull\/(\d+)/g, (match, owner, repo, prNumber) => `[PR #${prNumber}](${match})`)
.replace(/https:\/\/github\.com\/([\w-]+)\/([\w-]+)\/issues\/(\d+)/g, (match, owner, repo, issueNumber) => `[Issue #${issueNumber}](${match})`)
.replace(/https:\/\/github\.com\/([\w-]+)\/([\w-]+)\/compare\/([v\w.-]+)\.\.\.([v\w.-]+)/g, (match, owner, repo, fromVersion, toVersion) => `[${fromVersion}...${toVersion}](${match})`);
// Reinsert the original markdown links
return processedText.replace(/__MARKDOWN_LINK_PLACEHOLDER_(\d+)__/g, (match, index) => markdownLinks[parseInt(index, 10)]);
};
/**
* Stylizes a markdown body into an appropriate embed message style.
* @param {string} description The description to format.
* @returns {string} The formatted description.
*/
const formatDescription = (description) => {
let edit = removeCarriageReturn(description ?? '');
edit = removeHTMLComments(edit);
edit = reduceNewlines(edit);
if (core.getBooleanInput('remove_github_reference_links')) {
edit = removeGithubReferenceLinks(edit);
}
edit = convertMentionsToLinks(edit);
edit = convertLinksToMarkdown(edit);
edit = edit.trim();
if (core.getBooleanInput('reduce_headings')) {
edit = reduceHeadings(edit);
}
return edit;
};
/**
* Gets the max description length, defaulting to 4096 if not set or invalid.
* @returns {number} The max description length.
*/
const getMaxDescription = () => {
try {
const max = core.getInput('max_description');
if (max && !isNaN(max)) {
return Math.min(parseInt(max, 10), 4096);
}
} catch (err) {
core.warning(`Invalid max_description: ${err}`);
}
return 4096;
};
/**
* Get the context of the action, returning a GitHub Release payload.
* @returns {object} The context with release details.
*/
const getContext = () => {
return resolveReleaseContext(github.context.payload.release, {
body: core.getInput('release_body'),
name: core.getInput('release_name'),
html_url: core.getInput('release_html_url')
});
};
/**
* Resolves release data from either a GitHub release event or manual inputs.
* @param {object|null|undefined} release The release payload from the event.
* @param {object} manualInputs Manual fallback inputs.
* @returns {object} The resolved release context.
*/
const resolveReleaseContext = (release, manualInputs) => {
if (release) {
return {
body: release.body || '',
name: release.name || '',
html_url: release.html_url || ''
};
}
return {
body: manualInputs.body || '',
name: manualInputs.name || '',
html_url: manualInputs.html_url || ''
};
};
/**
* Limits the string to a maximum length, optionally adding a URL or clipping at a newline.
* @param {string} str The string to limit.
* @param {number} maxLength The maximum allowed length.
* @param {string} [url] Optional URL for linking the truncated text.
* @param {boolean} [clipAtLine=false] Whether to clip at the nearest newline.
* @returns {string} The limited string.
*/
const limitString = (str, maxLength, url, clipAtLine = false) => {
if (str.length <= maxLength) return str;
const replacement = url
? `${clipAtLine ? '\n' : ''}([…](${url}))`
: (clipAtLine ? '\n…' : '…');
maxLength -= replacement.length;
str = str.substring(0, maxLength);
const lastBreak = str.search(clipAtLine ? /[^\n]*$/ : /\S*$/);
if (lastBreak > 0) {
str = str.substring(0, lastBreak);
}
return str + replacement;
};
/**
* Builds the embed message for the Discord webhook.
* @param {string} name The title or name of the release.
* @param {string} html_url The URL of the release.
* @param {string} description The formatted description of the release.
* @returns {object} The embed message to send in the webhook.
*/
const buildEmbedMessage = (name, html_url, description) => {
const embedMsg = {
title: limitString(name, 256),
color: core.getInput('color'),
description: limitString(description, Math.min(getMaxDescription(), 6000 - name.length)),
footer: {}
};
if (html_url) {
embedMsg.url = html_url;
}
if (core.getInput('custom_html_url')) {
embedMsg.url = core.getInput('custom_html_url');
}
if (core.getInput('footer_title')) {
embedMsg.footer.text = limitString(core.getInput('footer_title'), 2048);
}
if (core.getInput('footer_icon_url')) {
embedMsg.footer.icon_url = core.getInput('footer_icon_url');
}
if (core.getInput('footer_timestamp') === 'true') {
embedMsg.timestamp = new Date().toISOString();
}
return embedMsg;
};
/**
* Sends the webhook request to Discord, handling rate limits (429) with retries.
* @param {string} webhookUrl The URL of the Discord webhook.
* @param {object} requestBody The payload to send in the webhook.
* @param {number} [maxRetries=3] Maximum number of retries on rate limit.
*/
const sendWebhook = async (webhookUrl, requestBody, maxRetries = 3) => {
let attempt = 0;
while (attempt <= maxRetries) {
try {
const response = await fetch(`${webhookUrl}?wait=true`, {
method: 'POST',
body: JSON.stringify(requestBody),
headers: { 'Content-Type': 'application/json' }
});
if (response.status === 429) {
// Rate limited, get retry-after
const retryAfter = parseInt(response.headers.get('retry-after') || '1', 10);
core.warning(`Rate limited by Discord. Retrying after ${retryAfter} seconds (attempt ${attempt + 1}/${maxRetries})`);
await new Promise(res => setTimeout(res, retryAfter * 1000));
attempt++;
continue;
}
const data = await response.json();
if (!response.ok) {
core.setFailed(`Discord webhook error: ${JSON.stringify(data)}`);
} else {
core.info(JSON.stringify(data));
}
break;
} catch (err) {
core.setFailed(err.message);
break;
}
}
if (attempt > maxRetries) {
core.setFailed('Exceeded maximum Discord webhook retry attempts due to rate limiting.');
}
};
/**
* Builds the request body for the Discord webhook.
* @param {object} embedMsg The embed message to include in the request body.
* @returns {object} The request body for the webhook.
*/
const buildRequestBody = (embedMsg) => {
return {
embeds: [embedMsg],
...(core.getInput('username') && { username: core.getInput('username') }),
...(core.getInput('avatar_url') && { avatar_url: core.getInput('avatar_url') }),
...(core.getInput('content') && { content: core.getInput('content') })
};
};
/**
* Main function to handle the action: get inputs, format the message, and send the webhook.
*/
const run = async () => {
const webhookUrl = core.getInput('webhook_url');
if (!webhookUrl) return core.setFailed('webhook_url not set.');
const { body, html_url, name } = getContext();
if (!name) {
return core.setFailed('No GitHub release payload found. When using workflow_dispatch, pass release_name to the action.');
}
const description = formatDescription(body);
const embedMsg = buildEmbedMessage(name, html_url, description);
const requestBody = buildRequestBody(embedMsg);
await sendWebhook(webhookUrl, requestBody);
};
const isMainModule = process.argv[1] && fileURLToPath(import.meta.url) === process.argv[1];
if (isMainModule) {
run()
.then(() => core.info('Action completed successfully'))
.catch(err => core.setFailed(err.message));
}
// Export utility functions for testing
export {
removeCarriageReturn,
removeHTMLComments,
reduceNewlines,
convertMentionsToLinks,
removeGithubReferenceLinks,
reduceHeadings,
convertLinksToMarkdown,
limitString,
formatDescription,
resolveReleaseContext,
getContext,
run
};