From 47952b0d6c7a79398b299ab787e0e274b51567ee Mon Sep 17 00:00:00 2001 From: Yoshi Takahashi <12172895+yoshitakahashi@users.noreply.github.com> Date: Tue, 21 Dec 2021 01:35:44 -0500 Subject: [PATCH] feat: add option to specify slack icon (#80) Co-authored-by: Yoshi --- README.md | 2 ++ lib/fail.js | 7 +++++-- lib/getSlackVars.js | 8 ++++++-- lib/postMessage.js | 7 ++++++- lib/success.js | 7 +++++-- 5 files changed, 24 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index ffe436a..52da0fe 100644 --- a/README.md +++ b/README.md @@ -160,6 +160,8 @@ Alternatively, you can pass the webhook as a configuration option or use an Acce | `slackToken` | Slack bot token. | value of the environment variable matching `slackWebhookEnVar` | | `slackChannelEnVar` | This decides what the environment variable for exporting the slack channel value. | SLACK_WEBHOOK | | `slackChannel` | Slack channel name or id to send notifications to. | value of the environment variable matching `slackWebhookEnVar` | +| `slackIconEnVar` | This decides what the environment variable for specifying the slack bot icon with slack emoji. ex. `:smile:` or `smile` (without semicolons) | SLACK_ICON | +| `slackIcon` | Slack bot icon | value of the environment variable matching `slackIconEnVar` | | `packageName` | Override or add package name instead of npm package name | SEMANTIC_RELEASE_PACKAGE or npm package name | | `unsafeMaxLength` | Maximum character length for the release notes before truncation. If unsafeMaxLength is too high, messages can be dropped. [Read here](https://github.com/juliuscc/semantic-release-slack-bot/issues/26#issuecomment-569804359) for more information. Set to '0' to turn off truncation entirely. | 2900 | | `branchesConfig` | Allow to specify a custom configuration for branches which match a given pattern. For every branches matching a branch config, the config will be merged with the one put at the root. A key "pattern" used to filter the branch using glob expression must be contained in every branchesConfig. | [] | diff --git a/lib/fail.js b/lib/fail.js index ac9b0ad..fdb237f 100644 --- a/lib/fail.js +++ b/lib/fail.js @@ -14,7 +14,9 @@ module.exports = async (pluginConfig, context) => { const configToUse = getConfigToUse(pluginConfig, context) const { packageName } = configToUse - const { slackWebhook, slackToken, slackChannel } = getSlackVars(configToUse) + const { slackWebhook, slackToken, slackChannel, slackIcon } = getSlackVars( + configToUse + ) const package_name = SEMANTIC_RELEASE_PACKAGE || packageName || npm_package_name @@ -117,6 +119,7 @@ module.exports = async (pluginConfig, context) => { await postMessage(slackMessage, logger, { slackWebhook, slackChannel, - slackToken + slackToken, + slackIcon }) } diff --git a/lib/getSlackVars.js b/lib/getSlackVars.js index aa18a79..9c37bd7 100644 --- a/lib/getSlackVars.js +++ b/lib/getSlackVars.js @@ -5,7 +5,9 @@ module.exports = config => { slackTokenEnVar = 'SLACK_TOKEN', slackToken = process.env[slackTokenEnVar], slackChannelEnVar = 'SLACK_CHANNEL', - slackChannel = process.env[slackChannelEnVar] + slackChannel = process.env[slackChannelEnVar], + slackIconEnVar = 'SLACK_ICON', + slackIcon = process.env[slackIconEnVar] } = config return { slackWebhookEnVar, @@ -13,6 +15,8 @@ module.exports = config => { slackChannelEnVar, slackChannel, slackTokenEnVar, - slackToken + slackToken, + slackIconEnVar, + slackIcon } } diff --git a/lib/postMessage.js b/lib/postMessage.js index 16cc774..4671dfd 100644 --- a/lib/postMessage.js +++ b/lib/postMessage.js @@ -4,11 +4,16 @@ const SemanticReleaseError = require('@semantic-release/error') module.exports = async ( message, logger, - { slackWebhook, slackToken, slackChannel } + { slackWebhook, slackToken, slackChannel, slackIcon } ) => { let response let bodyText try { + if (slackIcon) { + const hasSemicolons = slackIcon.startsWith(':') && slackIcon.endsWith(':') + message['icon_emoji'] = hasSemicolons ? slackIcon : `:${slackIcon}:` + } + if (slackToken && slackChannel) { message.channel = slackChannel response = await fetch('https://slack.com/api/chat.postMessage', { diff --git a/lib/success.js b/lib/success.js index 25ac3a8..e22d9d1 100644 --- a/lib/success.js +++ b/lib/success.js @@ -20,7 +20,9 @@ module.exports = async (pluginConfig, context) => { const configToUse = getConfigToUse(pluginConfig, context) const { unsafeMaxLength = MAX_LENGTH, packageName } = configToUse - const { slackWebhook, slackToken, slackChannel } = getSlackVars(configToUse) + const { slackWebhook, slackToken, slackChannel, slackIcon } = getSlackVars( + configToUse + ) const package_name = SEMANTIC_RELEASE_PACKAGE || packageName || npm_package_name @@ -113,6 +115,7 @@ module.exports = async (pluginConfig, context) => { await postMessage(slackMessage, logger, { slackWebhook, slackChannel, - slackToken + slackToken, + slackIcon }) }