Create git branch from Jira issue summary
By chimo on (updated on )We use Jira to track issues/tasks at work. I got tired of switching to the browser to refer to (or copy, paste and edit) the Jira issue summary to give my git branches meaningful names.
I wrote a quick script that:
- Takes a Jira issue key as an input
(ex: TARDIS-1234). - Gets my Jira credentials from my password manager
(rbw). - Calls the Jira API to retrieve the Jira issue “Summary”
(ex: “Implement Time-Traveling”). - Turns the summary into a slug
(thanks, github.com/Mayeu/slugify) - Creates and switches to a new branch
(ex: `git checkout -b tardis-1234-implement-time-traveling`).
#!/bin/sh -eu
# https://github.com/Mayeu/slugify/blob/master/slugify#L24
to_slug() {
# Forcing the POSIX local so alnum is only 0-9A-Za-z
export LANG=POSIX
export LC_ALL=POSIX
# Keep only alphanumeric value
sed -e 's/[^[:alnum:]]/-/g' |
# Keep only one dash if there is multiple one consecutively
tr -s '-' |
# Lowercase everything
tr A-Z a-z |
# Remove last dash if there is nothing after
sed -e 's/-$//'
}
issue_key="${1}"
issue_key=$(echo "${issue_key}" | tr A-Z a-z)
username=$(rbw get -f username jira)
password=$(rbw get -f password jira)
jira_api_root="https://example.org/rest/api/2"
issue_summary=$(
curl -s -u "${username}:${password}" --request GET \
--url "${jira_api_root}/issue/${issue_key}?fields=summary" \
--header "Accept: application/json" \
| jq .fields.summary -r | to_slug
)
git checkout -b "${issue_key}-${issue_summary}"
I might call this from a git-alias in the future. Right now it’s just a script in my $PATH.