jira.plugin.zsh 3.32 KB
Newer Older
1
# CLI support for JIRA interaction
2
#
3
4
5
6
7
# Setup: 
#   Add a .jira-url file in the base of your project
#   You can also set $JIRA_URL in your .zshrc or put .jira-url in your home directory
#   A .jira-url in the current directory takes precedence. 
#   The same goes with .jira-prefix and $JIRA_PREFIX.
8
#
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#   For example:
#     cd to/my/project
#     echo "https://name.jira.com" >> .jira-url
#
# Variables:
#  $JIRA_RAPID_BOARD     - set to "true" if you use Rapid Board
#  $JIRA_DEFAULT_ACTION  - action to do when `jira` is called witn no args
#                          defaults to "new"
#  $JIRA_NAME            - Your JIRA username. Used as default for assigned/reported
#  $JIRA_PREFIX          - Prefix added to issue ID arguments
#
#
# Usage: 
#   jira            # Performs the default action
#   jira new        # opens a new issue
#   jira reported [username]
#   jira assigned [username]
#   jira dashboard
#   jira ABC-123    # Opens an existing issue
#   jira ABC-123 m  # Opens an existing issue for adding a comment

: ${JIRA_DEFAULT_ACTION:=new}

function jira() {
  local action=${1:=$JIRA_DEFAULT_ACTION}

  local jira_url jira_prefix
  if [[ -f .jira-url ]]; then
37
    jira_url=$(cat .jira-url)
38
  elif [[ -f ~/.jira-url ]]; then
39
    jira_url=$(cat ~/.jira-url)
40
  elif [[ -n "${JIRA_URL}" ]]; then
41
    jira_url=${JIRA_URL}
42
  else
Andrew Stuart's avatar
Andrew Stuart committed
43
    jira_url_help
44
    return 1
45
46
  fi

47
  if [[ -f .jira-prefix ]]; then
48
    jira_prefix=$(cat .jira-prefix)
49
  elif [[ -f ~/.jira-prefix ]]; then
50
    jira_prefix=$(cat ~/.jira-prefix)
51
  elif [[ -n "${JIRA_PREFIX}" ]]; then
52
    jira_prefix=${JIRA_PREFIX}
53
54
55
56
  else
    jira_prefix=""
  fi

57
58

  if [[ $action == "new" ]]; then
59
    echo "Opening new issue"
60
    open_command "${jira_url}/secure/CreateIssue!default.jspa"
61
  elif [[ "$action" == "assigned" || "$action" == "reported" ]]; then
62
    jira_query $@
63
64
65
  elif [[ "$action" == "dashboard" ]]; then
    echo "Opening dashboard"
    open_command "${jira_url}/secure/Dashboard.jspa"
66
  else
67
68
69
70
    # Anything that doesn't match a special action is considered an issue name
    local issue_arg=$action
    local issue="${jira_prefix}${issue_arg}"
    local url_fragment=''
David Hartmann's avatar
David Hartmann committed
71
    if [[ "$2" == "m" ]]; then
72
73
      url_fragment="#add-comment"
      echo "Add comment to issue #$issue"
David Hartmann's avatar
David Hartmann committed
74
    else
75
      echo "Opening issue #$issue"
David Hartmann's avatar
David Hartmann committed
76
    fi
77
    if [[ "$JIRA_RAPID_BOARD" == "true" ]]; then
78
      open_command "${jira_url}/issues/${issue}${url_fragment}"
David Hartmann's avatar
David Hartmann committed
79
    else
80
      open_command "${jira_url}/browse/${issue}${url_fragment}"
David Hartmann's avatar
David Hartmann committed
81
    fi
82
83
84
  fi
}

85
function jira_url_help() {
Andrew Stuart's avatar
Andrew Stuart committed
86
87
  cat << EOF
JIRA url is not specified anywhere.
Andrew Stuart's avatar
Andrew Stuart committed
88
Valid options, in order of precedence:
Andrew Stuart's avatar
Andrew Stuart committed
89
  .jira-url file
Andrew Stuart's avatar
Andrew Stuart committed
90
  \$HOME/.jira-url file
Andrew Stuart's avatar
Andrew Stuart committed
91
92
93
94
  JIRA_URL environment variable
EOF
}

95
function jira_query() {
96
  local verb="$1"
97
98
  local jira_name lookup preposition query
  if [[ "${verb}" == "reported" ]]; then
99
100
    lookup=reporter
    preposition=by
101
  elif [[ "${verb}" == "assigned" ]]; then
102
103
104
    lookup=assignee
    preposition=to
  else
105
    echo "not a valid lookup: $verb" >&2
106
107
    return 1
  fi
108
109
110
  jira_name=${2:=$JIRA_NAME}
  if [[ -z $jira_name ]]; then
    echo "JIRA_NAME not specified" >&2
111
112
    return 1
  fi
113

114
  echo "Browsing issues ${verb} ${preposition} ${jira_name}"
115
116
  query="${lookup}+%3D+%22${jira_name}%22+AND+resolution+%3D+unresolved+ORDER+BY+priority+DESC%2C+created+ASC"
  open_command "${jira_url}/secure/IssueNavigator.jspa?reset=true&jqlQuery=${query}"
117
118
}