diff --git a/README.md b/README.md index 5e691924..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: 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.'); } } 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 () => {