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

How to Enforce Naming Standards for Commit Message?

trent-dailey December 30, 2016
 

5 answers

1 accepted

2 votes
Answer accepted
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.
January 5, 2017

You can do this with ScriptRunner for Bitbucket Server by going to Admin -> Script Pre Hooks -> Protect git refs and configure it like in the screenshot below:

Screen Shot 2017-01-05 at 12.00.36.png

The condition is:

def commits = refChanges.getCommits(repository)

def regex = "[A-Z]{2,9}-\\d+\\s[A-Za-z]+.*"

commits*.message.any { message ->
   	!(message  ==~ /${regex}/)
}

Note that this won't work if you configure it from the repository settings menu for Script Pre Hooks due to security issues we limit what you can use in the condition.

You can change the error message to what you like.

If you require to set the error message dynamically you can use something like the following under Admin -> Script Pre Hooks -> Custom script hook:

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<RefChange>
def hookResponse = hookResponse as HookResponse

def msg = new StringBuilder()

def commits = refChanges.getCommits(repository)

def regex = "[A-Z]{2,9}-\\d+\\s[A-Za-z]+.*"

commits*.message.each { message ->
    if (!(message  ==~ /${regex}/)) {
        msg << "$message does not match $regex"
    }
}

if (msg) {
    hookResponse.out().write BitbucketCannedScriptUtils.wrapHookResponse(msg)
    // block push as commits don't match regex
    return false
}

// allow push
return true

Note that we are working on SRBITB-7 which in a few weeks when we release will allow you to dynamically set the error message in the first script also without you requiring to write all the code in the second example.

Let us know how you get on with this.

Esther Strom November 30, 2017

I know this is an older question, but we just started looking into doing this. We use Bitbucket Server for viewing and merging pull requests, but our commits and pushes are done via git bash. Will this solution work? Or does it only work if all git work is done via Bitbucket?

Jinisha Thacker August 30, 2018

-&gt is not working for me in script in script-runner prehook

Like Vignesh Kumar likes this
Dani July 2, 2020

Late reply but I think -> is erroneously converted in the snippet example above so it should be replaced by the following character instead:

->

Richard W June 15, 2021

When I put in the condition code above with the changes for the character encoding, I get the following error:

 

rg.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
Script103.groovy: 5: [Static type checking] - No such property: message for class: java.lang.Iterable 
 @ line 5, column 1.
   commits.message.any { message ->

 Just in case I didn't substitute correctly, here is the snippet

def commits = refChanges.getCommits(repository)

def regex = "[A-Z]{2,9}-\\d+\\s[A-Za-z]+.*"

commits*.message.any { message ->
!(message ==~ /${regex}/)
}

0 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.
January 5, 2017

@Trent Dailey thanks for confirming. I have provided a solution for you.

siddhesh.harmalkar July 6, 2020

Hi Adammarkham,

We have following requirement,

1. Comment should start with
2. apha numeric upper case - min 2 char (for example for CM)
3. hyphen Mandatory
4. Number (0-9) - Atleast 1 digit
5. No spaces around hyphen.
5.  :(colon) is compulsory after jira story number
6. Space required after jira story number. And atleast 1 word comment afterward

can you suggest the regex format.

Thanks

0 votes
trent-dailey January 3, 2017

We are attempting to use scriptrunner to get this completed.  What we are looking for is:
1. Comment should start with 
2. apha numeric upper case - min 2 char (for example for CM)
3. hyphen Mandatory
4. Number (0-9) - Atleast 1 digit
5. No spaces around hyphen.
6. Space required after jira story number. And atleast 1 word comment afterward.

ex. CM-1234 Adding pom changes

0 votes
Niek Neuij January 2, 2017

I use this add-on:

https://marketplace.atlassian.com/plugins/com.isroot.stash.plugin.yacc/server/overview

You can use regex or it validates against a JQL query, or both.

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.
January 2, 2017

Apologies I automatically assumed you were talking about ScriptRunner for Bitbucket Server as that has something similar. Maybe mention that plugin in the question tags and you'll be more likely to get an answer from the right people.

Like Daniel Mowlam likes this
Niek Neuij January 2, 2017

Good morning, I was responding to Trent Daily, he's the OP.

0 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.
January 2, 2017

Could you tell us what naming standard you want the commit message to match and I will put an example together for you?

You'll likely get a faster response if you tag your question with: com.onresolve.stash.groovy.groovyrunner in the future. I've just done this for you.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events