5fd2059e5eda17ee07501a21d47189d4b96d6ff3

Author
Ken van der Eerden <15888558+Ken-vdE@users.noreply.github.com>
Committer
GitHub <noreply@github.com>
Date

Message

feat(jira): allow branch name customization (#12850)

Co-authored-by: Carlo Sala <carlosalag@protonmail.com>

Diff

 1diff --git a/plugins/jira/README.md b/plugins/jira/README.md
 2index 7cfb81b19c8c11902483249e1d5990177d7eb8c0..19266e7f0721af5476adfc10e7b6cd25f7e40bf0 100644
 3--- a/plugins/jira/README.md
 4+++ b/plugins/jira/README.md
 5@@ -43,6 +43,22 @@ starting with "_": "MP-1234_fix_dashboard". In both these cases, the issue opene
 6 This is also checks if the prefix is in the name, and adds it if not, so: "MP-1234" opens the issue "MP-1234",
 7 "mp-1234" opens the issue "mp-1234", and "1234" opens the issue "MP-1234".
 8 
 9+If your branch naming convention deviates, you can overwrite the jira_branch function to determine and echo the Jira issue key yourself.
10+Define a function `jira_branch` after sourcing `oh-my-zsh.sh` in your `.zshrc`.
11+Example:
12+```zsh
13+# Determine branch name from naming convention 'type/KEY-123/description'.
14+function jira_branch() {
15+  # Get name of the branch
16+  issue_arg=$(git rev-parse --abbrev-ref HEAD)
17+  # Strip prefixes like feature/ or bugfix/
18+  issue_arg=${issue_arg#*/}
19+  # Strip suffixes like /some-branch-description
20+  issue_arg=${issue_arg%%/*}
21+  # Return the value
22+  echo $issue_arg
23+}
24+```
25 
26 
27 #### Debugging usage
28diff --git a/plugins/jira/jira.plugin.zsh b/plugins/jira/jira.plugin.zsh
29index 9bcf4cc7b07fb5d46add4cc1467f935d4a32506a..22e0c82c7fcc60738d282acc322907ef21c56db6 100644
30--- a/plugins/jira/jira.plugin.zsh
31+++ b/plugins/jira/jira.plugin.zsh
32@@ -17,6 +17,30 @@ jira branch                     Opens an existing issue matching the current bra
33 EOF
34 }
35 
36+# If your branch naming convention deviates, you can partially override this plugin function
37+# to determine the jira issue key based on your formatting.
38+# See https://github.com/ohmyzsh/ohmyzsh/wiki/Customization#partially-overriding-an-existing-plugin
39+function jira_branch() {
40+  # Get name of the branch
41+  issue_arg=$(git rev-parse --abbrev-ref HEAD)
42+  # Strip prefixes like feature/ or bugfix/
43+  issue_arg=${issue_arg##*/}
44+  # Strip suffixes starting with _
45+  issue_arg=(${(s:_:)issue_arg})
46+  # If there is only one part, it means that there is a different delimiter. Try with -
47+  if [[ ${#issue_arg[@]} = 1 && ${issue_arg} == *-* ]]; then
48+    issue_arg=(${(s:-:)issue_arg})
49+    issue_arg="${issue_arg[1]}-${issue_arg[2]}"
50+  else
51+    issue_arg=${issue_arg[1]}
52+  fi
53+  if [[ "${issue_arg:l}" = ${jira_prefix:l}* ]]; then
54+    echo "${issue_arg}"
55+  else
56+    echo "${jira_prefix}${issue_arg}"
57+  fi
58+}
59+
60 function jira() {
61   emulate -L zsh
62   local action jira_url jira_prefix
63@@ -91,24 +115,7 @@ function jira() {
64     # but `branch` is a special case that will parse the current git branch
65     local issue_arg issue
66     if [[ "$action" == "branch" ]]; then
67-      # Get name of the branch
68-      issue_arg=$(git rev-parse --abbrev-ref HEAD)
69-      # Strip prefixes like feature/ or bugfix/
70-      issue_arg=${issue_arg##*/}
71-      # Strip suffixes starting with _
72-      issue_arg=(${(s:_:)issue_arg})
73-      # If there is only one part, it means that there is a different delimiter. Try with -
74-      if [[ ${#issue_arg[@]} = 1 && ${issue_arg} == *-* ]]; then
75-        issue_arg=(${(s:-:)issue_arg})
76-        issue_arg="${issue_arg[1]}-${issue_arg[2]}"
77-      else
78-        issue_arg=${issue_arg[1]}
79-      fi
80-      if [[ "${issue_arg:l}" = ${jira_prefix:l}* ]]; then
81-        issue="${issue_arg}"
82-      else
83-        issue="${jira_prefix}${issue_arg}"
84-      fi
85+      issue=$(jira_branch)
86     else
87       issue_arg=${(U)action}
88       issue="${jira_prefix}${issue_arg}"