Skip to content

Commit

Permalink
feat: allow to have a custom configuration per branches
Browse files Browse the repository at this point in the history
  • Loading branch information
KillianHmyd committed Jan 28, 2021
1 parent 41f7dee commit 987879b
Show file tree
Hide file tree
Showing 10 changed files with 449 additions and 105 deletions.
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 every 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
7 changes: 6 additions & 1 deletion 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,7 +10,11 @@ module.exports = async (pluginConfig, context) => {
errors,
env: { SEMANTIC_RELEASE_PACKAGE, npm_package_name }
} = context
const { slackWebhook = process.env.SLACK_WEBHOOK, packageName } = pluginConfig

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

const package_name =
SEMANTIC_RELEASE_PACKAGE || packageName || npm_package_name
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 }
}
3 changes: 2 additions & 1 deletion 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 @@ -21,7 +22,7 @@ module.exports = async (pluginConfig, context) => {
slackWebhook = process.env[slackWebhookEnVar],
unsafeMaxLength = MAX_LENGTH,
packageName
} = pluginConfig
} = getConfigToUse(pluginConfig, context)

const package_name =
SEMANTIC_RELEASE_PACKAGE || packageName || npm_package_name
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. Make "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. Make "branchesConfig" is properly set in your configuration option.`
)
}
}
Loading

0 comments on commit 987879b

Please sign in to comment.