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 (https://developer.atlassian.co...https://developer.atlassian.co... 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 (repository.name == "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?
Thanks

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.

1 votes

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 (repository.name == "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"
    }
    hookResponse.out().print(BitbucketCannedScriptUtils.wrapHookResponse(msg))
    return false
}

return true

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

Suggest an answer

Log in or Join to answer
Community showcase
Piotr Plewa
Published Dec 27, 2017 in Bitbucket

Recipe: Deploying AWS Lambda functions with Bitbucket Pipelines

Bitbucket Pipelines helps me manage and automate a number of serverless deployments to AWS Lambda and this is how I do it. I'm building Node.js Lambda functions using node-lambda&nbsp...

672 views 0 4
Read article

Atlassian User Groups

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

Find a group

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

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you
Atlassian Team Tour

Join us on the Team Tour

We're bringing product updates and pro tips on teamwork to ten cities around the world.

Save your spot