Skip to content

Instantly share code, notes, and snippets.

@tristantarrant
Created October 26, 2018 15:25
Show Gist options
  • Select an option

  • Save tristantarrant/f06f986979b399c02e71d72c73e48e59 to your computer and use it in GitHub Desktop.

Select an option

Save tristantarrant/f06f986979b399c02e71d72c73e48e59 to your computer and use it in GitHub Desktop.
GitHub Pull Request script
#!/bin/bash
#
# This script can be set as a git alias (I alias it to `pr`)
# It will force push the current branch to your fork (github.userfork) and open your browser to the PR creation page already
# compiled with a link to the corresponding Jira (assuming the local branch begins with the Jira issue name)
#
# Configure the variables below for your environment
ORIGIN_REMOTE=`git config --get github.upstream`
if [ -z "$ORIGIN_REMOTE" ]; then
echo "Missing git config property 'github.upstream'. Set it to the name of the remote repository which represents upstream"
echo "e.g. git config github.upstream origin"
exit 1
fi
USER_REMOTE=`git config --get github.userfork`
if [ -z "$USER_REMOTE" ]; then
echo "Missing git config property 'github.userfork'. Set it to the name of the remote repository which represents your fork of the upstream repository"
echo "e.g. git config github.upstream myfork"
exit 1
fi
urlencode() {
# urlencode <string>
local length="${#1}"
for (( i = 0; i < length; i++ )); do
local c="${1:i:1}"
case $c in
[a-zA-Z0-9.~_-]) printf "$c" ;;
*) printf '%%%02X' "'$c"
esac
done
}
urldecode() {
# urldecode <string>
local url_encoded="${1//+/ }"
printf '%b' "${url_encoded//%/\\x}"
}
# Obtain
USER_PUSH_URL=`git remote get-url --push "$USER_REMOTE"| cut -f2 -d: | cut -f1 -d/`
ORIGIN_URL=`git remote get-url --push "$ORIGIN_REMOTE"| cut -f2 -d: | sed "s/\.git$//"`
BRANCH=`git rev-parse --abbrev-ref HEAD`
ISSUE=`echo "$BRANCH" | cut -d / -f 1`
UPSTREAM=`git rev-parse --symbolic-full-name --abbrev-ref "$BRANCH"@{u}|grep -Po "[a-zA-Z0-9]+/\K.*"`
if [ "$UPSTREAM" != "master" ]; then
BASE_FORK="$ORIGIN_URL:$UPSTREAM..."
else
BASE_FORK="master..."
fi
AMP='\&'
ISSUEURL=`urlencode "https://issues.jboss.org/browse/$ISSUE"`
PR="https://github.com/$ORIGIN_URL/compare/$BASE_FORK$USER_PUSH_URL:$BRANCH?pull_request[body]=$ISSUEURL"
echo "Branch: $BRANCH"
echo "Upstream: $UPSTREAM"
echo "PR: $PR"
echo "Issue: $ISSUE"
git push -f "$USER_REMOTE" "$BRANCH"
gio open "$PR"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment