Skip to content

Instantly share code, notes, and snippets.

@viktor-yakubiv
Last active January 26, 2022 10:31
Show Gist options
  • Save viktor-yakubiv/c584a9f6880aa37255a3b63a4a5fcd3c to your computer and use it in GitHub Desktop.
Save viktor-yakubiv/c584a9f6880aa37255a3b63a4a5fcd3c to your computer and use it in GitHub Desktop.
Smart GitHub PR creation

Smart GitHub PR

Store the script somewhere, where it could be accessible as a binary.

Create an alias like below to use is easily.

alias pr="gh-smart-pr.sh"

Dependencies

  • jq for parsing JSON response
  • gh for creating/opening PRs
#!/usr/bin/env sh
#
# Smart GitHub PR resolution
#
# If there is a PR for the current branch, opens it immediately in the browser,
# or creates one otherwise.
#
# For a fresh PR it parses the branch name to retrieve labels.
#
# If you are not in a git repository, it suggest to review all your PRs
# on GitHub website.
branch=$(git rev-parse --abbrev-ref HEAD 2> /dev/null)
if [ $? != 0 ]; then
printf 'Not a git repository. Do you want to check all your PRs? (y|N): '
read -rn 1 answer
case $answer in
y | Y)
open 'https://github.com/pulls';;
esac
exit 0
fi
if gh pr status --json=number | jq '.currentBranch.number' | grep -qE '\d+'; then
gh pr view -w
else
assinee='@me'
label=''
case "${branch%%/*}" in
feature | enhancement) label=enhancement;;
fixup | bug) label=bug
esac
gh pr create -w -a "$assinee" -l "$label"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment