-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathghf-view-url.bash
44 lines (41 loc) · 1.11 KB
/
ghf-view-url.bash
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
function _ghf_gh_view_url() {
local URL=$( "$2" "$3" view "$1" --json url --jq .url )
if [[ ${_GH_FZF_VIEWER} == "short_url" ]]; then
local RESPONSE="$( curl --silent -i https://git.io -F "url=$URL" )"
echo $RESPONSE \
| command grep "Location:.*" \
| command grep -oE "https://git\.io/.*"
else
echo $URL
fi
}
function _ghf_glabf_view_url() {
case "$3" in
"issue" )
local API="issues/$( echo "$1" | command grep -oE "[0-9]+" )";;
"mr" )
local API="merge_requests/$( echo "$1" | command grep -oE "[0-9]+" )";;
"release" )
local API="releases/$1";;
* )
echo "glabf cannot view url for '$1' of '$3'." 1>&2
return 1
esac
local URL="$(
"$2" api -X GET "/projects/:id/${API}" \
| command jq ".web_url" \
| command sed -E -e 's/^\"|\"$//g'
)"
if [[ ${_GH_FZF_VIEWER} == "short_url" ]]; then
echo "$( bitly shorten "$URL" )"
else
echo "$URL"
fi
}
function _ghf_view_url() {
local CMD="$( echo "$2" | sed -E 's/.*\///' )"
case "$CMD" in
"gh" ) _ghf_gh_view_url "$@";;
"glab" ) _ghf_glabf_view_url "$@";;
esac
}