Skip to content

Instantly share code, notes, and snippets.

@jeffypooo
Created April 6, 2023 18:47
Show Gist options
  • Save jeffypooo/361e7349c8894b0cb41037c991687082 to your computer and use it in GitHub Desktop.
Save jeffypooo/361e7349c8894b0cb41037c991687082 to your computer and use it in GitHub Desktop.
Script to automate checking out a new git branch associated with a JIRA ticket, by parsing the ticket URL
#!/bin/bash
###############################################################################
# Script to create a new git branch for a JIRA ticket, using the ticket URL. #
# #
# Usage: gb-ticket.sh <ticket_url> <desc> #
###############################################################################
# Require parameters and parse them
if [ $# -ne 2 ]; then
echo "Usage: gb-ticket.sh <ticket_url> <desc>"
exit 1
fi
url=$1
desc=$2
# Get username from git config
username=$(git config --get user.name)
# Parse project name and ticket number from URL
regex='^.*\/([A-Z]{3})-([0-9]+)\?.*$'
if [[ $url =~ $regex ]]; then
project=${BASH_REMATCH[1]}
ticket_number=${BASH_REMATCH[2]}
else
echo "Error: Invalid URL"
exit 1
fi
# Derive a new branch using the scheme: <username>/<project>-<ticket_number>/<desc>
branch=$username/$project-$ticket_number/$desc
# Checkout branch and push to remote
git checkout -b $branch && git push -u origin $branch
# Check for errors
if [ $? -eq 0 ]; then
echo "Created branch $branch"
else
echo "Error creating branch $branch"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment