angular-cn/scripts/github/merge-pr

114 lines
3.5 KiB
Bash
Executable File

#!/bin/sh
set -u -e -o pipefail
BASEDIR=$(dirname "$0")
BASEDIR=`(cd $BASEDIR; pwd)`
if [ $# -eq 0 ]; then
echo "Merge github PR into the target branches"
echo
echo "$0 PR_NUMBER"
echo
exit 0
fi
PR_NUMBER="$1"
CURRENT_BRANCH=`git rev-parse --abbrev-ref HEAD`
PR_SHA_COUNT=`curl -s https://api.github.com/repos/angular/angular/pulls/$PR_NUMBER | node $BASEDIR/utils/json_extract.js commits`
PR_LABELS=`curl -s https://api.github.com/repos/angular/angular/issues/$PR_NUMBER/labels`
PR_ACTION=`echo "$PR_LABELS" | node $BASEDIR/utils/json_extract.js "name=^PR action:"`
PR_TARGET=`echo "$PR_LABELS" | node $BASEDIR/utils/json_extract.js "name=^PR target:"`
PR_CLA=`echo "$PR_LABELS" | node $BASEDIR/utils/json_extract.js "name=^cla"`
MASTER_BRANCH='master'
SHA=`git rev-parse HEAD`
PATCH_BRANCH=`git ls-remote --heads git@github.com:angular/angular.git | grep -E 'refs\/heads\/\d+\.\d+\.x' | cut -d '/' -f3 | sort -r | head -n1`
if [[ "$PR_ACTION" != "PR action: merge" ]]; then
echo The PR is missing 'PR action: merge' label, found: $PR_ACTION
exit 1
fi
if [[ "$PR_CLA" != "cla: yes" ]]; then
echo The PR is missing 'cla: Yes' label, found: $PR_CLA
exit 1
fi
if [[ $PR_TARGET == "PR target: master & patch" ]]; then
MERGE_MASTER=1
MERGE_PATCH=1
elif [[ $PR_TARGET == "PR target: master-only" ]]; then
MERGE_MASTER=1
MERGE_PATCH=0
elif [[ $PR_TARGET == "PR target: patch-only" ]]; then
MERGE_MASTER=0
MERGE_PATCH=1
else
echo "Unknown PR target format: $PR_TARGET"
exit 1;
fi
CHECKOUT_MASTER="git checkout merge_pr_master"
CHECKOUT_PATCH="git checkout merge_pr_$PATCH_BRANCH"
RESTORE_BRANCH="git checkout $CURRENT_BRANCH"
FETCH_PR="git fetch git@github.com:angular/angular.git pull/$PR_NUMBER/head:angular/pr/$PR_NUMBER heads/master:merge_pr_master heads/$PATCH_BRANCH:merge_pr_$PATCH_BRANCH -f"
PUSH_BRANCHES="git push git@github.com:angular/angular.git merge_pr_master:master merge_pr_$PATCH_BRANCH:$PATCH_BRANCH"
CHERRY_PICK_PR="git cherry-pick angular/pr/$PR_NUMBER~$PR_SHA_COUNT..angular/pr/$PR_NUMBER"
REWRITE_MESSAGE="git filter-branch -f --msg-filter \"$BASEDIR/utils/github_closes.js $PR_NUMBER\" HEAD~$PR_SHA_COUNT..HEAD"
echo "======================"
echo "GitHub Merge PR Steps"
echo "======================"
echo " $FETCH_PR"
if [[ $MERGE_MASTER == 1 ]]; then
echo " $CHECKOUT_MASTER"
echo " $CHERRY_PICK_PR"
echo " $REWRITE_MESSAGE"
fi
if [[ $MERGE_PATCH == 1 ]]; then
echo " $CHECKOUT_PATCH"
echo " $CHERRY_PICK_PR"
echo " $REWRITE_MESSAGE"
fi
echo " $PUSH_BRANCHES"
echo " $RESTORE_BRANCH"
echo "----------------------"
$FETCH_PR
if [[ $MERGE_MASTER == 1 ]]; then
echo
echo ">>> Checkout master: $CHECKOUT_MASTER"
$CHECKOUT_MASTER
echo
echo ">>> Cherry pick pr: $CHERRY_PICK_PR"
$CHERRY_PICK_PR
echo
echo ">>> Rewrite message: $REWRITE_MESSAGE"
# Next line should work, but it errors, hence copy paste the command.
# $REWRITE_MESSAGE
git filter-branch -f --msg-filter "$BASEDIR/utils/github_closes.js $PR_NUMBER" HEAD~$PR_SHA_COUNT..HEAD
fi
if [[ $MERGE_PATCH == 1 ]]; then
echo
echo ">>> Checkout $PATCH_BRANCH: $CHECKOUT_PATCH"
$CHECKOUT_PATCH
echo
echo ">>> Cherry pick pr: $CHERRY_PICK_PR"
$CHERRY_PICK_PR
echo
echo ">>> Rewrite message: $REWRITE_MESSAGE"
# Next line should work, but it errors, hence copy paste the command.
# $REWRITE_MESSAGE
git filter-branch -f --msg-filter "$BASEDIR/utils/github_closes.js $PR_NUMBER" HEAD~$PR_SHA_COUNT..HEAD
fi
$RESTORE_BRANCH
echo ">>> Push branches to angular repo"
$PUSH_BRANCHES
echo
echo ">>>>>> SUCCESS <<<<<< PR#$PR_NUMBER merged."