Require commit messages contain a specific amount of alpha characters

In an effort to encourage helpful commit messages, I'm interested in the best approach to require a specific amount of alpha characters for each commit message.

 

I appreciate the responses - thanks.

1 answer

1 accepted

1 vote

This is possible with the following custom pre-receive hook in ScriptRunner for Bitbucket Server.

You need to configure it by going to: Admin → Script Pre Hooks -> Custom script hook 

It will also display the commit ids that do not have a message with at least 10 alpha characters.

If you need alphanumeric you can replace the regex with /[A-Za-z0-9]/

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 refChanges = refChanges as Collection<RefChange>
def repository = repository as Repository
def hookResponse = hookResponse as HookResponse

def commits = refChanges.getCommits(repository)

def offendingCommits = commits.findAll { commit ->
    if (! commit.message) {
        return true
    }

	def regex = /[A-Za-z]/
    def alphaCharactersCount = (commit.message =~ regex).count

    alphaCharactersCount < 10
}

if (offendingCommits) {
    def msg = new StringBuilder()
    msg << "The following commits should have a message containing at least 10 characters:\n"

    offendingCommits.each { commit ->
        msg << commit.displayId
    }

    hookResponse.out().write(BitbucketCannedScriptUtils.wrapHookResponse(msg))

    return false
}

return true

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

707 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