|
| 1 | +#!/bin/bash |
| 2 | +# |
| 3 | +# Copyright 2021 The AMP HTML Authors. All Rights Reserved. |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS-IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the license. |
| 16 | + |
| 17 | +# This script checks if a PR branch is using the most recent CircleCI config. |
| 18 | +# Reference: https://circleci.com/docs/2.0/env-vars/#built-in-environment-variables |
| 19 | + |
| 20 | +set -e |
| 21 | +err=0 |
| 22 | + |
| 23 | +GREEN() { echo -e "\n\033[0;32m$1\033[0m"; } |
| 24 | +RED() { echo -e "\n\033[0;31m$1\033[0m"; } |
| 25 | +YELLOW() { echo -e "\033[0;33m$1\033[0m"; } |
| 26 | +CYAN() { echo -e "\033[0;36m$1\033[0m"; } |
| 27 | + |
| 28 | +# Push builds are only run against the main branch and amp-release branches. |
| 29 | +if [[ "$CIRCLE_BRANCH" == "main" || "$CIRCLE_BRANCH" =~ ^amp-release-* ]]; then |
| 30 | + echo $(GREEN "Nothing to do because $CIRCLE_BRANCH is not a PR branch.") |
| 31 | + exit 0 |
| 32 | +fi |
| 33 | + |
| 34 | +# Check if the PR branch contains the most recent revision the CircleCI config. |
| 35 | +CONFIG_REV=`git rev-list main -1 -- .circleci/config.yml` |
| 36 | +(set -x && git merge-base --is-ancestor $CONFIG_REV $CIRCLE_SHA1) || err=$? |
| 37 | + |
| 38 | +if [[ "$err" -ne "0" ]]; then |
| 39 | + echo "$(RED "ERROR:") $(CYAN $CIRCLE_BRANCH) is missing the latest CircleCI config revision $(CYAN $CONFIG_REV)." |
| 40 | + echo -e "\n" |
| 41 | + |
| 42 | + echo $(YELLOW "This can be fixed in three ways:") |
| 43 | + echo -e "\n" |
| 44 | + |
| 45 | + echo "1. Click the $(CYAN "\"Update branch\"") button on GitHub and follow instructions." |
| 46 | + echo " ⤷ It can be found towards the bottom of the PR, after the list of checks." |
| 47 | + echo -e "\n" |
| 48 | + |
| 49 | + echo "2. Pull the latest commits from $(CYAN "main") and re-push the PR branch." |
| 50 | + echo " ⤷ Follow these steps:" |
| 51 | + echo "" |
| 52 | + echo " $(CYAN "git checkout main")" |
| 53 | + echo " $(CYAN "git pull")" |
| 54 | + echo " $(CYAN "git checkout <PR branch>")" |
| 55 | + echo " $(CYAN "git merge main")" |
| 56 | + echo " $(CYAN "git push origin")" |
| 57 | + echo -e "\n" |
| 58 | + |
| 59 | + echo "3. Rebase on $(CYAN "main") and re-push the PR branch." |
| 60 | + echo " ⤷ Follow these steps:" |
| 61 | + echo "" |
| 62 | + echo " $(CYAN "git checkout main")" |
| 63 | + echo " $(CYAN "git pull")" |
| 64 | + echo " $(CYAN "git checkout <PR branch>")" |
| 65 | + echo " $(CYAN "git rebase main")" |
| 66 | + echo " $(CYAN "git push origin --force")" |
| 67 | + echo -e "\n" |
| 68 | + exit 1 |
| 69 | +fi |
| 70 | + |
| 71 | +echo $(GREEN "$CIRCLE_BRANCH is using the latest CircleCI config.") |
0 commit comments