It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Forcing Bitbucket pushes to have Jira issue number using custom groovy Script

Hi Jamie Echlin

We would like to require our users to enter jira issue number when they commit before they push the local repository in bitbucket. I looked at the Atlassian JAVADOC API and I found these packages ( to use. Also I found this groovy from Scriptrunner website which is similar what we want. 
import com.atlassian.bitbucket.hook.HookResponse 
import com.atlassian.bitbucket.repository.RefChange
import com.atlassian.bitbucket.repository.Repository

Repository repository = repository

Collection<refchange> refChanges = refChanges
HookResponse hookResponse = hookResponse

if ( == "test") {
hookResponse.out().print("You cannot commit at the moment to any repository named 'test'\n")
return false
return true

Could you please tell me how I can modify this in order to require users to enter jira issue number when they want push to bitbucket?

2 answers

The documentation on pre-receive hooks notes a built-in hook that matches what you're looking for. You'll provide a JQL query that defines what issues are valid, and the script will require that the JIRA issue be present in the branch name or commit message.

There's similar (and simpler) built-in merge check that can require JIRA issues as well.

@Jonny Carter

Is there a way to do with custom groovy Script that does not required to link Bitbucket link to JIRA? 

There are ways. 

On the simpler side, you could plausibly just use a regular expression to validate that the commit message matches to a JIRA issue in some way.

If you actually want to pull information from the JIRA instance for your commit hook, you're going to have to use some other means of communication between the two, such as REST calls. Otherwise, how would Bitbucket get info about the JIRA instance? That's very likely going to be more complicated than creating the application link, and debatably worth it. I think I'd just hard-code a regex and have done if it were me.

Jonny is right here that you either need to use the built in pre-receive hook with an application link setup for JIRA or you need to use a regex to check the commit message contains a JIRA issue key. There is no other way.

The following script checks that the commit message starts with a JIRA issue key:

import com.atlassian.bitbucket.hook.HookResponse
import com.atlassian.bitbucket.repository.RefChange
import com.atlassian.bitbucket.repository.Repository
import com.onresolve.scriptrunner.canned.bitbucket.util.BitbucketCannedScriptUtils

def repository = repository as Repository
def refChanges = refChanges as Collection&lt;RefChange&gt;
def hookResponse = hookResponse as HookResponse

def commits = refChanges.getCommits(repository)

def commitsWithoutJiraKey = commits.findAll { commit -&gt;
    def regex = "[A-Z]{2,9}-\\d+.*"
    !(commit.message ==~ /${regex}/)

if ( == "rep_1" &amp;&amp; commitsWithoutJiraKey) {
    def msg = new StringBuilder()
    msg &lt;&lt; "Following commits have no JIRA key in their message!\n"
    commitsWithoutJiraKey.each { commit -&gt;
        msg &lt;&lt; "$commit.displayId\n"
    return false

return true

You should add this as a custom pre-receive hook.

Suggest an answer

Log in or Sign up to answer
Community showcase
Published in Bitbucket

Powering DevOps with Bitbucket Server & Data Center

Hi everyone, The Cloud team recently announced 12 new DevOps features that help developers ship better code, faster   ! While we’re all excited about the new improvements to Bitbucket ...

709 views 0 4
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you