Skip to content

Instantly share code, notes, and snippets.

@jkereako
Last active August 6, 2022 01:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jkereako/dc71f5301faefb4fdd047003ed03beac to your computer and use it in GitHub Desktop.
Save jkereako/dc71f5301faefb4fdd047003ed03beac to your computer and use it in GitHub Desktop.
Opens the current branch name in JIRA.
#!/usr/bin/env bash
#
# JIRA
# Copyright (c) 2018 - Jeff Kereakoglow
#
# Opens the current branch name in JIRA.
set -Eeuo pipefail
readonly COMPANY_NAME="yourdamncompanyname"
# Print out error messages to stderr.
function err() {
echo -e "\033[0;31mERROR: $@\033[0m" >&2;
}
function check_git() {
if ! git rev-parse --git-dir 1> /dev/null; then
err "Current directory is not tracked with Git"; exit 2;
fi;
}
function check_http_status() {
status=$(curl -s -o /dev/null -w "%{http_code}" "${url}")
# Don't attempt to open invalid URLs
if (( $status >= 400 && $status <= 599 )); then
err "URL ${url} cannot be found or JIRA is down"; exit 2;
fi
}
#-- Main
function main() {
args="$@"
check_git
base_url="https://${COMPANY_NAME}.atlassian.net/browse"
branch=$(git symbolic-ref --short HEAD)
# Branches often have prefixes like "feature/" or "bugfix/". Executing
# `basename` on the branch solves this problem.
ticket_id=$(basename $branch)
url="${base_url}/${ticket_id}"
check_http_status $url
echo "Opening ${url}"
open $url
}
main "$@"
@jkereako
Copy link
Author

jkereako commented Aug 3, 2022

Description

Opens the current branch name in JIRA.

First, the script verifies that the current directory tree is tracked with Git. If so, it verifies that Jira is reachable. If so, it opens Jira in your default browser.

Usage

It's meant to be simple:

$ jira
Opening https://jira.yourdamncompanyname.com/browse/JIRA-1234

Installation

Download the script and make it executable (chmod +x jira). I recommend to place this script in a subdirectory of home named bin. Create the directory bin and then edit .zshrc to add it to your path:

#-- Paths
path=(
    ~/bin 
    $path
)

Requirements

  1. The directory must be tracked in Git.
  2. The last path component of your branch must include the ticket ID. The following examples are will work with the script
    JIRA-1234
    JIRA-1234-my-new-haircut (Jira is smart enough to parse-out the ticket ID)
    feature/vinnybagadoughnuts/JIRA-1234

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment