Skip to content

Commit

Permalink
feat: add option to specify slack app name (#83)
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 Jan 5, 2022
1 parent 83b4699 commit 89ee876
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 20 deletions.
22 changes: 13 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,11 @@ With this example:
#### Note

[The official documentation](https://api.slack.com/messaging/webhooks) says that a developer cannot
override the default channel or icon that are associated with the webhook, but some users of this
library reported that they were able to.
override the default name, channel or icon that are associated with the webhook, but some users of
this library reported that they were able to.

- use `slackIcon` (or `SLACK_ICON` env var) for overriding slack icon
- use `slackName` (or `SLACK_NAME` env var) for overriding slack app name
- use `slackIcon` (or `SLACK_ICON` env var) for overriding slack app icon
- use `slackChannel` (or `SLACK_CHANNEL` env var) for overriding slack channel

**WARNING: This is not mentioned in the official documentation, so use at your own risk.**
Expand Down Expand Up @@ -153,6 +154,7 @@ Alternatively, you can pass the webhook as a configuration option or use an Acce
| `SLACK_TOKEN` | Slack bot Access token. |
| `SLACK_CHANNEL` | Slack channel name or id to send notifications to. |
| `SLACK_ICON` | Slack bot app icon. |
| `SLACK_NAME` | Slack bot app name. |
| `SEMANTIC_RELEASE_PACKAGE` | Override or add package name instead of npm package name |

### Options
Expand All @@ -168,12 +170,14 @@ Alternatively, you can pass the webhook as a configuration option or use an Acce
| `markdownReleaseNotes` | Pass release notes through markdown to slack formatter before rendering. | false |
| `slackWebhookEnVar` | This decides what the environment variable for exporting the slack webhook value. | SLACK_WEBHOOK |
| `slackWebhook` | Slack webhook created when adding app to workspace. | value of the environment variable matching `slackWebhookEnVar` |
| `slackTokenEnVar` | This decides what the environment variable for exporting the slack token value. | SLACK_WEBHOOK |
| `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` |
| `slackTokenEnVar` | This decides what the environment variable for exporting the slack token value. | SLACK_TOKEN |
| `slackToken` | Slack bot token. | value of the environment variable matching `slackTokenEnVar` |
| `slackChannelEnVar` | This decides what the environment variable for exporting the slack channel value. | SLACK_CHANNEL |
| `slackChannel` | Slack channel name or id to send notifications to. | value of the environment variable matching `slackChannelEnVar` |
| `slackIconEnVar` | This decides what the environment variable for specifying the slack app icon with slack emoji. ex. `:smile:` or `smile` (without semicolons) | SLACK_ICON |
| `slackIcon` | Slack app icon | value of the environment variable matching `slackIconEnVar` |
| `slackNameEnVar` | This decides what the environment variable for specifying the slack app name | SLACK_NAME |
| `slackName` | Slack app name | value of the environment variable matching `slackNameEnVar` |
| `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
13 changes: 9 additions & 4 deletions lib/fail.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,13 @@ module.exports = async (pluginConfig, context) => {

const configToUse = getConfigToUse(pluginConfig, context)
const { packageName } = configToUse
const { slackWebhook, slackToken, slackChannel, slackIcon } = getSlackVars(
configToUse
)
const {
slackWebhook,
slackToken,
slackChannel,
slackIcon,
slackName
} = getSlackVars(configToUse)

const package_name =
SEMANTIC_RELEASE_PACKAGE || packageName || npm_package_name
Expand Down Expand Up @@ -120,6 +124,7 @@ module.exports = async (pluginConfig, context) => {
slackWebhook,
slackChannel,
slackToken,
slackIcon
slackIcon,
slackName
})
}
8 changes: 6 additions & 2 deletions lib/getSlackVars.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ module.exports = config => {
slackChannelEnVar = 'SLACK_CHANNEL',
slackChannel = process.env[slackChannelEnVar],
slackIconEnVar = 'SLACK_ICON',
slackIcon = process.env[slackIconEnVar]
slackIcon = process.env[slackIconEnVar],
slackNameEnVar = 'SLACK_NAME',
slackName = process.env[slackNameEnVar]
} = config
return {
slackWebhookEnVar,
Expand All @@ -17,6 +19,8 @@ module.exports = config => {
slackTokenEnVar,
slackToken,
slackIconEnVar,
slackIcon
slackIcon,
slackNameEnVar,
slackName
}
}
6 changes: 5 additions & 1 deletion lib/postMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const SemanticReleaseError = require('@semantic-release/error')
module.exports = async (
message,
logger,
{ slackWebhook, slackToken, slackChannel, slackIcon }
{ slackWebhook, slackToken, slackChannel, slackIcon, slackName }
) => {
let response
let bodyText
Expand All @@ -14,6 +14,10 @@ module.exports = async (
message['icon_emoji'] = hasSemicolons ? slackIcon : `:${slackIcon}:`
}

if (slackName) {
message.username = slackName
}

if (slackToken && slackChannel) {
message.channel = slackChannel
response = await fetch('https://slack.com/api/chat.postMessage', {
Expand Down
13 changes: 9 additions & 4 deletions lib/success.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,13 @@ module.exports = async (pluginConfig, context) => {

const configToUse = getConfigToUse(pluginConfig, context)
const { unsafeMaxLength = MAX_LENGTH, packageName } = configToUse
const { slackWebhook, slackToken, slackChannel, slackIcon } = getSlackVars(
configToUse
)
const {
slackWebhook,
slackToken,
slackChannel,
slackIcon,
slackName
} = getSlackVars(configToUse)

const package_name =
SEMANTIC_RELEASE_PACKAGE || packageName || npm_package_name
Expand Down Expand Up @@ -116,6 +120,7 @@ module.exports = async (pluginConfig, context) => {
slackWebhook,
slackChannel,
slackToken,
slackIcon
slackIcon,
slackName
})
}

0 comments on commit 89ee876

Please sign in to comment.