Skip to content

Instantly share code, notes, and snippets.

@josefdlange
Created November 21, 2019 19:03
Show Gist options
  • Save josefdlange/d3621135e66563294d5cf735f31f28e5 to your computer and use it in GitHub Desktop.
Save josefdlange/d3621135e66563294d5cf735f31f28e5 to your computer and use it in GitHub Desktop.
Pull Request Generator
#!/bin/zsh
usage() {
echo "Usage: mpr [-h]"
printf "\n"
echo "Opens or creates a pull-request for the given branch."
echo "If branch is prefixed with a branch-type (feature/etc),"
echo "that will be used as a label upon creation."
printf "\n"
exit 2
}
yesno() {
read "?$1 (y/N): " answer
case ${answer:0:1} in
y|Y )
echo 1
;;
* )
echo 0
;;
esac
}
while getopts 'ph' c; do
case $c in
h) usage ;;
esac
done
BRANCH=$(git rev-parse --abbrev-ref HEAD)
EXISTING_PR=$(hub pr list -h $BRANCH -f "%I")
if [[ "$EXISTING_PR" == "" ]]; then
echo "No PR found for this branch. Creating."
else
echo "PR #$EXISTING_PR exists for this branch."
if [[ "$(yesno 'Would you like to open the PR?')" == "1" ]]; then
hub pr show $EXISTING_PR
fi
exit 0
fi
LABELS=""
if [[ "$BRANCH" == *"/"* ]]; then
KIND=$(cut -d'/' -f1 <<< $BRANCH)
LABELS="$LABELS$KIND,"
fi
LABELS=$(echo $LABELS | sed 's/,$//')
DESCRIPTION=$(echo $BRANCH | sed 's/^[A-Za-z]*\///' | sed 's/-/ /g' | sed 's/^\(.\)/\1/g' )
DESCRIPTION=$(echo $DESCRIPTION | awk '{ print toupper(substr($0, 1, 1)) substr($0, 2) }')
if [[ "$(yesno 'Is there a Notion story to attach?')" == "1" ]]; then
read "?Story URL: " URL
DESCRIPTION=$(printf "$DESCRIPTION\n\nNotion Story: $URL")
fi
printf "\n"
echo "Creating Pull Request "
echo " - From $BRANCH to master."
echo " - Labels will be: $LABELS"
echo "--------------------------"
echo $DESCRIPTION
echo "--------------------------"
printf "\n"
if [[ "$(yesno 'Ready?')" == "1" ]]; then
hub pull-request -d -l $LABELS --browse -F - <<< $DESCRIPTION
else
echo "Would have run:"
echo "hub pull-request $DRAFT -l $LABELS --browse -F - <<< $DESCRIPTION"
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment