Skip to content

Commit

Permalink
chore(actions): Add Discord message release hook (#29)
Browse files Browse the repository at this point in the history
* Copy over discord-message action

* Update Node LTS in actions

* Add discord-message hook

* Remove node-fetch
  • Loading branch information
kitten authored Apr 18, 2024
1 parent a224701 commit c063ef6
Show file tree
Hide file tree
Showing 6 changed files with 267 additions and 2 deletions.
85 changes: 85 additions & 0 deletions .github/actions/discord-message/action.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import * as core from '@actions/core';
import * as github from '@actions/github';

const GITHUB_TOKEN = process.env.GITHUB_TOKEN;
const WEBHOOK_URL = process.env.DISCORD_WEBHOOK_URL;

const octokit = github.getOctokit(GITHUB_TOKEN);

const formatBody = (input) => {
const titleRe = /(?:^|\n)#+[^\n]+/g;
const updatedDepsRe = /\n-\s*Updated dependencies[\s\S]+\n(\n\s+-[\s\S]+)*/gi;
const markdownLinkRe = /\[([^\]]+)\]\(([^\)]+)\)/g;
const creditRe = new RegExp(`Submitted by (?:undefined|${markdownLinkRe.source})`, 'ig');
const repeatedNewlineRe = /(\n[ ]*)+/g;
return input
.replace(titleRe, '')
.replace(updatedDepsRe, '')
.replace(creditRe, (_match, text, url) => {
if (!text || /@kitten|@JoviDeCroock/i.test(text)) return '';
return `Submitted by [${text}](${url})`;
})
.replace(markdownLinkRe, (_match, text, url) => {
return `[${text}](<${url}>)`;
})
.replace(repeatedNewlineRe, '\n')
.trim();
};

async function getReleaseBody(name, version) {
const tag = `${name}@${version}`;
const result = await octokit.rest.repos.getReleaseByTag({
owner: 'urql-graphql',
repo: 'urql',
tag,
});

const release = result.status === 200 ? result.data : undefined;
if (!release || !release.body) return;

const title = `:package: [${tag}](<${release.html_url}>)`;
const body = formatBody(release.body);
if (!body) return;

return `${title}\n${body}`;
}

async function main() {
const inputPackages = core.getInput('publishedPackages');
let packages;

try {
packages = JSON.parse(inputPackages);
} catch (e) {
console.error('invalid JSON in publishedPackages input.');
return;
}

// Get releases
const releasePromises = packages.map((entry) => {
return getReleaseBody(entry.name, entry.version);
});

const content = (await Promise.allSettled(releasePromises))
.map((x) => x.status === 'fulfilled' && x.value)
.filter(Boolean)
.join('\n\n');

// Send message through a discord webhook or bot
const response = fetch(WEBHOOK_URL, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ content }),
});

if (!response.ok) {
console.log('Something went wrong while sending the discord webhook.');
return;
}

return response;
}

main().then().catch(console.error);
9 changes: 9 additions & 0 deletions .github/actions/discord-message/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
name: 'Send a discord message'
description: 'Send a discord message as a result of a gql.tada publish.'
inputs:
publishedPackages:
description: >
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"}]`
runs:
using: 'node20'
main: 'action.mjs'
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
- name: Setup Node
uses: actions/setup-node@v1
with:
node-version: 18
node-version: 20

- name: Setup pnpm
uses: pnpm/[email protected]
Expand Down
13 changes: 12 additions & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
- name: Setup Node
uses: actions/setup-node@v3
with:
node-version: 18
node-version: 20

- name: Setup pnpm
uses: pnpm/[email protected]
Expand Down Expand Up @@ -59,6 +59,17 @@ jobs:
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Notify discord
id: discord-msg
if: steps.changesets.outputs.published == 'true'
uses: ./.github/actions/discord-message
with:
publishedPackages: ${{ steps.changesets.outputs.publishedPackages }}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
DISCORD_WEBHOOK_URL: ${{ secrets.DISCORD_WEBHOOK_URL }}

- name: Publish Prerelease
if: steps.changesets.outputs.published != 'true'
env:
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@
]
},
"devDependencies": {
"@actions/core": "^1.10.0",
"@actions/github": "^5.1.1",
"@babel/plugin-transform-block-scoping": "^7.23.4",
"@babel/plugin-transform-typescript": "^7.23.6",
"@changesets/cli": "^2.27.1",
Expand Down
158 changes: 158 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit c063ef6

Please sign in to comment.