2018-02-22 15:07:17 -05:00
|
|
|
#!/usr/bin/env bash
|
2017-07-26 15:37:14 -04:00
|
|
|
|
2019-01-18 10:44:56 -05:00
|
|
|
# https://www.tldp.org/LDP/abs/html/options.html#AEN19601
|
|
|
|
# https://www.gnu.org/software/bash/manual/html_node/The-Set-Builtin.html
|
|
|
|
set -u -e -E -o pipefail
|
|
|
|
|
2020-05-19 14:17:45 -04:00
|
|
|
# Clippy ascii art copied from https://github.com/gbigwood/Clippo
|
|
|
|
echo -e "
|
|
|
|
################################################
|
|
|
|
/ \ _________________
|
|
|
|
| | / \\
|
|
|
|
@ @ | It looks like |
|
|
|
|
|| || | you are trying |
|
|
|
|
|| || <--| to merge a PR. |
|
|
|
|
|\_/| \_________________/
|
|
|
|
\___/
|
|
|
|
|
|
|
|
A new merge script is available using `ng-dev`!
|
|
|
|
|
|
|
|
In the future, this script will be removed in
|
|
|
|
favor of using ng-dev.
|
|
|
|
|
|
|
|
To merge a pr using the new tooling run:
|
|
|
|
|
2020-05-19 18:18:23 -04:00
|
|
|
$ yarn -s ng-dev pr merge <pr-number>
|
2020-05-19 14:17:45 -04:00
|
|
|
|
|
|
|
################################################"
|
|
|
|
# Sleep a short time to highlight deprecate notice.
|
|
|
|
sleep 2;
|
|
|
|
|
2019-04-19 13:54:10 -04:00
|
|
|
BOLD='printf \e[1m'
|
|
|
|
RESET_STYLES='printf \e[0m'
|
|
|
|
LIGHT_CYAN='printf \e[96m'
|
|
|
|
LIGHT_MAGENTA='printf \e[95m'
|
|
|
|
GREEN='printf \e[32m'
|
|
|
|
RED='printf \e[31m'
|
2017-07-26 15:37:14 -04:00
|
|
|
|
|
|
|
BASEDIR=$(dirname "$0")
|
|
|
|
BASEDIR=`(cd $BASEDIR; pwd)`
|
|
|
|
|
2018-01-25 18:55:00 -05:00
|
|
|
PR_NUMBER=0
|
|
|
|
PUSH_UPSTREAM=1
|
2018-01-26 16:17:45 -05:00
|
|
|
FORCE=0
|
2018-01-25 18:55:00 -05:00
|
|
|
while [[ $# -gt 0 ]]
|
|
|
|
do
|
|
|
|
key="$1"
|
|
|
|
|
|
|
|
case $key in
|
|
|
|
--dryrun)
|
|
|
|
PUSH_UPSTREAM=0
|
|
|
|
shift # past argument
|
|
|
|
;;
|
2018-01-26 16:17:45 -05:00
|
|
|
--force)
|
|
|
|
FORCE=1
|
|
|
|
shift # past argument
|
|
|
|
;;
|
2018-01-25 18:55:00 -05:00
|
|
|
*) # unknown option
|
|
|
|
PR_NUMBER="$1" # save it in an array for later
|
|
|
|
shift # past argument
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
|
|
|
if [ "$PR_NUMBER" -eq 0 ]; then
|
2018-01-26 16:17:45 -05:00
|
|
|
echo "Merge github PR into the target branches if status is green"
|
2017-07-26 15:37:14 -04:00
|
|
|
echo
|
2018-01-26 16:17:45 -05:00
|
|
|
echo "$0 PR_NUMBER [--dryrun] [--force]"
|
2017-07-26 15:37:14 -04:00
|
|
|
echo
|
2018-01-26 16:17:45 -05:00
|
|
|
echo --dryrun Performs all operations but does not push the merge back to git@github.com:angular/angular.git.
|
|
|
|
echo --force Continues even if PR status is not green.
|
2017-07-26 15:37:14 -04:00
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
|
2018-02-16 20:54:20 -05:00
|
|
|
if [ -z ${TOKEN:-''} ]; then
|
2019-04-19 13:54:10 -04:00
|
|
|
$RED
|
2018-02-16 20:54:20 -05:00
|
|
|
echo "############################################################"
|
|
|
|
echo "############################################################"
|
|
|
|
echo "WARNING: you should set the TOKEN variable to a github token"
|
|
|
|
echo "############################################################"
|
|
|
|
echo "############################################################"
|
2019-04-19 13:54:10 -04:00
|
|
|
$RESET_STYLES
|
2018-07-30 19:46:11 -04:00
|
|
|
GH_AUTH=""
|
2018-02-16 20:54:20 -05:00
|
|
|
else
|
2018-07-30 19:46:11 -04:00
|
|
|
GH_AUTH="Authorization: token $TOKEN"
|
2018-02-16 20:54:20 -05:00
|
|
|
fi
|
|
|
|
|
2018-07-30 19:46:11 -04:00
|
|
|
PULL_JSON=`curl -H "$GH_AUTH" -s https://api.github.com/repos/angular/angular/pulls/$PR_NUMBER`
|
2018-01-26 16:17:45 -05:00
|
|
|
PR_SHA_COUNT=`node $BASEDIR/utils/json_extract.js commits <<< """$PULL_JSON"""`
|
|
|
|
STATUS_JSON_URL=`node $BASEDIR/utils/json_extract.js _links.statuses.href <<< """$PULL_JSON"""`
|
2018-07-30 19:46:11 -04:00
|
|
|
STATUS=`curl -H "$GH_AUTH" -s $STATUS_JSON_URL | node $BASEDIR/utils/json_extract.js description | cut -d '|' -f1`
|
|
|
|
PR_LABELS=`curl -H "$GH_AUTH" -s https://api.github.com/repos/angular/angular/issues/$PR_NUMBER/labels`
|
2017-07-26 15:37:14 -04:00
|
|
|
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'
|
|
|
|
|
2018-02-23 14:54:09 -05:00
|
|
|
if [[ ! "$PR_ACTION" =~ "PR action: merge" ]]; then
|
2019-04-19 13:54:10 -04:00
|
|
|
$RED
|
2018-02-23 14:54:09 -05:00
|
|
|
echo The PR is missing 'PR action: merge(-assistance)' label, found: $PR_ACTION
|
2019-04-19 13:54:10 -04:00
|
|
|
$RESET_STYLES
|
2017-07-26 15:37:14 -04:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [[ "$PR_CLA" != "cla: yes" ]]; then
|
2019-04-19 13:54:10 -04:00
|
|
|
$RED
|
2017-07-26 15:37:14 -04:00
|
|
|
echo The PR is missing 'cla: Yes' label, found: $PR_CLA
|
2019-04-19 13:54:10 -04:00
|
|
|
$RESET_STYLES
|
2017-07-26 15:37:14 -04:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2018-01-26 16:17:45 -05:00
|
|
|
if [[ "$STATUS" != "All checks passed!" ]]; then
|
|
|
|
echo PR $PR_NUMBER is failing with: $STATUS
|
|
|
|
if [[ $FORCE == 1 ]]; then
|
2019-04-19 13:54:10 -04:00
|
|
|
$BOLD
|
|
|
|
$LIGHT_MAGENTA
|
2018-05-30 12:27:15 -04:00
|
|
|
echo FORCING: --force flag used to ignore PR status.
|
2019-04-19 13:54:10 -04:00
|
|
|
$RESET_STYLES
|
2018-02-16 20:54:20 -05:00
|
|
|
else
|
2019-04-19 13:54:10 -04:00
|
|
|
echo Exiting...
|
2018-01-26 16:17:45 -05:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
fi
|
2017-07-26 15:37:14 -04:00
|
|
|
|
|
|
|
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
|
2019-04-19 13:54:10 -04:00
|
|
|
$RED
|
2017-07-26 15:37:14 -04:00
|
|
|
echo "Unknown PR target format: $PR_TARGET"
|
2019-04-19 13:54:10 -04:00
|
|
|
$RESET_STYLES
|
2017-07-26 15:37:14 -04:00
|
|
|
exit 1;
|
|
|
|
fi
|
|
|
|
|
2018-02-23 14:54:09 -05:00
|
|
|
CURRENT_BRANCH=`git rev-parse --abbrev-ref HEAD`
|
|
|
|
PATCH_BRANCH=`git ls-remote --heads git@github.com:angular/angular.git | grep -E 'refs\/heads\/[0-9]+\.[0-9]+\.x' | cut -d '/' -f3 | sort -r | head -n1`
|
2017-07-26 15:37:14 -04:00
|
|
|
|
2018-01-23 19:49:20 -05:00
|
|
|
CHECKOUT_MASTER="git checkout merge_pr_master"
|
2018-01-25 18:55:00 -05:00
|
|
|
CHECKOUT_PATCH="git checkout merge_pr_patch"
|
2018-01-23 19:49:20 -05:00
|
|
|
RESTORE_BRANCH="git checkout $CURRENT_BRANCH"
|
2018-02-23 14:54:09 -05:00
|
|
|
|
2018-05-30 12:27:15 -04:00
|
|
|
FETCH_PR="git fetch git@github.com:angular/angular.git pull/$PR_NUMBER/head:merge_pr heads/$MASTER_BRANCH:merge_pr_master heads/$PATCH_BRANCH:merge_pr_patch -f"
|
2018-01-25 18:55:00 -05:00
|
|
|
BASE_PR="git checkout merge_pr~$PR_SHA_COUNT -B merge_pr_base"
|
|
|
|
SQUASH_PR="git rebase --autosquash --interactive merge_pr_base merge_pr"
|
|
|
|
REWRITE_MESSAGE="git filter-branch -f --msg-filter \"$BASEDIR/utils/github_closes.js $PR_NUMBER\" merge_pr_base..merge_pr"
|
2018-05-30 12:27:15 -04:00
|
|
|
PUSH_BRANCHES="git push git@github.com:angular/angular.git merge_pr_master:$MASTER_BRANCH merge_pr_patch:$PATCH_BRANCH"
|
2018-02-23 14:54:09 -05:00
|
|
|
CHERRY_PICK_PR="git cherry-pick merge_pr_base..merge_pr"
|
2017-07-26 15:37:14 -04:00
|
|
|
|
2019-01-18 10:44:56 -05:00
|
|
|
# Checks that each PR branch to be merged upstream contains SHAs of commits that significantly changed our CI infrastructure.
|
|
|
|
#
|
|
|
|
# This check is used to enforce that we don't merge PRs that have not been rebased recently and could result in merging
|
|
|
|
# of non-approved or otherwise bad changes.
|
2020-04-21 18:14:17 -04:00
|
|
|
REQUIRED_BASE_SHA_MASTER="4341743b4a6d7e23c6f944aa9e34166b701369a1" # `commit-message` script update
|
|
|
|
REQUIRED_BASE_SHA_PATCH="2a53f471592f424538802907aca1f60f1177a86d" # `commit-message` script update
|
2019-01-18 10:44:56 -05:00
|
|
|
if [[ $MERGE_MASTER == 1 ]]; then
|
|
|
|
REQUIRED_BASE_SHA="$REQUIRED_BASE_SHA_MASTER"
|
|
|
|
# check patch only if patch-only PR
|
|
|
|
elif [[ $MERGE_PATCH == 1 ]]; then
|
|
|
|
REQUIRED_BASE_SHA="$REQUIRED_BASE_SHA_PATCH"
|
|
|
|
fi
|
|
|
|
CHECK_IF_PR_REBASED="git branch --quiet merge_pr --contains $REQUIRED_BASE_SHA"
|
|
|
|
|
2019-04-19 13:54:10 -04:00
|
|
|
$BOLD
|
|
|
|
$LIGHT_CYAN
|
2017-07-26 15:37:14 -04:00
|
|
|
echo "======================"
|
|
|
|
echo "GitHub Merge PR Steps"
|
|
|
|
echo "======================"
|
2017-12-27 18:04:51 -05:00
|
|
|
echo " $FETCH_PR"
|
2018-01-25 18:55:00 -05:00
|
|
|
echo " $BASE_PR"
|
2019-01-18 10:44:56 -05:00
|
|
|
echo " $CHECK_IF_PR_REBASED"
|
2018-01-25 18:55:00 -05:00
|
|
|
echo " $SQUASH_PR"
|
|
|
|
echo " $REWRITE_MESSAGE"
|
2018-01-23 19:49:20 -05:00
|
|
|
if [[ $MERGE_MASTER == 1 ]]; then
|
2018-01-25 18:55:00 -05:00
|
|
|
echo " $CHECKOUT_MASTER && $CHERRY_PICK_PR"
|
2018-01-23 19:49:20 -05:00
|
|
|
fi
|
|
|
|
if [[ $MERGE_PATCH == 1 ]]; then
|
2018-01-25 18:55:00 -05:00
|
|
|
echo " $CHECKOUT_PATCH && $CHERRY_PICK_PR"
|
2018-01-23 19:49:20 -05:00
|
|
|
fi
|
|
|
|
echo " $PUSH_BRANCHES"
|
|
|
|
echo " $RESTORE_BRANCH"
|
2017-07-26 15:37:14 -04:00
|
|
|
echo "----------------------"
|
2019-04-19 13:54:10 -04:00
|
|
|
$RESET_STYLES
|
|
|
|
$BOLD
|
|
|
|
echo ""
|
2018-01-25 18:55:00 -05:00
|
|
|
echo ">>> Fetch PR: $FETCH_PR"
|
2019-04-19 13:54:10 -04:00
|
|
|
$RESET_STYLES
|
2017-12-27 18:04:51 -05:00
|
|
|
$FETCH_PR
|
2019-04-19 13:54:10 -04:00
|
|
|
$BOLD
|
|
|
|
echo ""
|
2018-01-25 18:55:00 -05:00
|
|
|
echo ">>> Mark base: $BASE_PR"
|
2019-04-19 13:54:10 -04:00
|
|
|
$RESET_STYLES
|
2018-01-25 18:55:00 -05:00
|
|
|
$BASE_PR
|
2019-04-19 13:54:10 -04:00
|
|
|
$BOLD
|
|
|
|
echo ""
|
2019-01-18 10:44:56 -05:00
|
|
|
echo ">>> Check if PR rebased: $CHECK_IF_PR_REBASED"
|
2019-04-19 13:54:10 -04:00
|
|
|
$RESET_STYLES
|
2019-01-18 10:44:56 -05:00
|
|
|
if [[ $($CHECK_IF_PR_REBASED) != "" ]]; then
|
2019-04-19 13:54:10 -04:00
|
|
|
$GREEN
|
2019-01-18 10:44:56 -05:00
|
|
|
echo "The PR is sufficiently rebased!"
|
2019-04-19 13:54:10 -04:00
|
|
|
$RESET_STYLES
|
2019-01-18 10:44:56 -05:00
|
|
|
else
|
2019-04-19 13:54:10 -04:00
|
|
|
$RED
|
2019-01-18 10:44:56 -05:00
|
|
|
echo ""
|
|
|
|
echo ""
|
|
|
|
echo "Failed to merge pull request #${PR_NUMBER} because it hasn't been rebased recently and could be bypassing new or updated CI checks!"
|
|
|
|
echo ""
|
|
|
|
echo "Please rebase the PR and try again."
|
2019-04-19 13:54:10 -04:00
|
|
|
echo ""|
|
|
|
|
$RESET_STYLES
|
2019-01-18 10:44:56 -05:00
|
|
|
$RESTORE_BRANCH
|
|
|
|
exit 1
|
|
|
|
fi
|
2019-04-19 13:54:10 -04:00
|
|
|
$BOLD
|
|
|
|
echo ""
|
2018-01-25 18:55:00 -05:00
|
|
|
echo ">>> Autosquash: $SQUASH_PR"
|
2019-04-19 13:54:10 -04:00
|
|
|
$RESET_STYLES
|
2018-01-25 18:55:00 -05:00
|
|
|
GIT_EDITOR=echo $SQUASH_PR
|
2019-04-19 13:54:10 -04:00
|
|
|
$BOLD
|
|
|
|
echo ""
|
2018-01-25 18:55:00 -05:00
|
|
|
echo ">>> Rewrite message: $REWRITE_MESSAGE"
|
|
|
|
# Next line should work, but it errors, hence copy paste the command.
|
|
|
|
# $REWRITE_MESSAGE
|
2019-04-19 13:54:10 -04:00
|
|
|
$RESET_STYLES
|
2018-01-25 18:55:00 -05:00
|
|
|
git filter-branch -f --msg-filter "$BASEDIR/utils/github_closes.js $PR_NUMBER" merge_pr_base..merge_pr
|
2018-01-23 19:49:20 -05:00
|
|
|
if [[ $MERGE_MASTER == 1 ]]; then
|
2019-04-19 13:54:10 -04:00
|
|
|
$BOLD
|
2018-01-23 19:49:20 -05:00
|
|
|
echo
|
2018-01-25 18:55:00 -05:00
|
|
|
echo ">>> Cherry pick to master: $CHECKOUT_MASTER && $CHERRY_PICK_PR"
|
2019-04-19 13:54:10 -04:00
|
|
|
$RESET_STYLES
|
2018-01-23 19:49:20 -05:00
|
|
|
$CHECKOUT_MASTER
|
|
|
|
$CHERRY_PICK_PR
|
|
|
|
fi
|
|
|
|
if [[ $MERGE_PATCH == 1 ]]; then
|
2019-04-19 13:54:10 -04:00
|
|
|
$BOLD
|
2018-01-23 19:49:20 -05:00
|
|
|
echo
|
2019-04-19 13:54:10 -04:00
|
|
|
echo ">>> Cherry pick to patch: $CHECKOUT_PATCH && $CHERRY_PICK_PR"
|
|
|
|
$RESET_STYLES
|
2018-01-23 19:49:20 -05:00
|
|
|
$CHECKOUT_PATCH
|
|
|
|
$CHERRY_PICK_PR
|
|
|
|
fi
|
|
|
|
$RESTORE_BRANCH
|
2017-07-26 15:37:14 -04:00
|
|
|
|
2018-01-25 18:55:00 -05:00
|
|
|
if [[ $PUSH_UPSTREAM == 1 ]]; then
|
2019-04-19 13:54:10 -04:00
|
|
|
$BOLD
|
|
|
|
echo
|
2018-01-25 18:55:00 -05:00
|
|
|
echo ">>> Push branches to angular repo"
|
2019-04-19 13:54:10 -04:00
|
|
|
$RESET_STYLES
|
2018-01-25 18:55:00 -05:00
|
|
|
$PUSH_BRANCHES
|
|
|
|
fi
|
2019-04-19 13:54:10 -04:00
|
|
|
$BOLD
|
|
|
|
$GREEN
|
2017-07-26 15:37:14 -04:00
|
|
|
echo
|
2018-01-23 19:49:20 -05:00
|
|
|
echo ">>>>>> SUCCESS <<<<<< PR#$PR_NUMBER merged."
|
2019-04-19 13:54:10 -04:00
|
|
|
$RESET_STYLES
|