Skip to content

Commit b452451

Browse files
rzumerzimeg
andauthored
feat: make the payload delimiter configurable for workflow webhook triggers (#281)
Co-authored-by: @zimeg <[email protected]>
1 parent c50e848 commit b452451

File tree

4 files changed

+66
-3
lines changed

4 files changed

+66
-3
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,13 @@ The recommended way to use this action is with Slack's Workflow Builder (if you'
2424

2525
> ❗️ This approach requires a paid Slack plan; it also doesn't support any text formatting
2626
27-
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.
27+
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/....`.
2828

2929
As part of the [workflow setup](https://slack.com/help/articles/360041352714-Create-more-advanced-workflows-using-webhooks#workflow-setup),
3030
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.
3131

32+
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.
33+
3234
#### Setup
3335

3436
* [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
4446
- name: Send GitHub Action trigger data to Slack workflow
4547
id: slack
4648
uses: slackapi/[email protected]
49+
with:
50+
payload-delimiter: "_"
4751
env:
4852
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
4953
```

action.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ inputs:
1010
payload: # JSON payload to send to Slack via webhook
1111
description: 'JSON payload to send to Slack if webhook route. If not supplied, json from GitHub event will be sent instead'
1212
required: false
13+
payload-delimiter: # custom delimiter used to flatten nested values in the JSON payload
14+
description: 'Custom delimiter used to flatten nested values in the JSON payload. If not supplied, defaults to a period (".").'
15+
required: false
1316
payload-file-path: # path to JSON payload to send to Slack via webhook
1417
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'
1518
required: false

src/slack-send.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,9 @@ module.exports = async function slackSend(core) {
101101
}
102102

103103
if (webhookType === SLACK_WEBHOOK_TYPES.WORKFLOW_TRIGGER) {
104-
// flatten JSON payload (no nested attributes)
105-
const flatPayload = flatten(payload);
104+
// flatten JSON payload (no nested attributes).
105+
const payloadDelimiter = core.getInput('payload-delimiter') || '.';
106+
const flatPayload = flatten(payload, { delimiter: payloadDelimiter });
106107

107108
// workflow builder requires values to be strings
108109
// iterate over every value and convert it to string

test/slack-send-test.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,61 @@ describe('slack-send', () => {
261261
assert(AxiosMock.post.calledWithExactly('https://someurl', payload, {}));
262262
});
263263
});
264+
describe('flatten', () => {
265+
const mockPayload = {
266+
apples: 'tree',
267+
bananas: { truthiness: true },
268+
};
269+
beforeEach(() => {
270+
fakeCore.getInput.withArgs('payload').returns(JSON.stringify(mockPayload));
271+
});
272+
afterEach(() => {
273+
delete process.env.SLACK_WEBHOOK_TYPE;
274+
});
275+
it('defaults to using a period to flatten nested paylods', async () => {
276+
process.env.SLACK_WEBHOOK_TYPE = 'WORKFLOW_TRIGGER';
277+
await slackSend(fakeCore);
278+
const expected = {
279+
apples: 'tree',
280+
'bananas.truthiness': 'true',
281+
};
282+
const count = AxiosMock.post.callCount;
283+
assert.equal(count, 1);
284+
const post = AxiosMock.post.getCall(0);
285+
const [url, actual] = post.args;
286+
assert.equal(url, 'https://someurl');
287+
assert.deepEqual(actual, expected);
288+
});
289+
it('replaces delimiter with provided payload settings', async () => {
290+
fakeCore.getInput.withArgs('payload-delimiter').returns('_');
291+
process.env.SLACK_WEBHOOK_TYPE = 'WORKFLOW_TRIGGER';
292+
await slackSend(fakeCore);
293+
const expected = {
294+
apples: 'tree',
295+
bananas_truthiness: 'true',
296+
};
297+
const count = AxiosMock.post.callCount;
298+
assert.equal(count, 1);
299+
const post = AxiosMock.post.getCall(0);
300+
const [url, actual] = post.args;
301+
assert.equal(url, 'https://someurl');
302+
assert.deepEqual(actual, expected);
303+
});
304+
it('does not flatten nested values of incoming webhook', async () => {
305+
process.env.SLACK_WEBHOOK_TYPE = 'INCOMING_WEBHOOK';
306+
await slackSend(fakeCore);
307+
const expected = {
308+
apples: 'tree',
309+
bananas: { truthiness: true },
310+
};
311+
const count = AxiosMock.post.callCount;
312+
assert.equal(count, 1);
313+
const post = AxiosMock.post.getCall(0);
314+
const [url, actual] = post.args;
315+
assert.equal(url, 'https://someurl');
316+
assert.deepEqual(actual, expected);
317+
});
318+
});
264319
});
265320
describe('sad path', () => {
266321
it('should set an error if the POST to the webhook fails without a response', async () => {

0 commit comments

Comments
 (0)