Skip to content

Commit cedad0d

Browse files
committed
Guard master against pull requests from forks and non-release branches
1 parent a7fe2e1 commit cedad0d

1 file changed

Lines changed: 40 additions & 0 deletions

File tree

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Fails any pull request into master that does not come from this repository's
2+
# own develop, release/* or hotfix/* branch. master is what production runs, so
3+
# outside contributions land on develop first and reach master only in a release
4+
# merge (hotfix/* is the documented emergency path, see the release process).
5+
# master's branch protection requires this check, so a fork pull request aimed
6+
# at master cannot be merged by mistake.
7+
#
8+
# pull_request_target reads this file from the repository's own branch, never
9+
# from the pull request, so a pull request cannot edit the guard away. The job
10+
# reads only event metadata and never checks out pull request code.
11+
name: Base branch guard
12+
13+
on:
14+
pull_request_target:
15+
branches: [master]
16+
# edited fires when an open pull request is retargeted onto master.
17+
types: [opened, reopened, synchronize, edited]
18+
19+
permissions: {}
20+
21+
jobs:
22+
base-branch-guard:
23+
name: base-branch guard
24+
runs-on: ubuntu-latest
25+
steps:
26+
- name: Require develop, release/* or hotfix/* from this repository
27+
# Event fields go through env, never inline, so a branch name cannot inject shell.
28+
env:
29+
HEAD_REPO: ${{ github.event.pull_request.head.repo.full_name }}
30+
HEAD_REF: ${{ github.event.pull_request.head.ref }}
31+
THIS_REPO: ${{ github.repository }}
32+
run: |
33+
if [ "$HEAD_REPO" != "$THIS_REPO" ]; then
34+
echo "::error::Pull requests from forks go to develop, not master. Change the base branch to develop."
35+
exit 1
36+
fi
37+
case "$HEAD_REF" in
38+
develop|release/*|hotfix/*) echo "ok: $HEAD_REF into master" ;;
39+
*) echo "::error::master only accepts develop, release/* or hotfix/*; retarget $HEAD_REF to develop."; exit 1 ;;
40+
esac

0 commit comments

Comments
 (0)