Skip to content

Commit c063ef6

Browse files
authored
chore(actions): Add Discord message release hook (#29)
* Copy over discord-message action * Update Node LTS in actions * Add discord-message hook * Remove node-fetch
1 parent a224701 commit c063ef6

File tree

6 files changed

+267
-2
lines changed

6 files changed

+267
-2
lines changed
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import * as core from '@actions/core';
2+
import * as github from '@actions/github';
3+
4+
const GITHUB_TOKEN = process.env.GITHUB_TOKEN;
5+
const WEBHOOK_URL = process.env.DISCORD_WEBHOOK_URL;
6+
7+
const octokit = github.getOctokit(GITHUB_TOKEN);
8+
9+
const 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[ ]*)+/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) => {
23+
return `[${text}](<${url}>)`;
24+
})
25+
.replace(repeatedNewlineRe, '\n')
26+
.trim();
27+
};
28+
29+
async function getReleaseBody(name, version) {
30+
const tag = `${name}@${version}`;
31+
const result = await octokit.rest.repos.getReleaseByTag({
32+
owner: 'urql-graphql',
33+
repo: 'urql',
34+
tag,
35+
});
36+
37+
const release = result.status === 200 ? result.data : undefined;
38+
if (!release || !release.body) return;
39+
40+
const title = `:package: [${tag}](<${release.html_url}>)`;
41+
const body = formatBody(release.body);
42+
if (!body) return;
43+
44+
return `${title}\n${body}`;
45+
}
46+
47+
async function main() {
48+
const inputPackages = core.getInput('publishedPackages');
49+
let packages;
50+
51+
try {
52+
packages = JSON.parse(inputPackages);
53+
} catch (e) {
54+
console.error('invalid JSON in publishedPackages input.');
55+
return;
56+
}
57+
58+
// Get releases
59+
const releasePromises = packages.map((entry) => {
60+
return getReleaseBody(entry.name, entry.version);
61+
});
62+
63+
const content = (await Promise.allSettled(releasePromises))
64+
.map((x) => x.status === 'fulfilled' && x.value)
65+
.filter(Boolean)
66+
.join('\n\n');
67+
68+
// Send message through a discord webhook or bot
69+
const response = fetch(WEBHOOK_URL, {
70+
method: 'POST',
71+
headers: {
72+
'Content-Type': 'application/json',
73+
},
74+
body: JSON.stringify({ content }),
75+
});
76+
77+
if (!response.ok) {
78+
console.log('Something went wrong while sending the discord webhook.');
79+
return;
80+
}
81+
82+
return response;
83+
}
84+
85+
main().then().catch(console.error);
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
name: 'Send a discord message'
2+
description: 'Send a discord message as a result of a gql.tada publish.'
3+
inputs:
4+
publishedPackages:
5+
description: >
6+
A JSON array to present the published packages. The format is `[{"name": "@xx/xx", "version": "1.2.0"}, {"name": "@xx/xy", "version": "0.8.9"}]`
7+
runs:
8+
using: 'node20'
9+
main: 'action.mjs'

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ jobs:
2020
- name: Setup Node
2121
uses: actions/setup-node@v1
2222
with:
23-
node-version: 18
23+
node-version: 20
2424

2525
- name: Setup pnpm
2626
uses: pnpm/[email protected]

.github/workflows/release.yml

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ jobs:
2525
- name: Setup Node
2626
uses: actions/setup-node@v3
2727
with:
28-
node-version: 18
28+
node-version: 20
2929

3030
- name: Setup pnpm
3131
uses: pnpm/[email protected]
@@ -59,6 +59,17 @@ jobs:
5959
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
6060
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
6161

62+
- name: Notify discord
63+
id: discord-msg
64+
if: steps.changesets.outputs.published == 'true'
65+
uses: ./.github/actions/discord-message
66+
with:
67+
publishedPackages: ${{ steps.changesets.outputs.publishedPackages }}
68+
env:
69+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
70+
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
71+
DISCORD_WEBHOOK_URL: ${{ secrets.DISCORD_WEBHOOK_URL }}
72+
6273
- name: Publish Prerelease
6374
if: steps.changesets.outputs.published != 'true'
6475
env:

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@
7676
]
7777
},
7878
"devDependencies": {
79+
"@actions/core": "^1.10.0",
80+
"@actions/github": "^5.1.1",
7981
"@babel/plugin-transform-block-scoping": "^7.23.4",
8082
"@babel/plugin-transform-typescript": "^7.23.6",
8183
"@changesets/cli": "^2.27.1",

pnpm-lock.yaml

Lines changed: 158 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)