From a6cd17bb4d12e06465415536eb90b5ea8286f67b Mon Sep 17 00:00:00 2001 From: Dane Bertram Date: Thu, 7 Nov 2024 15:50:59 -0500 Subject: [PATCH 01/16] refactor how curl is called and errors are captured - use --fail-with-body so HTTP status of >= 400 triggers an error exit code - use long form curl args in general for better readability/comprehensibility - break up curl args over multiple lines to improve readability - log raw response before processing it - tweak multi-line GITHUB_OUTPUT capture to more closely match doc examples --- action.yml | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/action.yml b/action.yml index efacb2f..3383d50 100644 --- a/action.yml +++ b/action.yml @@ -23,11 +23,17 @@ runs: using: 'composite' steps: - run: | + response=$(curl --fail-with-body --silent --show-error \ + --request POST \ + --header "Authorization: Bearer ${{ env.SLACK_BOT_TOKEN || inputs.token }}" \ + --header "Content-Type: application/json; charset=utf-8" \ + --url https://slack.com/api/chat.postMessage \ + --data "{\"channel\": \"${{ inputs.channel }}\", \"thread_ts\": \"${{ inputs.thread_ts }}\", \"text\": \"${{ inputs.text }}\"}" \ + ) + echo "Slack API response:\n$response" { - echo "ts=<> "$GITHUB_OUTPUT" shell: bash From 2cd262a4abfa6c9a141b68a01023f630c30cf4c9 Mon Sep 17 00:00:00 2001 From: Dane Bertram Date: Thu, 7 Nov 2024 15:51:48 -0500 Subject: [PATCH 02/16] add support for optional unfurl_links argument --- action.yml | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/action.yml b/action.yml index 3383d50..7f778ec 100644 --- a/action.yml +++ b/action.yml @@ -13,6 +13,10 @@ inputs: text: description: "The message text to post" required: true + unfurl_links: + description: "Whether links in the message should be unfurled" + type: boolean + default: true outputs: ts: description: "The timestamp ID of the message that was just posted" @@ -28,7 +32,7 @@ runs: --header "Authorization: Bearer ${{ env.SLACK_BOT_TOKEN || inputs.token }}" \ --header "Content-Type: application/json; charset=utf-8" \ --url https://slack.com/api/chat.postMessage \ - --data "{\"channel\": \"${{ inputs.channel }}\", \"thread_ts\": \"${{ inputs.thread_ts }}\", \"text\": \"${{ inputs.text }}\"}" \ + --data "{\"channel\": \"${{ inputs.channel }}\", \"thread_ts\": \"${{ inputs.thread_ts }}\", \"unfurl_links\": ${{ inputs.unfurl_links }}, \"text\": \"${{ inputs.text }}\"}" \ ) echo "Slack API response:\n$response" { From 1f4882eb92b6ee6cc14e964c0f1e736d61a9bb2d Mon Sep 17 00:00:00 2001 From: Dane Bertram Date: Thu, 14 Nov 2024 10:42:09 -0500 Subject: [PATCH 03/16] move 'shell: bash' to top of step --- action.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/action.yml b/action.yml index 7f778ec..a47efb3 100644 --- a/action.yml +++ b/action.yml @@ -26,7 +26,8 @@ branding: runs: using: 'composite' steps: - - run: | + - shell: bash + run: | response=$(curl --fail-with-body --silent --show-error \ --request POST \ --header "Authorization: Bearer ${{ env.SLACK_BOT_TOKEN || inputs.token }}" \ @@ -40,4 +41,3 @@ runs: echo "$response" | jq --raw-output '.ts' echo "EOF" } >> "$GITHUB_OUTPUT" - shell: bash From 9cbadd29f2312976e1b9030b407f2e6d428ca14d Mon Sep 17 00:00:00 2001 From: Dane Bertram Date: Thu, 14 Nov 2024 12:57:50 -0500 Subject: [PATCH 04/16] GH Action: try testing the action from a GH Action Based on notes from https://dev.to/cicirello/how-to-test-a-github-action-with-github-actions-2hag --- .github/workflows/branch-builder.yml | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 .github/workflows/branch-builder.yml diff --git a/.github/workflows/branch-builder.yml b/.github/workflows/branch-builder.yml new file mode 100644 index 0000000..b5ec29b --- /dev/null +++ b/.github/workflows/branch-builder.yml @@ -0,0 +1,23 @@ +name: Branch Builder + +on: + pull_request: + types: [opened, reopened, synchronize] + push: + branches: [ main ] + tags-ignore: "**" + workflow_dispatch: +env: + BRANCH_NAME: ${{ github.head_ref || github.ref_name }} # github.head_ref for pull_request event and github.ref_name for push event + SLACK_BOT_TOKEN: ${{ secrets.SLACK_TEST_TOKEN }} + SLACK_CHANNEL_ID: ${{ secrets.SLACK_TEST_CHANNEL_ID }} +jobs: + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - name: Test Slack message with default settings + uses: ./ + with: + channel: ${{ env.SLACK_CHANNEL_ID }} + text: "branch-builder message from `ynab/slack-post-message-action/${{ env.BRANCH_NAME }}`:\n\n*Testing!*\n\nhttps://www.ynab.com" From 885089187ac8cd58b0004774b209e2037186d16d Mon Sep 17 00:00:00 2001 From: Dane Bertram Date: Thu, 14 Nov 2024 14:25:01 -0500 Subject: [PATCH 05/16] using single quotes for data payload to avoid need to escape text input ...other than single quotes --- action.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/action.yml b/action.yml index a47efb3..10194b1 100644 --- a/action.yml +++ b/action.yml @@ -33,7 +33,7 @@ runs: --header "Authorization: Bearer ${{ env.SLACK_BOT_TOKEN || inputs.token }}" \ --header "Content-Type: application/json; charset=utf-8" \ --url https://slack.com/api/chat.postMessage \ - --data "{\"channel\": \"${{ inputs.channel }}\", \"thread_ts\": \"${{ inputs.thread_ts }}\", \"unfurl_links\": ${{ inputs.unfurl_links }}, \"text\": \"${{ inputs.text }}\"}" \ + --data '{"channel": "${{ inputs.channel }}", "thread_ts": "${{ inputs.thread_ts }}", "unfurl_links": ${{ inputs.unfurl_links }}, "text": "${{ inputs.text }}"}' \ ) echo "Slack API response:\n$response" { From a6ca8d45321b3ade8e3a76b7718b77a2b8493cc3 Mon Sep 17 00:00:00 2001 From: Dane Bertram Date: Thu, 14 Nov 2024 14:29:12 -0500 Subject: [PATCH 06/16] tweak branch builder slack text --- .github/workflows/branch-builder.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/branch-builder.yml b/.github/workflows/branch-builder.yml index b5ec29b..f2f23cf 100644 --- a/.github/workflows/branch-builder.yml +++ b/.github/workflows/branch-builder.yml @@ -20,4 +20,4 @@ jobs: uses: ./ with: channel: ${{ env.SLACK_CHANNEL_ID }} - text: "branch-builder message from `ynab/slack-post-message-action/${{ env.BRANCH_NAME }}`:\n\n*Testing!*\n\nhttps://www.ynab.com" + text: "Test message from :\n\nMessage sent from: \nWorkflow Log: " From 37deab1358cd3206e5c019dd187a43efe8005647 Mon Sep 17 00:00:00 2001 From: Dane Bertram Date: Thu, 14 Nov 2024 14:48:13 -0500 Subject: [PATCH 07/16] log ts before sending to --- action.yml | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/action.yml b/action.yml index 10194b1..a80dfca 100644 --- a/action.yml +++ b/action.yml @@ -36,8 +36,6 @@ runs: --data '{"channel": "${{ inputs.channel }}", "thread_ts": "${{ inputs.thread_ts }}", "unfurl_links": ${{ inputs.unfurl_links }}, "text": "${{ inputs.text }}"}' \ ) echo "Slack API response:\n$response" - { - echo "ts<> "$GITHUB_OUTPUT" + ts=$(echo "$response" | jq --raw-output '.ts') + echo "ts of Slack message: $ts" + echo "ts=$ts" >> "$GITHUB_OUTPUT" From aaf89b015271e541cdf49afde67cbad6d4acff17 Mon Sep 17 00:00:00 2001 From: Dane Bertram Date: Thu, 14 Nov 2024 14:52:26 -0500 Subject: [PATCH 08/16] test slack reply --- .github/workflows/branch-builder.yml | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/.github/workflows/branch-builder.yml b/.github/workflows/branch-builder.yml index f2f23cf..bb07aaa 100644 --- a/.github/workflows/branch-builder.yml +++ b/.github/workflows/branch-builder.yml @@ -17,7 +17,14 @@ jobs: steps: - uses: actions/checkout@v4 - name: Test Slack message with default settings + id: basic-message uses: ./ with: channel: ${{ env.SLACK_CHANNEL_ID }} text: "Test message from :\n\nMessage sent from: \nWorkflow Log: " + - name: Test Slack reply + uses: ./ + with: + thread_ts: ${{ steps.basic-message.outputs.ts }} + channel: ${{ env.SLACK_CHANNEL_ID }} + text: "Test reply with a link to unfurl: https://www.ynab.com" From a432e4c84e11e9b631b96872af6346799cfe4d1b Mon Sep 17 00:00:00 2001 From: Dane Bertram Date: Thu, 14 Nov 2024 14:58:45 -0500 Subject: [PATCH 09/16] validate required inputs are set ...b/c the GH runner doesn't: https://github.com/actions/runner/issues/1070 --- action.yml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/action.yml b/action.yml index a80dfca..90aa69f 100644 --- a/action.yml +++ b/action.yml @@ -26,6 +26,10 @@ branding: runs: using: 'composite' steps: + - shell: bash + run: | + [[ "${{ inputs.channel }}" ]] || { echo "required input 'channel' not specified" ; exit 1; } + [[ "${{ inputs.text }}" ]] || { echo "required input 'text' not specified" ; exit 1; } - shell: bash run: | response=$(curl --fail-with-body --silent --show-error \ From 921aa79fcb92e301436dfe079018cb57581d9455 Mon Sep 17 00:00:00 2001 From: Dane Bertram Date: Thu, 14 Nov 2024 15:07:16 -0500 Subject: [PATCH 10/16] name steps --- action.yml | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/action.yml b/action.yml index 90aa69f..b26643c 100644 --- a/action.yml +++ b/action.yml @@ -26,11 +26,13 @@ branding: runs: using: 'composite' steps: - - shell: bash + - name: Validate required inputs + shell: bash run: | [[ "${{ inputs.channel }}" ]] || { echo "required input 'channel' not specified" ; exit 1; } [[ "${{ inputs.text }}" ]] || { echo "required input 'text' not specified" ; exit 1; } - - shell: bash + - name: POST to chat.postMessage API + shell: bash run: | response=$(curl --fail-with-body --silent --show-error \ --request POST \ From be9d6c999154748da4204fb308e85f66fce510fd Mon Sep 17 00:00:00 2001 From: Dane Bertram Date: Thu, 14 Nov 2024 15:07:25 -0500 Subject: [PATCH 11/16] branch-builder tweaks --- .github/workflows/branch-builder.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/branch-builder.yml b/.github/workflows/branch-builder.yml index bb07aaa..add5d0f 100644 --- a/.github/workflows/branch-builder.yml +++ b/.github/workflows/branch-builder.yml @@ -22,9 +22,9 @@ jobs: with: channel: ${{ env.SLACK_CHANNEL_ID }} text: "Test message from :\n\nMessage sent from: \nWorkflow Log: " - - name: Test Slack reply + - name: Test Slack reply message uses: ./ with: - thread_ts: ${{ steps.basic-message.outputs.ts }} channel: ${{ env.SLACK_CHANNEL_ID }} + thread_ts: ${{ steps.basic-message.outputs.ts }} text: "Test reply with a link to unfurl: https://www.ynab.com" From 58b13959c098a151f88640c9d496c3f04a9692e6 Mon Sep 17 00:00:00 2001 From: Dane Bertram Date: Thu, 14 Nov 2024 15:13:31 -0500 Subject: [PATCH 12/16] fix setting step output? --- action.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/action.yml b/action.yml index b26643c..66d8396 100644 --- a/action.yml +++ b/action.yml @@ -44,4 +44,4 @@ runs: echo "Slack API response:\n$response" ts=$(echo "$response" | jq --raw-output '.ts') echo "ts of Slack message: $ts" - echo "ts=$ts" >> "$GITHUB_OUTPUT" + echo "ts=$ts" >> $GITHUB_OUTPUT From 5d6132ac66f56991ee6b32db46faac44a47f14c3 Mon Sep 17 00:00:00 2001 From: Dane Bertram Date: Thu, 14 Nov 2024 15:18:17 -0500 Subject: [PATCH 13/16] wrap text in single quotes to avoid shelling out text --- .github/workflows/branch-builder.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/branch-builder.yml b/.github/workflows/branch-builder.yml index add5d0f..312cada 100644 --- a/.github/workflows/branch-builder.yml +++ b/.github/workflows/branch-builder.yml @@ -21,10 +21,10 @@ jobs: uses: ./ with: channel: ${{ env.SLACK_CHANNEL_ID }} - text: "Test message from :\n\nMessage sent from: \nWorkflow Log: " + text: 'Test message from :\n\nMessage sent from: \nWorkflow Log: ' - name: Test Slack reply message uses: ./ with: channel: ${{ env.SLACK_CHANNEL_ID }} thread_ts: ${{ steps.basic-message.outputs.ts }} - text: "Test reply with a link to unfurl: https://www.ynab.com" + text: 'Test reply with a link to unfurl: ' From 9be0fb15fe12f187e7b4516f007f0191b2eff18d Mon Sep 17 00:00:00 2001 From: Dane Bertram Date: Thu, 14 Nov 2024 15:22:42 -0500 Subject: [PATCH 14/16] sanity test hard-coded thread_ts --- .github/workflows/branch-builder.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/branch-builder.yml b/.github/workflows/branch-builder.yml index 312cada..07a096c 100644 --- a/.github/workflows/branch-builder.yml +++ b/.github/workflows/branch-builder.yml @@ -26,5 +26,5 @@ jobs: uses: ./ with: channel: ${{ env.SLACK_CHANNEL_ID }} - thread_ts: ${{ steps.basic-message.outputs.ts }} + thread_ts: 1731615525.315399 text: 'Test reply with a link to unfurl: ' From 69633309d1f4872084e2ec9f58ba612ac985557a Mon Sep 17 00:00:00 2001 From: Dane Bertram Date: Thu, 14 Nov 2024 15:27:25 -0500 Subject: [PATCH 15/16] just test basic message for now --- .github/workflows/branch-builder.yml | 6 ------ 1 file changed, 6 deletions(-) diff --git a/.github/workflows/branch-builder.yml b/.github/workflows/branch-builder.yml index 07a096c..a93dd75 100644 --- a/.github/workflows/branch-builder.yml +++ b/.github/workflows/branch-builder.yml @@ -22,9 +22,3 @@ jobs: with: channel: ${{ env.SLACK_CHANNEL_ID }} text: 'Test message from :\n\nMessage sent from: \nWorkflow Log: ' - - name: Test Slack reply message - uses: ./ - with: - channel: ${{ env.SLACK_CHANNEL_ID }} - thread_ts: 1731615525.315399 - text: 'Test reply with a link to unfurl: ' From 902326563924022ff6c5a0e6dde3bc6c4eb5b5a1 Mon Sep 17 00:00:00 2001 From: Dane Bertram Date: Thu, 14 Nov 2024 15:30:29 -0500 Subject: [PATCH 16/16] try setting ts output differently --- .github/workflows/branch-builder.yml | 6 ++++++ action.yml | 6 +++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/.github/workflows/branch-builder.yml b/.github/workflows/branch-builder.yml index a93dd75..312cada 100644 --- a/.github/workflows/branch-builder.yml +++ b/.github/workflows/branch-builder.yml @@ -22,3 +22,9 @@ jobs: with: channel: ${{ env.SLACK_CHANNEL_ID }} text: 'Test message from :\n\nMessage sent from: \nWorkflow Log: ' + - name: Test Slack reply message + uses: ./ + with: + channel: ${{ env.SLACK_CHANNEL_ID }} + thread_ts: ${{ steps.basic-message.outputs.ts }} + text: 'Test reply with a link to unfurl: ' diff --git a/action.yml b/action.yml index 66d8396..8206ef3 100644 --- a/action.yml +++ b/action.yml @@ -44,4 +44,8 @@ runs: echo "Slack API response:\n$response" ts=$(echo "$response" | jq --raw-output '.ts') echo "ts of Slack message: $ts" - echo "ts=$ts" >> $GITHUB_OUTPUT + { + echo "ts<> "$GITHUB_OUTPUT"