• Skip to primary navigation
  • Skip to main content
Sal Ferrarello
  • About Sal Ferrarello
  • Speaking
  • Connect
    Mastodon GitHub Twitter (inactive)
You are here: Home / Dev Tips / Git alias to open Jira URL

Git alias to open Jira URL

Last updated on January 29, 2021 by Sal Ferrarello

As a developer, I spend a lot of time going back and forth between the command line and Jira tickets. I realized that since I always include the Jira ticket ID (e.g. sf-123) in the branch name (in a reliable position), I could write a git alias to open the corresponding URL. This is how I implemented this behavior.

My Branch Naming Convention

I name all of my branches using the following convention.

{type}/{jiraId}-{description}

e.g.

  • feature/sf-123-add-new-button
  • bug/tmnt-987-remove-infinite-loop
  • chore/sf-456-remove-copyright-infringement-in-example

Opening the Jira URL from the Command Line

Assuming your Jira sub-domain is ferrarello, running the following command on the command line will open the Jira URL.

open https://ferrarello.atlassian.net/browse/$(git rev-parse --abbrev-ref HEAD | sed 's/^.*\/\([a-zA-Z]\{1,\}-[0-9]\{1,\}\).*/\1/')

You can use this line by replacing ferrarello with your own Jira sub-domain.

Creating a Git Alias

You can create a git alias (git jira) to do this for you. Once again, you’ll want to replace ferrarello with your own Jira sub-domain.

By running the following line, you’ll add the command we just created as a git alias in your global gitconfig file (usually found at ~/.gitconfig).

git config --global alias.jira '!f() { open https://ferrarello.atlassian.net/browse/$(git rev-parse --abbrev-ref HEAD | sed '"'"'s/^.*\/\([a-zA-z]\{1,\}-[0-9]\{1,\}\).*/\1/'"'"'); }; f'

Now when you run git jira, it will parse the current branch name looking for the Jira ID and use it to open the Jira URL.

How it Works

Get the Current Branch

On the command line, I can get the current branch name with

git rev-parse --abbrev-ref HEAD

Extracting the Jira ID

Once I have a branch name (like feature/sf-123-add-new-button), I can use sed with a regular expression to extract the Jira ID.

For example

echo 'feature/sf-123-add-new-button' | sed 's/^.*\/\([a-zA-Z]\{1,\}-[0-9]\{1,\}\).*/\1/'

The output of this example should be sf-123

Extracting the Jira ID from the Current Branch

Here we get the current branch and pass it into sed with the regular expression to extract the ID.

git rev-parse --abbrev-ref HEAD | sed 's/^.*\/\([a-zA-Z]\{1,\}-[0-9]\{1,\}\).*/\1/'

Opening the URL

Since the Jira instance has a consistent URL format of

https://ferrarello.atlassian.net/browse/{jiraId}

if the Jira ID is sf-123, I know the URL will be https://ferrarello.atlassian.net/browse/sf-123

By prepending this URL with open we can trigger the default browser.

open https://ferrarello.atlassian.net/browse/sf-123

Extracting the Jira ID and Opening the URL

Here we open the URL https://ferrarello.atlassian.net/browse/{jiraId} where jiraId is the result of our earlier command to extract the Jira ID from the current branch.

open https://ferrarello.atlassian.net/browse/$(git rev-parse --abbrev-ref HEAD | sed 's/^.*\/\([a-zA-Z]\{1,\}-[0-9]\{1,\}\).*/\1/')

How Does the Regular Expression Work

echo 'feature/sf-123-add-new-button' | sed 's/^.*\/\([a-zA-Z]\{1,\}-[0-9]\{1,\}\).*/\1/'

The sed command takes our string 'feature/sf-123-add-new-button' and applies the regular expression

s/^.*\/\([a-zA-Z]\{1,\}-[0-9]\{1,\}\).*/\1/

This is a substitution (s/{from}/{to}/)

Our from clause with the escaping backslashes removed is

`^.*/([a-zA-Z]{1,}-[0-9]{1,}).*

which says

  • starting at the beginning of the string ^
  • find any characters up to the first forward slash .*/
  • start a capture group (
    • followed by one or more letters in a row [a-zA-z]{1,}
    • followed by a dash -
    • followed by one or more digits in a row [0-9]{1,}
  • stop the capture group
  • find any characters after that in the line .*

Our to clause says replace everything in the from clause with

  • just the content of the capture group \1

Extended Git Solution

In my collection of Git enhancements, I’m adding a more flexible solution that uses a custom Git config value (salcode.jiraSubdomain) to create the Jira URL, see Issue 18: Add ‘git jira’ command

This has the advantage that in addition to a global value for the Jira URL, I can set a different Jira sub-domain in the project level Git config (e.g. if I have a project that uses a different Jira instance/sub-domain).

Sal Ferrarello
Sal Ferrarello (@salcode)
Sal is a PHP developer with a focus on the WordPress platform. He is a conference speaker with a background including Piano Player, Radio DJ, Magician/Juggler, Beach Photographer, and High School Math Teacher. Sal can be found professionally at WebDevStudios, where he works as a senior backend engineer.

Share this post:

Share on TwitterShare on FacebookShare on LinkedInShare on EmailShare on Reddit
Warning! This is a draft, not a finalized post. See full draft disclosure.

Filed Under: Dev Tips, Draft, Solution Tagged With: Git, git alias, gitconfig, Jira, regular expressions

Reader Interactions

Comments

  1. Chrispian says

    July 22, 2020 at 8:00 pm

    Kudos for using sed. I don’t use them nearly enough to have them memorized but sed/awk are both super useful. I remember a while back having to write a shell script to do a secure login and needed to use sed/awk and expect but it’s been so long I can’t remember why lol.

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Copyright © 2023 · Bootstrap4 Genesis on Genesis Framework · WordPress · Log in