Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

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

Ehsan khaligh August 10, 2016

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

3 votes
Jonny Carter
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
August 10, 2016

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.

Ehsan khaligh August 11, 2016

@Jonny Carter

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

Jonny Carter
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
August 11, 2016

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.

2 votes
adammarkham
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
August 11, 2016

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 Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events