jira.plugin.zsh 3.71 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
Dmitry's avatar
Dmitry committed
57
58
59
  elif [[ "$action" == "tempo" ]]; then
    echo "Opening tempo"
    open_command "${jira_url}/secure/Tempo.jspa"
60
61
62
63
64
65
  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"
66
  else
67
    # Anything that doesn't match a special action is considered an issue name
68
    # but `branch` is a special case that will parse the current git branch
Marc Cornellà's avatar
Marc Cornellà committed
69
    local issue_arg issue
70
    if [[ "$action" == "branch" ]]; then
Marc Cornellà's avatar
Marc Cornellà committed
71
72
      # Get name of the branch
      issue_arg=$(git rev-parse --abbrev-ref HEAD)
73
74
75
      # Strip prefixes like feature/ or bugfix/
      issue_arg=${issue_arg##*/}
      # Strip suffixes starting with _
Marc Cornellà's avatar
Marc Cornellà committed
76
77
78
79
      issue_arg=(${(s:_:)issue_arg})
      issue_arg=${issue_arg[1]}
      if [[ "$issue_arg" = ${jira_prefix}* ]]; then
        issue="${issue_arg}"
RoToRx88's avatar
RoToRx88 committed
80
      else
Marc Cornellà's avatar
Marc Cornellà committed
81
        issue="${jira_prefix}${issue_arg}"
RoToRx88's avatar
RoToRx88 committed
82
      fi
83
    else
Marc Cornellà's avatar
Marc Cornellà committed
84
85
      issue_arg=${(U)action}
      issue="${jira_prefix}${issue_arg}"
86
    fi
Marc Cornellà's avatar
Marc Cornellà committed
87
88

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

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

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

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

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