Skip to content

Instantly share code, notes, and snippets.

@RoseSecurity
Created October 15, 2024 17:07
Show Gist options
  • Save RoseSecurity/399facb135e43df20d4c75bcf37b1587 to your computer and use it in GitHub Desktop.
Save RoseSecurity/399facb135e43df20d4c75bcf37b1587 to your computer and use it in GitHub Desktop.
Generates a selection of pull requests when an owner and repository are provided. Upon selection, the pull request is opened in the web browser.
#!/bin/bash
# Help menu
usage() {
echo "Usage: $0 -o REPO_OWNER -r REPO_NAME"
exit 1
}
while getopts ":o:r:" opt; do
case ${opt} in
o )
REPO_OWNER=$OPTARG
;;
r )
REPO_NAME=$OPTARG
;;
\? )
echo "Invalid option: -$OPTARG" >&2
usage
;;
: )
echo "Option -$OPTARG requires an argument." >&2
usage
;;
esac
done
# Check if REPO_OWNER and REPO_NAME were provided
if [ -z "$REPO_OWNER" ] || [ -z "$REPO_NAME" ]; then
echo "Both REPO_OWNER and REPO_NAME are required."
usage
fi
# Use gh api to get a list of open PRs in the repository with their numbers and titles
PR_LIST=$(gh api /repos/$REPO_OWNER/$REPO_NAME/pulls?state=open | jq -r '.[] | "\(.number): \(.title)"')
# Pipe the output to fzf for filtering and selection
selected_pr=$(fzf -m --select-1 <<< "$PR_LIST")
# If a PR is selected, extract the PR number
if [ $? -eq 0 ] && [ -n "$selected_pr" ]; then
pr_number=$(echo "$selected_pr" | cut -d':' -f1)
# Open the selected PR in the browser using the PR number without requiring a local repo
gh pr view "$pr_number" --repo "$REPO_OWNER/$REPO_NAME" --web
else
echo "No PR selected"
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment