Skip to content

Commit

Permalink
feat: add option to specify slack icon (#80)
Browse files Browse the repository at this point in the history
Co-authored-by: Yoshi <ytakahashi@dataminr.com>
  • Loading branch information
yoshitakahashi and Yoshi committed Dec 21, 2021
1 parent aa1eef6 commit 47952b0
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 7 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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. | [] |
Expand Down
7 changes: 5 additions & 2 deletions lib/fail.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -117,6 +119,7 @@ module.exports = async (pluginConfig, context) => {
await postMessage(slackMessage, logger, {
slackWebhook,
slackChannel,
slackToken
slackToken,
slackIcon
})
}
8 changes: 6 additions & 2 deletions lib/getSlackVars.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,18 @@ 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,
slackWebhook,
slackChannelEnVar,
slackChannel,
slackTokenEnVar,
slackToken
slackToken,
slackIconEnVar,
slackIcon
}
}
7 changes: 6 additions & 1 deletion lib/postMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', {
Expand Down
7 changes: 5 additions & 2 deletions lib/success.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -113,6 +115,7 @@ module.exports = async (pluginConfig, context) => {
await postMessage(slackMessage, logger, {
slackWebhook,
slackChannel,
slackToken
slackToken,
slackIcon
})
}

0 comments on commit 47952b0

Please sign in to comment.