|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# |
| 4 | +# The purpose of this script is to examine the base branch that this PR is |
| 5 | +# set to merge into by usig the GitHub API. We are only querying a public |
| 6 | +# repo here, so we do not need to use the GITHUB_TOKEN. |
| 7 | +# |
| 8 | + |
| 9 | +# Exit if we are not running on Circle CI. |
| 10 | +if [ -z "$CIRCLECI" ] ; then |
| 11 | + exit 0 |
| 12 | +fi |
| 13 | + |
| 14 | +# We only need to make this check for branches forked from default (right) / master (wrong). |
| 15 | +# Skip the test for the default branch. (The .circleci directory will never be added to the master branch). |
| 16 | +if [ "$CIRCLE_BRANCH" == "default" ] ; then |
| 17 | + exit 0 |
| 18 | +fi |
| 19 | + |
| 20 | +# We cannot continue unless we have a pull request. |
| 21 | +if [ -z "$CIRCLE_PULL_REQUEST" ] ; then |
| 22 | + echo "No CIRCLE_PULL_REQUEST defined; please create a pull request." |
| 23 | + exit 1 |
| 24 | +fi |
| 25 | + |
| 26 | +# CIRCLE_PULL_REQUEST=https://github.com/ORG/PROJECT/pull/NUMBER |
| 27 | +PR_NUMBER=$(echo $CIRCLE_PULL_REQUEST | sed -e 's#.*/pull/##') |
| 28 | + |
| 29 | +# Display the API call we are using |
| 30 | +echo curl https://api.github.com/repos/$CIRCLE_PROJECT_USERNAME/$CIRCLE_PROJECT_REPONAME/pulls/$PR_NUMBER |
| 31 | + |
| 32 | +base=$(curl https://api.github.com/repos/$CIRCLE_PROJECT_USERNAME/$CIRCLE_PROJECT_REPONAME/pulls/$PR_NUMBER 2>/dev/null | jq .base.ref) |
| 33 | + |
| 34 | +echo "The base branch is $base" |
| 35 | + |
| 36 | +# If the PR merges into 'default', then it is safe to merge. |
| 37 | +if [ "$base" == '"default"' ] ; then |
| 38 | + echo "It is safe to merge this PR into the $base branch" |
| 39 | + exit 0 |
| 40 | +fi |
| 41 | + |
| 42 | +# Force a test failure if the PR's base is the master branch. |
| 43 | +if [ "$base" == '"master"' ] ; then |
| 44 | + echo "ERROR: merging this PR into the $base branch is not allowed. Change the base branch for the PR to merge into the \"default\" branch instead." |
| 45 | + exit 1 |
| 46 | +fi |
| 47 | + |
| 48 | +echo "Merging probably okay, if you are merging one PR into another. Use caution; do not merge to the \"master\" branch." |
0 commit comments