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

pre hook seems not working

Hi Support,

I wrote the following hook script in our Bitbucket for one of the repos, making the system check if commit message contains JIRA issue key string. 


import com.atlassian.bitbucket.commit.Commit
import com.atlassian.bitbucket.repository.RefChangeType
import com.atlassian.bitbucket.hook.HookResponse
import com.atlassian.bitbucket.repository.RefChange
import org.apache.commons.lang.StringUtils

println "***************"
def now = new Date()
hookResponse.err().println now
def commits = refChanges.getCommits(repository)


def reg2 = ~/.*(?i)[A-Z]{2,6}-\d+.*/
def reg3 = ~/.*[A-Z]{2,6}-\d+-.*/


def k
for (RefChange refChange : refChanges) {

def branchName = StringUtils.substringAfterLast(refChange.refId, "/")

hookResponse.err().println "bbranch: "+branchName

if (!( branchName ==~ reg3 )){
hookResponse.err().println "branch name format incorrect"
return false

def r1 = branchName.split(/-/)[0]
def r2 = branchName.split(/-/)[1]
println "issue key: "+k
hookResponse.err().println("issue key k="+k)

HookResponse hookResponse = hookResponse
// Loop through commits
for (commit in commits) {
hookResponse.err().println "commit="+commit
hookResponse.err().println "ssize="+commit.getParents().size()
if(commit.getParents().size() > 1)
return true

def message = commit.getMessage()
hookResponse.err().println "commit message="+message

def regexStr = ~/.*(?i)${k}.*/

hookResponse.err().println "regexStr="+regexStr
if (!(message ==~ regexStr)) {
hookResponse.err().println("incorrect match of commit message with JIRA issue key"+regexStr)
return false


hookResponse.err().println "before true"
return true



When I tested push to remote with an invalid commit message, I could see error message "incorrect match of commit message with key..." printed out but the commit is actually successfully pushed to remote repo, which means the hook is not working as designed.

I doubt "return false" after  this line: "hookResponse.err().println("incorrect match of commit message with JIRA issue key"+regexStr)" actually works. Or should I change it to something else. I assume "return false" instead of "return true" should be used in my case - please let me know if I am wrong.

Not sure if I miss something causing the hook ineffective.

Could you please help? 






2 answers

0 votes

This looks like its for ScriptRunner for Bitbucket Server? If so can you tag your question with com.onresolve.jira.stash.groovyrunner then the right people will be able to find it otherwise it will get lost amongst the other Bitbucket Server questions. I have done this for you now.

The simple hook below checks that each commit message contains a valid JIRA issue key and if not adds them to the message when the hook blocks:

import com.onresolve.scriptrunner.canned.bitbucket.util.BitbucketCannedScriptUtils

def commits = refChanges.getCommits(repository)

def issueKeyRegex = ".*[A-Z]{2,9}-\\d+.*"

def sb = new StringBuilder()
commits.each { commit ->

    if(commit.getParents().size() > 1)
        return true
    def commitMessage = commit.getMessage()

    if (!(commitMessage ==~ /${issueKeyRegex}/)) {
        sb << "commit message=$commitMessage\n"
        sb << "regexStr=$issueKeyRegex\n"
        sb << "incorrect match of commit message with JIRA issue key for regex: $issueKeyRegex\n"

if (sb) {
    def msg = BitbucketCannedScriptUtils.wrapHookResponse(sb)
    return false

return true

I think you were quite close.

As for blocking certain branch names I think you would be better off using the "naming standard enforcement" pre-receive hook in addition to this, which takes a regex for checking the branch names. This can be found in Admin -> Script Pre Hooks -> Naming standard enforcement. If you don't specify an error message then the default one should give you the correct reasons the push was declined.

If you have any questions don't hesitate to add a comment.

Hope this helps.


I replaced the whole script with just one single line: 

return false 

and it doesn't even block any push. That indicates to me the problem is not with the script but with something outside it.

Do you have any messages in the logs after the push that indicate the reason? I sometimes see the following warn message that the hook could not be executed, which is a Bitbucket Server issue:

2016-11-03 09:58:49 [http-bio-8080-exec-7] WARN  c.a.s.i.s.g.p.http.HttpReceivePack - PROJECT_1/rep_1[1]: Git hooks have not been called. Please verify that the hooks are configured correctly - see for details

Suggest an answer

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

Atlassian supported Jenkins integration for Bitbucket Server

We’ve been building a plugin to integrate Bitbucket Server and Jenkins CI, and I’m excited to announce that our alpha is ready to download and install. It lets you seamlessly configure a Jenkins job ...

395 views 0 9
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