jira.plugin.zsh 3.02 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
    # Anything that doesn't match a special action is considered an issue name
54
55
56
57
58
59
60
61
    # but `branch` is a special case that will parse the current git branch
    if [[ "$action" == "br" ]]; then
      local issue_arg=$(git rev-parse --abbrev-ref HEAD)
      local issue="${jira_prefix}${issue_arg}"
    else
      local issue_arg=$action
      local issue="${jira_prefix}${issue_arg}"
    fi
62
    local url_fragment=''
David Hartmann's avatar
David Hartmann committed
63
    if [[ "$2" == "m" ]]; then
64
65
      url_fragment="#add-comment"
      echo "Add comment to issue #$issue"
David Hartmann's avatar
David Hartmann committed
66
    else
67
      echo "Opening issue #$issue"
David Hartmann's avatar
David Hartmann committed
68
    fi
69
    if [[ "$JIRA_RAPID_BOARD" == "true" ]]; then
70
      open_command "${jira_url}/issues/${issue}${url_fragment}"
David Hartmann's avatar
David Hartmann committed
71
    else
72
      open_command "${jira_url}/browse/${issue}${url_fragment}"
David Hartmann's avatar
David Hartmann committed
73
    fi
74
75
76
  fi
}

Andrew Janke's avatar
Andrew Janke committed
77
function _jira_url_help() {
Andrew Stuart's avatar
Andrew Stuart committed
78
  cat << EOF
79
80
error: JIRA URL is not specified anywhere.

Andrew Stuart's avatar
Andrew Stuart committed
81
Valid options, in order of precedence:
Andrew Stuart's avatar
Andrew Stuart committed
82
  .jira-url file
Andrew Stuart's avatar
Andrew Stuart committed
83
  \$HOME/.jira-url file
84
  \$JIRA_URL environment variable
Andrew Stuart's avatar
Andrew Stuart committed
85
86
87
EOF
}

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

108
  echo "Browsing issues ${verb} ${preposition} ${jira_name}"
109
110
  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}"
111
}