Skip to main content
Example scripts
arrow icon
To homepage
Bitbucket
Data centre icon
Data Center

Prevent Push If Commit Message Does Not Match Pattern

Features
Pre hooks
Tags
Created 1 year ago, Updated 8 day(s) ago
App in script
ScriptRunner For Bitbucket
ScriptRunner For Bitbucket
by Adaptavist
Compatibility
compatibility bullet
Bitbucket (6.3 - 7.12)
compatibility bullet
ScriptRunner For Bitbucket (6.11.0)
Language |
groovy
import com.atlassian.bitbucket.hook.repository.CommitAddedDetails
import com.atlassian.bitbucket.hook.repository.PreRepositoryHookCommitCallback
import com.atlassian.bitbucket.hook.repository.RepositoryHookCommitFilter
import com.atlassian.bitbucket.hook.repository.RepositoryHookResult

import javax.annotation.Nonnull

def requiredCommitMessagePattern = ~/BUGFIX:.*/
commitCallback = new PreRepositoryHookCommitCallback() {

        @Override
        boolean onCommitAdded(@Nonnull CommitAddedDetails commitDetails) {
            def commit = commitDetails.commit

            if (!(commit.message ==~ requiredCommitMessagePattern)) {
                def msg = "Commit message: (${commit.message}) does not match required pattern: $requiredCommitMessagePattern"
                resultBuilder.veto(msg, msg)

                return false
            }

            true
        }

        @Override
        RepositoryHookResult getResult() {
            resultBuilder.build()
        }
    }

commitFilters << RepositoryHookCommitFilter.ADDED_TO_ANY_REF
RepositoryHookResult.accepted()
Having an issue with this script?
Report it here