jira.plugin.zsh 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. # CLI support for JIRA interaction
  2. #
  3. # See README.md for details
  4. : ${JIRA_DEFAULT_ACTION:=new}
  5. function jira() {
  6. emulate -L zsh
  7. local action=${1:=$JIRA_DEFAULT_ACTION}
  8. local jira_url jira_prefix
  9. if [[ -f .jira-url ]]; then
  10. jira_url=$(cat .jira-url)
  11. elif [[ -f ~/.jira-url ]]; then
  12. jira_url=$(cat ~/.jira-url)
  13. elif [[ -n "${JIRA_URL}" ]]; then
  14. jira_url=${JIRA_URL}
  15. else
  16. _jira_url_help
  17. return 1
  18. fi
  19. if [[ -f .jira-prefix ]]; then
  20. jira_prefix=$(cat .jira-prefix)
  21. elif [[ -f ~/.jira-prefix ]]; then
  22. jira_prefix=$(cat ~/.jira-prefix)
  23. elif [[ -n "${JIRA_PREFIX}" ]]; then
  24. jira_prefix=${JIRA_PREFIX}
  25. else
  26. jira_prefix=""
  27. fi
  28. if [[ $action == "new" ]]; then
  29. echo "Opening new issue"
  30. open_command "${jira_url}/secure/CreateIssue!default.jspa"
  31. elif [[ "$action" == "assigned" || "$action" == "reported" ]]; then
  32. _jira_query $@
  33. elif [[ "$action" == "dashboard" ]]; then
  34. echo "Opening dashboard"
  35. open_command "${jira_url}/secure/Dashboard.jspa"
  36. elif [[ "$action" == "dumpconfig" ]]; then
  37. echo "JIRA_URL=$jira_url"
  38. echo "JIRA_PREFIX=$jira_prefix"
  39. echo "JIRA_NAME=$JIRA_NAME"
  40. echo "JIRA_RAPID_BOARD=$JIRA_RAPID_BOARD"
  41. echo "JIRA_DEFAULT_ACTION=$JIRA_DEFAULT_ACTION"
  42. else
  43. # Anything that doesn't match a special action is considered an issue name
  44. local issue_arg=$action
  45. local issue="${jira_prefix}${issue_arg}"
  46. local url_fragment=''
  47. if [[ "$2" == "m" ]]; then
  48. url_fragment="#add-comment"
  49. echo "Add comment to issue #$issue"
  50. else
  51. echo "Opening issue #$issue"
  52. fi
  53. if [[ "$JIRA_RAPID_BOARD" == "true" ]]; then
  54. open_command "${jira_url}/issues/${issue}${url_fragment}"
  55. else
  56. open_command "${jira_url}/browse/${issue}${url_fragment}"
  57. fi
  58. fi
  59. }
  60. function _jira_url_help() {
  61. cat << EOF
  62. error: JIRA URL is not specified anywhere.
  63. Valid options, in order of precedence:
  64. .jira-url file
  65. \$HOME/.jira-url file
  66. \$JIRA_URL environment variable
  67. EOF
  68. }
  69. function _jira_query() {
  70. emulate -L zsh
  71. local verb="$1"
  72. local jira_name lookup preposition query
  73. if [[ "${verb}" == "reported" ]]; then
  74. lookup=reporter
  75. preposition=by
  76. elif [[ "${verb}" == "assigned" ]]; then
  77. lookup=assignee
  78. preposition=to
  79. else
  80. echo "error: not a valid lookup: $verb" >&2
  81. return 1
  82. fi
  83. jira_name=${2:=$JIRA_NAME}
  84. if [[ -z $jira_name ]]; then
  85. echo "error: JIRA_NAME not specified" >&2
  86. return 1
  87. fi
  88. echo "Browsing issues ${verb} ${preposition} ${jira_name}"
  89. query="${lookup}+%3D+%22${jira_name}%22+AND+resolution+%3D+unresolved+ORDER+BY+priority+DESC%2C+created+ASC"
  90. open_command "${jira_url}/secure/IssueNavigator.jspa?reset=true&jqlQuery=${query}"
  91. }