jira.plugin.zsh 5.1 KB

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