Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow to have a custom configuration per branches #63

Merged
merged 2 commits into from
Feb 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,19 @@ The plugin can be configured in the [**semantic-release** configuration file](ht
"semantic-release-slack-bot",
{
"notifyOnSuccess": false,
"notifyOnFail": true
"notifyOnFail": false,
"slackWebhook": "https://my-webhook.com",
"branchesConfig": [
{
"pattern": "lts/*",
"notifyOnFail": true
},
{
"pattern": "master1",
"notifyOnSuccess": true,
"notifyOnFail": true
}
]
}
]
]
Expand All @@ -45,8 +57,10 @@ The plugin can be configured in the [**semantic-release** configuration file](ht

With this example:

- Slack notifications are skipped on a successful release
- Slack notifications are sent on a failed release
- Slack notification will always be sent using the "https://my-webhook.com" webhook url
- Slack notifications are sent on a failure release from branches matching "lts/\*"
- Slack notifications are sent on a failure or successful release from branch "master"
- Slack notifications are skipped on all other branches

## Screenshots

Expand Down Expand Up @@ -93,6 +107,7 @@ Alternatively, you could pass the webhook as a configuration option.
| `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 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. | [] |

### Function

Expand Down
15 changes: 9 additions & 6 deletions lib/fail.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* eslint-disable camelcase */
const getConfigToUse = require('./getConfigToUse')
const postMessage = require('./postMessage')
const template = require('./template')

Expand All @@ -9,12 +10,14 @@ module.exports = async (pluginConfig, context) => {
errors,
env: { SEMANTIC_RELEASE_PACKAGE, npm_package_name }
} = context
const { slackWebhook = process.env.SLACK_WEBHOOK, packageName } = pluginConfig

const configToUse = getConfigToUse(pluginConfig, context)
const { slackWebhook = process.env.SLACK_WEBHOOK, packageName } = configToUse

const package_name =
SEMANTIC_RELEASE_PACKAGE || packageName || npm_package_name

if (!pluginConfig.notifyOnFail) {
if (!configToUse.notifyOnFail) {
logger.log('Notifying on fail skipped')
return
}
Expand All @@ -29,10 +32,10 @@ module.exports = async (pluginConfig, context) => {
const repoURL = repoPath && `https://github.com/${repoPath}`

// Override default fail message
if (pluginConfig.onFailFunction) {
slackMessage = pluginConfig.onFailFunction(pluginConfig, context)
} else if (pluginConfig.onFailTemplate) {
slackMessage = template(pluginConfig.onFailTemplate, {
if (configToUse.onFailFunction) {
slackMessage = configToUse.onFailFunction(configToUse, context)
} else if (configToUse.onFailTemplate) {
slackMessage = template(configToUse.onFailTemplate, {
package_name,
repo_path: repoPath,
repo_url: repoURL
Expand Down
14 changes: 14 additions & 0 deletions lib/getConfigToUse.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const micromatch = require('micromatch')

module.exports = (pluginConfig, context) => {
const {
branch: { name }
} = context

const { branchesConfig = [], ...globalPluginConfig } = pluginConfig
const { pattern, ...branchConfig } =
branchesConfig.find(({ pattern }) => micromatch.isMatch(name, pattern)) ||
{}

return { ...globalPluginConfig, ...branchConfig }
}
16 changes: 9 additions & 7 deletions lib/success.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const postMessage = require('./postMessage')
const template = require('./template')
const truncate = require('./truncate')
const getRepoInfo = require('./getRepoInfo')
const getConfigToUse = require('./getConfigToUse')

// 2900 is the limit for a message block of type 'section'.
const MAX_LENGTH = 2900
Expand All @@ -16,17 +17,18 @@ module.exports = async (pluginConfig, context) => {
env: { SEMANTIC_RELEASE_PACKAGE, npm_package_name }
} = context

const configToUse = getConfigToUse(pluginConfig, context)
const {
slackWebhookEnVar = 'SLACK_WEBHOOK',
slackWebhook = process.env[slackWebhookEnVar],
unsafeMaxLength = MAX_LENGTH,
packageName
} = pluginConfig
} = configToUse

const package_name =
SEMANTIC_RELEASE_PACKAGE || packageName || npm_package_name

if (!pluginConfig.notifyOnSuccess) {
if (!configToUse.notifyOnSuccess) {
logger.log('Notifying on success skipped')
return
}
Expand All @@ -37,7 +39,7 @@ module.exports = async (pluginConfig, context) => {

let releaseNotes = nextRelease.notes

if (pluginConfig.markdownReleaseNotes) {
if (configToUse.markdownReleaseNotes) {
// Creating slack format from the markdown notes.
releaseNotes = slackifyMarkdown(releaseNotes)
}
Expand All @@ -49,10 +51,10 @@ module.exports = async (pluginConfig, context) => {

let slackMessage = {}
// Override default success message
if (pluginConfig.onSuccessFunction) {
slackMessage = pluginConfig.onSuccessFunction(pluginConfig, context)
} else if (pluginConfig.onSuccessTemplate) {
slackMessage = template(pluginConfig.onSuccessTemplate, {
if (configToUse.onSuccessFunction) {
slackMessage = configToUse.onSuccessFunction(configToUse, context)
} else if (configToUse.onSuccessTemplate) {
slackMessage = template(configToUse.onSuccessTemplate, {
package_name,
npm_package_version: nextRelease.version,
repo_path: repo.path,
Expand Down
24 changes: 24 additions & 0 deletions lib/verifyConditions.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,28 @@ module.exports = (pluginConfig, context) => {
`A name for the package must be created. Run through npm (npm run <semantic-release-script> to use npm package name or define packageName in the plugin config or \`SEMANTIC_RELEASE_PACKAGE\` in the environment`
)
}

if (
pluginConfig.branchesConfig &&
!Array.isArray(pluginConfig.branchesConfig)
) {
logger.log('branchesConfig is defined and is not an array')
throw new SemanticReleaseError(
'branchesConfig is not an array.',
'EINVALIDBRANCHCONFIG',
`Provided branches configuration is not an array. Ensure "branchesConfig" is properly set in your configuration option.`
)
}

if (
pluginConfig.branchesConfig &&
pluginConfig.branchesConfig.some(({ pattern }) => !pattern)
) {
logger.log('pattern is not defined in branchesConfig')
throw new SemanticReleaseError(
'pattern is not defined in branchesConfig.',
'ENOPATTERN',
`A pattern for the branch configuration must be added. Ensure "branchesConfig" is properly set in your configuration option.`
)
}
}
Loading