jira.plugin.zsh 3.4 KB
Newer Older
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
9
10
  if [[ -n "$1" ]]; then
    action=$1
  elif [[ -f .jira-default-action ]]; then
11
12
13
14
15
16
17
18
    action=$(cat .jira-default-action)
  elif [[ -f ~/.jira-default-action ]]; then
    action=$(cat ~/.jira-default-action)
  elif [[ -n "${JIRA_DEFAULT_ACTION}" ]]; then
    action=${JIRA_DEFAULT_ACTION}
  else
    action="new"
  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
Andrew Janke's avatar
Andrew Janke committed
27
    _jira_url_help
28
    return 1
29
30
  fi

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
38
39
40
  else
    jira_prefix=""
  fi

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}
Zopanix's avatar
Zopanix committed
47
48
49
  elif [[ "$action" == "myissues" ]]; then
    echo "Opening my issues"
    open_command "${jira_url}/issues/?filter=-1"
50
51
  elif [[ "$action" == "dashboard" ]]; then
    echo "Opening dashboard"
52
53
54
55
56
    if [[ "$JIRA_RAPID_BOARD" == "true" ]]; then
      open_command "${jira_url}/secure/RapidBoard.jspa"
    else
      open_command "${jira_url}/secure/Dashboard.jspa"
    fi
57
58
59
60
61
62
  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"
63
  else
64
    # Anything that doesn't match a special action is considered an issue name
65
    # but `branch` is a special case that will parse the current git branch
66
    if [[ "$action" == "branch" ]]; then
67
68
69
70
71
72
      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
73
    local url_fragment=''
David Hartmann's avatar
David Hartmann committed
74
    if [[ "$2" == "m" ]]; then
75
76
      url_fragment="#add-comment"
      echo "Add comment to issue #$issue"
David Hartmann's avatar
David Hartmann committed
77
    else
78
      echo "Opening issue #$issue"
David Hartmann's avatar
David Hartmann committed
79
    fi
80
    if [[ "$JIRA_RAPID_BOARD" == "true" ]]; then
81
      open_command "${jira_url}/issues/${issue}${url_fragment}"
David Hartmann's avatar
David Hartmann committed
82
    else
83
      open_command "${jira_url}/browse/${issue}${url_fragment}"
David Hartmann's avatar
David Hartmann committed
84
    fi
85
86
87
  fi
}

Andrew Janke's avatar
Andrew Janke committed
88
function _jira_url_help() {
Andrew Stuart's avatar
Andrew Stuart committed
89
  cat << EOF
90
91
error: JIRA URL is not specified anywhere.

Andrew Stuart's avatar
Andrew Stuart committed
92
Valid options, in order of precedence:
Andrew Stuart's avatar
Andrew Stuart committed
93
  .jira-url file
Andrew Stuart's avatar
Andrew Stuart committed
94
  \$HOME/.jira-url file
95
  \$JIRA_URL environment variable
Andrew Stuart's avatar
Andrew Stuart committed
96
97
98
EOF
}

Andrew Janke's avatar
Andrew Janke committed
99
function _jira_query() {
100
  emulate -L zsh
101
  local verb="$1"
102
103
  local jira_name lookup preposition query
  if [[ "${verb}" == "reported" ]]; then
104
105
    lookup=reporter
    preposition=by
106
  elif [[ "${verb}" == "assigned" ]]; then
107
108
109
    lookup=assignee
    preposition=to
  else
110
    echo "error: not a valid lookup: $verb" >&2
111
112
    return 1
  fi
113
114
  jira_name=${2:=$JIRA_NAME}
  if [[ -z $jira_name ]]; then
115
    echo "error: JIRA_NAME not specified" >&2
116
117
    return 1
  fi
118

119
  echo "Browsing issues ${verb} ${preposition} ${jira_name}"
120
121
  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}"
122
}