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

Enforcing Jira ticket numbers for commits on selected branches

kamuzz July 10, 2024

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? 

3 answers

1 vote
Aron Gombas _Midori_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
July 10, 2024

@kamuzz We have a custom merge check which:

  1. ... can verify the Jira issue keys mentioned in the commit messages. (Even better, check if they are matching a custom JQL search, so you can restrict the valid issue keys to a project, status, etc. -- it is very powerful!)
  2. ... support excluding branches using a regex pattern. (Basically you can configure a pattern that matches every other branch but those where you want the check to be "active". It is also very flexible!)

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

kamuzz July 10, 2024

@Aron Gombas _Midori_ Thanks for that information.  I'll try out the suggestions below and come back to you if required. 

Like Aron Gombas _Midori_ likes this
0 votes
Saxea _Flowie_
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.
July 10, 2024

Hi @kamuzz

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.

0 votes
Kuğbe July 10, 2024

Hi @kamuzz 

Enforcing Jira ticket numbers for commits on specific branches:

First:

  • Create a custom merge check plugin using Atlassian SDK
  • You can implement the [ Merge Check ] interface
  • Verify commit message against a Jira pattern.
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:

  • Place the script in '.git/hook/pre-commit'.
  • Make it executable.
#!/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
  1. Add Script to '.git/hooks/pre-commit'.
  2. Run 'chmod +x .git/hooks/pre-commit'.

These methods ensure that commits to specified branches include Jira ticket numbers.

 

Aron Gombas _Midori_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
July 10, 2024

This will not work on Cloud!

Your answer is applicable only to self-hosted Jira deployments.

Kuğbe July 10, 2024

For cloud based environment, you can use Bitbucket Pipelines with a custom script.

  1. Create a bitbucket-pipelines.yml file in your repository
    This file defines the steps and scripts to be executed in your CI/CD pipeline

example:

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.

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
PREMIUM
PERMISSIONS LEVEL
Product Admin
TAGS
AUG Leaders

Atlassian Community Events