gitbitbucketjirabitbucket-serverbitbucket-cloud

How to stop repo:push if webhooks condition fails in BitBucket


I'm trying to stop the repo push in Bitbucket if the commit statement doesn't have the defined Jira issue id in the remark. I have successfully invoked the URL for checking the commit remark via webhooks but unable to figure out the way to stop the push in case of failure.


Solution

  • Customise the following script from this example of a msg-commit git hook.

    #!/bin/bash
    
    MSG="$1"
    
    if ! grep -qE "updated" "$MSG";then
        cat "$MSG"
        echo "Your commit message must contain the word 'updated'"
        exit 1
    fi
    

    Check this code into git, and then run

    # give the script "execute" permission
    chmod 755 commit-msg
    # put the script in the correct place
    cp commit-msg .git/hooks/commit-msg 
    

    If you try and create a commit without the word 'updated' in the message, then this hook will prevent the commit from being created successfully.

    Source: https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks

    The commit-msg hook takes one parameter, which again is the path to a temporary file that contains the commit message written by the developer. If this script exits non-zero, Git aborts the commit process, so you can use it to validate your project state or commit message before allowing a commit to go through. In the last section of this chapter, We’ll demonstrate using this hook to check that your commit message is conformant to a required pattern.

    Note that anyone else who clones your repository will also need to copy the hook into .git/hooks/commit-msg