jira.plugin.zsh 2.46 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
23
24
25
  if [ -f .jira-url ]; then
    jira_url=$(cat .jira-url)
  elif [ -f ~/.jira-url ]; then
    jira_url=$(cat ~/.jira-url)
26
27
  elif [[ "${JIRA_URL}" != "" ]]; then
    jira_url=${JIRA_URL}
28
  else
29
    echo "JIRA url is not specified anywhere."
30
    return 1
31
32
  fi

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

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

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

jira_query () {
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
  verb="$1"
  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"
97
98
}

99
alias jira='open_jira_issue'
100