]> src.twobees.de Git - dotfiles.git/blob - stow/oh-my-zsh/.oh-my-zsh/plugins/jira/jira.plugin.zsh
initial
[dotfiles.git] / stow / oh-my-zsh / .oh-my-zsh / plugins / jira / jira.plugin.zsh
1 # CLI support for JIRA interaction
2 #
3 # See README.md for details
4
5 function jira() {
6   emulate -L zsh
7   local action jira_url jira_prefix
8   if [[ -n "$1" ]]; then
9     action=$1
10   elif [[ -f .jira-default-action ]]; then
11     action=$(cat .jira-default-action)
12   elif [[ -f ~/.jira-default-action ]]; then
13     action=$(cat ~/.jira-default-action)
14   elif [[ -n "${JIRA_DEFAULT_ACTION}" ]]; then
15     action=${JIRA_DEFAULT_ACTION}
16   else
17     action="new"
18   fi
19
20   if [[ -f .jira-url ]]; then
21     jira_url=$(cat .jira-url)
22   elif [[ -f ~/.jira-url ]]; then
23     jira_url=$(cat ~/.jira-url)
24   elif [[ -n "${JIRA_URL}" ]]; then
25     jira_url=${JIRA_URL}
26   else
27     _jira_url_help
28     return 1
29   fi
30
31   if [[ -f .jira-prefix ]]; then
32     jira_prefix=$(cat .jira-prefix)
33   elif [[ -f ~/.jira-prefix ]]; then
34     jira_prefix=$(cat ~/.jira-prefix)
35   elif [[ -n "${JIRA_PREFIX}" ]]; then
36     jira_prefix=${JIRA_PREFIX}
37   else
38     jira_prefix=""
39   fi
40
41
42   if [[ $action == "new" ]]; then
43     echo "Opening new issue"
44     open_command "${jira_url}/secure/CreateIssue!default.jspa"
45   elif [[ "$action" == "assigned" || "$action" == "reported" ]]; then
46     _jira_query ${@:-$action}
47   elif [[ "$action" == "myissues" ]]; then
48     echo "Opening my issues"
49     open_command "${jira_url}/issues/?filter=-1"
50   elif [[ "$action" == "dashboard" ]]; then
51     echo "Opening dashboard"
52     if [[ "$JIRA_RAPID_BOARD" == "true" ]]; then
53       open_command "${jira_url}/secure/RapidBoard.jspa"
54     else
55       open_command "${jira_url}/secure/Dashboard.jspa"
56     fi
57   elif [[ "$action" == "tempo" ]]; then
58     echo "Opening tempo"
59     open_command "${jira_url}/secure/Tempo.jspa"
60   elif [[ "$action" == "dumpconfig" ]]; then
61     echo "JIRA_URL=$jira_url"
62     echo "JIRA_PREFIX=$jira_prefix"
63     echo "JIRA_NAME=$JIRA_NAME"
64     echo "JIRA_RAPID_BOARD=$JIRA_RAPID_BOARD"
65     echo "JIRA_DEFAULT_ACTION=$JIRA_DEFAULT_ACTION"
66   else
67     # Anything that doesn't match a special action is considered an issue name
68     # but `branch` is a special case that will parse the current git branch
69     local issue_arg issue
70     if [[ "$action" == "branch" ]]; then
71       # Get name of the branch
72       issue_arg=$(git rev-parse --abbrev-ref HEAD)
73       # Strip prefixes like feature/ or bugfix/
74       issue_arg=${issue_arg##*/}
75       # Strip suffixes starting with _
76       issue_arg=(${(s:_:)issue_arg})
77       issue_arg=${issue_arg[1]}
78       if [[ "$issue_arg" = ${jira_prefix}* ]]; then
79         issue="${issue_arg}"
80       else
81         issue="${jira_prefix}${issue_arg}"
82       fi
83     else
84       issue_arg=${(U)action}
85       issue="${jira_prefix}${issue_arg}"
86     fi
87
88     local url_fragment
89     if [[ "$2" == "m" ]]; then
90       url_fragment="#add-comment"
91       echo "Add comment to issue #$issue"
92     else
93       echo "Opening issue #$issue"
94     fi
95     open_command "${jira_url}/browse/${issue}${url_fragment}"
96   fi
97 }
98
99 function _jira_url_help() {
100   cat << EOF
101 error: JIRA URL is not specified anywhere.
102
103 Valid options, in order of precedence:
104   .jira-url file
105   \$HOME/.jira-url file
106   \$JIRA_URL environment variable
107 EOF
108 }
109
110 function _jira_query() {
111   emulate -L zsh
112   local verb="$1"
113   local jira_name lookup preposition query
114   if [[ "${verb}" == "reported" ]]; then
115     lookup=reporter
116     preposition=by
117   elif [[ "${verb}" == "assigned" ]]; then
118     lookup=assignee
119     preposition=to
120   else
121     echo "error: not a valid lookup: $verb" >&2
122     return 1
123   fi
124   jira_name=${2:=$JIRA_NAME}
125   if [[ -z $jira_name ]]; then
126     echo "error: JIRA_NAME not specified" >&2
127     return 1
128   fi
129
130   echo "Browsing issues ${verb} ${preposition} ${jira_name}"
131   query="${lookup}+%3D+%22${jira_name}%22+AND+resolution+%3D+unresolved+ORDER+BY+priority+DESC%2C+created+ASC"
132   open_command "${jira_url}/secure/IssueNavigator.jspa?reset=true&jqlQuery=${query}"
133 }