Example scripts
To homepage
Bitbucket

Post message on Slack when a branch is created
App in script

ScriptRunner For Bitbucket
by Adaptavist
Compatibility

Bitbucket (6.0 - 7.17)

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