Mirror: The spec-compliant minimum of client-side GraphQL.
1import * as core from '@actions/core';
2import * as github from '@actions/github';
3
4const GITHUB_TOKEN = process.env.GITHUB_TOKEN;
5const WEBHOOK_URL = process.env.DISCORD_WEBHOOK_URL;
6
7const octokit = github.getOctokit(GITHUB_TOKEN);
8
9const formatBody = (input) => {
10 const titleRe = /(?:^|\n)#+[^\n]+/g;
11 const updatedDepsRe = /\n-\s*Updated dependencies[\s\S]+\n(\n\s+-[\s\S]+)*/gi;
12 const markdownLinkRe = /\[([^\]]+)\]\(([^\)]+)\)/g;
13 const creditRe = new RegExp(`Submitted by (?:undefined|${markdownLinkRe.source})`, 'ig');
14 const repeatedNewlineRe = /(?:\n[ ]*)*(\n[ ]*)/g;
15 return input
16 .replace(titleRe, '')
17 .replace(updatedDepsRe, '')
18 .replace(creditRe, (_match, text, url) => {
19 if (!text || /@kitten|@JoviDeCroock/i.test(text)) return '';
20 return `Submitted by [${text}](${url})`;
21 })
22 .replace(markdownLinkRe, (_match, text, url) => `[${text}](<${url}>)`)
23 .replace(repeatedNewlineRe, (_match, text) => text ? ` ${text}` : '\n')
24 .trim();
25};
26
27async function getReleaseBody(name, version) {
28 const tag = `${name}@${version}`;
29 const [owner, repo] = process.env.GITHUB_REPOSITORY.split('/');
30 const result = await octokit.rest.repos.getReleaseByTag({ owner, repo, tag });
31
32 const release = result.status === 200 ? result.data : undefined;
33 if (!release || !release.body) return;
34
35 const title = `:package: [${tag}](<${release.html_url}>)`;
36 const body = formatBody(release.body);
37 if (!body) return;
38
39 return `${title}\n${body}`;
40}
41
42async function main() {
43 const inputPackages = core.getInput('publishedPackages');
44 let packages;
45
46 try {
47 packages = JSON.parse(inputPackages);
48 } catch (e) {
49 console.error('invalid JSON in publishedPackages input.');
50 return;
51 }
52
53 // Get releases
54 const releasePromises = packages.map((entry) => {
55 return getReleaseBody(entry.name, entry.version);
56 });
57
58 const content = (await Promise.allSettled(releasePromises))
59 .map((x) => x.status === 'fulfilled' && x.value)
60 .filter(Boolean)
61 .join('\n\n');
62
63 // Send message through a discord webhook or bot
64 const response = await fetch(WEBHOOK_URL, {
65 method: 'POST',
66 headers: {
67 'Content-Type': 'application/json',
68 },
69 body: JSON.stringify({ content }),
70 });
71
72 if (!response.ok) {
73 console.error('Something went wrong while sending the discord webhook.', response.status);
74 console.error(await response.text());
75 }
76}
77
78main().then().catch(console.error);