From a6cd17bb4d12e06465415536eb90b5ea8286f67b Mon Sep 17 00:00:00 2001 From: Dane Bertram Date: Thu, 7 Nov 2024 15:50:59 -0500 Subject: [PATCH 1/5] 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 2/5] 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 3/5] 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 c9717da6c5907d92bfae4e22bc6b7fb11bc86d63 Mon Sep 17 00:00:00 2001 From: Dane Bertram Date: Mon, 25 Nov 2024 15:11:23 -0500 Subject: [PATCH 4/5] action: validate inputs, single quotes for JSON, set ts output correctly --- action.yml | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/action.yml b/action.yml index a47efb3..e1b2cfd 100644 --- a/action.yml +++ b/action.yml @@ -20,24 +20,30 @@ inputs: outputs: ts: description: "The timestamp ID of the message that was just posted" + value: ${{steps.slack-post-message.outputs.ts}} branding: icon: 'tag' color: 'blue' 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; } + - name: POST to chat.postMessage API + id: slack-post-message + shell: bash 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 }}\", \"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" - { - echo "ts<> "$GITHUB_OUTPUT" + echo "Slack message API response:\n$response" + ts=$(echo "$response" | jq --raw-output '.ts') + echo "Setting ts output to $ts" + echo "ts=$ts" >> $GITHUB_OUTPUT From bd7f445efbdc6ae21fafaa224a52e4c03c1daa2a Mon Sep 17 00:00:00 2001 From: Dane Bertram Date: Mon, 25 Nov 2024 15:12:33 -0500 Subject: [PATCH 5/5] GH Workflow: add branch-bulider.yml to test changes before merge - test sending a Slack message with all optional inputs using their default values - test sending a Slack message with all optional inputs provided (reply message + link unfurling disabled) --- .github/workflows/branch-builder.yml | 31 ++++++++++++++++++++++++++++ 1 file changed, 31 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..155b94d --- /dev/null +++ b/.github/workflows/branch-builder.yml @@ -0,0 +1,31 @@ +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 + id: basic-message + uses: ./ + with: + channel: ${{ env.SLACK_CHANNEL_ID }} + text: 'Test message from :\n\nMessage sent from: \nWorkflow Log: ' + - name: Test Slack message with optional inputs specified (reply message + unfurl_links disabled) + uses: ./ + with: + channel: ${{ env.SLACK_CHANNEL_ID }} + thread_ts: ${{ steps.basic-message.outputs.ts }} + text: 'Test reply with a link to unfurl (that should _not_ unfurl): ' + unfurl_links: false