Can anyone recommend a way to enforce Jira ticket numbers for commits into certain branches.
The default option enforces ticks on all commits in the entire repository, which doesn't work for us.
I've seen similar questions but no real answers.
Has anyone got this working by building a custom merge check?
@Karen We have a custom merge check which:
This is actually part of the Cloud version of the popular Better Commit Policy app and we're using it internally. If you are interested, I will ask around if it is possible to invite you to the app. Interested?
(Discl. it is the cloud version of an existing paid and supported app developed by our team.)
@Aron Gombas _Midori_ Thanks for that information. I'll try out the suggestions below and come back to you if required.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @Karen
We provide a Bitbucket cloud addon for workflow automation that supports merge checks based on branches. You can use the commit-policy plugin with a REGEX to enforce the ticket number in the commit message. Currently, it doesn't validate the ticket against Jira, but we have plans to add a rule for that as well.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @Karen
Enforcing Jira ticket numbers for commits on specific branches:
First:
public class JiraTicketMergeCheck implements MergeRequestCheck {
private static final Pattern JIRA_TICKET_PATTERN = Pattern.compile("^[A-Z]+-\\d+");
@Override
public boolean onReceive(MergeRequestCheckContext context) {
if (isSelectedBranch(context.getMergeRequest().getToRef().getDisplayId())) {
return context.getCommitMessages().stream()
.allMatch(msg -> JIRA_TICKET_PATTERN.matcher(msg).find());
}
return true;
}
private boolean isSelectedBranch(String branchName) {
return branchName.equals("main") || branchName.startsWith("release/");
}
}
Git Hooks:
Create a pre-commit Hook:
#!/bin/bash
branch=$(git rev-parse --abbrev-ref HEAD)
enforced_branches=("main" "release/*")
for enforced_branch in "${enforced_branches[@]}"; do
if [[ $branch == $enforced_branch || $branch == $enforced_branch* ]]; then
commit_message=$(git log -1 --pretty=%B)
if ! [[ $commit_message =~ [A-Z]+-[0-9]+ ]]; then
echo "Error: Commit message must contain a Jira ticket number (e.g., PROJECT-123)."
exit 1
fi
fi
done
These methods ensure that commits to specified branches include Jira ticket numbers.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
This will not work on Cloud!
Your answer is applicable only to self-hosted Jira deployments.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
For cloud based environment, you can use Bitbucket Pipelines with a custom script.
bitbucket-pipelines.yml
file in your repositoryexample:
pipelines:
default:
- step:
name: "Check Jira Ticket in Commit Messages"
script:
- ./scripts/check-jira-ticket.sh
- Create the script check-jira-ticket.sh
in a scripts
directory similar to a hook.
#!/bin/bash
# Define the branches where Jira ticket enforcement is needed
enforced_branches=("main" "release/")
# Get the branch name from the environment variable
branch=$BITBUCKET_BRANCH
# Check if the current branch is in the list of enforced branches
for enforced_branch in "${enforced_branches[@]}"; do
if [[ $branch == $enforced_branch* ]]; then
# Get the commit messages in the push
commit_messages=$(git log origin/$branch..HEAD --pretty=format:%s)
# Check each commit message
for message in $commit_messages; do
if ! [[ $message =~ [A-Z]+-[0-9]+ ]]; then
echo "Error: Commit message '$message' must contain a Jira ticket number (e.g., PROJECT-123)."
exit 1
fi
done
fi
done
then use chmod against the script file to change permission:
chmod +x scripts/check-jira-ticket.sh
These doc should help:
Bitbucket Pipelines Documentation: https://support.atlassian.com/bitbucket-cloud/docs/configure-your-first-pipeline/
Bitbucket Pipelines Configuration: https://support.atlassian.com/bitbucket-cloud/docs/bitbucket-pipelines-configuration-reference/
Git Commit Message Guidelines: https://support.atlassian.com/bitbucket-cloud/docs/use-smart-commits/
Note: modifications maybe need to be made in the scripts relating to the variable...etc but I truly hope this helps.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.