jira.plugin.zsh 3.2 KB

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