Enforcing Jira ticket numbers for commits on selected branches
Asked by Karen:

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?


Answers:
author: Aron Gombas _Midori_
body: [@Karen](/t5/user/viewprofilepage/user-id/560783) 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](https://marketplace.atlassian.com/apps/1213415/better-commit-policy-for-jira-git-svn?hosting=datacenter&tab=overview) 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.)*

comments: author: Karen
body: [@Aron Gombas _Midori_](/t5/user/viewprofilepage/user-id/502447) Thanks for that information. I'll try out the suggestions below and come back to you if required.


author: Saxea _Flowie_
body: Hi [@Karen](/t5/user/viewprofilepage/user-id/560783)

We provide a Bitbucket cloud [addon](https://marketplace.atlassian.com/apps/1231101) for workflow automation that supports merge checks based on branches. You can use the [commit-policy](https://flowie.app/docs/en/plugins/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.


author: Ku?be
body: Hi [@Karen](/t5/user/viewprofilepage/user-id/560783)   

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.

comments: author: Aron Gombas _Midori_
body: This will *not* work on Cloud!

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


author: Ku?be
body: 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
```

<br />

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.


Tags: bitbucket-cloud

cloud