From 17ecd5df18a018162596e09cde91efca6757ee24 Mon Sep 17 00:00:00 2001 From: Nik Ho Date: Wed, 4 Oct 2023 18:49:18 +1300 Subject: [PATCH 1/8] feat: add action to notify Slack with Publish workflow status --- .github/actions/README.md | 1 + .github/actions/action.yml | 19 +++++++++++++++ .github/actions/index.js | 50 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+) create mode 100644 .github/actions/README.md create mode 100644 .github/actions/action.yml create mode 100644 .github/actions/index.js diff --git a/.github/actions/README.md b/.github/actions/README.md new file mode 100644 index 0000000000..02993bbf84 --- /dev/null +++ b/.github/actions/README.md @@ -0,0 +1 @@ +# Slack Notify diff --git a/.github/actions/action.yml b/.github/actions/action.yml new file mode 100644 index 0000000000..3a1f59086c --- /dev/null +++ b/.github/actions/action.yml @@ -0,0 +1,19 @@ +--- +name: 'Notify Slack Publish Status' +description: 'Notify Slack with the completion status of the publish action' +inputs: + status: + description: 'Status of the action' + required: true + default: 'failure' + version: + description: 'Version of the SDK that was published' + required: true + default: '0.0.0' + docs_pr_url: + description: 'URL of the docs PR' + required: false + default: '' +runs: + using: 'node20' + main: 'index.js' diff --git a/.github/actions/index.js b/.github/actions/index.js new file mode 100644 index 0000000000..35e4a37575 --- /dev/null +++ b/.github/actions/index.js @@ -0,0 +1,50 @@ +const core = require('@actions/core'); +const { WebClient } = require('@slack/web-api'); +const web = process.env.SLACK_TOKEN && new WebClient(process.env.SLACK_TOKEN); +const channel = `C051FKT784S` // #team-sdk channel + +const run = async () => { + if (web) { + try { + await postSlackNotification(); + } catch (e) { + console.log(e); + throw new Error(`failed because : ${e}`) + } + } else { + throw new Error('No SLACK_TOKEN environment variable found'); + } +} + +const postSlackNotification = async () => { + const status = core.getInput('status'); + const version = core.getInput('version'); + const docsUrl = core.getInput('docs_pr_url'); + let message = ''; + + if (version === '0.0.0') return; + + if (status === 'success') { + message = `✅ Version ${version} of the SDK has been deployed to NPM 🎉`; + + if (docsUrl) { + message += `\n\nYou can view the SDK reference docs PR here: ${docsUrl}` + } + } else { + message = `❌ Version ${version} of the SDK failed to deploy to NPM`; + } + + await web.chat.postMessage({ + channel, + username: 'Github workflow alert bot', + blocks: [{ + type: 'section', + text: { + type: 'mrkdwn', + text: message, + }, + }] + }) +} + +run(); From 1275263f1512c095954b66673aabc7abfdb801d1 Mon Sep 17 00:00:00 2001 From: Nik Ho Date: Wed, 4 Oct 2023 18:58:27 +1300 Subject: [PATCH 2/8] refactor: make action more general --- .github/actions/action.yml | 6 +++--- .github/actions/index.js | 14 +++++--------- 2 files changed, 8 insertions(+), 12 deletions(-) diff --git a/.github/actions/action.yml b/.github/actions/action.yml index 3a1f59086c..85525c1d8c 100644 --- a/.github/actions/action.yml +++ b/.github/actions/action.yml @@ -10,9 +10,9 @@ inputs: description: 'Version of the SDK that was published' required: true default: '0.0.0' - docs_pr_url: - description: 'URL of the docs PR' - required: false + message: + description: 'Message to send to Slack' + required: true default: '' runs: using: 'node20' diff --git a/.github/actions/index.js b/.github/actions/index.js index 35e4a37575..bdd46f2d34 100644 --- a/.github/actions/index.js +++ b/.github/actions/index.js @@ -19,19 +19,15 @@ const run = async () => { const postSlackNotification = async () => { const status = core.getInput('status'); const version = core.getInput('version'); - const docsUrl = core.getInput('docs_pr_url'); - let message = ''; + const message = core.getInput('message'); + let slackMessage = ''; if (version === '0.0.0') return; if (status === 'success') { - message = `✅ Version ${version} of the SDK has been deployed to NPM 🎉`; - - if (docsUrl) { - message += `\n\nYou can view the SDK reference docs PR here: ${docsUrl}` - } + slackMessage = `✅ ${message}`; } else { - message = `❌ Version ${version} of the SDK failed to deploy to NPM`; + slackMessage = `❌ ${message}`; } await web.chat.postMessage({ @@ -41,7 +37,7 @@ const postSlackNotification = async () => { type: 'section', text: { type: 'mrkdwn', - text: message, + text: slackMessage, }, }] }) From a997c46dd71d8a87d2d3335e293650fb91e2e1e9 Mon Sep 17 00:00:00 2001 From: Nik Ho Date: Thu, 5 Oct 2023 11:23:44 +1300 Subject: [PATCH 3/8] Move action files to folder. Refactor to only take a message input. --- .../{ => notify-slack-publish-status}/README.md | 0 .../{ => notify-slack-publish-status}/action.yml | 8 -------- .../{ => notify-slack-publish-status}/index.js | 13 +++---------- 3 files changed, 3 insertions(+), 18 deletions(-) rename .github/actions/{ => notify-slack-publish-status}/README.md (100%) rename .github/actions/{ => notify-slack-publish-status}/action.yml (56%) rename .github/actions/{ => notify-slack-publish-status}/index.js (72%) diff --git a/.github/actions/README.md b/.github/actions/notify-slack-publish-status/README.md similarity index 100% rename from .github/actions/README.md rename to .github/actions/notify-slack-publish-status/README.md diff --git a/.github/actions/action.yml b/.github/actions/notify-slack-publish-status/action.yml similarity index 56% rename from .github/actions/action.yml rename to .github/actions/notify-slack-publish-status/action.yml index 85525c1d8c..3a0160bca2 100644 --- a/.github/actions/action.yml +++ b/.github/actions/notify-slack-publish-status/action.yml @@ -2,14 +2,6 @@ name: 'Notify Slack Publish Status' description: 'Notify Slack with the completion status of the publish action' inputs: - status: - description: 'Status of the action' - required: true - default: 'failure' - version: - description: 'Version of the SDK that was published' - required: true - default: '0.0.0' message: description: 'Message to send to Slack' required: true diff --git a/.github/actions/index.js b/.github/actions/notify-slack-publish-status/index.js similarity index 72% rename from .github/actions/index.js rename to .github/actions/notify-slack-publish-status/index.js index bdd46f2d34..eb696d2bac 100644 --- a/.github/actions/index.js +++ b/.github/actions/notify-slack-publish-status/index.js @@ -17,17 +17,10 @@ const run = async () => { } const postSlackNotification = async () => { - const status = core.getInput('status'); - const version = core.getInput('version'); const message = core.getInput('message'); - let slackMessage = ''; - if (version === '0.0.0') return; - - if (status === 'success') { - slackMessage = `✅ ${message}`; - } else { - slackMessage = `❌ ${message}`; + if (!message) { + throw new Error('No message input found'); } await web.chat.postMessage({ @@ -37,7 +30,7 @@ const postSlackNotification = async () => { type: 'section', text: { type: 'mrkdwn', - text: slackMessage, + text: message, }, }] }) From fe24e4bf4815606c8976f7bdb30c0fc8b6d1f46f Mon Sep 17 00:00:00 2001 From: Nik Ho Date: Thu, 5 Oct 2023 11:24:18 +1300 Subject: [PATCH 4/8] use notify slack action in workflows --- .github/workflows/publish-docs.yaml | 13 +++++++++++++ .github/workflows/publish.yaml | 13 +++++++++++++ 2 files changed, 26 insertions(+) diff --git a/.github/workflows/publish-docs.yaml b/.github/workflows/publish-docs.yaml index a8a5f66a72..3826c8de61 100644 --- a/.github/workflows/publish-docs.yaml +++ b/.github/workflows/publish-docs.yaml @@ -65,5 +65,18 @@ jobs: run: ./.github/scripts/update-docs-link.sh - name: Create SDK Docs PR + id: docs_pr run: ./.github/scripts/push-docs-pr.sh shell: bash + + - name: Notify SDK Slack Docs PR Success + if: ${{ success() && steps.docs_pr.conclusion == 'success' }} + uses: ./.github/actions/notify-slack-publish-status + with: + message: "✅ Docs PR published successfully - please review and merge (Release SDK reference docs v${{ env.VERSION }})[https://github.com/immutable/imx-docs/pulls]" + + - name: Notify SDK Slack Docs PR Failure + if: ${{ failure() && steps.docs_pr.conclusion == 'failure' }} + uses: ./.github/actions/notify-slack-publish-status + with: + message: "❌ Docs PR failed to publish. Please check the logs for more details." diff --git a/.github/workflows/publish.yaml b/.github/workflows/publish.yaml index d3f746f092..00954b6983 100644 --- a/.github/workflows/publish.yaml +++ b/.github/workflows/publish.yaml @@ -119,6 +119,7 @@ jobs: run: npm config set //registry.npmjs.org/:_authToken ${{ secrets.PLATFORM_SA_NPM_TOKEN }} - name: Release + id: npm_release if: contains(github.event.inputs.release_type, 'release') run: yarn release --ci --no-increment -c .release-it.json $( ${{ github.event.inputs.dry_run }} && echo "--dry-run" || echo "") --github.tokenRef=${{ secrets.RELEASE_TOKEN }} @@ -132,3 +133,15 @@ jobs: run: | echo "RELEASE_NAME=$(gh release view --json name | jq -r .name)" >> $GITHUB_OUTPUT echo "RELEASE_URL=$(gh release view --json url | jq -r .url)" >> $GITHUB_OUTPUT + + - name: Notify SDK Slack Publish Success + if: ${{ success() && steps.npm_release.conclusion == 'success' && github.event.inputs.dry_run == 'false' }} + uses: ./.github/actions/notify-slack-publish-status + with: + message: "✅ Successfully published SDK version ${{steps.version.outputs.NEXT_VERSION}} to NPM.\n\nhttps://www.npmjs.com/package/@imtbl/sdk/v/${{steps.version.outputs.NEXT_VERSION}}" + + - name: Notify SDK Slack Publish Failure + if: ${{ failure() && steps.npm_release.conclusion == 'failure' && github.event.inputs.dry_run == 'false' }} + uses: ./.github/actions/notify-slack-publish-status + with: + message: "❌ Failed to publish SDK version ${{steps.version.outputs.NEXT_VERSION}} to NPM. Please check the logs for more details." From bb7d3a4aac747df5f4db6200ed06b1155da45123 Mon Sep 17 00:00:00 2001 From: Nik Ho Date: Thu, 5 Oct 2023 11:26:19 +1300 Subject: [PATCH 5/8] fix markdown link syntax --- .github/workflows/publish-docs.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/publish-docs.yaml b/.github/workflows/publish-docs.yaml index 3826c8de61..d6ef348bc0 100644 --- a/.github/workflows/publish-docs.yaml +++ b/.github/workflows/publish-docs.yaml @@ -73,7 +73,7 @@ jobs: if: ${{ success() && steps.docs_pr.conclusion == 'success' }} uses: ./.github/actions/notify-slack-publish-status with: - message: "✅ Docs PR published successfully - please review and merge (Release SDK reference docs v${{ env.VERSION }})[https://github.com/immutable/imx-docs/pulls]" + message: "✅ Docs PR published successfully - please review and merge [Release SDK reference docs v${{ env.VERSION }}](https://github.com/immutable/imx-docs/pulls)" - name: Notify SDK Slack Docs PR Failure if: ${{ failure() && steps.docs_pr.conclusion == 'failure' }} From 13f97ad5f05c00e33822dc8341331714b775d9a9 Mon Sep 17 00:00:00 2001 From: Nik Ho Date: Fri, 6 Oct 2023 14:59:19 +1300 Subject: [PATCH 6/8] use Slack webhook instead of web api client --- .../notify-slack-publish-status/index.js | 45 ++++++++++++------- package.json | 1 + yarn.lock | 20 +++++++++ 3 files changed, 50 insertions(+), 16 deletions(-) diff --git a/.github/actions/notify-slack-publish-status/index.js b/.github/actions/notify-slack-publish-status/index.js index eb696d2bac..b7cf9e8528 100644 --- a/.github/actions/notify-slack-publish-status/index.js +++ b/.github/actions/notify-slack-publish-status/index.js @@ -1,10 +1,8 @@ const core = require('@actions/core'); -const { WebClient } = require('@slack/web-api'); -const web = process.env.SLACK_TOKEN && new WebClient(process.env.SLACK_TOKEN); -const channel = `C051FKT784S` // #team-sdk channel +const webhook = process.env.SDK_PUBLISH_SLACK_WEBHOOK; const run = async () => { - if (web) { + if (webhook) { try { await postSlackNotification(); } catch (e) { @@ -12,7 +10,7 @@ const run = async () => { throw new Error(`failed because : ${e}`) } } else { - throw new Error('No SLACK_TOKEN environment variable found'); + throw new Error('No SDK_PUBLISH_SLACK_WEBHOOK environment variable found'); } } @@ -23,17 +21,32 @@ const postSlackNotification = async () => { throw new Error('No message input found'); } - await web.chat.postMessage({ - channel, - username: 'Github workflow alert bot', - blocks: [{ - type: 'section', - text: { - type: 'mrkdwn', - text: message, - }, - }] - }) + const options = { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ + text: message, + blocks: [ + { + type: 'section', + text: { + type: 'mrkdwn', + text: message, + } + } + ] + }), + }; + + const response = await fetch(webhook, options); + + if (response.status == 200) { + console.log('Posted message to Slack successfully'); + } else { + throw new Error(`Failed to post message to Slack. Status code: ${response.status}`); + } } run(); diff --git a/package.json b/package.json index 033afabea3..78a3a66741 100644 --- a/package.json +++ b/package.json @@ -5,6 +5,7 @@ "author": "Immutable", "bugs": "https://github.com/immutable/ts-immutable-sdk/issues", "devDependencies": { + "@actions/core": "^1.10.1", "@release-it-plugins/workspaces": "^3.2.0", "@typescript-eslint/eslint-plugin": "^5.57.1", "@typescript-eslint/parser": "^5.57.1", diff --git a/yarn.lock b/yarn.lock index e5dd14f70b..4076ef3f7a 100644 --- a/yarn.lock +++ b/yarn.lock @@ -196,6 +196,25 @@ __metadata: languageName: node linkType: hard +"@actions/core@npm:^1.10.1": + version: 1.10.1 + resolution: "@actions/core@npm:1.10.1" + dependencies: + "@actions/http-client": ^2.0.1 + uuid: ^8.3.2 + checksum: 96524c2725e70e3c3176b4e4d93a1358a86f3c5ca777db9a2f65eadfa672f00877db359bf60fffc416c33838ffb4743db93bcc5bf53e76199dd28bf7f7ff8e80 + languageName: node + linkType: hard + +"@actions/http-client@npm:^2.0.1": + version: 2.1.1 + resolution: "@actions/http-client@npm:2.1.1" + dependencies: + tunnel: ^0.0.6 + checksum: 5a3fd0407020a11cd3864b6c9ed8ef36912e08418df34fac675d15fc71543abb419db236ddb8fbd649f8ad8b5057bd78f1ac301f87283dfc706aa85578a90658 + languageName: node + linkType: hard + "@adobe/css-tools@npm:^4.0.1": version: 4.2.0 resolution: "@adobe/css-tools@npm:4.2.0" @@ -28865,6 +28884,7 @@ __metadata: version: 0.0.0-use.local resolution: "ts-immutable-sdk@workspace:." dependencies: + "@actions/core": ^1.10.1 "@release-it-plugins/workspaces": ^3.2.0 "@typescript-eslint/eslint-plugin": ^5.57.1 "@typescript-eslint/parser": ^5.57.1 From 33fb0c4f28fed8f42098c0d7192b056016daa36b Mon Sep 17 00:00:00 2001 From: Nik Ho Date: Fri, 6 Oct 2023 17:31:37 +1300 Subject: [PATCH 7/8] set Slack secret, send docs PR link to slack --- .github/scripts/push-docs-pr.sh | 5 +++-- .github/workflows/publish-docs.yaml | 7 ++++--- .github/workflows/publish.yaml | 1 + 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/.github/scripts/push-docs-pr.sh b/.github/scripts/push-docs-pr.sh index 96742c7e56..d15faeeb17 100755 --- a/.github/scripts/push-docs-pr.sh +++ b/.github/scripts/push-docs-pr.sh @@ -58,9 +58,10 @@ then sleep 60 echo "Creating a pull request" - gh pr create --title "Release SDK reference docs v$VERSION" \ + PR=$(gh pr create --title "Release SDK reference docs v$VERSION" \ --body "Released from ts-immutable-sdk" \ - --reviewer "$GITHUB_ACTOR" + --reviewer "$GITHUB_ACTOR") + echo "PR=$PR" >> $GITHUB_ENV else echo "No changes detected" fi diff --git a/.github/workflows/publish-docs.yaml b/.github/workflows/publish-docs.yaml index d6ef348bc0..03fb3439ee 100644 --- a/.github/workflows/publish-docs.yaml +++ b/.github/workflows/publish-docs.yaml @@ -12,6 +12,7 @@ jobs: runs-on: ubuntu-latest env: GH_TOKEN: ${{ secrets.PLATFORM_SA_GITHUB_TOKEN }} + SDK_PUBLISH_SLACK_WEBHOOK: ${{ secrets.SDK_PUBLISH_SLACK_WEBHOOK }} steps: - name: Checkout uses: actions/checkout@v3 @@ -73,10 +74,10 @@ jobs: if: ${{ success() && steps.docs_pr.conclusion == 'success' }} uses: ./.github/actions/notify-slack-publish-status with: - message: "✅ Docs PR published successfully - please review and merge [Release SDK reference docs v${{ env.VERSION }}](https://github.com/immutable/imx-docs/pulls)" + message: "✅ SDK reference documents PR created successfully - please review and merge ${{ env.PR }} into main." - name: Notify SDK Slack Docs PR Failure - if: ${{ failure() && steps.docs_pr.conclusion == 'failure' }} + if: ${{ failure() }} uses: ./.github/actions/notify-slack-publish-status with: - message: "❌ Docs PR failed to publish. Please check the logs for more details." + message: "❌ Failed to create SDK reference documents PR. Please check the logs for more details." diff --git a/.github/workflows/publish.yaml b/.github/workflows/publish.yaml index 00954b6983..0717093f13 100644 --- a/.github/workflows/publish.yaml +++ b/.github/workflows/publish.yaml @@ -34,6 +34,7 @@ jobs: env: GH_TOKEN: ${{ secrets.PLATFORM_SA_GITHUB_TOKEN }} NODE_OPTIONS: --max-old-space-size=14366 + SDK_PUBLISH_SLACK_WEBHOOK: ${{ secrets.SDK_PUBLISH_SLACK_WEBHOOK }} steps: - name: Check Public Release Branch if: contains(github.event.inputs.release_type, 'release') && (github.ref != 'refs/heads/main') From 4127181e79db17a940ea7c4dc5ec6fc07beae407 Mon Sep 17 00:00:00 2001 From: Nik Ho Date: Fri, 6 Oct 2023 18:06:03 +1300 Subject: [PATCH 8/8] post Slack message on pre-releases --- .github/workflows/publish.yaml | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/.github/workflows/publish.yaml b/.github/workflows/publish.yaml index 0717093f13..e0831ff73d 100644 --- a/.github/workflows/publish.yaml +++ b/.github/workflows/publish.yaml @@ -107,6 +107,7 @@ jobs: - name: Pre Release Step if: contains(github.event.inputs.release_type, 'alpha') + id: pre_release uses: JS-DevTools/npm-publish@v1 with: token: ${{ secrets.PLATFORM_SA_NPM_TOKEN }} @@ -136,13 +137,13 @@ jobs: echo "RELEASE_URL=$(gh release view --json url | jq -r .url)" >> $GITHUB_OUTPUT - name: Notify SDK Slack Publish Success - if: ${{ success() && steps.npm_release.conclusion == 'success' && github.event.inputs.dry_run == 'false' }} + if: ${{ success() && (steps.npm_release.conclusion == 'success' || steps.pre_release.conclusion == 'success') && github.event.inputs.dry_run == 'false' }} uses: ./.github/actions/notify-slack-publish-status with: message: "✅ Successfully published SDK version ${{steps.version.outputs.NEXT_VERSION}} to NPM.\n\nhttps://www.npmjs.com/package/@imtbl/sdk/v/${{steps.version.outputs.NEXT_VERSION}}" - name: Notify SDK Slack Publish Failure - if: ${{ failure() && steps.npm_release.conclusion == 'failure' && github.event.inputs.dry_run == 'false' }} + if: ${{ failure() && (steps.npm_release.conclusion == 'failure' || steps.pre_release.conclusion == 'failure') && github.event.inputs.dry_run == 'false' }} uses: ./.github/actions/notify-slack-publish-status with: message: "❌ Failed to publish SDK version ${{steps.version.outputs.NEXT_VERSION}} to NPM. Please check the logs for more details."