Parent directory

jira.plugin.zsh

5693 bytes
  1# CLI support for JIRA interaction
  2#
  3# See README.md for details
  4
  5function _jira_usage() {
  6cat <<EOF
  7jira                            Performs the default action
  8jira new                        Opens a new Jira issue dialogue
  9jira ABC-123                    Opens an existing issue
 10jira ABC-123 m                  Opens an existing issue for adding a comment
 11jira project ABC                Opens JIRA project summary
 12jira dashboard [rapid_view]     Opens your JIRA dashboard
 13jira mine                       Queries for your own issues
 14jira tempo                      Opens your JIRA Tempo
 15jira reported [username]        Queries for issues reported by a user
 16jira assigned [username]        Queries for issues assigned to a user
 17jira branch                     Opens an existing issue matching the current branch name
 18EOF
 19}
 20
 21# If your branch naming convention deviates, you can partially override this plugin function
 22# to determine the jira issue key based on your formatting.
 23# See https://github.com/ohmyzsh/ohmyzsh/wiki/Customization#partially-overriding-an-existing-plugin
 24function jira_branch() {
 25  # Get name of the branch
 26  issue_arg=$(git rev-parse --abbrev-ref HEAD)
 27  # Strip prefixes like feature/ or bugfix/
 28  issue_arg=${issue_arg##*/}
 29  # Strip suffixes starting with _
 30  issue_arg=(${(s:_:)issue_arg})
 31  # If there is only one part, it means that there is a different delimiter. Try with -
 32  if [[ ${#issue_arg[@]} = 1 && ${issue_arg} == *-* ]]; then
 33    issue_arg=(${(s:-:)issue_arg})
 34    issue_arg="${issue_arg[1]}-${issue_arg[2]}"
 35  else
 36    issue_arg=${issue_arg[1]}
 37  fi
 38  if [[ "${issue_arg:l}" = ${jira_prefix:l}* ]]; then
 39    echo "${issue_arg}"
 40  else
 41    echo "${jira_prefix}${issue_arg}"
 42  fi
 43}
 44
 45function jira() {
 46  emulate -L zsh
 47  local action jira_url jira_prefix
 48  if [[ -n "$1" ]]; then
 49    action=$1
 50  elif [[ -f .jira-default-action ]]; then
 51    action=$(cat .jira-default-action)
 52  elif [[ -f ~/.jira-default-action ]]; then
 53    action=$(cat ~/.jira-default-action)
 54  elif [[ -n "${JIRA_DEFAULT_ACTION}" ]]; then
 55    action=${JIRA_DEFAULT_ACTION}
 56  else
 57    action="new"
 58  fi
 59
 60  if [[ -f .jira-url ]]; then
 61    jira_url=$(cat .jira-url)
 62  elif [[ -f ~/.jira-url ]]; then
 63    jira_url=$(cat ~/.jira-url)
 64  elif [[ -n "${JIRA_URL}" ]]; then
 65    jira_url=${JIRA_URL}
 66  else
 67    _jira_url_help
 68    return 1
 69  fi
 70
 71  if [[ -f .jira-prefix ]]; then
 72    jira_prefix=$(cat .jira-prefix)
 73  elif [[ -f ~/.jira-prefix ]]; then
 74    jira_prefix=$(cat ~/.jira-prefix)
 75  elif [[ -n "${JIRA_PREFIX}" ]]; then
 76    jira_prefix=${JIRA_PREFIX}
 77  else
 78    jira_prefix=""
 79  fi
 80
 81
 82  if [[ $action == "new" ]]; then
 83    echo "Opening new issue"
 84    open_command "${jira_url}/secure/CreateIssue!default.jspa"
 85  elif [[ "$action" == "assigned" || "$action" == "reported" ]]; then
 86    _jira_query ${@:-$action}
 87  elif [[ "$action" == "help" || "$action" == "usage" ]]; then
 88    _jira_usage
 89  elif [[ "$action" == "mine" ]]; then
 90    echo "Opening my issues"
 91    open_command "${jira_url}/issues/?filter=-1"
 92  elif [[ "$action" == "project" ]]; then
 93    echo "Opening project"
 94    open_command "${jira_url}/jira/software/c/projects/${2}/summary"
 95  elif [[ "$action" == "dashboard" ]]; then
 96    echo "Opening dashboard"
 97    if [[ "$JIRA_RAPID_BOARD" == "true" ]]; then
 98      _jira_rapid_board ${@}
 99    else
100      open_command "${jira_url}/secure/Dashboard.jspa"
101    fi
102  elif [[ "$action" == "tempo" ]]; then
103    echo "Opening tempo"
104    if [[ -n "$JIRA_TEMPO_PATH" ]]; then
105      open_command "${jira_url}${JIRA_TEMPO_PATH}"
106    else
107      open_command "${jira_url}/secure/Tempo.jspa"
108    fi
109  elif [[ "$action" == "dumpconfig" ]]; then
110    echo "JIRA_URL=$jira_url"
111    echo "JIRA_PREFIX=$jira_prefix"
112    echo "JIRA_NAME=$JIRA_NAME"
113    echo "JIRA_RAPID_VIEW=$JIRA_RAPID_VIEW"
114    echo "JIRA_RAPID_BOARD=$JIRA_RAPID_BOARD"
115    echo "JIRA_DEFAULT_ACTION=$JIRA_DEFAULT_ACTION"
116    echo "JIRA_TEMPO_PATH=$JIRA_TEMPO_PATH"
117  else
118    # Anything that doesn't match a special action is considered an issue name
119    # but `branch` is a special case that will parse the current git branch
120    local issue_arg issue
121    if [[ "$action" == "branch" ]]; then
122      issue=$(jira_branch)
123    else
124      issue_arg=${(U)action}
125      issue="${jira_prefix}${issue_arg}"
126    fi
127
128    local url_fragment
129    if [[ "$2" == "m" ]]; then
130      url_fragment="#add-comment"
131      echo "Add comment to issue #$issue"
132    else
133      echo "Opening issue #$issue"
134    fi
135    open_command "${jira_url}/browse/${issue}${url_fragment}"
136  fi
137}
138
139function _jira_url_help() {
140  cat << EOF
141error: JIRA URL is not specified anywhere.
142
143Valid options, in order of precedence:
144  .jira-url file
145  \$HOME/.jira-url file
146  \$JIRA_URL environment variable
147EOF
148}
149
150function _jira_rapid_board() {
151  rapid_view=${2:=$JIRA_RAPID_VIEW}
152
153  if [[ -z $rapid_view ]]; then
154    open_command "${jira_url}/secure/RapidBoard.jspa"
155  else
156    open_command "${jira_url}/secure/RapidBoard.jspa?rapidView=$rapid_view"
157  fi
158}
159
160function _jira_query() {
161  emulate -L zsh
162  local verb="$1"
163  local jira_name lookup preposition query
164  if [[ "${verb}" == "reported" ]]; then
165    lookup=reporter
166    preposition=by
167  elif [[ "${verb}" == "assigned" ]]; then
168    lookup=assignee
169    preposition=to
170  else
171    echo "error: not a valid lookup: $verb" >&2
172    return 1
173  fi
174  jira_name=${2:=$JIRA_NAME}
175  if [[ -z $jira_name ]]; then
176    echo "error: JIRA_NAME not specified" >&2
177    return 1
178  fi
179
180  echo "Browsing issues ${verb} ${preposition} ${jira_name}"
181  query="${lookup}+%3D+%22${jira_name}%22+AND+resolution+%3D+unresolved+ORDER+BY+priority+DESC%2C+created+ASC"
182  open_command "${jira_url}/secure/IssueNavigator.jspa?reset=true&jqlQuery=${query}"
183}