From 1a590313a7a1a22eb57e5e0dd173eab99260e3c8 Mon Sep 17 00:00:00 2001 From: Randy Brown Date: Fri, 29 Oct 2021 12:30:14 -0500 Subject: [PATCH 1/3] validate payload and pass to message --- index.js | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/index.js b/index.js index 982b4642..8dffcdf3 100644 --- a/index.js +++ b/index.js @@ -13,35 +13,37 @@ try { throw 'Need to provide at least one botToken or webhookUrl' } + if(payload) { + try { + // confirm it is valid json + payload = JSON.parse(payload); + } catch (e) { + // passed in payload wasn't valid json + console.error("passed in payload was invalid JSON") + throw 'Need to provide valid JSON payload' + } + } + if (typeof botToken !== 'undefined' && botToken.length > 0) { const message = core.getInput('slack-message'); const channelId = core.getInput('channel-id'); const web = new WebClient(botToken); - if(channelId.length > 0 && message.length > 0) { + if(channelId.length > 0 && (message.length > 0 || payload)) { // post message - web.chat.postMessage({text: message, channel: channelId}); + web.chat.postMessage({channel: channelId, text: message, ...(payload ?? {})}); } else { - console.log('missing either channel-id or slack-message! Did not send a message via chat.postMessage with botToken'); + console.log('missing either channel-id, slack-message or payload! Did not send a message via chat.postMessage with botToken'); } - } - + } + if (typeof webhookUrl !== 'undefined' && webhookUrl.length > 0) { - if (payload.length < 1) { + if (!payload) { // No Payload was passed in console.log('no custom payload was passed in, using default payload that triggered the GitHub Action') // Get the JSON webhook payload for the event that triggered the workflow payload = github.context.payload; - } else { - try { - // confirm it is valid json - payload = JSON.parse(payload); - } catch (e) { - // passed in payload wasn't valid json - console.error("passed in payload was invalid JSON") - throw 'Need to provide valid JSON payload' - } } // flatten JSON payload (no nested attributes) From 16bc1738b709232380b3d201fbbd015881176111 Mon Sep 17 00:00:00 2001 From: Randy Brown Date: Fri, 29 Oct 2021 12:33:50 -0500 Subject: [PATCH 2/3] two json workflow samples --- .../JSONTextPayload.yml | 15 ++++++ .../customJSONPayload.yml | 47 +++++++++++++++++++ 2 files changed, 62 insertions(+) create mode 100644 example-workflows/Bot token post to channel/JSONTextPayload.yml create mode 100644 example-workflows/Bot token post to channel/customJSONPayload.yml diff --git a/example-workflows/Bot token post to channel/JSONTextPayload.yml b/example-workflows/Bot token post to channel/JSONTextPayload.yml new file mode 100644 index 00000000..d617184b --- /dev/null +++ b/example-workflows/Bot token post to channel/JSONTextPayload.yml @@ -0,0 +1,15 @@ +on: [push] + +jobs: + new_push_job: + runs-on: ubuntu-latest + name: New push to repo + steps: + - name: Send GitHub trigger payload to Slack Workflow Builder + id: slack + uses: slackapi/slack-github-action@v1.15.0 + with: + channel-id: 'SLACK_CHANNEL_ID' # ID of Slack Channel you want to post to + payload: "{\"text\":\"posting from a github action\"}" + env: + SLACK_BOT_TOKEN: ${{ secrets.SLACK_BOT_TOKEN }} diff --git a/example-workflows/Bot token post to channel/customJSONPayload.yml b/example-workflows/Bot token post to channel/customJSONPayload.yml new file mode 100644 index 00000000..2483be8d --- /dev/null +++ b/example-workflows/Bot token post to channel/customJSONPayload.yml @@ -0,0 +1,47 @@ +on: [push] + +jobs: + new_push_job: + runs-on: ubuntu-latest + name: New push to repo + steps: + - name: Send GitHub trigger payload to Slack Workflow Builder + id: slack + uses: slackapi/slack-github-action@v1.15.0 + with: + channel-id: 'SLACK_CHANNEL_ID' # ID of Slack Channel you want to post to + payload: | + { + "blocks": [ + { "type": "divider" }, + { + "type": "image", + "title": { + "type": "plain_text", + "text": "Slack Slack Slack", + "emoji": true + }, + "image_url": "https://media.makeameme.org/created/a-slack-this.jpg", + "alt_text": "marg" + }, + { + "type": "actions", + "elements": [ + { + "type": "button", + "text": { + "type": "plain_text", + "text": $values.button_text + }, + "url": $values.button_url + } + ] + } + ], + channel-id": "NEW_TARGET_CHANNEL_ID" + } + env: + SLACK_BOT_TOKEN: ${{ secrets.SLACK_BOT_TOKEN }} + values: | + button_text: ${{ github.sha }} + button_url: https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }} \ No newline at end of file From 3cdd60ef1c67a771d87cd46e6a5f457526feed70 Mon Sep 17 00:00:00 2001 From: Randy Brown Date: Fri, 29 Oct 2021 14:54:42 -0500 Subject: [PATCH 3/3] nullish -> or --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index 8dffcdf3..b2e49c5c 100644 --- a/index.js +++ b/index.js @@ -31,7 +31,7 @@ try { if(channelId.length > 0 && (message.length > 0 || payload)) { // post message - web.chat.postMessage({channel: channelId, text: message, ...(payload ?? {})}); + web.chat.postMessage({channel: channelId, text: message, ...(payload || {})}); } else { console.log('missing either channel-id, slack-message or payload! Did not send a message via chat.postMessage with botToken'); }