DEV Community

Ben Mezger (seds)
Ben Mezger (seds)

Posted on • Originally published at seds.nl

Attaching Jira issues to commit

TL;DR: Dynamically attach Jira attributes to commit body using git-hook.
Check the project’s README.

The place I work at requires Jira story ID and task ID attached to the commit body. Initially, I was attaching the ID to the commit body manually, by checking either my previous commit or opening up the Jira board, however, after working some hours I was easily forgetting to attach the IDs to the commit and getting annoying having to either reword them and perhaps having to lookup Jira again.

This was tedious and frustrating, so I wrote a Git hook using pre-commit to handle and install the hook. Our workflow requires the task ID to be attached to the branch same, like so: SKYR-123_branch-description, so Jira is capable of logging commits related to task branches1.
This makes it easy for the hook to know which task are you working on before writing to the commit body. As it checks whether you are in a task branch or any other branch. The Jira ID branch is configurable by specifying a regex for the Jira ID, like so: SKYR-\d+.

Git provides a pre-commit-msg hook, which prepares the default commit message before prompting the user for the commit description/body. To allow extensibility, the hook handles custom Git template with Jinja, so each project may have a custom commit template.

For example, the following template will write the task ID, story ID (if any) and task description.

Task description: {{ summary }}
Jira task ID: {{ key }}
{% if parent__key %}Jira story ID: {{ parent__key }}{% endif %}

The fields are related to Jira’s REST fields. Inner fields such as parent.key should replace the dot (.) with a double underscore (__).

I named the project GJira , as of Git-Jira.

GitHub logo benmezger / gjira

Git hook for adding Jira issues and stories to commit body

CircleCI

GJira

GJira fetches a Jira issue based on the current branch name and appends to the commit body.

GJira allows dynamic branches to be set per project and commit template by using dynamic Jira attributes.

Why?

This came as a requirement from projects I work where makes heavy use of Jira Jira has support for Smart commits which we use in all projects where I work. This allows us to dynamically move cards around depending on their status, and link commits and branches to them.

It's a neat feature for developers and projects managers, as it removes the overhead from developers by having to move cards around manually on each push and gives the project manager an insight of the current development workload.

Requirements

Setup

Git commit template

GJira requires a commit template file. GJira supports Jinja2, which allows customizable templates based on Jira context. For example:

Original post: https://seds.nl/posts/attaching-jira-issues-to-commits/


  1. https://www.atlassian.com/blog/bitbucket/integration-tips-jira-software-bitbucket-server 

Top comments (0)