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

feat(message): support multiple channel IDs #126

Merged
merged 6 commits into from
Sep 13, 2022
Merged
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
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
18 changes: 10 additions & 8 deletions src/slack-send.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 || {}) });
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, good call to trim it here.

} 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.');
}
}
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 @@ -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 () => {
Expand Down