Skip to content

Instantly share code, notes, and snippets.

@aktau
Forked from tarruda/.bashrc
Last active August 29, 2015 14:01
Show Gist options
  • Save aktau/5cf2a067b993e6157a86 to your computer and use it in GitHub Desktop.
Save aktau/5cf2a067b993e6157a86 to your computer and use it in GitHub Desktop.
Also works in .zshrc
# Start working on a pull request, this requires a .git/user-repo file
# containing the string "user/repository"
pr () {
if [[ ! -r .git/user-repo ]]; then
echo "Need to setup user/repo" >&2
return 1
fi
local user_repo=$(< .git/user-repo)
local pr_num=$1
if [[ -z $pr_num ]]; then
echo "Need the pull request number" >&2
return 1
fi
local branch=merge-pr-$pr_num
if [[ -e .git/refs/heads/$branch ]]; then
echo "Already working on pull request $pr_num, delete branch '$branch' and try again" >&2
return 1
fi
(
set -e
local user_repo=$(< .git/user-repo)
git checkout -b $branch
curl "https://github.com/$(< .git/user-repo)/pull/$pr_num.patch" 2> /dev/null | git am --3way
)
}
# Finish working on a pull request, besides the .git/user-repo file,
# this requires a .git/ghtok file containing the oauth token for accessing the
# repository
mpr () {
if [[ ! -r .git/user-repo ]]; then
echo "Need to setup user/repo" >&2
return 1
fi
local user_repo=$(< .git/user-repo)
local ghtok=
if [[ -r .git/ghtok ]]; then
ghtok=$(< .git/ghtok)
elif [[ -n "$GITHUB_TOKEN" ]]; then
echo "Couldn't find github token at .git/ghtok, using env \
variable GITHUB_TOKEN" >&2
ghtok="$GITHUB_TOKEN"
else
echo "Couldn't find neither .git/ghtok nor GITHUB_TOKEN, exiting" >&2
return 1
fi
local pr_num=$1
if [[ -z $pr_num ]]; then
echo "Need the pull request number" >&2
return 1
fi
local branch=merge-pr-$pr_num
if [[ ! -e .git/refs/heads/$branch ]]; then
echo "Not working on $pr_num" >&2
return 1
fi
(
set -e
echo "Will push commits and comment/close on PR $pr_num"
git checkout master
echo "Retrieving the PR title..."
local sed="sed"
if [[ `uname` == 'Darwin' ]]; then
export sed="gsed"
fi
# check if this is a prefixed PR ([RFC], [WIP], ...)
local pr_meta="$(curl https://api.github.com/repos/$user_repo/issues/$pr_num 2> /dev/null)"
local pr_title=
if egrep -q '^\[\w+\]' <<< $pr_meta; then
local pr_title="$($sed -n -e 's/.*"title":\s\+"\([^"]\+\)".*/\1/g' -e 's/^\[\(\w\+\)]\s*\(.\+\)/\2/p' <<< $pr_meta)"
else
local pr_title="$($sed -n -e 's/.*"title":\s\+"\([^"]\+\)".*/\1/p' <<< $pr_meta)"
fi
git merge --no-ff -m "Merge pull request #$pr_num '$pr_title'" $branch
git branch -D $branch
git log --graph --decorate --pretty=oneline --abbrev-commit --all --max-count=20
echo "Continue with the merge?[y/N]"
local confirm
read confirm
if [[ $confirm != "y" ]]; then
echo "Merge cancelled" >&2
git reset --hard HEAD~1
exit 1
fi
git push
curl \
-X POST \
-H "Authorization: token $ghtok" \
-d '{"body": ":+1: merged, thanks"}' \
"https://api.github.com/repos/$user_repo/issues/$pr_num/comments" > /dev/null
curl \
-X PATCH \
-H "Authorization: token $ghtok" \
-d '{"state": "closed"}' \
"https://api.github.com/repos/$user_repo/issues/$pr_num" > /dev/null
echo "Done"
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment