Last active
February 14, 2020 19:20
-
-
Save scjudd/1ae16ad34c5e742baed56d5fe68534f7 to your computer and use it in GitHub Desktop.
git-jira: easily associate a ticket URL with a branch, allowing for quickly opening tickets in your browser
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -eo pipefail | |
refspec="+refs/jira/*:refs/jira/*" | |
function default_remote { | |
git config jira.remote || { | |
>&2 echo "No remote specified and no default configured." | |
>&2 echo "Run \`git jira setup <REMOTE_NAME>\` to set a default remote." | |
exit 1 | |
} | |
} | |
function current_branch { | |
git rev-parse --abbrev-ref HEAD | |
} | |
function validate_url { | |
url=$1 | |
regex='^http\(s\)\?:\/\/[a-z0-9_-]\+\.atlassian\.net\/browse\/[A-Z0-9]\+-[0-9]\+\/\?$' | |
grep "$regex" <<< "$url" || { | |
>&2 echo "Invalid ticket URL: $url" | |
exit 1 | |
} | |
} | |
function associate { | |
url=$1 | |
branch=${2:-$(current_branch)} | |
if [ -z "$url" ]; then | |
>&2 echo "Please specify a jira URL to associate with this branch" | |
exit 1 | |
fi | |
validate_url $url | |
git hash-object -w --stdin <<< "$url" \ | |
| xargs git update-ref refs/jira/${branch} | |
echo "$branch -> $url" | |
} | |
function open { | |
branch=${1:-$(current_branch)} | |
object=$(git rev-parse refs/jira/${branch} 2>/dev/null) || { | |
>&2 echo "No jira URL has been associated with this branch!" | |
>&2 echo "Try a \`git jira fetch\` and try again." | |
exit 1 | |
} | |
url=$(validate_url "$(git cat-file -p $object)") | |
echo "Opening $url in your default browser" | |
xdg-open $url >/dev/null & disown | |
} | |
function fetch { | |
remote=${1:-$(default_remote)} | |
git fetch $remote $refspec | |
} | |
function push { | |
remote=${1:-$(default_remote)} | |
git push $remote $refspec | |
} | |
function setup { | |
remote=$1 | |
if [ -z "$remote" ]; then | |
>&2 echo "Please specify a remote to fetch jira refs from (e.g., 'origin')" | |
exit 1 | |
fi | |
git config jira.remote $remote | |
git config --add remote.${remote}.fetch $refspec | |
} | |
function invalid_subcommand { | |
>&2 echo "$1 is not a valid git-jira subcommand." | |
>&2 usage | |
exit 1 | |
} | |
function usage { | |
echo "Usage:" | |
echo " git jira [open [BRANCH]]" | |
echo " git jira setup <REMOTE>" | |
echo " git jira associate <TICKET_URL> [BRANCH]" | |
echo " git jira fetch [REMOTE]" | |
echo " git jira push [REMOTE]" | |
echo " git jira help" | |
} | |
if [ -z "$1" ]; then | |
subcommand=open | |
else | |
subcommand=$1 | |
shift | |
fi | |
case $subcommand in | |
"open") | |
open $@ | |
;; | |
"setup") | |
setup $@ | |
;; | |
"associate") | |
associate $@ | |
;; | |
"fetch") | |
fetch $@ | |
;; | |
"push") | |
push $@ | |
;; | |
"help") | |
usage | |
;; | |
*) | |
invalid_subcommand $subcommand | |
;; | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment