Skip to main content
Example scripts
arrow icon
To homepage
Bitbucket
Data centre icon
Data Center

Post message on Slack when a branch is created

Features
Post hooks
Created 1 year ago, Updated 5 month(s) ago
App in script
ScriptRunner For Bitbucket
ScriptRunner For Bitbucket
by Adaptavist
Compatibility
compatibility bullet
Bitbucket (6.0 - 7.17)
compatibility bullet
ScriptRunner For Bitbucket (7.10.0)
Language |
groovy
import com.atlassian.bitbucket.repository.RefChangeType
import com.atlassian.bitbucket.repository.StandardRefType
import com.onresolve.scriptrunner.parameters.annotation.ShortTextInput
import com.onresolve.scriptrunner.slack.SlackUtil

@ShortTextInput(label = 'Slack connection', description = "The Slack Connection name, that matches the 'Connection Name' field set in your SR resources.")
String slackConnection

@ShortTextInput(label = 'Slack channel', description = 'The Slack channel where this message will be posted.')
String slackChannel

/**
 * A list of branch prefixes that should be ignored.
 * For example, if you add 'dev' as a prefix to ignore, the script will not post a message when a branch named 'dev/brc-123' is created
 */
@ShortTextInput(label = 'Ignore branch prefixes', description = 'The branch prefixes that should be ignored, separated by commas.')
String ignorePrefixesStr
String[] ignorePrefixes = ignorePrefixesStr.trim().split("\\s*,\\s*")

refChanges.each {
    if (it.type == RefChangeType.ADD && it.ref.type == StandardRefType.BRANCH &&
        !ignorePrefixes.find { type -> it.ref.id.startsWith("refs/heads/${type}/") }) {

        String msg = "New branch created: $it.ref.displayId"
        SlackUtil.message(slackConnection, slackChannel, msg)
    }
}
Having an issue with this script?
Report it here