jira.plugin.zsh 2.57 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
  local open_cmd
16
  if [[ "$OSTYPE" = darwin* ]]; then
17
18
19
20
21
    open_cmd='open'
  else
    open_cmd='xdg-open'
  fi

22
  local jira_url
23
24
25
26
  if [ -f .jira-url ]; then
    jira_url=$(cat .jira-url)
  elif [ -f ~/.jira-url ]; then
    jira_url=$(cat ~/.jira-url)
27
28
  elif [[ "${JIRA_URL}" != "" ]]; then
    jira_url=${JIRA_URL}
29
  else
30
    echo "JIRA url is not specified anywhere."
31
    return 1
32
33
  fi

34
  local jira_prefix
35
36
37
38
  if [ -f .jira-prefix ]; then
    jira_prefix=$(cat .jira-prefix)
  elif [ -f ~/.jira-prefix ]; then
    jira_prefix=$(cat ~/.jira-prefix)
39
40
  elif [[ "${JIRA_PREFIX}" != "" ]]; then
    jira_prefix=${JIRA_PREFIX}
41
42
43
44
  else
    jira_prefix=""
  fi

45
46
  if [ -z "$1" ]; then
    echo "Opening new issue"
47
48
49
    $open_cmd "${jira_url}/secure/CreateIssue!default.jspa"
  elif [[ "$1" = "assigned" || "$1" = "reported" ]]; then
    jira_query $@
50
  else
David Hartmann's avatar
David Hartmann committed
51
52
53
54
55
56
57
    local addcomment=''
    if [[ "$2" == "m" ]]; then
      addcomment="#add-comment"
      echo "Add comment to issue #$1"
    else
      echo "Opening issue #$1"
    fi
58

59
    if [[ "$JIRA_RAPID_BOARD" == "true" ]]; then
60
      $open_cmd "${jira_url}/issues/${jira_prefix}${1}${addcomment}"
David Hartmann's avatar
David Hartmann committed
61
    else
62
      $open_cmd "${jira_url}/browse/${jira_prefix}${1}${addcomment}"
David Hartmann's avatar
David Hartmann committed
63
    fi
64
65
66
  fi
}

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

jira_query () {
81
82
  local jira_name
  local verb
83
  verb="$1"
84
85
  local lookup
  local preposition
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
  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}"
  $open_cmd "${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"
103
104
}

105
alias jira='open_jira_issue'
106