Skip to content

Commit

Permalink
Fix #219: either use the bot token to post a message, or POST to the …
Browse files Browse the repository at this point in the history
…webhook URL. Do not do both.
  • Loading branch information
filmaj committed Jun 27, 2023
1 parent c70e795 commit 19c39a5
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 12 deletions.
23 changes: 11 additions & 12 deletions src/slack-send.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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) {
Expand Down
12 changes: 12 additions & 0 deletions test/slack-send-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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?');
Expand Down

0 comments on commit 19c39a5

Please sign in to comment.