Skip to content

Instantly share code, notes, and snippets.

@sangdth
Last active April 6, 2023 16:59
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sangdth/9b18c5ca66acba42dd52ac4f808373a0 to your computer and use it in GitHub Desktop.
Save sangdth/9b18c5ca66acba42dd52ac4f808373a0 to your computer and use it in GitHub Desktop.
Generate git branch based on Jira tasks

Required:

Setup:

  1. Install Jira CLI, generate the API token and follow the initiation process. Make sure you can see a list of issues by using jira issue list.
  2. Install fzf and setup some default configs if you like.
  3. Put this code below into your .zshrc (I'm not sure about .bashrc, did not test it.
_fzf_complete_git() {
  ARGS="$@"
  CODE="$(echo $ARGS | sed 's/git checkout -b //g')"

  if [[ $CODE == "" ]]; then
    ME="-a$(jira me)"
    PROJECT_QUERY="AND project IS NOT EMPTY"
  else
    ME=""
    PROJECT_QUERY="AND project IN ($CODE)"
  fi

  local tasks
  tasks=$(jira issue list --plain --no-headers --columns key,summary $ME -q "status NOT IN (Done, Dismissed, Released, 'In Progress', 'Ready for release') $PROJECT_QUERY")
  if [[ $ARGS == 'git checkout -b'* ]]; then
    _fzf_complete --ansi --reverse --multi --prompt="fzf> " -- "git checkout -b " < <(
      echo $tasks | sed 's/[[:space:]]/-/g' | tr -dc '[:alnum:]-\n'
    )
  fi
}
  1. Source the .zshrc and you can use.

Usage:

  • Type the git command: git checkout -b CODE ** then press Tab.
  • The CODE is the prefix of your ticket code. Usually they are in format ABC-123.
  • If the CODE is omitted, it will try to search only tasks assigned to you.
  • If you already changed your trigger to ~~, replace ** with ~~.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment