Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #219: either use the bot token or the webhook URL #220

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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