jira.plugin.zsh 2.58 KB
Newer Older
Paul Redmond's avatar
Paul Redmond committed
1
# To use: add a .jira-url file in the base of your project
2
#         You can also set JIRA_URL in your .zshrc or put .jira-url in your home directory
3
4
#         .jira-url in the current directory takes precedence. The same goes with .jira-prefix
#         and JIRA_PREFIX.
5
6
#
# If you use Rapid Board, set:
7
#JIRA_RAPID_BOARD="true"
8
9
# in you .zshrc
#
Paul Redmond's avatar
Paul Redmond committed
10
# Setup: cd to/my/project
Paul Redmond's avatar
Paul Redmond committed
11
#        echo "https://name.jira.com" >> .jira-url
12
13
14
# Usage: jira           # opens a new issue
#        jira ABC-123   # Opens an existing issue
open_jira_issue () {
15
16
17
18
  if [ -f .jira-url ]; then
    jira_url=$(cat .jira-url)
  elif [ -f ~/.jira-url ]; then
    jira_url=$(cat ~/.jira-url)
19
20
  elif [[ "${JIRA_URL}" != "" ]]; then
    jira_url=${JIRA_URL}
21
  else
Andrew Stuart's avatar
Andrew Stuart committed
22
    jira_url_help
23
    return 1
24
25
  fi

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

37
38
  if [ -z "$1" ]; then
    echo "Opening new issue"
39
    open_command "${jira_url}/secure/CreateIssue!default.jspa"
40
41
  elif [[ "$1" = "assigned" || "$1" = "reported" ]]; then
    jira_query $@
42
  else
David Hartmann's avatar
David Hartmann committed
43
44
45
46
47
48
49
    local addcomment=''
    if [[ "$2" == "m" ]]; then
      addcomment="#add-comment"
      echo "Add comment to issue #$1"
    else
      echo "Opening issue #$1"
    fi
50
    if [[ "$JIRA_RAPID_BOARD" == "true" ]]; then
51
      open_command "${jira_url}/issues/${jira_prefix}${1}${addcomment}"
David Hartmann's avatar
David Hartmann committed
52
    else
53
      open_command "${jira_url}/browse/${jira_prefix}${1}${addcomment}"
David Hartmann's avatar
David Hartmann committed
54
    fi
55
56
57
  fi
}

Andrew Stuart's avatar
Andrew Stuart committed
58
59
60
jira_url_help() {
  cat << EOF
JIRA url is not specified anywhere.
Andrew Stuart's avatar
Andrew Stuart committed
61
Valid options, in order of precedence:
Andrew Stuart's avatar
Andrew Stuart committed
62
  .jira-url file
Andrew Stuart's avatar
Andrew Stuart committed
63
  \$HOME/.jira-url file
Andrew Stuart's avatar
Andrew Stuart committed
64
65
66
67
  JIRA_URL environment variable
EOF
}

68
69
jira_name () {
  if [[ -z "$1" ]]; then
70
    if [[ "${JIRA_NAME}" != "" ]]; then
71
72
73
74
75
76
77
78
79
80
81
      jira_name=${JIRA_NAME}
    else
      echo "JIRA_NAME not specified"
      return 1
    fi
  else
    jira_name=$@
  fi
}

jira_query () {
82
83
  local verb="$1"
  local jira_name lookup preposition
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
  if [[ "${verb}" = "reported" ]]; then
    lookup=reporter
    preposition=by
  elif [[ "${verb}" = "assigned" ]]; then
    lookup=assignee
    preposition=to
  else
    echo "not a valid lookup $verb"
    return 1
  fi
  shift 1
  jira_name $@
  if [[ $? = 1 ]]; then
    return 1
  fi
  echo "Browsing issues ${verb} ${preposition} ${jira_name}"
100
  open_command "${jira_url}/secure/IssueNavigator.jspa?reset=true&jqlQuery=${lookup}+%3D+%22${jira_name}%22+AND+resolution+%3D+unresolved+ORDER+BY+priority+DESC%2C+created+ASC"
101
102
}

103
alias jira='open_jira_issue'
104