Skip to content

Commit

Permalink
feat: allows a custom environment variable name for Slack webhook (#36)
Browse files Browse the repository at this point in the history
This is particularly useful when using CircleCI and their `context` system.
In our organisation, the context has a default SLACK_WEBHOOK value but we need to override it for some repositories (but still use the context).

Since in CircleCI the context override custom environment variables we need this parameter to use another variable.
  • Loading branch information
KillianHmyd authored and juliuscc committed Mar 23, 2020
1 parent 87ab8bb commit a8bec27
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 13 deletions.
21 changes: 11 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,16 +80,17 @@ Alternatively, you could pass the webhook as a configuration option.

### Options

| Option | Description | Default |
| ---------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------- |
| `notifyOnSuccess` | Determines if a succesfull release should trigger a slack message to be sent. If `false` this plugin does nothing on success. | false |
| `notifyOnFail` | Determines if a failed release should trigger a slack message to be sent. If `false` this plugin does nothing on fail. | false |
| `onSuccessTemplate` | Provides a template for the slack message object on success when `notifyOnSuccess` is `true`. See [templating](#templating). | undefined |
| `onFailTemplate` | Provides a template for the slack message object on fail when `notifyOnFail` is `true`. See [templating](#templating). | undefined |
| `markdownReleaseNotes` | Pass release notes through markdown to slack formatter before rendering. | false |
| `slackWebhook` | Slack webhook created when adding app to workspace. | SLACK_WEBHOOK |
| `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 maxLength is too high, messages can be dropped. [Read here](https://github.com/juliuscc/semantic-release-slack-bot/issues/26#issuecomment-569804359) for more information. | 2900 |
| Option | Description | Default |
| ---------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------- |
| `notifyOnSuccess` | Determines if a succesfull release should trigger a slack message to be sent. If `false` this plugin does nothing on success. | false |
| `notifyOnFail` | Determines if a failed release should trigger a slack message to be sent. If `false` this plugin does nothing on fail. | false |
| `onSuccessTemplate` | Provides a template for the slack message object on success when `notifyOnSuccess` is `true`. See [templating](#templating). | undefined |
| `onFailTemplate` | Provides a template for the slack message object on fail when `notifyOnFail` is `true`. See [templating](#templating). | undefined |
| `markdownReleaseNotes` | Pass release notes through markdown to slack formatter before rendering. | false |
| `slackWebhookEnVar` | This decides what the environment variable for exporting the slack webhook is called. | SLACK_WEBHOOK |
| `slackWebhook` | Slack webhook created when adding app to workspace. | value of the environment variable matching `slackWebhookEnVar` |
| `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 maxLength is too high, messages can be dropped. [Read here](https://github.com/juliuscc/semantic-release-slack-bot/issues/26#issuecomment-569804359) for more information. | 2900 |

### Templating

Expand Down
3 changes: 2 additions & 1 deletion lib/success.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ module.exports = async (pluginConfig, context) => {
env: { SEMANTIC_RELEASE_PACKAGE, npm_package_name }
} = context
const {
slackWebhook = process.env.SLACK_WEBHOOK,
slackWebhookEnVar = 'SLACK_WEBHOOK',
slackWebhook = process.env[slackWebhookEnVar],
unsafeMaxLength = MAX_LENGTH,
packageName
} = pluginConfig
Expand Down
7 changes: 5 additions & 2 deletions lib/verifyConditions.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@ const SemanticReleaseError = require('@semantic-release/error')

module.exports = (pluginConfig, context) => {
const { logger } = context
const { slackWebhook = process.env.SLACK_WEBHOOK } = pluginConfig
const {
slackWebhookEnVar = 'SLACK_WEBHOOK',
slackWebhook = process.env[slackWebhookEnVar]
} = pluginConfig

if (!slackWebhook) {
logger.log('SLACK_WEBHOOK has not been defined.')
throw new SemanticReleaseError(
'No Slack web-hook defined.',
'ENOSLACKHOOK',
`A Slack Webhook must be created and set in the \`SLACK_WEBHOOK\` environment variable on your CI environment.\n\n\nPlease make sure to create a Slack Webhook and to set it in the \`SLACK_WEBHOOK\` environment variable on your CI environment. Alternatively, provide \`slackWebhook\` as a configuration option.`
`A Slack Webhook must be created and set in the \`${slackWebhookEnVar}\` environment variable on your CI environment.\n\n\nPlease make sure to create a Slack Webhook and to set it in the \`${slackWebhookEnVar}\` environment variable on your CI environment. Alternatively, provide \`slackWebhook\` as a configuration option.`
)
}

Expand Down
64 changes: 64 additions & 0 deletions test/test_verifyConditions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
const SemanticReleaseError = require('@semantic-release/error')
const assert = require('assert')

const verifyConditions = require('../lib/verifyConditions')

describe('test verifyConditions', () => {
beforeEach(() => {
delete process.env.SLACK_WEBHOOK
})

describe('test slack webhooks options', () => {
const fakeContext = { logger: { log: () => undefined }, env: {} }
const defaultPluginConfig = { packageName: 'test' }
const getMissingWebhookError = slackWebhookEnVar => {
return `A Slack Webhook must be created and set in the \`${slackWebhookEnVar}\` environment variable on your CI environment.\n\n\nPlease make sure to create a Slack Webhook and to set it in the \`${slackWebhookEnVar}\` environment variable on your CI environment. Alternatively, provide \`slackWebhook\` as a configuration option.`
}

it('should throw if neither slackWebhook and environment variable SLACK_WEBHOOK are set', () => {
assert.throws(
() => verifyConditions(defaultPluginConfig, fakeContext),
new SemanticReleaseError(
'No Slack web-hook defined.',
'ENOSLACKHOOK',
getMissingWebhookError('SLACK_WEBHOOK')
)
)
})

it('should throw if slackWebhook is not set and slackWebhookEnVar give an empty environment variable', () => {
assert.throws(
() =>
verifyConditions(
{ ...defaultPluginConfig, slackWebhookEnVar: 'CUSTOM_WEBHOOK' },
fakeContext
),
new SemanticReleaseError(
'No Slack web-hook defined.',
'ENOSLACKHOOK',
getMissingWebhookError('CUSTOM_WEBHOOK')
)
)
})

it('should not throw if slackWebhook is provided', () => {
verifyConditions(
{ ...defaultPluginConfig, slackWebhook: 'MY SLACK WEBHOOK' },
fakeContext
)
})

it('should not throw if SLACK_WEBHOOK is set', () => {
process.env.SLACK_WEBHOOK = 'MY SLACK WEBHOOK'
verifyConditions(defaultPluginConfig, fakeContext)
})

it('should not throw if slackWebhookEnVar give an environment variable', () => {
process.env.CUSTOM_WEBHOOK = 'MY SLACK WEBHOOK'
verifyConditions(
{ ...defaultPluginConfig, slackWebhookEnVar: 'CUSTOM_WEBHOOK' },
fakeContext
)
})
})
})

0 comments on commit a8bec27

Please sign in to comment.