diff --git a/.github/workflows/TestFail.yml b/.github/workflows/TestFail.yml new file mode 100644 index 0000000..2e70551 --- /dev/null +++ b/.github/workflows/TestFail.yml @@ -0,0 +1,14 @@ +on: [push] + +jobs: + new_push_job: + runs-on: ubuntu-latest + name: New push to repo + steps: + - name: Should Fail + id: slack + uses: slackapi/slack-github-action@v1.13.0 + + # Use the output from the `hello` step + - name: Get the output time + run: echo "The time was ${{ steps.hello.outputs.time }}" diff --git a/.github/workflows/main.yml b/.github/workflows/botPostMessage.yml similarity index 91% rename from .github/workflows/main.yml rename to .github/workflows/botPostMessage.yml index ebc57c5..b73c771 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/botPostMessage.yml @@ -7,7 +7,7 @@ jobs: steps: - name: Publish to slack id: slack - uses: slackapi/slack-github-action@v1.12.0 + uses: slackapi/slack-github-action@v1.13.0 with: channel-id: 'CTAAHRA79' slack-message: 'posting from a github action!' diff --git a/.github/workflows/customJson.yml b/.github/workflows/webhookCustomJSON.yml similarity index 80% rename from .github/workflows/customJson.yml rename to .github/workflows/webhookCustomJSON.yml index cf0ab2f..f0bc15f 100644 --- a/.github/workflows/customJson.yml +++ b/.github/workflows/webhookCustomJSON.yml @@ -7,11 +7,10 @@ jobs: steps: - name: Publish to slack id: slack - uses: slackapi/slack-github-action@v1.12.0 + uses: slackapi/slack-github-action@v1.13.0 with: payload: "{\"key\":\"value\",\"foo\":\"bar\"}" env: - SLACK_BOT_TOKEN: ${{ secrets.SLACK_BOT_TOKEN }} SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }} # Use the output from the `hello` step diff --git a/README.md b/README.md index 38f3fed..5e49b6b 100644 --- a/README.md +++ b/README.md @@ -5,11 +5,13 @@ Send data into Slack using this GitHub Action! This package has two different te 1) Send data to Slack's Workflow Builder (requires a paid Slack instance). 2) Send data via a Slack app to post to a specific channel (use an existing custom app or create a new one). -The recommended way to use this action is with Slack's Workflow Builder (if your on a paid Slack plan). +The recommended way to use this action is with Slack's Workflow Builder (if you're on a paid Slack plan). ## Slack Workflow Builder Route -Sending data to [Slack's Workflow builder](https://slack.com/intl/en-ca/features/workflow-automation) is the recommended way to use this action. This action will flatten and stringify the json payload from GitHub and send it over to a Slack Workflow webhook URL. Follow [these steps to create a Slack workflow using webhooks](https://slack.com/intl/en-ca/help/articles/360041352714-Create-more-advanced-workflows-using-webhooks). The Slack workflow webhook url will be in the form of `https://hooks.slack.com/workflows/....`. Add this workflow +Sending data to [Slack's Workflow builder](https://slack.com/intl/en-ca/features/workflow-automation) is the recommended way to use this action. This action will send data into Slack via a webhook URL. Follow [these steps to create a Slack workflow using webhooks](https://slack.com/intl/en-ca/help/articles/360041352714-Create-more-advanced-workflows-using-webhooks). The Slack workflow webhook url will be in the form of `https://hooks.slack.com/workflows/....`. The payload sent by this GitHub action will be flattened (all nested keys moved top level) and stringified since Slack's workflow builder only supports top level string values in payloads. + +You need to define expected variables in the payload the webhook will receive. If these variables are missing in the payload, an error is returned. ### Setup @@ -21,15 +23,35 @@ Sending data to [Slack's Workflow builder](https://slack.com/intl/en-ca/features ### Usage ``` -- name: Send data to Slack workflow +- name: Send GitHub Action trigger data to Slack workflow id: slack - uses: slackapi/slack-github-action@v1.4.0 + uses: slackapi/slack-github-action@v1.13.0 + env: + SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }} +``` + +or + +``` +- name: Send custom JSON data to Slack workflow + id: slack + uses: slackapi/slack-github-action@v1.13.0 + with: + payload: "{\"key\":\"value\",\"foo\":\"bar\"}" # This data can be any valid JSON from a previous step in the GitHub Action env: SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }} ``` ## Slack App Route +This route allows + +## Setup + +* [Create a slack app]() for your workspace (alternatively use an existing app you have already created and installed) + * Add the `chat.write` scope under **OAuth & Permissions** +* Install the app to your workspace + ### Usage ``` diff --git a/dist/index.js b/dist/index.js index b71ef5e..517947c 100644 --- a/dist/index.js +++ b/dist/index.js @@ -16051,35 +16051,7 @@ const axios = __nccwpck_require__(8020); try { const botToken = process.env.SLACK_BOT_TOKEN; const webhookUrl = process.env.SLACK_WEBHOOK_URL; - - console.log('botToken', botToken, typeof botToken) - console.log('webhookUrl', webhookUrl, typeof webhookUrl) - - let payload = core.getInput('payload'); - console.log('payload:') - console.log(payload) - console.log(typeof payload, payload.length) - - if (payload.length < 1) { - console.log('undefined payload'); - console.log('no payload passed in, using payload that triggered the GitHub Action') - // Get the JSON webhook payload for the event that triggered the workflow - payload = github.context.payload; - } else { - console.log('in else') - try { - // confirm it is valid json - payload = JSON.parse(payload); - console.log('vaid json') - } catch (e) { - // passed in payload wasn't valid json - console.error("passed in payload was invalid") - throw 'Need to provide valid json payload' - } - } - - // console.log(`The event payload: ${JSON.stringify(payload, undefined, 2)}`); if (botToken === undefined && webhookUrl === undefined) { throw 'Need to provide at least one botToken or webhookUrl' @@ -16088,9 +16060,6 @@ try { if (botToken.length > 0) { const message = core.getInput('slack-message'); const channelId = core.getInput('channel-id'); - console.log('message', message, typeof message) - console.log('channelId', channelId, typeof channelId) - const web = new WebClient(botToken); if(channelId.length > 0 && message.length > 0) { @@ -16101,10 +16070,25 @@ try { } } - console.log('webhookURL type', typeof webhookUrl) if (typeof webhookUrl !== 'undefined' && webhookUrl.length > 0) { - console.log('flattening'); - // send flat payload to webhookUrl + + if (payload.length < 1) { + // 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) const flatPayload = flatten(payload); // workflow builder requires values to be strings @@ -16113,18 +16097,20 @@ try { flatPayload[key] = '' + flatPayload[key]; }) - console.log('flattened Payload'); - console.log(flatPayload); - - console.log('sending webhook') - axios.post(webhookUrl, flatPayload) + axios.post(webhookUrl, flatPayload).then(response => { + // Successful post! + }).catch(err => { + console.log("axios post failed, double check the payload being sent includes the keys Slack expects") + console.log(payload) + console.log(err) + throw err + }) } const time = (new Date()).toTimeString(); core.setOutput("time", time); } catch (error) { - console.log('error in big try') core.setFailed(error.message); } diff --git a/example-workflows/Bot token post to channel/main.yml b/example-workflows/Bot token post to channel/main.yml index 1127b53..050cf84 100644 --- a/example-workflows/Bot token post to channel/main.yml +++ b/example-workflows/Bot token post to channel/main.yml @@ -7,7 +7,7 @@ jobs: steps: - name: Publish to slack channel via bot token id: slack - uses: slackapi/slack-github-action@v1.4.0 + uses: slackapi/slack-github-action@v1.13.0 with: channel-id: 'SLACK_CHANNEL_ID' # ID of Slack Channel you want to post to slack-message: 'posting from a github action!' # The message you want to post diff --git a/example-workflows/Workflow builder/customJSONPayload.yml b/example-workflows/Workflow builder/customJSONPayload.yml new file mode 100644 index 0000000..b26c400 --- /dev/null +++ b/example-workflows/Workflow builder/customJSONPayload.yml @@ -0,0 +1,14 @@ +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.13.0 + with: + payload: "{\"key\":\"value\",\"foo\":\"bar\"}" + env: + SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }} diff --git a/example-workflows/Workflow builder /main.yml b/example-workflows/Workflow builder/githubTriggerPayload.yml similarity index 61% rename from example-workflows/Workflow builder /main.yml rename to example-workflows/Workflow builder/githubTriggerPayload.yml index 631fe31..a1ba1c2 100644 --- a/example-workflows/Workflow builder /main.yml +++ b/example-workflows/Workflow builder/githubTriggerPayload.yml @@ -5,8 +5,8 @@ jobs: runs-on: ubuntu-latest name: New push to repo steps: - - name: Send data to Slack Workflow Builder + - name: Send GitHub trigger payload to Slack Workflow Builder id: slack - uses: slackapi/slack-github-action@v1.4.0 + uses: slackapi/slack-github-action@v1.13.0 env: SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }} diff --git a/index.js b/index.js index a938ca1..5d4a6c1 100644 --- a/index.js +++ b/index.js @@ -7,35 +7,7 @@ const axios = require('axios'); try { const botToken = process.env.SLACK_BOT_TOKEN; const webhookUrl = process.env.SLACK_WEBHOOK_URL; - - console.log('botToken', botToken, typeof botToken) - console.log('webhookUrl', webhookUrl, typeof webhookUrl) - - let payload = core.getInput('payload'); - console.log('payload:') - console.log(payload) - console.log(typeof payload, payload.length) - - if (payload.length < 1) { - console.log('undefined payload'); - console.log('no payload passed in, using payload that triggered the GitHub Action') - // Get the JSON webhook payload for the event that triggered the workflow - payload = github.context.payload; - } else { - console.log('in else') - try { - // confirm it is valid json - payload = JSON.parse(payload); - console.log('vaid json') - } catch (e) { - // passed in payload wasn't valid json - console.error("passed in payload was invalid") - throw 'Need to provide valid json payload' - } - } - - // console.log(`The event payload: ${JSON.stringify(payload, undefined, 2)}`); if (botToken === undefined && webhookUrl === undefined) { throw 'Need to provide at least one botToken or webhookUrl' @@ -44,9 +16,6 @@ try { if (botToken.length > 0) { const message = core.getInput('slack-message'); const channelId = core.getInput('channel-id'); - console.log('message', message, typeof message) - console.log('channelId', channelId, typeof channelId) - const web = new WebClient(botToken); if(channelId.length > 0 && message.length > 0) { @@ -57,10 +26,25 @@ try { } } - console.log('webhookURL type', typeof webhookUrl) if (typeof webhookUrl !== 'undefined' && webhookUrl.length > 0) { - console.log('flattening'); - // send flat payload to webhookUrl + + if (payload.length < 1) { + // 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) const flatPayload = flatten(payload); // workflow builder requires values to be strings @@ -69,17 +53,19 @@ try { flatPayload[key] = '' + flatPayload[key]; }) - console.log('flattened Payload'); - console.log(flatPayload); - - console.log('sending webhook') - axios.post(webhookUrl, flatPayload) + axios.post(webhookUrl, flatPayload).then(response => { + // Successful post! + }).catch(err => { + console.log("axios post failed, double check the payload being sent includes the keys Slack expects") + console.log(payload) + console.log(err) + throw err + }) } const time = (new Date()).toTimeString(); core.setOutput("time", time); } catch (error) { - console.log('error in big try') core.setFailed(error.message); } diff --git a/package.json b/package.json index b598b4d..4c1e999 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "slack-github-action", - "version": "1.12.0", + "version": "1.13.0", "description": "The official slack github action. Use this to send data into your Slack workspace", "main": "dist/index.js", "scripts": {