jira.plugin.zsh 3.23 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
8
9
10
11
12
13
14
15
16
  local action jira_url jira_prefix
  if [[ -f .jira-default-action ]]; then
    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
17
18

  if [[ -f .jira-url ]]; then
19
    jira_url=$(cat .jira-url)
20
  elif [[ -f ~/.jira-url ]]; then
21
    jira_url=$(cat ~/.jira-url)
22
  elif [[ -n "${JIRA_URL}" ]]; then
23
    jira_url=${JIRA_URL}
24
  else
Andrew Janke's avatar
Andrew Janke committed
25
    _jira_url_help
26
    return 1
27
28
  fi

29
  if [[ -f .jira-prefix ]]; then
30
    jira_prefix=$(cat .jira-prefix)
31
  elif [[ -f ~/.jira-prefix ]]; then
32
    jira_prefix=$(cat ~/.jira-prefix)
33
  elif [[ -n "${JIRA_PREFIX}" ]]; then
34
    jira_prefix=${JIRA_PREFIX}
35
36
37
38
  else
    jira_prefix=""
  fi

39
40

  if [[ $action == "new" ]]; then
41
    echo "Opening new issue"
42
    open_command "${jira_url}/secure/CreateIssue!default.jspa"
43
  elif [[ "$action" == "assigned" || "$action" == "reported" ]]; then
Andrew Janke's avatar
Andrew Janke committed
44
    _jira_query $@
45
46
  elif [[ "$action" == "dashboard" ]]; then
    echo "Opening dashboard"
47
48
49
50
51
    if [[ "$JIRA_RAPID_BOARD" == "true" ]]; then
      open_command "${jira_url}/secure/RapidBoard.jspa"
    else
      open_command "${jira_url}/secure/Dashboard.jspa"
    fi
52
53
54
55
56
57
  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"
58
  else
59
    # Anything that doesn't match a special action is considered an issue name
60
61
62
63
64
65
66
67
    # 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
68
    local url_fragment=''
David Hartmann's avatar
David Hartmann committed
69
    if [[ "$2" == "m" ]]; then
70
71
      url_fragment="#add-comment"
      echo "Add comment to issue #$issue"
David Hartmann's avatar
David Hartmann committed
72
    else
73
      echo "Opening issue #$issue"
David Hartmann's avatar
David Hartmann committed
74
    fi
75
    if [[ "$JIRA_RAPID_BOARD" == "true" ]]; then
76
      open_command "${jira_url}/issues/${issue}${url_fragment}"
David Hartmann's avatar
David Hartmann committed
77
    else
78
      open_command "${jira_url}/browse/${issue}${url_fragment}"
David Hartmann's avatar
David Hartmann committed
79
    fi
80
81
82
  fi
}

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

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

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

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