Skip to content

Instantly share code, notes, and snippets.

@drewbrokke
Last active November 2, 2020 17:00
Show Gist options
  • Save drewbrokke/702a5711e71cb49e0807d41e6e7395d3 to your computer and use it in GitHub Desktop.
Save drewbrokke/702a5711e71cb49e0807d41e6e7395d3 to your computer and use it in GitHub Desktop.
Passes `gh` pull data to an interactive `fzf` instance
#!/bin/bash
# check_dependency fzf gh jq || exit 1
DEFAULT_USER="$(git config user.name)"
REPO_NAME="$(git rev-parse --show-toplevel | xargs basename)"
USER="${1:-${DEFAULT_USER}}"
PULLS_JSON="$(gh api "/repos/${USER}/${REPO_NAME}/pulls")"
# Check to see if gh responded with an error. Will exit if a bad username was given.
# shellcheck disable=SC2181
if [ $? -gt 0 ]
then
echo "Invalid username: ${USER}"
exit 1
fi
if [ "$(echo "${PULLS_JSON}" | jq '. | length')" = "0" ]
then
echo "No pull requests for ${USER}/${REPO_NAME}"
exit 0
fi
function data() {
echo "${PULLS_JSON}" | jq --raw-output "$1"
}
DELIMITER=" :: "
function gh_fuzzy_cmd() {
for n in $(seq 1 "$(data '. | length')")
do
i=$((n - 1))
NUMBER="$(data ".[$i].number")"
TITLE="$(data ".[$i].title")"
USER_LOGIN="$(data ".[$i].user.login")"
echo "${NUMBER}${DELIMITER}${USER_LOGIN}${DELIMITER}${TITLE}"
done
}
GH_PR_COMMAND="gh pr --repo ${USER}/${REPO_NAME}"
HEADER="""
alt-bspace: execute(${GH_PR_COMMAND} close {1})+accept
alt-space: execute(${GH_PR_COMMAND} view {1})
ctrl-b: execute-silent(${GH_PR_COMMAND} view {1} --web)
ctrl-d: execute(${GH_PR_COMMAND} diff {1})
ctrl-o: execute-silent(${GH_PR_COMMAND} view {1} --web)
ctrl-s: execute(${GH_PR_COMMAND} checks {1})
ctrl-space: execute(${GH_PR_COMMAND} view {1})
ctrl-y: execute(echo {1} | pbcopy)
enter: execute(${GH_PR_COMMAND} checkout {1})+accept
"""
gh_fuzzy_cmd |
fzf \
--bind="alt-bspace:execute(${GH_PR_COMMAND} close {1})+accept" \
--bind="alt-space:execute(${GH_PR_COMMAND} view {1})" \
--bind="ctrl-b:execute-silent(${GH_PR_COMMAND} view {1} --web)" \
--bind="ctrl-d:execute(${GH_PR_COMMAND} diff {1})" \
--bind="ctrl-o:execute-silent(${GH_PR_COMMAND} view {1} --web)" \
--bind="ctrl-s:execute(${GH_PR_COMMAND} checks {1} | less)" \
--bind="ctrl-space:execute(${GH_PR_COMMAND} view {1})" \
--bind="ctrl-y:execute(echo {1} | pbcopy)" \
--bind="enter:execute(${GH_PR_COMMAND} checkout {1})+accept" \
\
--delimiter="${DELIMITER}" \
--header="${HEADER}" \
--info="inline" \
--exit-0 \
--no-mouse \
--preview="${GH_PR_COMMAND} diff {1}" \
--preview-window="bottom:70%" \
--reverse \
;
exit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment