Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make payload delimiter configurable #281

Merged
merged 6 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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].
Expand All @@ -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 }}
```
Expand Down
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions src/slack-send.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
55 changes: 55 additions & 0 deletions test/slack-send-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down