Skip to content

Instantly share code, notes, and snippets.

@jeffypooo
Created April 6, 2023 18:47
Show Gist options
  • Select an option

  • Save jeffypooo/361e7349c8894b0cb41037c991687082 to your computer and use it in GitHub Desktop.

Select an option

Save jeffypooo/361e7349c8894b0cb41037c991687082 to your computer and use it in GitHub Desktop.

Revisions

  1. jeffypooo created this gist Apr 6, 2023.
    40 changes: 40 additions & 0 deletions gb-ticket.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,40 @@
    #!/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