Skip to content

Commit bdd7068

Browse files
committed
Add git-open-jira
If your git branch starts with `JIRAPROJECT-NNNN-`, finds the associated jira ticket and opens it. macOS only for now. Signed-off-by: Joe Block <jpb@unixorn.net>
1 parent 9b0ea56 commit bdd7068

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ If you wrote one of these scripts and want it removed from this collection, plea
119119
| `git-nuke` | Zach Holman's [dotfiles](https://github.com/holman/dotfiles) | Nukes a branch locally and on the origin remote. |
120120
| `git-object-deflate` | Ryan Tomayko's dotfiles | Deflate an loose object file and write to standard output. |
121121
| `git-oldest-common-ancestor` | Lee Dohm's [dotfiles](https://github.com/lee-dohm/dotfiles/blob/main/bin/git-oldest-ancestor) | Finds the oldest common ancestor commit between two branches. |
122+
| `git-open-jira` | If the branch name starts with `JIRAPROJECT-NNNN`, will open that issue in jira. Assumes that your JIRA server location is in `~/.jira-server` or in the `$JIRA_SERVER` environment variable. |
122123
| `git-origin-head` | Don't recall, maybe twitter | Prints the name of the origin remote's default branch. Not every repository uses `main` or `master`. |
123124
| `git-outgoing` | Michael Markert's [dotfiles](https://github.com/cofi/dotfiles) | Show commits that are on the local branch that have not been pushed to the tracking branch. |
124125
| `git-overwritten` | Mislav Marohnić's [dotfiles](https://github.com/mislav/dotfiles) | Aggregates `git blame` information about original owners of lines changed or removed in the '<base>...<head>' diff. |

bin/git-open-jira

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#!/usr/bin/env bash
2+
#
3+
# If you name your branches JIRATICKET-something-or-other, will open the jira for you automagically
4+
#
5+
# Copyright 2022, Joe Bock <jpb@unixorn.net>
6+
7+
set -o pipefail
8+
if [[ -n "$TRACE" ]]; then
9+
set -x
10+
fi
11+
12+
function debug() {
13+
if [[ -n "$DEBUG" ]]; then
14+
echo "$@"
15+
fi
16+
}
17+
18+
function fail() {
19+
printf '%s\n' "$1" >&2 ## Send message to stderr. Exclude >&2 if you don't want it that way.
20+
exit "${2-1}" ## Return a code specified by $2 or 1 by default.
21+
}
22+
23+
function has() {
24+
# Check if a command is in $PATH
25+
which "$@" > /dev/null 2>&1
26+
}
27+
28+
function get-jira-server() {
29+
JIRA_SERVER_F=${JIRA_SERVER_F:-"$HOME/.jira-server"}
30+
debug "JIRA_SERVER_F: $JIRA_SERVER_F"
31+
if [[ -n "$JIRA_SERVER" ]]; then
32+
if [[ -r "$JIRA_SERVER_F" ]]; then
33+
JIRA_SERVER=$(cat "$JIRA_SERVER_F")
34+
debug "JIRA_SERVER: $JIRA_SERVER"
35+
else
36+
fail "JIRA_SERVER not in environment, and can't read it from $JIRA_SERVER_F"
37+
fi
38+
fi
39+
if [[ -z "$JIRA_SERVER" ]]; then
40+
fail "JIRA_SERVER unset, failing"
41+
fi
42+
debug "JIRA_SERVER: $JIRA_SERVER"
43+
}
44+
45+
function get-jira-ticket(){
46+
echo $(git rev-parse --abbrev-ref HEAD | cut -f 1,2 -d -)
47+
}
48+
49+
function jira-open(){
50+
open "${1}/browse/${$2}"
51+
}
52+
53+
if !has open; then
54+
fail "Could not find 'open' in your PATH"
55+
fi
56+
57+
jiraticket=$(get-jira-ticket)
58+
if [[ -z "$jiraticket" ]]; then
59+
branch=$(git rev-parse --abbrev-ref HEAD)
60+
fail "$branch is not prefixed with a JIRA ticket id"
61+
fi
62+
debug "jiraticket: $jiraticket"
63+
64+
get-jira-server
65+
jira-open "$JIRA_SERVER" "$jiraticket"

0 commit comments

Comments
 (0)