jira.plugin.zsh 2.67 KB
Newer Older
1
# CLI support for JIRA interaction
2
#
3
# See README.md for details
4
5
6
7

: ${JIRA_DEFAULT_ACTION:=new}

function jira() {
8
  emulate -L zsh
9
10
11
12
  local action=${1:=$JIRA_DEFAULT_ACTION}

  local jira_url jira_prefix
  if [[ -f .jira-url ]]; then
13
    jira_url=$(cat .jira-url)
14
  elif [[ -f ~/.jira-url ]]; then
15
    jira_url=$(cat ~/.jira-url)
16
  elif [[ -n "${JIRA_URL}" ]]; then
17
    jira_url=${JIRA_URL}
18
  else
Andrew Janke's avatar
Andrew Janke committed
19
    _jira_url_help
20
    return 1
21
22
  fi

23
  if [[ -f .jira-prefix ]]; then
24
    jira_prefix=$(cat .jira-prefix)
25
  elif [[ -f ~/.jira-prefix ]]; then
26
    jira_prefix=$(cat ~/.jira-prefix)
27
  elif [[ -n "${JIRA_PREFIX}" ]]; then
28
    jira_prefix=${JIRA_PREFIX}
29
30
31
32
  else
    jira_prefix=""
  fi

33
34

  if [[ $action == "new" ]]; then
35
    echo "Opening new issue"
36
    open_command "${jira_url}/secure/CreateIssue!default.jspa"
37
  elif [[ "$action" == "assigned" || "$action" == "reported" ]]; then
Andrew Janke's avatar
Andrew Janke committed
38
    _jira_query $@
39
40
41
  elif [[ "$action" == "dashboard" ]]; then
    echo "Opening dashboard"
    open_command "${jira_url}/secure/Dashboard.jspa"
42
43
44
45
46
47
  elif [[ "$action" == "dumpconfig" ]]; then
    echo "JIRA_URL=$jira_url"
    echo "JIRA_PREFIX=$jira_prefix"
    echo "JIRA_NAME=$JIRA_NAME"
    echo "JIRA_RAPID_BOARD=$JIRA_RAPID_BOARD"
    echo "JIRA_DEFAULT_ACTION=$JIRA_DEFAULT_ACTION"
48
  else
49
50
51
52
    # 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
53
    if [[ "$2" == "m" ]]; then
54
55
      url_fragment="#add-comment"
      echo "Add comment to issue #$issue"
David Hartmann's avatar
David Hartmann committed
56
    else
57
      echo "Opening issue #$issue"
David Hartmann's avatar
David Hartmann committed
58
    fi
59
    if [[ "$JIRA_RAPID_BOARD" == "true" ]]; then
60
      open_command "${jira_url}/issues/${issue}${url_fragment}"
David Hartmann's avatar
David Hartmann committed
61
    else
62
      open_command "${jira_url}/browse/${issue}${url_fragment}"
David Hartmann's avatar
David Hartmann committed
63
    fi
64
65
66
  fi
}

Andrew Janke's avatar
Andrew Janke committed
67
function _jira_url_help() {
Andrew Stuart's avatar
Andrew Stuart committed
68
  cat << EOF
69
70
error: JIRA URL is not specified anywhere.

Andrew Stuart's avatar
Andrew Stuart committed
71
Valid options, in order of precedence:
Andrew Stuart's avatar
Andrew Stuart committed
72
  .jira-url file
Andrew Stuart's avatar
Andrew Stuart committed
73
  \$HOME/.jira-url file
74
  \$JIRA_URL environment variable
Andrew Stuart's avatar
Andrew Stuart committed
75
76
77
EOF
}

Andrew Janke's avatar
Andrew Janke committed
78
function _jira_query() {
79
  emulate -L zsh
80
  local verb="$1"
81
82
  local jira_name lookup preposition query
  if [[ "${verb}" == "reported" ]]; then
83
84
    lookup=reporter
    preposition=by
85
  elif [[ "${verb}" == "assigned" ]]; then
86
87
88
    lookup=assignee
    preposition=to
  else
89
    echo "error: not a valid lookup: $verb" >&2
90
91
    return 1
  fi
92
93
  jira_name=${2:=$JIRA_NAME}
  if [[ -z $jira_name ]]; then
94
    echo "error: JIRA_NAME not specified" >&2
95
96
    return 1
  fi
97

98
  echo "Browsing issues ${verb} ${preposition} ${jira_name}"
99
100
  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}"
101
102
}