diff --git a/src/slack-send.js b/src/slack-send.js index 4ef5b26b..d662834f 100644 --- a/src/slack-send.js +++ b/src/slack-send.js @@ -83,13 +83,21 @@ module.exports = async function slackSend(core) { webResponse = await web.chat.postMessage({ channel: channelId.trim(), text: message, ...(payload || {}) }); } })); + // TODO: I suppose if multiple channels are provided, the output here will return only one of the messages' + // data, not all of them... + if (webResponse && webResponse.ok) { + core.setOutput('ts', webResponse.ts); + // return the thread_ts if it exists, if not return the ts + const thread_ts = webResponse.thread_ts ? webResponse.thread_ts : webResponse.ts; + core.setOutput('thread_ts', thread_ts); + // return id of the channel from the response + core.setOutput('channel_id', webResponse.channel); + } } else { 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.'); } - } - - if (typeof webhookUrl !== 'undefined' && webhookUrl.length > 0) { + } else if (typeof webhookUrl !== 'undefined' && webhookUrl.length > 0) { if (!payload) { // No Payload was passed in console.log('no custom payload was passed in, using default payload that triggered the GitHub Action'); @@ -142,15 +150,6 @@ module.exports = async function slackSend(core) { } } - if (webResponse && webResponse.ok) { - core.setOutput('ts', webResponse.ts); - // return the thread_ts if it exists, if not return the ts - const thread_ts = webResponse.thread_ts ? webResponse.thread_ts : webResponse.ts; - core.setOutput('thread_ts', thread_ts); - // return id of the channel from the response - core.setOutput('channel_id', webResponse.channel); - } - const time = (new Date()).toTimeString(); core.setOutput('time', time); } catch (error) { diff --git a/test/slack-send-test.js b/test/slack-send-test.js index e47d8d51..a970231d 100644 --- a/test/slack-send-test.js +++ b/test/slack-send-test.js @@ -53,6 +53,18 @@ describe('slack-send', () => { process.env.SLACK_BOT_TOKEN = 'xoxb-xxxxx'; delete process.env.SLACK_WEBHOOK_URL; }); + describe('but also setting a webhook URL', () => { + const fakeWebhookURL = 'https://hooks.slack.com/somethingsomething'; + beforeEach(() => { + process.env.SLACK_WEBHOOK_URL = fakeWebhookURL; + }); + it('should not POST to the webhook URL if using bot token / providing channel-id', async () => { + fakeCore.getInput.withArgs('slack-message').returns('who let the dogs out?'); + fakeCore.getInput.withArgs('channel-id').returns('C123456'); + await slackSend(fakeCore); + assert(!AxiosMock.post.calledWith(fakeWebhookURL), 'Webhook URL should not be called'); + }); + }); describe('happy path', () => { it('should send a message using the postMessage API', async () => { fakeCore.getInput.withArgs('slack-message').returns('who let the dogs out?');