diff --git a/README.md b/README.md index 6690427b..157863df 100644 --- a/README.md +++ b/README.md @@ -24,11 +24,13 @@ The recommended way to use this action is with Slack's Workflow Builder (if you' > ❗️ This approach requires a paid Slack plan; it also doesn't support any text formatting -This technique sends data into Slack via a webhook URL created using [Slack's Workflow builder](https://slack.com/features/workflow-automation). Follow [these steps to create a Slack workflow using webhooks][create-webhook]. The Slack workflow webhook URL will be in the form `https://hooks.slack.com/workflows/....`. The payload sent by this GitHub action will be flattened (all nested keys moved to the top level) and stringified since Slack's workflow builder only supports top level string values in payloads. +This technique sends data into Slack via a webhook URL created using [Slack's Workflow builder](https://slack.com/features/workflow-automation). Follow [these steps to create a Slack workflow using webhooks][create-webhook]. The Slack workflow webhook URL will be in the form `https://hooks.slack.com/workflows/....`. As part of the [workflow setup](https://slack.com/help/articles/360041352714-Create-more-advanced-workflows-using-webhooks#workflow-setup), you will need to define expected variables in the payload the webhook will receive (described in the "Create custom variables" section of the docs). If these variables are missing in the payload, an error is returned. +To match the webhook input format expected by Workflow Builder, the payload will be flattened and stringified (all nested keys are moved to the top level) before being sent. The default delimiter used to flatten payloads is a period (".") but should be changed to an underscore ("_") using the `payload-delimiter` parameter if you're using nested payloads as input values in your own workflows. + #### Setup * [Create a Slack workflow webhook][create-webhook]. @@ -44,6 +46,8 @@ Add this Action as a [step][job-step] to your project's GitHub Action Workflow f - name: Send GitHub Action trigger data to Slack workflow id: slack uses: slackapi/slack-github-action@v1.26.0 + with: + payload-delimiter: "_" env: SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }} ``` diff --git a/action.yml b/action.yml index a2fcd2a5..30d98dd4 100644 --- a/action.yml +++ b/action.yml @@ -10,6 +10,9 @@ inputs: payload: # JSON payload to send to Slack via webhook description: 'JSON payload to send to Slack if webhook route. If not supplied, json from GitHub event will be sent instead' required: false + payload-delimiter: # custom delimiter used to flatten nested values in the JSON payload + description: 'Custom delimiter used to flatten nested values in the JSON payload. If not supplied, defaults to a period (".").' + required: false payload-file-path: # path to JSON payload to send to Slack via webhook description: 'path to JSON payload to send to Slack if webhook route. If not supplied, json from GitHub event will be sent instead. If payload is provided, it will take preference over this field' required: false diff --git a/src/slack-send.js b/src/slack-send.js index cfe015d1..7c2065e3 100644 --- a/src/slack-send.js +++ b/src/slack-send.js @@ -101,8 +101,9 @@ module.exports = async function slackSend(core) { } if (webhookType === SLACK_WEBHOOK_TYPES.WORKFLOW_TRIGGER) { - // flatten JSON payload (no nested attributes) - const flatPayload = flatten(payload); + // flatten JSON payload (no nested attributes). + const payloadDelimiter = core.getInput('payload-delimiter') || '.'; + const flatPayload = flatten(payload, { delimiter: payloadDelimiter }); // workflow builder requires values to be strings // iterate over every value and convert it to string diff --git a/test/slack-send-test.js b/test/slack-send-test.js index d5e525ee..584cb27b 100644 --- a/test/slack-send-test.js +++ b/test/slack-send-test.js @@ -261,6 +261,61 @@ describe('slack-send', () => { assert(AxiosMock.post.calledWithExactly('https://someurl', payload, {})); }); }); + describe('flatten', () => { + const mockPayload = { + apples: 'tree', + bananas: { truthiness: true }, + }; + beforeEach(() => { + fakeCore.getInput.withArgs('payload').returns(JSON.stringify(mockPayload)); + }); + afterEach(() => { + delete process.env.SLACK_WEBHOOK_TYPE; + }); + it('defaults to using a period to flatten nested paylods', async () => { + process.env.SLACK_WEBHOOK_TYPE = 'WORKFLOW_TRIGGER'; + await slackSend(fakeCore); + const expected = { + apples: 'tree', + 'bananas.truthiness': 'true', + }; + const count = AxiosMock.post.callCount; + assert.equal(count, 1); + const post = AxiosMock.post.getCall(0); + const [url, actual] = post.args; + assert.equal(url, 'https://someurl'); + assert.deepEqual(actual, expected); + }); + it('replaces delimiter with provided payload settings', async () => { + fakeCore.getInput.withArgs('payload-delimiter').returns('_'); + process.env.SLACK_WEBHOOK_TYPE = 'WORKFLOW_TRIGGER'; + await slackSend(fakeCore); + const expected = { + apples: 'tree', + bananas_truthiness: 'true', + }; + const count = AxiosMock.post.callCount; + assert.equal(count, 1); + const post = AxiosMock.post.getCall(0); + const [url, actual] = post.args; + assert.equal(url, 'https://someurl'); + assert.deepEqual(actual, expected); + }); + it('does not flatten nested values of incoming webhook', async () => { + process.env.SLACK_WEBHOOK_TYPE = 'INCOMING_WEBHOOK'; + await slackSend(fakeCore); + const expected = { + apples: 'tree', + bananas: { truthiness: true }, + }; + const count = AxiosMock.post.callCount; + assert.equal(count, 1); + const post = AxiosMock.post.getCall(0); + const [url, actual] = post.args; + assert.equal(url, 'https://someurl'); + assert.deepEqual(actual, expected); + }); + }); }); describe('sad path', () => { it('should set an error if the POST to the webhook fails without a response', async () => {