jira.plugin.zsh 3.66 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
Marc Cornellà's avatar
Marc Cornellà committed
66
    local issue_arg issue
67
    if [[ "$action" == "branch" ]]; then
Marc Cornellà's avatar
Marc Cornellà committed
68
69
70
71
72
73
74
      # Get name of the branch
      issue_arg=$(git rev-parse --abbrev-ref HEAD)
      # Split by _ character and get the first element
      issue_arg=(${(s:_:)issue_arg})
      issue_arg=${issue_arg[1]}
      if [[ "$issue_arg" = ${jira_prefix}* ]]; then
        issue="${issue_arg}"
RoToRx88's avatar
RoToRx88 committed
75
      else
Marc Cornellà's avatar
Marc Cornellà committed
76
        issue="${jira_prefix}${issue_arg}"
RoToRx88's avatar
RoToRx88 committed
77
      fi
78
    else
Marc Cornellà's avatar
Marc Cornellà committed
79
80
      issue_arg=${(U)action}
      issue="${jira_prefix}${issue_arg}"
81
    fi
Marc Cornellà's avatar
Marc Cornellà committed
82
83

    local url_fragment
David Hartmann's avatar
David Hartmann committed
84
    if [[ "$2" == "m" ]]; then
85
86
      url_fragment="#add-comment"
      echo "Add comment to issue #$issue"
David Hartmann's avatar
David Hartmann committed
87
    else
88
      echo "Opening issue #$issue"
David Hartmann's avatar
David Hartmann committed
89
    fi
90
    if [[ "$JIRA_RAPID_BOARD" == "true" ]]; then
91
      open_command "${jira_url}/issues/${issue}${url_fragment}"
David Hartmann's avatar
David Hartmann committed
92
    else
93
      open_command "${jira_url}/browse/${issue}${url_fragment}"
David Hartmann's avatar
David Hartmann committed
94
    fi
95
96
97
  fi
}

Andrew Janke's avatar
Andrew Janke committed
98
function _jira_url_help() {
Andrew Stuart's avatar
Andrew Stuart committed
99
  cat << EOF
100
101
error: JIRA URL is not specified anywhere.

Andrew Stuart's avatar
Andrew Stuart committed
102
Valid options, in order of precedence:
Andrew Stuart's avatar
Andrew Stuart committed
103
  .jira-url file
Andrew Stuart's avatar
Andrew Stuart committed
104
  \$HOME/.jira-url file
105
  \$JIRA_URL environment variable
Andrew Stuart's avatar
Andrew Stuart committed
106
107
108
EOF
}

Andrew Janke's avatar
Andrew Janke committed
109
function _jira_query() {
110
  emulate -L zsh
111
  local verb="$1"
112
113
  local jira_name lookup preposition query
  if [[ "${verb}" == "reported" ]]; then
114
115
    lookup=reporter
    preposition=by
116
  elif [[ "${verb}" == "assigned" ]]; then
117
118
119
    lookup=assignee
    preposition=to
  else
120
    echo "error: not a valid lookup: $verb" >&2
121
122
    return 1
  fi
123
124
  jira_name=${2:=$JIRA_NAME}
  if [[ -z $jira_name ]]; then
125
    echo "error: JIRA_NAME not specified" >&2
126
127
    return 1
  fi
128

129
  echo "Browsing issues ${verb} ${preposition} ${jira_name}"
130
131
  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}"
132
}