jira.plugin.zsh 5.6 KB

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