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