-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
Showing
6 changed files
with
267 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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] | ||
|
@@ -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: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.