From b1da3bbc6f5e26cd0b06cd858e696d4f47f340d2 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Patryk=20Ma=C5=82ek?= <malekpatryk@gmail.com>
Date: Mon, 6 Jul 2015 22:00:53 +0200
Subject: [PATCH 01/10] Adding ability to add JIRA_PREFIX as an env variable
 (e.g. in .zshrc) and minor refactor to bash curly variable braces

---
 plugins/jira/jira.plugin.zsh | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)

diff --git a/plugins/jira/jira.plugin.zsh b/plugins/jira/jira.plugin.zsh
index ca540c84..a22e1936 100644
--- a/plugins/jira/jira.plugin.zsh
+++ b/plugins/jira/jira.plugin.zsh
@@ -1,6 +1,7 @@
 # To use: add a .jira-url file in the base of your project
 #         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
+#         .jira-url in the current directory takes precedence. The same goes with .jira-prefix
+#         and JIRA_PREFIX.
 #
 # If you use Rapid Board, set:
 #JIRA_RAPID_BOARD="true"
@@ -22,8 +23,8 @@ open_jira_issue () {
     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
+  elif [[ "${JIRA_URL}" != "" ]]; then
+    jira_url=${JIRA_URL}
   else
     echo "JIRA url is not specified anywhere."
     return 1
@@ -33,6 +34,8 @@ open_jira_issue () {
     jira_prefix=$(cat .jira-prefix)
   elif [ -f ~/.jira-prefix ]; then
     jira_prefix=$(cat ~/.jira-prefix)
+  elif [[ "${JIRA_PREFIX}" != "" ]]; then
+    jira_prefix=${JIRA_PREFIX}
   else
     jira_prefix=""
   fi
@@ -51,7 +54,7 @@ open_jira_issue () {
       echo "Opening issue #$1"
     fi
     
-    if [[ "x$JIRA_RAPID_BOARD" = "xtrue" ]]; then
+    if [[ "$JIRA_RAPID_BOARD" = "true" ]]; then
       $open_cmd  "$jira_url/issues/$jira_prefix$1$addcomment"
     else
       $open_cmd  "$jira_url/browse/$jira_prefix$1$addcomment"
@@ -61,7 +64,7 @@ open_jira_issue () {
 
 jira_name () {
   if [[ -z "$1" ]]; then
-    if [[ "x${JIRA_NAME}" != "x" ]]; then
+    if [[ "${JIRA_NAME}" != "" ]]; then
       jira_name=${JIRA_NAME}
     else
       echo "JIRA_NAME not specified"

From 88a30a3e2f3089556fe40c719f4cbed8426a144e Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Patryk=20Ma=C5=82ek?= <malekpatryk@gmail.com>
Date: Tue, 7 Jul 2015 18:50:23 +0200
Subject: [PATCH 02/10] Changing string comparison for more zsh-like

---
 plugins/jira/jira.plugin.zsh | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/plugins/jira/jira.plugin.zsh b/plugins/jira/jira.plugin.zsh
index a22e1936..042fcd7a 100644
--- a/plugins/jira/jira.plugin.zsh
+++ b/plugins/jira/jira.plugin.zsh
@@ -54,7 +54,7 @@ open_jira_issue () {
       echo "Opening issue #$1"
     fi
     
-    if [[ "$JIRA_RAPID_BOARD" = "true" ]]; then
+    if [[ "$JIRA_RAPID_BOARD" == "true" ]]; then
       $open_cmd  "$jira_url/issues/$jira_prefix$1$addcomment"
     else
       $open_cmd  "$jira_url/browse/$jira_prefix$1$addcomment"

From 1e99168627eef4c61114cb0572047473b7cb56be Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Patryk=20Ma=C5=82ek?= <malekpatryk@gmail.com>
Date: Tue, 7 Jul 2015 19:56:22 +0200
Subject: [PATCH 03/10] Changing indents to be more consistent (2 spaces)

---
 plugins/jira/jira.plugin.zsh | 36 ++++++++++++++++++------------------
 1 file changed, 18 insertions(+), 18 deletions(-)

diff --git a/plugins/jira/jira.plugin.zsh b/plugins/jira/jira.plugin.zsh
index 042fcd7a..769bf236 100644
--- a/plugins/jira/jira.plugin.zsh
+++ b/plugins/jira/jira.plugin.zsh
@@ -76,24 +76,24 @@ jira_name () {
 }
 
 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}"
-    $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"
+  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"
 }
 
 alias jira='open_jira_issue'

From 37f45eb621b04a1286ac1667b7474111e6e0b4ce Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Patryk=20Ma=C5=82ek?= <malekpatryk@gmail.com>
Date: Tue, 7 Jul 2015 20:03:58 +0200
Subject: [PATCH 04/10] Making variables local in jira plugin

---
 plugins/jira/jira.plugin.zsh | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)

diff --git a/plugins/jira/jira.plugin.zsh b/plugins/jira/jira.plugin.zsh
index 769bf236..7839e2a8 100644
--- a/plugins/jira/jira.plugin.zsh
+++ b/plugins/jira/jira.plugin.zsh
@@ -19,6 +19,7 @@ open_jira_issue () {
     open_cmd='xdg-open'
   fi
 
+  local jira_url
   if [ -f .jira-url ]; then
     jira_url=$(cat .jira-url)
   elif [ -f ~/.jira-url ]; then
@@ -30,6 +31,7 @@ open_jira_issue () {
     return 1
   fi
 
+  local jira_prefix
   if [ -f .jira-prefix ]; then
     jira_prefix=$(cat .jira-prefix)
   elif [ -f ~/.jira-prefix ]; then
@@ -45,7 +47,7 @@ open_jira_issue () {
     $open_cmd "${jira_url}/secure/CreateIssue!default.jspa"
   elif [[ "$1" = "assigned" || "$1" = "reported" ]]; then
     jira_query $@
-  else 
+  else
     local addcomment=''
     if [[ "$2" == "m" ]]; then
       addcomment="#add-comment"
@@ -53,11 +55,11 @@ open_jira_issue () {
     else
       echo "Opening issue #$1"
     fi
-    
+
     if [[ "$JIRA_RAPID_BOARD" == "true" ]]; then
-      $open_cmd  "$jira_url/issues/$jira_prefix$1$addcomment"
+      $open_cmd "$jira_url/issues/$jira_prefix$1$addcomment"
     else
-      $open_cmd  "$jira_url/browse/$jira_prefix$1$addcomment"
+      $open_cmd "$jira_url/browse/$jira_prefix$1$addcomment"
     fi
   fi
 }
@@ -76,7 +78,11 @@ jira_name () {
 }
 
 jira_query () {
+  local jira_name
+  local verb
   verb="$1"
+  local lookup
+  local preposition
   if [[ "${verb}" = "reported" ]]; then
     lookup=reporter
     preposition=by

From b1772c5333fcc43610ade75384d9cf87c8ccde61 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Patryk=20Ma=C5=82ek?= <malekpatryk@gmail.com>
Date: Tue, 7 Jul 2015 20:07:13 +0200
Subject: [PATCH 05/10] More consistent curly braces variables

---
 plugins/jira/jira.plugin.zsh | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/plugins/jira/jira.plugin.zsh b/plugins/jira/jira.plugin.zsh
index 7839e2a8..566e1f1f 100644
--- a/plugins/jira/jira.plugin.zsh
+++ b/plugins/jira/jira.plugin.zsh
@@ -57,9 +57,9 @@ open_jira_issue () {
     fi
 
     if [[ "$JIRA_RAPID_BOARD" == "true" ]]; then
-      $open_cmd "$jira_url/issues/$jira_prefix$1$addcomment"
+      $open_cmd "${jira_url}/issues/${jira_prefix}${1}${addcomment}"
     else
-      $open_cmd "$jira_url/browse/$jira_prefix$1$addcomment"
+      $open_cmd "${jira_url}/browse/${jira_prefix}${1}${addcomment}"
     fi
   fi
 }

From ad4675cb0a8f732abef7b9a2b3df2141afbf79aa Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Patryk=20Ma=C5=82ek?= <malekpatryk@gmail.com>
Date: Tue, 7 Jul 2015 20:26:00 +0200
Subject: [PATCH 06/10] Concise local variables declarations

---
 plugins/jira/jira.plugin.zsh | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)

diff --git a/plugins/jira/jira.plugin.zsh b/plugins/jira/jira.plugin.zsh
index 566e1f1f..22984549 100644
--- a/plugins/jira/jira.plugin.zsh
+++ b/plugins/jira/jira.plugin.zsh
@@ -78,11 +78,8 @@ jira_name () {
 }
 
 jira_query () {
-  local jira_name
-  local verb
-  verb="$1"
-  local lookup
-  local preposition
+  local verb="$1"
+  local jira_name lookup preposition
   if [[ "${verb}" = "reported" ]]; then
     lookup=reporter
     preposition=by

From afa30572f522969e07b0f85f0cf0fe0e32dd8831 Mon Sep 17 00:00:00 2001
From: Andrew Stuart <andrew.stuart2@gmail.com>
Date: Mon, 13 Jul 2015 13:08:18 -0700
Subject: [PATCH 07/10] Add jira_url_help

---
 plugins/jira/jira.plugin.zsh | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)

diff --git a/plugins/jira/jira.plugin.zsh b/plugins/jira/jira.plugin.zsh
index ca540c84..f624648a 100644
--- a/plugins/jira/jira.plugin.zsh
+++ b/plugins/jira/jira.plugin.zsh
@@ -25,7 +25,7 @@ open_jira_issue () {
   elif [[ "x$JIRA_URL" != "x" ]]; then
     jira_url=$JIRA_URL
   else
-    echo "JIRA url is not specified anywhere."
+    jira_url_help
     return 1
   fi
 
@@ -59,6 +59,16 @@ open_jira_issue () {
   fi
 }
 
+jira_url_help() {
+  cat << EOF
+JIRA url is not specified anywhere.
+Valid options, in order of preference:
+  .jira-url file
+  $HOME/.jira-url file
+  JIRA_URL environment variable
+EOF
+}
+
 jira_name () {
   if [[ -z "$1" ]]; then
     if [[ "x${JIRA_NAME}" != "x" ]]; then

From 2120b2e079dbcc887a13aaf976c04edaf10fb626 Mon Sep 17 00:00:00 2001
From: Andrew Stuart <andrew.stuart2@gmail.com>
Date: Wed, 29 Jul 2015 13:58:22 -0700
Subject: [PATCH 08/10] Add escaped home

---
 plugins/jira/jira.plugin.zsh | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/plugins/jira/jira.plugin.zsh b/plugins/jira/jira.plugin.zsh
index f624648a..ed1a1f47 100644
--- a/plugins/jira/jira.plugin.zsh
+++ b/plugins/jira/jira.plugin.zsh
@@ -62,9 +62,9 @@ open_jira_issue () {
 jira_url_help() {
   cat << EOF
 JIRA url is not specified anywhere.
-Valid options, in order of preference:
+Valid options, in order of precedence:
   .jira-url file
-  $HOME/.jira-url file
+  \$HOME/.jira-url file
   JIRA_URL environment variable
 EOF
 }

From 7cf892ecaa86f064aa3f78b9d5bee3b7db208ca4 Mon Sep 17 00:00:00 2001
From: Andrew Janke <andrew@apjanke.net>
Date: Tue, 18 Aug 2015 04:46:52 -0400
Subject: [PATCH 09/10] Add dashboard support and default-action configuration.
 Some refactoring.

---
 plugins/jira/jira.plugin.zsh | 118 ++++++++++++++++++++---------------
 1 file changed, 66 insertions(+), 52 deletions(-)

diff --git a/plugins/jira/jira.plugin.zsh b/plugins/jira/jira.plugin.zsh
index 842dff5c..a10feec3 100644
--- a/plugins/jira/jira.plugin.zsh
+++ b/plugins/jira/jira.plugin.zsh
@@ -1,61 +1,88 @@
-# To use: add a .jira-url file in the base of your project
-#         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. The same goes with .jira-prefix
-#         and JIRA_PREFIX.
+# CLI support for JIRA interaction
 #
-# If you use Rapid Board, set:
-#JIRA_RAPID_BOARD="true"
-# in you .zshrc
+# Setup: 
+#   Add a .jira-url file in the base of your project
+#   You can also set $JIRA_URL in your .zshrc or put .jira-url in your home directory
+#   A .jira-url in the current directory takes precedence. 
+#   The same goes with .jira-prefix and $JIRA_PREFIX.
 #
-# Setup: cd to/my/project
-#        echo "https://name.jira.com" >> .jira-url
-# Usage: jira           # opens a new issue
-#        jira ABC-123   # Opens an existing issue
-open_jira_issue () {
-  if [ -f .jira-url ]; then
+#   For example:
+#     cd to/my/project
+#     echo "https://name.jira.com" >> .jira-url
+#
+# Variables:
+#  $JIRA_RAPID_BOARD     - set to "true" if you use Rapid Board
+#  $JIRA_DEFAULT_ACTION  - action to do when `jira` is called witn no args
+#                          defaults to "new"
+#  $JIRA_NAME            - Your JIRA username. Used as default for assigned/reported
+#  $JIRA_PREFIX          - Prefix added to issue ID arguments
+#
+#
+# Usage: 
+#   jira            # Performs the default action
+#   jira new        # opens a new issue
+#   jira reported [username]
+#   jira assigned [username]
+#   jira dashboard
+#   jira ABC-123    # Opens an existing issue
+#   jira ABC-123 m  # Opens an existing issue for adding a comment
+
+: ${JIRA_DEFAULT_ACTION:=new}
+
+function jira() {
+  local action=${1:=$JIRA_DEFAULT_ACTION}
+
+  local jira_url jira_prefix
+  if [[ -f .jira-url ]]; then
     jira_url=$(cat .jira-url)
-  elif [ -f ~/.jira-url ]; then
+  elif [[ -f ~/.jira-url ]]; then
     jira_url=$(cat ~/.jira-url)
-  elif [[ "${JIRA_URL}" != "" ]]; then
+  elif [[ -n "${JIRA_URL}" ]]; then
     jira_url=${JIRA_URL}
   else
     jira_url_help
     return 1
   fi
 
-  local jira_prefix
-  if [ -f .jira-prefix ]; then
+  if [[ -f .jira-prefix ]]; then
     jira_prefix=$(cat .jira-prefix)
-  elif [ -f ~/.jira-prefix ]; then
+  elif [[ -f ~/.jira-prefix ]]; then
     jira_prefix=$(cat ~/.jira-prefix)
-  elif [[ "${JIRA_PREFIX}" != "" ]]; then
+  elif [[ -n "${JIRA_PREFIX}" ]]; then
     jira_prefix=${JIRA_PREFIX}
   else
     jira_prefix=""
   fi
 
-  if [ -z "$1" ]; then
+
+  if [[ $action == "new" ]]; then
     echo "Opening new issue"
     open_command "${jira_url}/secure/CreateIssue!default.jspa"
-  elif [[ "$1" = "assigned" || "$1" = "reported" ]]; then
+  elif [[ "$action" == "assigned" || "$action" == "reported" ]]; then
     jira_query $@
+  elif [[ "$action" == "dashboard" ]]; then
+    echo "Opening dashboard"
+    open_command "${jira_url}/secure/Dashboard.jspa"
   else
-    local addcomment=''
+    # Anything that doesn't match a special action is considered an issue name
+    local issue_arg=$action
+    local issue="${jira_prefix}${issue_arg}"
+    local url_fragment=''
     if [[ "$2" == "m" ]]; then
-      addcomment="#add-comment"
-      echo "Add comment to issue #$1"
+      url_fragment="#add-comment"
+      echo "Add comment to issue #$issue"
     else
-      echo "Opening issue #$1"
+      echo "Opening issue #$issue"
     fi
     if [[ "$JIRA_RAPID_BOARD" == "true" ]]; then
-      open_command "${jira_url}/issues/${jira_prefix}${1}${addcomment}"
+      open_command "${jira_url}/issues/${issue}${url_fragment}"
     else
-      open_command "${jira_url}/browse/${jira_prefix}${1}${addcomment}"
+      open_command "${jira_url}/browse/${issue}${url_fragment}"
     fi
   fi
 }
 
-jira_url_help() {
+function jira_url_help() {
   cat << EOF
 JIRA url is not specified anywhere.
 Valid options, in order of precedence:
@@ -65,40 +92,27 @@ Valid options, in order of precedence:
 EOF
 }
 
-jira_name () {
-  if [[ -z "$1" ]]; then
-    if [[ "${JIRA_NAME}" != "" ]]; then
-      jira_name=${JIRA_NAME}
-    else
-      echo "JIRA_NAME not specified"
-      return 1
-    fi
-  else
-    jira_name=$@
-  fi
-}
-
-jira_query () {
+function jira_query() {
   local verb="$1"
-  local jira_name lookup preposition
-  if [[ "${verb}" = "reported" ]]; then
+  local jira_name lookup preposition query
+  if [[ "${verb}" == "reported" ]]; then
     lookup=reporter
     preposition=by
-  elif [[ "${verb}" = "assigned" ]]; then
+  elif [[ "${verb}" == "assigned" ]]; then
     lookup=assignee
     preposition=to
   else
-    echo "not a valid lookup $verb"
+    echo "not a valid lookup: $verb" >&2
     return 1
   fi
-  shift 1
-  jira_name $@
-  if [[ $? = 1 ]]; then
+  jira_name=${2:=$JIRA_NAME}
+  if [[ -z $jira_name ]]; then
+    echo "JIRA_NAME not specified" >&2
     return 1
   fi
+
   echo "Browsing issues ${verb} ${preposition} ${jira_name}"
-  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"
+  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}"
 }
 
-alias jira='open_jira_issue'
-

From dc42e8d488d7b7b5285e71947940c7d6fb19355b Mon Sep 17 00:00:00 2001
From: Andrew Janke <andrew@apjanke.net>
Date: Tue, 18 Aug 2015 05:05:54 -0400
Subject: [PATCH 10/10] jira: add completion

---
 plugins/jira/_jira           | 21 +++++++++++++++++++++
 plugins/jira/jira.plugin.zsh |  8 ++++----
 2 files changed, 25 insertions(+), 4 deletions(-)
 create mode 100644 plugins/jira/_jira

diff --git a/plugins/jira/_jira b/plugins/jira/_jira
new file mode 100644
index 00000000..b0ea658e
--- /dev/null
+++ b/plugins/jira/_jira
@@ -0,0 +1,21 @@
+#compdef jira
+#autoload
+
+local -a _1st_arguments
+_1st_arguments=(
+  'new:create a new issue'
+  'dashboard:open the dashboard'
+  'reported:search for issues reported by a user'
+  'assigned:search for issues assigned to a user'
+)
+
+_arguments -C \
+  ':command:->command' \
+  '*::options:->options'
+
+case $state in
+  (command)
+    _describe -t commands "jira subcommand" _1st_arguments
+    return
+   ;;
+esac
diff --git a/plugins/jira/jira.plugin.zsh b/plugins/jira/jira.plugin.zsh
index a10feec3..89559767 100644
--- a/plugins/jira/jira.plugin.zsh
+++ b/plugins/jira/jira.plugin.zsh
@@ -40,7 +40,7 @@ function jira() {
   elif [[ -n "${JIRA_URL}" ]]; then
     jira_url=${JIRA_URL}
   else
-    jira_url_help
+    _jira_url_help
     return 1
   fi
 
@@ -59,7 +59,7 @@ function jira() {
     echo "Opening new issue"
     open_command "${jira_url}/secure/CreateIssue!default.jspa"
   elif [[ "$action" == "assigned" || "$action" == "reported" ]]; then
-    jira_query $@
+    _jira_query $@
   elif [[ "$action" == "dashboard" ]]; then
     echo "Opening dashboard"
     open_command "${jira_url}/secure/Dashboard.jspa"
@@ -82,7 +82,7 @@ function jira() {
   fi
 }
 
-function jira_url_help() {
+function _jira_url_help() {
   cat << EOF
 JIRA url is not specified anywhere.
 Valid options, in order of precedence:
@@ -92,7 +92,7 @@ Valid options, in order of precedence:
 EOF
 }
 
-function jira_query() {
+function _jira_query() {
   local verb="$1"
   local jira_name lookup preposition query
   if [[ "${verb}" == "reported" ]]; then