Created
February 19, 2015 18:07
-
-
Save imperialwicket/16ca80d37ef98da15d3b to your computer and use it in GitHub Desktop.
Bash alias for generating GitHub Pull Requests
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Bash alias to generate pull requests on github.com | |
# | |
# PreRequisites: | |
# Create an auth token (bypasses 2 factor auth, when enabled): | |
# https://github.com/settings/tokens/new | |
# | |
# Use any name, something like 'pull requests' makes sense. | |
# Allow permissions 'repo' and 'public repo'. | |
# Create. | |
# | |
# Store the auth token: | |
# echo "export GITHUB_PR_TOKEN=<token>" >> ~/.bashrc | |
# | |
# | |
# Use: | |
# `pr` | |
# | |
# pr will attempt a regex capture on the branch name to determine a | |
# target issue for the pull request. You can override with a `read` | |
# prompt. | |
# | |
# The 'head' branch is your current branch. | |
# | |
# The 'base' branch is 'master'. | |
# | |
# | |
# Known issues: | |
# - Titled and bodied pull requests are not supported. | |
# - Issue candidate is not confirmed (weird results if you create a PR for | |
# closed issues). | |
# - Branch is not confirmed on origin, missing 'head' branch on origin | |
# causes the PR to fail. | |
# | |
# | |
function pr { | |
gh_root="https://api.github.com" | |
gh_base="master" | |
[ -z "$GITHUB_PR_TOKEN" ] && echo 'pr requires the $GITHUB_PR_TOKEN variable.' && return | |
#[ -z "$1" ] && echo "pr requires an issue number." && return | |
repoTest=$(git config --list | grep 'remote\.origin\.url' | grep 'github\.com' | awk -F ":" '{print $2}') | |
[ -z "$repoTest" ] && echo "This doesn't seem like a github repository." && return | |
cur=$(git rev-parse --abbrev-ref HEAD) | |
userRepo=${repoTest/.git/} | |
user=${userRepo%/*} | |
repo=${userRepo#*/} | |
# Try to retrieve the issue from the current branch name. Captures any digits. | |
issueRegex='([a-zA-Z_/-]+)?([0-9]+)([a-zA-Z_/-]+)?' | |
[[ $cur =~ $issueRegex ]] && issue="${BASH_REMATCH[2]}" | |
echo -n "Provide an issue for $cur or press Enter to use [$issue]: " | |
read userIssue | |
#TODO validate that the issue is valid ? | |
#TODO validate that the branch is pushed to origin ? | |
postdata="{\"issue\":\"${userIssue:-$issue}\",\"head\":\"$cur\",\"base\":\"$gh_base\"}" | |
#TODO trap/redirect the curl output | |
curl -u $GITHUB_PR_TOKEN:x-oauth-basic -d $postdata "$gh_root/repos/$user/$repo/pulls" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment