jira.plugin.zsh 4.4 KB

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