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]
k=r1+"-"+r2
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("size="+commit.getParents().size())
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("fine")

}

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? 

Thanks,

Jueli

 

 

 

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)
    hookResponse.out().print(msg)
    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.

Adam

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 https://confluence.atlassian.com/display/BITBUCKETSERVERKB/Git+hook+scripts+are+not+executing for details

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...

625 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