Automate the boring stuff: New feature branches

Repitition sucks. Repeating the same things manually over and over again. All of this boring, unnecessary manual work needs to be annihilated,

Today: Creating new feature branches

At work, we use Atlassian JIRA and Bitbucket. While there are ways to create a bitbucket branch automatically on the remote when transitioning a ticket, those are not satisfying enough. Still need to move the ticket, and pull the remote branch. It's way easier to do that from the command line, the place to be all the time. I use git all the day in the terminal.

This is where custom git sub commands come into play. I created a git command, called git go, to start a new feature branch and automatically transition the corresponding JIRA issue to the Work in Progress/Doing column.

The usage is straightforward, the only thing required is the ticket number to work on: git go JIRA-1234 checks out a new branch from latest master and transitions the JIRA ticket.

git-go

#!/usr/bin/env bash

set -e

ticket_transition="Directly to work"
branch="$1"

if [[ -z "$branch" ]]; then
    echo "usage git go <ticket>"
    exit 1
fi

git fetch
git checkout origin/master
git checkout -b "$branch"

# transition ticket to doing
jira transition --noedit "$ticket_transition" "$branch"

In order to have the custom sub-command registered with git, prefix it with git-, make it executable (chmod +x git-go) , and stick it somewhere into your PATH.

This script has one more requirement: this jira command line client: https://github.com/go-jira/jira. Refer to the README instructions there on how to set it up.

You need to update the ticket_transition variable at the top of the script to match the transition name in use for your JIRA project (ask your JIRA administrator for that).

Bash completion

To make things extra nice, add bash completion for the new subcommand:

custom-git.completion

#!/usr/bin/env bash

_git_go ()
{
    if [[ ! -r "/tmp/jira_issue_cache" ]]; then
        jira next > /tmp/jira_issue_cache
    fi

    __gitcomp_nl "$(cat /tmp/jira_issue_cache)"
}

The completion script fetches a list of potential ticket candidates for the only argument to the script. The ticket list is cached until the next reboot to speed up completion. jira next is a custom command for the jira tool, selecting all of the tickets in the current sprint backlog.

~/.jira.d/config.yml

  - name: next
    help: display issues for active sprint which are in (Doing, backlog)
    script: |-
      if [ -n "$JIRA_PROJECT" ]; then
          # if `project: ...` configured just list the issues for current project
          {{jira}} list --template key-only --query "sprint in openSprints() and type != epic and status IN (Doing,\"Sprint Backlog\") and project=$JIRA_PROJECT ORDER BY rank asc, created"
      else
          echo "\"project: ...\" configuration missing from .jira.d/config.yml"
      fi
comments powered by Disqus