jira.plugin.zsh 3.27 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
Andrew Janke's avatar
Andrew Janke committed
46
    _jira_query $@
47
48
  elif [[ "$action" == "dashboard" ]]; then
    echo "Opening dashboard"
49
50
51
52
53
    if [[ "$JIRA_RAPID_BOARD" == "true" ]]; then
      open_command "${jira_url}/secure/RapidBoard.jspa"
    else
      open_command "${jira_url}/secure/Dashboard.jspa"
    fi
54
55
56
57
58
59
  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"
60
  else
61
    # Anything that doesn't match a special action is considered an issue name
62
63
64
65
66
67
68
69
    # 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
70
    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
}

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

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

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

116
  echo "Browsing issues ${verb} ${preposition} ${jira_name}"
117
118
  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}"
119
}