jira.plugin.zsh 2.79 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
  elif [[ "$action" == "dashboard" ]]; then
    echo "Opening dashboard"
41
42
43
44
45
    if [[ "$JIRA_RAPID_BOARD" == "true" ]]; then
      open_command "${jira_url}/secure/RapidBoard.jspa"
    else
      open_command "${jira_url}/secure/Dashboard.jspa"
    fi
46
47
48
49
50
51
  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"
52
  else
53
54
55
56
    # 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
57
    if [[ "$2" == "m" ]]; then
58
59
      url_fragment="#add-comment"
      echo "Add comment to issue #$issue"
David Hartmann's avatar
David Hartmann committed
60
    else
61
      echo "Opening issue #$issue"
David Hartmann's avatar
David Hartmann committed
62
    fi
63
    if [[ "$JIRA_RAPID_BOARD" == "true" ]]; then
64
      open_command "${jira_url}/issues/${issue}${url_fragment}"
David Hartmann's avatar
David Hartmann committed
65
    else
66
      open_command "${jira_url}/browse/${issue}${url_fragment}"
David Hartmann's avatar
David Hartmann committed
67
    fi
68
69
70
  fi
}

Andrew Janke's avatar
Andrew Janke committed
71
function _jira_url_help() {
Andrew Stuart's avatar
Andrew Stuart committed
72
  cat << EOF
73
74
error: JIRA URL is not specified anywhere.

Andrew Stuart's avatar
Andrew Stuart committed
75
Valid options, in order of precedence:
Andrew Stuart's avatar
Andrew Stuart committed
76
  .jira-url file
Andrew Stuart's avatar
Andrew Stuart committed
77
  \$HOME/.jira-url file
78
  \$JIRA_URL environment variable
Andrew Stuart's avatar
Andrew Stuart committed
79
80
81
EOF
}

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

102
  echo "Browsing issues ${verb} ${preposition} ${jira_name}"
103
104
  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}"
105
}