Skip to content

Instantly share code, notes, and snippets.

@ingmarh
Last active July 27, 2021 08:09
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 ingmarh/1d7b7e23b50cfb9d204f0e2d4bbd944d to your computer and use it in GitHub Desktop.
Save ingmarh/1d7b7e23b50cfb9d204f0e2d4bbd944d to your computer and use it in GitHub Desktop.
Git: Check out automatically named new branch for selected Jira ticket
# Get branch name based on Jira ticket details.
# Output format: $type/$name-$id
#
# - Name is the kebab cased ticket summary,
# excluding special chars and details in braces.
# - Type is determined by the ticket‘s issuetype unless specified by
# the optional second argument (e.g. chore, new, upgrade).
#
# Requires https://github.com/go-jira/jira
#
# Example usage:
# jira-branch-name JIRA-869
# jira-branch-name https://example.atlassian.net/browse/JIRA-869
# jira-branch-name https://example.atlassian.net/browse/JIRA-615 new
function jira-branch-name() {
local id=$(echo $1 | xargs basename)
local details=$(jira view $id)
local name=$(echo "$details" | grep summary | sed -e 's/summary://' -e 's/([^()]*)//g' | tr -dc '[:alnum:][:blank:]' | tr '[:upper:]' '[:lower:]' | xargs basename | paste -sd '-' -)
if [ -n "$2" ]; then
local type=$2
else
local issuetype=$(echo "$details" | grep issuetype | sed 's/issuetype://' | xargs)
case $issuetype in
Bug) local type="fix";;
*) local type="update";;
esac
fi
echo "$type/$name-$id"
}
# Select a Jira ticket and switch to an automatically named new Git branch.
# Optional: Define the branch name‘s type with an argument, e.g. `jb new`.
function jb() {
local id=$(jira ls -q 'status in (Open, "Ready for Dev", "In Progress") AND assignee in (currentUser()) ORDER BY priority DESC' | fzf | cut -d: -f1)
[ -n "$id" ] && git checkout -b $(jira-branch-name $id $1)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment