jira.plugin.zsh 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. # CLI support for JIRA interaction
  2. #
  3. # Setup:
  4. # Add a .jira-url file in the base of your project
  5. # You can also set $JIRA_URL in your .zshrc or put .jira-url in your home directory
  6. # A .jira-url in the current directory takes precedence.
  7. # The same goes with .jira-prefix and $JIRA_PREFIX.
  8. #
  9. # For example:
  10. # cd to/my/project
  11. # echo "https://name.jira.com" >> .jira-url
  12. #
  13. # Variables:
  14. # $JIRA_RAPID_BOARD - set to "true" if you use Rapid Board
  15. # $JIRA_DEFAULT_ACTION - action to do when `jira` is called witn no args
  16. # defaults to "new"
  17. # $JIRA_NAME - Your JIRA username. Used as default for assigned/reported
  18. # $JIRA_PREFIX - Prefix added to issue ID arguments
  19. #
  20. #
  21. # Usage:
  22. # jira # Performs the default action
  23. # jira new # opens a new issue
  24. # jira reported [username]
  25. # jira assigned [username]
  26. # jira dashboard
  27. # jira ABC-123 # Opens an existing issue
  28. # jira ABC-123 m # Opens an existing issue for adding a comment
  29. : ${JIRA_DEFAULT_ACTION:=new}
  30. function jira() {
  31. local action=${1:=$JIRA_DEFAULT_ACTION}
  32. local jira_url jira_prefix
  33. if [[ -f .jira-url ]]; then
  34. jira_url=$(cat .jira-url)
  35. elif [[ -f ~/.jira-url ]]; then
  36. jira_url=$(cat ~/.jira-url)
  37. elif [[ -n "${JIRA_URL}" ]]; then
  38. jira_url=${JIRA_URL}
  39. else
  40. _jira_url_help
  41. return 1
  42. fi
  43. if [[ -f .jira-prefix ]]; then
  44. jira_prefix=$(cat .jira-prefix)
  45. elif [[ -f ~/.jira-prefix ]]; then
  46. jira_prefix=$(cat ~/.jira-prefix)
  47. elif [[ -n "${JIRA_PREFIX}" ]]; then
  48. jira_prefix=${JIRA_PREFIX}
  49. else
  50. jira_prefix=""
  51. fi
  52. if [[ $action == "new" ]]; then
  53. echo "Opening new issue"
  54. open_command "${jira_url}/secure/CreateIssue!default.jspa"
  55. elif [[ "$action" == "assigned" || "$action" == "reported" ]]; then
  56. _jira_query $@
  57. elif [[ "$action" == "dashboard" ]]; then
  58. echo "Opening dashboard"
  59. open_command "${jira_url}/secure/Dashboard.jspa"
  60. else
  61. # Anything that doesn't match a special action is considered an issue name
  62. local issue_arg=$action
  63. local issue="${jira_prefix}${issue_arg}"
  64. local url_fragment=''
  65. if [[ "$2" == "m" ]]; then
  66. url_fragment="#add-comment"
  67. echo "Add comment to issue #$issue"
  68. else
  69. echo "Opening issue #$issue"
  70. fi
  71. if [[ "$JIRA_RAPID_BOARD" == "true" ]]; then
  72. open_command "${jira_url}/issues/${issue}${url_fragment}"
  73. else
  74. open_command "${jira_url}/browse/${issue}${url_fragment}"
  75. fi
  76. fi
  77. }
  78. function _jira_url_help() {
  79. cat << EOF
  80. JIRA url is not specified anywhere.
  81. Valid options, in order of precedence:
  82. .jira-url file
  83. \$HOME/.jira-url file
  84. JIRA_URL environment variable
  85. EOF
  86. }
  87. function _jira_query() {
  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 "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 "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. }