From ee8e3137e4a1d785305a7b8e92bb6d7808908b2b Mon Sep 17 00:00:00 2001 From: Tregan Date: Thu, 8 Sep 2022 15:00:57 +0200 Subject: [PATCH 1/6] feat(message): support multiple channelIds --- src/slack-send.js | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/slack-send.js b/src/slack-send.js index 98dbf561..5bb66438 100644 --- a/src/slack-send.js +++ b/src/slack-send.js @@ -60,25 +60,27 @@ module.exports = async function slackSend(core) { if (typeof botToken !== 'undefined' && botToken.length > 0) { const message = core.getInput('slack-message') || ''; - const channelId = core.getInput('channel-id') || ''; + const channelIds = core.getInput('channel-id') || ''; const web = new WebClient(botToken); - if (channelId.length <= 0) { + if (channelIds.length <= 0) { console.log('Channel ID is required to run this action. An empty one has been provided'); throw new Error('Channel ID is required to run this action. An empty one has been provided'); } if (message.length > 0 || payload) { const ts = core.getInput('update-ts'); - if (ts) { + await Promise.all(channelIds.split(',').map(async (channelId) => { + if (ts) { // update message - webResponse = await web.chat.update({ ts, channel: channelId, text: message, ...(payload || {}) }); - } else { + webResponse = await web.chat.update({ ts, channel: channelId.trim(), text: message, ...(payload || {}) }); + } else { // post message - webResponse = await web.chat.postMessage({ channel: channelId, text: message, ...(payload || {}) }); - } + webResponse = await web.chat.postMessage({ channel: channelId.trim(), text: message, ...(payload || {}) }); + } + })); } else { - console.log('Missing slack-message or payload! Did not send a message via chat.postMessage with botToken', { channel: channelId, text: message, ...(payload) }); + console.log('Missing slack-message or payload! Did not send a message via chat.postMessage with botToken', { channel: channelIds, text: message, ...(payload) }); throw new Error('Missing message content, please input a valid payload or message to send. No Message has been send.'); } } From ff21d56f96b0bc49fade9a2b905e8ae002fa76e4 Mon Sep 17 00:00:00 2001 From: Tregan Date: Tue, 13 Sep 2022 19:41:44 +0200 Subject: [PATCH 2/6] test(postMessage): assert comma-separated channels --- test/slack-send-test.js | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/test/slack-send-test.js b/test/slack-send-test.js index 98c6b3b9..0556ded5 100644 --- a/test/slack-send-test.js +++ b/test/slack-send-test.js @@ -56,16 +56,19 @@ describe('slack-send', () => { describe('happy path', () => { it('should send a message using the postMessage API', async () => { fakeCore.getInput.withArgs('slack-message').returns('who let the dogs out?'); - fakeCore.getInput.withArgs('channel-id').returns('C123456'); + fakeCore.getInput.withArgs('channel-id').returns('C123456,C987654'); await slackSend(fakeCore); assert.equal(fakeCore.setOutput.firstCall.firstArg, 'ts', 'Output name set to ts'); assert.equal(fakeCore.setOutput.secondCall.firstArg, 'thread_ts', 'Output name set to thread_ts'); assert(fakeCore.setOutput.secondCall.lastArg.length > 0, 'Time output a non-zero-length string'); assert.equal(fakeCore.setOutput.lastCall.firstArg, 'time', 'Output name set to time'); assert(fakeCore.setOutput.lastCall.lastArg.length > 0, 'Time output a non-zero-length string'); - const chatArgs = ChatStub.postMessage.lastCall.firstArg; - assert.equal(chatArgs.channel, 'C123456', 'Correct channel provided to postMessage'); - assert.equal(chatArgs.text, 'who let the dogs out?', 'Correct message provided to postMessage'); + const firstChatArgs = ChatStub.postMessage.firstCall.firstArg; + const secondChatArgs = ChatStub.postMessage.lastCall.firstArg; + assert.oneOf('C123456', [firstChatArgs.channel, secondChatArgs.channel], 'First comma-separated channel provided to postMessage'); + assert.oneOf('C987654', [firstChatArgs.channel, secondChatArgs.channel], 'Second comma-separated channel provided to postMessage'); + assert.equal(firstChatArgs.text, 'who let the dogs out?', 'Correct message provided to postMessage with first comma-separated channel'); + assert.equal(secondChatArgs.text, 'who let the dogs out?', 'Correct message provided to postMessage with second comma-separated channel'); }); it('should send a message using the update API', async () => { From d3c2eabbd8734586cdc80c2a9e22a9313dc6ab7c Mon Sep 17 00:00:00 2001 From: Tregan Date: Tue, 13 Sep 2022 19:45:50 +0200 Subject: [PATCH 3/6] docs: multiple ID's example --- README.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/README.md b/README.md index 5e691924..23a334b0 100644 --- a/README.md +++ b/README.md @@ -124,6 +124,22 @@ Using JSON payload for constructing a message is also available: SLACK_BOT_TOKEN: ${{ secrets.SLACK_BOT_TOKEN }} ``` +Multiple channels can be sent the same message by providing a comma-separated list of ID's: + +```yaml +- name: Post to a Slack channel + id: slack + uses: slackapi/slack-github-action@v1.21.0 + with: + # Slack channel id, channel name, or user id to post message. + # See also: https://api.slack.com/methods/chat.postMessage#channels + channel-id: "CHANNEL_ID_123,CHANNEL_ID_987" + # For posting a simple plain text message + slack-message: "GitHub build result: ${{ job.status }}\n${{ github.event.pull_request.html_url || github.event.head_commit.url }}" + env: + SLACK_BOT_TOKEN: ${{ secrets.SLACK_BOT_TOKEN }} +``` + #### Update the message If you would like to notify the real-time updates on a build status, you can modify the message your build job posted in the subsequent steps. In order to do this, the steps after the first message posting can have `update-ts: ${{ steps.slack.outputs.ts }}` in their settings. With this, the step updates the already posted channel message instead of posting a new one. From 42c748e4238d5ec45a2c67943d7e8afd1dc17056 Mon Sep 17 00:00:00 2001 From: Tregan Date: Tue, 13 Sep 2022 20:08:23 +0200 Subject: [PATCH 4/6] docs: remove multiple ID section --- README.md | 19 ++----------------- 1 file changed, 2 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 23a334b0..5b044146 100644 --- a/README.md +++ b/README.md @@ -89,7 +89,8 @@ Add this Action as a [step][job-step] to your project's GitHub Action Workflow f with: # Slack channel id, channel name, or user id to post message. # See also: https://api.slack.com/methods/chat.postMessage#channels - channel-id: 'CHANNEL_ID' + # You can pass in multiple channels to post to by providing a comma-delimited list of channel IDs. + channel-id: 'CHANNEL_ID,ANOTHER_CHANNEL_ID' # For posting a simple plain text message slack-message: "GitHub build result: ${{ job.status }}\n${{ github.event.pull_request.html_url || github.event.head_commit.url }}" env: @@ -124,22 +125,6 @@ Using JSON payload for constructing a message is also available: SLACK_BOT_TOKEN: ${{ secrets.SLACK_BOT_TOKEN }} ``` -Multiple channels can be sent the same message by providing a comma-separated list of ID's: - -```yaml -- name: Post to a Slack channel - id: slack - uses: slackapi/slack-github-action@v1.21.0 - with: - # Slack channel id, channel name, or user id to post message. - # See also: https://api.slack.com/methods/chat.postMessage#channels - channel-id: "CHANNEL_ID_123,CHANNEL_ID_987" - # For posting a simple plain text message - slack-message: "GitHub build result: ${{ job.status }}\n${{ github.event.pull_request.html_url || github.event.head_commit.url }}" - env: - SLACK_BOT_TOKEN: ${{ secrets.SLACK_BOT_TOKEN }} -``` - #### Update the message If you would like to notify the real-time updates on a build status, you can modify the message your build job posted in the subsequent steps. In order to do this, the steps after the first message posting can have `update-ts: ${{ steps.slack.outputs.ts }}` in their settings. With this, the step updates the already posted channel message instead of posting a new one. From df3efce8efa0cbbf748e9c77b4ab776ea807383e Mon Sep 17 00:00:00 2001 From: Tregan Date: Tue, 13 Sep 2022 20:16:20 +0200 Subject: [PATCH 5/6] Revert "test(postMessage): assert comma-separated channels" This reverts commit ff21d56f96b0bc49fade9a2b905e8ae002fa76e4. --- test/slack-send-test.js | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/test/slack-send-test.js b/test/slack-send-test.js index 0556ded5..98c6b3b9 100644 --- a/test/slack-send-test.js +++ b/test/slack-send-test.js @@ -56,19 +56,16 @@ describe('slack-send', () => { describe('happy path', () => { it('should send a message using the postMessage API', async () => { fakeCore.getInput.withArgs('slack-message').returns('who let the dogs out?'); - fakeCore.getInput.withArgs('channel-id').returns('C123456,C987654'); + fakeCore.getInput.withArgs('channel-id').returns('C123456'); await slackSend(fakeCore); assert.equal(fakeCore.setOutput.firstCall.firstArg, 'ts', 'Output name set to ts'); assert.equal(fakeCore.setOutput.secondCall.firstArg, 'thread_ts', 'Output name set to thread_ts'); assert(fakeCore.setOutput.secondCall.lastArg.length > 0, 'Time output a non-zero-length string'); assert.equal(fakeCore.setOutput.lastCall.firstArg, 'time', 'Output name set to time'); assert(fakeCore.setOutput.lastCall.lastArg.length > 0, 'Time output a non-zero-length string'); - const firstChatArgs = ChatStub.postMessage.firstCall.firstArg; - const secondChatArgs = ChatStub.postMessage.lastCall.firstArg; - assert.oneOf('C123456', [firstChatArgs.channel, secondChatArgs.channel], 'First comma-separated channel provided to postMessage'); - assert.oneOf('C987654', [firstChatArgs.channel, secondChatArgs.channel], 'Second comma-separated channel provided to postMessage'); - assert.equal(firstChatArgs.text, 'who let the dogs out?', 'Correct message provided to postMessage with first comma-separated channel'); - assert.equal(secondChatArgs.text, 'who let the dogs out?', 'Correct message provided to postMessage with second comma-separated channel'); + const chatArgs = ChatStub.postMessage.lastCall.firstArg; + assert.equal(chatArgs.channel, 'C123456', 'Correct channel provided to postMessage'); + assert.equal(chatArgs.text, 'who let the dogs out?', 'Correct message provided to postMessage'); }); it('should send a message using the update API', async () => { From 3f0d59717fbd734036d40b3de2b906b326493c2c Mon Sep 17 00:00:00 2001 From: Tregan Date: Tue, 13 Sep 2022 20:21:18 +0200 Subject: [PATCH 6/6] test(postMessage): assert comma-separated channels --- test/slack-send-test.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/test/slack-send-test.js b/test/slack-send-test.js index 98c6b3b9..b6838f56 100644 --- a/test/slack-send-test.js +++ b/test/slack-send-test.js @@ -105,6 +105,18 @@ describe('slack-send', () => { assert.equal(chatArgs.oliver, 'benji', 'Correct message provided to postMessage'); assert.equal(chatArgs.actor, 'user123', 'Correct message provided to postMessage'); }); + + it('should send the same message to multiple channels', async () => { + fakeCore.getInput.withArgs('slack-message').returns('who let the dogs out?'); + fakeCore.getInput.withArgs('channel-id').returns('C123456,C987654'); + await slackSend(fakeCore); + const firstChatArgs = ChatStub.postMessage.firstCall.firstArg; + const secondChatArgs = ChatStub.postMessage.lastCall.firstArg; + assert.oneOf('C123456', [firstChatArgs.channel, secondChatArgs.channel], 'First comma-separated channel provided to postMessage'); + assert.oneOf('C987654', [firstChatArgs.channel, secondChatArgs.channel], 'Second comma-separated channel provided to postMessage'); + assert.equal(firstChatArgs.text, 'who let the dogs out?', 'Correct message provided to postMessage with first comma-separated channel'); + assert.equal(secondChatArgs.text, 'who let the dogs out?', 'Correct message provided to postMessage with second comma-separated channel'); + }); }); describe('sad path', () => { it('should set an error if payload cannot be JSON parsed', async () => {