From 8b770e90feb14786d098e92b1df3a47f27070d55 Mon Sep 17 00:00:00 2001 From: Daniel Lando Date: Fri, 29 Sep 2023 10:06:38 +0200 Subject: [PATCH] chore: discord bot for releases note (#3306) --- .github/bot-scripts/post-release-notes.js | 37 +++++++++++++++++++++++ .github/workflows/discord-publish.yml | 23 ++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 .github/bot-scripts/post-release-notes.js create mode 100644 .github/workflows/discord-publish.yml diff --git a/.github/bot-scripts/post-release-notes.js b/.github/bot-scripts/post-release-notes.js new file mode 100644 index 00000000000..49a602c9d8d --- /dev/null +++ b/.github/bot-scripts/post-release-notes.js @@ -0,0 +1,37 @@ +/// +// @ts-check + +/** + * @param {{github: Github, context: Context}} param + */ +async function main(param) { + const { github, context } = param; + + const discordWebhookUrl = process.env.DISCORD_WEBHOOK_URL || ''; + + // remove multiple spaces and put links between < > to prevent embeds + const releaseNotes = context.payload.release.body.replace(/(\r\n+|\n+|\r+)/gm, '\n').replace(/(https:\/\/[^)]+)/g, '<$1>'); + + try { + console.log('Posting release notes to Discord...'); + console.log(releaseNotes); + + const response = await fetch(discordWebhookUrl, { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ content: releaseNotes }), + }); + + if (response.ok) { + console.log('Release notes posted to Discord successfully.'); + } else { + console.error('Failed to post release notes to Discord:', response.status, response.statusText); + } + } catch (error) { + console.error('Error posting release notes to Discord:', error); + } +} +module.exports = main; + diff --git a/.github/workflows/discord-publish.yml b/.github/workflows/discord-publish.yml new file mode 100644 index 00000000000..ce906a1ab6c --- /dev/null +++ b/.github/workflows/discord-publish.yml @@ -0,0 +1,23 @@ +name: Post Release Notes to Discord + +on: + release: + types: + - published + +jobs: + post-release-notes: + runs-on: ubuntu-latest + + steps: + - name: Check out code + uses: actions/checkout@v3 + + - uses: actions/github-script@v6 + name: Post release notes to Discord + env: + DISCORD_WEBHOOK_URL: ${{ secrets.DISCORD_WEBHOOK_URL }} + with: + script: | + const script = require('./.github/bot-scripts/post-release-notes.js') + script({github, context}) \ No newline at end of file