Last active
August 22, 2022 12:31
-
-
Save jwhett/93be799fd49cf0161469b1c4d160c152 to your computer and use it in GitHub Desktop.
Interactive fish function for working with GH Issues
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
function gh-filter-issue --description "Filter GH Issues" | |
# Mutually exclusive flags: | |
# - label, interactive: you cannot bot specify a | |
# label and interactively filter on label. | |
argparse --name gh-filter-issue --exclusive l,i h/help r/repo= l/label= c/cmd= i/interactive s/state= -- $argv | |
or return # this is needed if argparse fails and we want to halt. | |
for req in gh fzf gum jq | |
if ! command -q $req | |
echo "$req is required to run this function." | |
return 1 | |
end | |
end | |
if set -q _flag_help | |
echo "gh-filter-issue [-h|--help] [-r|--repo ORG/REPO] [[-l|--label LABEL]|[-i|-interactive]] [-c|--cmd view|comment] [-s|--state open|closed|all]" | |
echo -e "\nDefaults:" | |
echo -e "-r|--repo\n\tRepo associtated with the current working directory." | |
echo -e "-l|--label\n\tDo not filter issues by label." | |
echo -e "-i|--interactive\n\tOff. Assumes default for interactive-enabled features." | |
echo -e "-c|--cmd\n\tView issue." | |
echo -e "-s|--state\n\tOpen issues." | |
return | |
end | |
# I attempted to use trap to clean up our | |
# temp file no matter our exit, but that | |
# was unsuccessful and should be revisited. | |
set -f tempfile (mktemp -t gh-filter-issue.XXX) | |
if set -q _flag_repo | |
# Omitting repo in gh issue commands will | |
# default to the CWD and will fail otherwise. | |
set repo '-R='$_flag_repo | |
end | |
if set -q _flag_state | |
# Omitting state in gh issue view defaults | |
# to "open". | |
set state '--state='$_flag_state | |
end | |
set -f labels | |
if set -q _flag_label | |
# Filter on the provided label. | |
# Omitting label in gh issue view will | |
# not filter issues. | |
set labels '--label="'$_flag_label'"' | |
end | |
if set -q _flag_interactive | |
# Interactively select labels read from the repository. | |
echo "Select labels to use as inclusive filters. Ctrl-c to skip." | |
for label in (gh api -X GET repos/$_flag_repo/labels | \ | |
jq -r '.[].name' | \ | |
gum choose --no-limit --height 10) | |
if contains 1 $pipestatus | |
echo "There was an error in the interactive pipeline..." | |
rm $tempfile | |
return 1 | |
end | |
set -a labels '--label="'$label'"' | |
end | |
end | |
# Send issue output to a temp file. | |
# This is needed in case no issues match the filtering. | |
# The sed below is needed for compatibility between | |
# Mac and Linux. | |
set -l templ "{{range .}}{{.number}}: {{.title}} ({{.updatedAt}}) | |
{{end}}" | |
gh issue list $repo $labels $state --json=number,title,updatedAt -t="$templ" > $tempfile | |
if test $status -ne 0 | |
or test (wc -l $tempfile | sed 's/^[[:space:]]*//' | cut -d" " -f1) -eq 0 | |
echo "No matching issues." | |
rm $tempfile | |
return | |
end | |
# Parse the issue number from the list output | |
# and allow the user to interactively select | |
# the issue to view. | |
set -f issueNumber ( | |
cat $tempfile | \ | |
gum filter --text.foreground="30" --prompt.foreground="81" | \ | |
cut -d ":" -f1 | |
) | |
# In case of issues during the above pipeline | |
if contains 1 $pipestatus | |
echo "There was an error in the pipeline..." | |
rm $tempfile | |
return 1 | |
end | |
switch $_flag_cmd | |
case view | |
set cmd view | |
case comment | |
set cmd comment | |
case '*' | |
set cmd view | |
end | |
gh issue $cmd $repo $issueNumber | |
if test $cmd = "view" | |
gum confirm "Would you like to comment on that issue?" | |
and gh issue comment $repo $issueNumber | |
end | |
rm -f $tempfile | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment