Skip to content

Instantly share code, notes, and snippets.

@cabloo
Last active March 3, 2017 04:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cabloo/00e2d9fbb4ce768dbbfe to your computer and use it in GitHub Desktop.
Save cabloo/00e2d9fbb4ce768dbbfe to your computer and use it in GitHub Desktop.
Create Pull Request on GitHub with same title as last commit message

Setup:

  • Generate a GitHub Access Token with repo:status access.
  • Put gitpr.py in a scripts directory (I use ~/scripts/python/) and update it with your github access token.
  • Save gitpr as /usr/bin/gitpr and make it executable (chmod +x /usr/bin/gitpr). Then update the value of scripts_dir to the directory you saved gitpr.py in.
  • Optional: Install pyperclip with sudo pip install pyperclip. If pyperclip is detected, the link to the Pull Request on GitHub will be copied to your clipboard.
  • Optional: if you are using a repository that takes Pull Requests at a branch other than master, you will have to add the repository to the target_branches dict with the repo as the key and the target branch as the value. See the code for an example.

Usage:

  • Fork a repository on GitHub
  • git clone your fork
  • cd to cloned repo directory
  • git remote add the original repository
  • Make changes and commit
  • Run gitpr

It will push your changes to your fork, then submit a pull request from your fork to the original repository.

ex. Fork https://github.com/atom/atom

git clone git@github.com:Cabloo/atom.git
cd atom
git remote add original_repo git@github.com:atom/atom.git
touch testfile.txt
git add testfile.txt
git commit -m 'This will be the commit message and the title of the pull request'
gitpr
#!/bin/bash
scripts_dir="/home/zane/scripts/python/"
head=$(git ls-remote --get-url origin | cut -d':' -f 2 | head -c -4)
branch=$(git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/')
commit=$(git log -1 HEAD --pretty=format:%s)
if [ "$branch" = "" ]; then
echo "No branch chosen"
exit 0
fi
push="$(git push origin $branch 2>&1)"
echo "===="
echo "git push origin $branch"
echo "$push"
echo "===="
if [ "$push" = "Everything up-to-date" ]; then
echo "Everything already synced to remote, trying to create Pull Request anyway"
# exit 0
fi
# Get first non-origin remote
remote_name=$(git remote | sed "/origin/d" | head -n 1)
remote=$(git ls-remote --get-url "$remote_name" | cut -d':' -f 2 | head -c -5)
python "$scripts_dir"gitpr.py "$head" "$branch" "$commit" "$remote"
#!/usr/bin/python
import sys
import json
import requests
access_token = "your_github_access_token"
target_branches = {
'some_org/some_repo_where_dev_branch_takes_pull_requests': 'dev',
}
head, branch, commit, repo = sys.argv[1:5]
url = "https://api.github.com/repos/{0}/pulls?access_token={1}"
data = json.dumps({
'title': commit,
'head': "{0}:{1}".format(head[:head.find("/")], branch),
'base': target_branches.get(repo, 'master'),
})
resp = requests.post(url.format(repo, access_token), data=data)
info = json.loads(resp.text)
if 'errors' in info and len(info['errors']) > 0:
for error in info['errors']:
print "ERROR: {0}".format(error['code'])
elif '_links' in info:
link = info['_links']['html']['href']
print "Pull Request Created: %s" % info['_links']['html']['href']
try:
import pyperclip
pyperclip.copy(link)
print "Link copied to clipboard."
except ImportError:
pass
elif 'message' in info:
print "ERROR: {0}".format(info['message'])
else:
print "Couldn't find link in output. Perhaps it is malformed?"
print resp.text
@cabloo
Copy link
Author

cabloo commented Dec 3, 2014

Setup:

  • Generate a GitHub Access Token with repo:status access.
  • Put gitpr.py in a scripts directory (I use ~/scripts/python/) and update it with your github access token.
  • Save gitpr as /usr/bin/gitpr and make it executable (chmod +x /usr/bin/gitpr). Then update the value of scripts_dir to the directory you saved gitpr.py in.
  • Optional: Install pyperclip with sudo pip install pyperclip. If pyperclip is detected, the link to the Pull Request on GitHub will be copied to your clipboard.
  • Optional: if you are using a repository that takes Pull Requests at a branch other than master, you will have to add the repository to the target_branches dict with the repo as the key and the target branch as the value. See the code for an example.

Usage:

  • Fork a repository on GitHub
  • git clone your fork
  • cd to cloned repo directory
  • git remote add the original repository
  • Make changes and commit
  • Run gitpr

It will push your changes to your fork, then submit a pull request from your fork to the original repository.

ex. Fork https://github.com/atom/atom

git clone git@github.com:Cabloo/atom.git
cd atom
git remote add original_repo git@github.com:atom/atom.git
touch testfile.txt
git add testfile.txt
git commit -m 'This will be the commit message and the title of the pull request'
gitpr

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment