Regex testing in test in zsh and launching a browser page

I get so many casual mentions of ticket numbers in our discussions and I keep hacking old URLs for tickets to look them up, or clicking through JIRA boards and views to navigate to the proper ticket, only to have them pull up in a sidebar in JIRA requiring more clicks to get to a useful view.

So, I wrote a quick script in JIRA called gojira to launch a browser window (macOS from the terminal):

open https://jira.example.com/browse/$1

With the above script, I can just invoke gojira PROJECT-1234 to bring up the ticket.

Ok, so that’s great but 90% of the ticket numbers are referencing the same project. So what if I want to only specify a ticket number some times? That’s when I finally looked up if you can test (alias [ ]) a regex in zsh. Turns out that you can.

If regex

The fairly common =~ operator is available for test/[] in zsh. You do have to double the square brackets for it to work (and single quote your regex):

if [[ "$1" =~ '[[:alpha:]]+-[[:digit:]]+' ]]
then
  open https://jira.sdlc.appriss.com/browse/$1
else
  # default to PROJECT if only a number is specified.
  open https://jira.sdlc.appriss.com/browse/PROJECT-$1
fi

Why not a function?

Of course, this can also be handled in a zsh function and added to your .zshrc or .zlogin:

function jira() {
  if [[ "$1" =~ [[:alpha:]] ]]
  then
    open https://jira.sdlc.appriss.com/browse/$1
  else
    open https://jira.sdlc.appriss.com/browse/PROJECT-$1
  fi
}