You can now vote for scripts to help people know if they're useful or not. Login or create an account to vote!

1

Post message on Slack when a branch is created

Overview

This script can post a message on a Slack channel, when a branch is created. By default this script will post a notification for any new branch, but you can optionally ignore specific branch prefixes.

Good to Know

In order to setup this script:

Requirements

  • Bitbucket Bitbucket (6.0 - 7.17)
  • ScriptRunner for Bitbucket ScriptRunner for Bitbucket (6.18.0)
    
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) } }
Discovered an issue? Report it here

Suggested for you