jira.plugin.zsh 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. if [[ "$JIRA_RAPID_BOARD" == "true" ]]; then
  36. open_command "${jira_url}/secure/RapidBoard.jspa"
  37. else
  38. open_command "${jira_url}/secure/Dashboard.jspa"
  39. fi
  40. elif [[ "$action" == "dumpconfig" ]]; then
  41. echo "JIRA_URL=$jira_url"
  42. echo "JIRA_PREFIX=$jira_prefix"
  43. echo "JIRA_NAME=$JIRA_NAME"
  44. echo "JIRA_RAPID_BOARD=$JIRA_RAPID_BOARD"
  45. echo "JIRA_DEFAULT_ACTION=$JIRA_DEFAULT_ACTION"
  46. else
  47. # Anything that doesn't match a special action is considered an issue name
  48. local issue_arg=$action
  49. local issue="${jira_prefix}${issue_arg}"
  50. local url_fragment=''
  51. if [[ "$2" == "m" ]]; then
  52. url_fragment="#add-comment"
  53. echo "Add comment to issue #$issue"
  54. else
  55. echo "Opening issue #$issue"
  56. fi
  57. if [[ "$JIRA_RAPID_BOARD" == "true" ]]; then
  58. open_command "${jira_url}/issues/${issue}${url_fragment}"
  59. else
  60. open_command "${jira_url}/browse/${issue}${url_fragment}"
  61. fi
  62. fi
  63. }
  64. function _jira_url_help() {
  65. cat << EOF
  66. error: JIRA URL is not specified anywhere.
  67. Valid options, in order of precedence:
  68. .jira-url file
  69. \$HOME/.jira-url file
  70. \$JIRA_URL environment variable
  71. EOF
  72. }
  73. function _jira_query() {
  74. emulate -L zsh
  75. local verb="$1"
  76. local jira_name lookup preposition query
  77. if [[ "${verb}" == "reported" ]]; then
  78. lookup=reporter
  79. preposition=by
  80. elif [[ "${verb}" == "assigned" ]]; then
  81. lookup=assignee
  82. preposition=to
  83. else
  84. echo "error: not a valid lookup: $verb" >&2
  85. return 1
  86. fi
  87. jira_name=${2:=$JIRA_NAME}
  88. if [[ -z $jira_name ]]; then
  89. echo "error: JIRA_NAME not specified" >&2
  90. return 1
  91. fi
  92. echo "Browsing issues ${verb} ${preposition} ${jira_name}"
  93. query="${lookup}+%3D+%22${jira_name}%22+AND+resolution+%3D+unresolved+ORDER+BY+priority+DESC%2C+created+ASC"
  94. open_command "${jira_url}/secure/IssueNavigator.jspa?reset=true&jqlQuery=${query}"
  95. }