Skip to content

Instantly share code, notes, and snippets.

@ichernev
Last active June 19, 2017 07:11
Show Gist options
  • Save ichernev/7de1d8bf59d7f8dc8c1b34a0d75f4423 to your computer and use it in GitHub Desktop.
Save ichernev/7de1d8bf59d7f8dc8c1b34a0d75f4423 to your computer and use it in GitHub Desktop.
#!/bin/bash -x
set -e
set -o pipefail
REPO="moment/moment"
function json_escape(){
echo -n "$1" | python -c 'import json,sys; print json.dumps(sys.stdin.read())'
}
function sed_i() {
if [ $(uname) = Darwin ]; then
sed -i '' "$@"
else
sed -i "$@"
fi
}
if [ $# -lt 1 ]; then
echo "$0 FILE" >&2
exit 1
fi
filename="$1"
shift
do_linkify=1
while [ $# -gt 0 ]; do
case $1 in
'--skip-linkify')
do_linkify=0
;;
*)
echo "unknown option" >&2
exit 1
esac
shift
done
description="$(head -n1 $filename)"
description=${description### }
basename=$(basename $filename)
if [ "$do_linkify" = 1 ]; then
cp "$filename" "${filename}.bac"
sed_i \
-e 's_#\([0-9][0-9]*\)_[#\1](https://github.com/'$REPO'/issues/\1)_g' \
-e 's_\[\([0-9]*\)\]_[#\1](https://github.com/'$REPO'/pull/\1)_g' \
$filename
fi
echo "$description"
cat > /tmp/abc <<EOF
quit with error ':cq'
basename: $basename
desc: $description
$(cat $filename)
EOF
if ! vim /tmp/abc; then
if [ "$do_linkify" = 1 ]; then
cp "$filename" "${filename}.new"
mv "${filename}.bac" "$filename"
fi
exit
fi
curl -H "Authorization: token $GITHUB_TOKEN" \
-H "Content-Type: application/json" \
-X POST \
-d "
{
\"description\":\"$description\",
\"public\":true,
\"files\": {
\"$basename\": {
\"content\": $(json_escape "$(cat $filename)")
}
}
}" \
"https://api.github.com/gists"
#!/bin/bash
set -e
set -o pipefail
EDIT_TITLE=1
repo="moment/moment"
upstream_remote="upstream"
main_branch="develop"
tmp_pr_branch="pr"
changeset_file=$HOME/tmp/moment/moment-nxt.md
tmp_dir="/tmp"
if [ $# -lt 1 ]; then
{
echo "$0 PR [--cont|--rebase|--fetch]"
echo
echo " --fetch only fetch the PRs code into branch $tmp_pr_branch"
echo " --rebase only fetch the PRs code and rebase it on top of $main_branch"
echo " --cont merge branch $tmp_pr_branch into $main_branch, run tests and push"
echo
echo " You can use --fetch to do a manual rebase in case the automatic one fails"
echo " You can use --rebase to add code to an existing PR just before you merge it in."
echo " You can use --cont after fixing rebase conflicts"
} >&2
exit 1
fi
get_mtime() {
if [ $(uname) = Darwin ]; then
stat -f '%m' "$1"
else
stat -c '%y'
fi
}
pr=$1
shift
after_rebase=0
just_rebase=0
skip_rebase=0
if [ "$1" = "--cont" ]; then
after_rebase=1
shift
fi
if [ "$1" = "--rebase" ]; then
just_rebase=1
shift
fi
if [ "$1" = "--fetch" ]; then
just_rebase=1
skip_rebase=1
fi
data_file="$tmp_dir/${pr}.json"
curl -s "https://api.github.com/repos/$repo/pulls/$pr" -o "$data_file"
title="$(jshon -e title -u -F $data_file)"
state="$(jshon -e state -u -F $data_file)"
remote_repo="$(jshon -e head -e repo -e html_url -u -F $data_file)"
remote_branch="$(jshon -e head -e ref -u -F $data_file)"
remote_user_branch="$(jshon -e head -e label -u -F $data_file)"
echo "title is $title"
echo "repo is $remote_repo"
echo "branch is $remote_branch"
echo "user/branch is $remote_user_branch"
echo
if [ "$state" != 'open' ]; then
echo "PR $pr state is $state (not open)" >&2
exit 1
fi
if [ $after_rebase = 1 -a $(git symbolic-ref --short HEAD) = "$tmp_pr_branch" ]; then
echo "Checking out $main_branch for YOUR convenience" >&2
git checkout $main_branch
fi
if [ "$(git symbolic-ref --short HEAD)" != "$main_branch" ]; then
echo "Use branch $main_branch. Refusing to continue" >&2
exit 1
fi
if [ $after_rebase = 0 ]; then
if git show-ref --verify --quiet refs/heads/$tmp_pr_branch; then
echo "branch $tmp_pr_branch exists. delete it first" >&2
exit 1
fi
git fetch "$remote_repo" "$remote_branch"
git checkout -b pr FETCH_HEAD
[ $skip_rebase = 0 ] && git rebase $main_branch
fi
if [ "$just_rebase" = 1 ]; then
exit
fi
if [ $EDIT_TITLE = 1 ]; then
title_edit=$(mktemp /tmp/edit-title-$pr.XXXXXX)
echo "$title" > $title_edit
title_edit_time="$(get_mtime $title_edit)"
vim $title_edit
if [ $? -ne 0 -o "$title_edit_time" = "$(get_mtime $title_edit)" ]; then
echo "Aborting merge. Save the title file to continue" >&2
exit 1
fi
title="$(cat $title_edit)"
fi
git checkout $main_branch
git merge $tmp_pr_branch --no-ff -m "Merge pull request #${pr} from $remote_user_branch
$title"
git branch -D $tmp_pr_branch
if [ -n "$changeset_file" ]; then
echo "* [$pr] $title" >> $changeset_file
fi
grunt
git push $upstream_remote
if [ -n "$GITHUB_TOKEN" ]; then
msg="Merged in $(git rev-parse HEAD)"
curl -H "Authorization: token $GITHUB_TOKEN" \
-H "Content-Type: application/json" \
-X POST -d "{\"body\":\"$msg\"}" "https://api.github.com/repos/$repo/issues/$pr/comments"
curl -H "Authorization: token $GITHUB_TOKEN" \
-H "Content-Type: application/json" \
-X PATCH -d "{\"state\":\"closed\", \"title\": \"$title\"}" "https://api.github.com/repos/$repo/issues/$pr"
else
echo "export \$GITHUB_TOKEN if you want to comment and close the PR automatically"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment