feat(message): support multiple channel IDs (#126)

* feat(message): support multiple channelIds
* test(postMessage): assert comma-separated channels
* docs: multiple ID's example
This commit is contained in:
Tregan
2022-09-13 14:29:04 -04:00
committed by GitHub
parent fd0446916a
commit 4707dbc272
3 changed files with 24 additions and 9 deletions
+2 -1
View File
@@ -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:
+10 -8
View File
@@ -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.');
}
}
+12
View File
@@ -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 () => {