From 3659e72a7c97e8d8a156d042f091191f0529b572 Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Wed, 30 Apr 2025 09:53:32 +0200 Subject: [PATCH 1/2] Enforce that PRs are not opened from the 'master' branch of a fork --- .github/workflows/enforce_branch_name.yml | 34 +++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 .github/workflows/enforce_branch_name.yml diff --git a/.github/workflows/enforce_branch_name.yml b/.github/workflows/enforce_branch_name.yml new file mode 100644 index 00000000000..7f31ec3c2ab --- /dev/null +++ b/.github/workflows/enforce_branch_name.yml @@ -0,0 +1,34 @@ +name: PR Branch Name Check + +on: + pull_request_target: + types: [opened, reopened, synchronize] + +jobs: + check-source-branch: + runs-on: ubuntu-latest + steps: + - name: Check PR source branch + run: | + # Check if PR is from a fork + if [[ "${{ github.event.pull_request.head.repo.fork }}" == "true" ]]; then + # Check if PR is from the master/main branch of a fork + if [[ "${{ github.event.pull_request.head.ref }}" == "master" || "${{ github.event.pull_request.head.ref }}" == "main" ]]; then + echo "ERROR: Pull requests from the master/main branch of forks are not allowed, because it prevents maintainers from contributing to your PR" + echo "Please create a feature branch in your fork and submit the PR from that branch instead." + exit 1 + fi + fi + + - name: Leave comment if PR is from master/main branch of fork + if: ${{ github.event.pull_request.head.repo.fork == 'true' && (github.event.pull_request.head.ref == 'master' || github.event.pull_request.head.ref == 'main') }} + uses: actions/github-script@v6 + with: + github-token: ${{ secrets.GITHUB_TOKEN }} + script: | + github.rest.issues.createComment({ + issue_number: context.issue.number, + owner: context.repo.owner, + repo: context.repo.repo, + body: '⚠️ **ERROR:** Pull requests from the `master`/`main` branch of forks are not allowed, because it prevents maintainers from contributing to your PR. Please create a feature branch in your fork and submit the PR from that branch instead.' + }) From bd175e791fb8bb435cb94c1a695c7b699f83cded Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Wed, 30 Apr 2025 10:48:29 +0200 Subject: [PATCH 2/2] Reverse order of the steps --- .github/workflows/enforce_branch_name.yml | 24 +++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/.github/workflows/enforce_branch_name.yml b/.github/workflows/enforce_branch_name.yml index 7f31ec3c2ab..7868426cb5b 100644 --- a/.github/workflows/enforce_branch_name.yml +++ b/.github/workflows/enforce_branch_name.yml @@ -8,18 +8,6 @@ jobs: check-source-branch: runs-on: ubuntu-latest steps: - - name: Check PR source branch - run: | - # Check if PR is from a fork - if [[ "${{ github.event.pull_request.head.repo.fork }}" == "true" ]]; then - # Check if PR is from the master/main branch of a fork - if [[ "${{ github.event.pull_request.head.ref }}" == "master" || "${{ github.event.pull_request.head.ref }}" == "main" ]]; then - echo "ERROR: Pull requests from the master/main branch of forks are not allowed, because it prevents maintainers from contributing to your PR" - echo "Please create a feature branch in your fork and submit the PR from that branch instead." - exit 1 - fi - fi - - name: Leave comment if PR is from master/main branch of fork if: ${{ github.event.pull_request.head.repo.fork == 'true' && (github.event.pull_request.head.ref == 'master' || github.event.pull_request.head.ref == 'main') }} uses: actions/github-script@v6 @@ -32,3 +20,15 @@ jobs: repo: context.repo.repo, body: '⚠️ **ERROR:** Pull requests from the `master`/`main` branch of forks are not allowed, because it prevents maintainers from contributing to your PR. Please create a feature branch in your fork and submit the PR from that branch instead.' }) + + - name: Check PR source branch + run: | + # Check if PR is from a fork + if [[ "${{ github.event.pull_request.head.repo.fork }}" == "true" ]]; then + # Check if PR is from the master/main branch of a fork + if [[ "${{ github.event.pull_request.head.ref }}" == "master" || "${{ github.event.pull_request.head.ref }}" == "main" ]]; then + echo "ERROR: Pull requests from the master/main branch of forks are not allowed, because it prevents maintainers from contributing to your PR" + echo "Please create a feature branch in your fork and submit the PR from that branch instead." + exit 1 + fi + fi