jira.plugin.zsh 2.27 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
3
4
5
#         You can also set JIRA_URL in your .zshrc or put .jira-url in your home directory
#         .jira-url in the current directory takes precedence
#
# If you use Rapid Board, set:
6
#JIRA_RAPID_BOARD="true"
7
8
# in you .zshrc
#
Paul Redmond's avatar
Paul Redmond committed
9
# Setup: cd to/my/project
Paul Redmond's avatar
Paul Redmond committed
10
#        echo "https://name.jira.com" >> .jira-url
11
12
13
# Usage: jira           # opens a new issue
#        jira ABC-123   # Opens an existing issue
open_jira_issue () {
14
15
16
17
18
19
  if [ -f .jira-url ]; then
    jira_url=$(cat .jira-url)
  elif [ -f ~/.jira-url ]; then
    jira_url=$(cat ~/.jira-url)
  elif [[ "x$JIRA_URL" != "x" ]]; then
    jira_url=$JIRA_URL
20
  else
21
    echo "JIRA url is not specified anywhere."
22
    return 1
23
24
  fi

25
26
27
28
29
30
31
32
  if [ -f .jira-prefix ]; then
    jira_prefix=$(cat .jira-prefix)
  elif [ -f ~/.jira-prefix ]; then
    jira_prefix=$(cat ~/.jira-prefix)
  else
    jira_prefix=""
  fi

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

55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
jira_name () {
  if [[ -z "$1" ]]; then
    if [[ "x${JIRA_NAME}" != "x" ]]; then
      jira_name=${JIRA_NAME}
    else
      echo "JIRA_NAME not specified"
      return 1
    fi
  else
    jira_name=$@
  fi
}

jira_query () {
    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}"
86
    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"
87
88
}

89
alias jira='open_jira_issue'
90