jira.plugin.zsh 5.4 KB

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