jira.plugin.zsh 3.3 KB

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