|
| 1 | +name: PR Target Branch Check |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + types: [opened, synchronize, reopened, edited] |
| 6 | + branches: |
| 7 | + - master |
| 8 | + |
| 9 | +jobs: |
| 10 | + check-pr-target: |
| 11 | + name: Check PR targets a release branch |
| 12 | + runs-on: ubuntu-latest |
| 13 | + permissions: |
| 14 | + pull-requests: write |
| 15 | + steps: |
| 16 | + - name: Validate source branch |
| 17 | + env: |
| 18 | + HEAD_REF: ${{ github.head_ref }} |
| 19 | + PR_NUMBER: ${{ github.event.pull_request.number }} |
| 20 | + GH_TOKEN: ${{ github.token }} |
| 21 | + REPO: ${{ github.repository }} |
| 22 | + run: | |
| 23 | + if [[ "$HEAD_REF" == rel/* ]] || [[ "$HEAD_REF" == ci/* ]]; then |
| 24 | + echo "Branch '$HEAD_REF' is allowed to target master." |
| 25 | + exit 0 |
| 26 | + fi |
| 27 | +
|
| 28 | + # Post a comment only if one hasn't been posted already |
| 29 | + MARKER="<!-- pr-target-check-warning -->" |
| 30 | + EXISTING=$(gh api "repos/$REPO/issues/$PR_NUMBER/comments" \ |
| 31 | + --jq ".[] | select(.body | contains(\"$MARKER\")) | .id" | head -1) |
| 32 | +
|
| 33 | + if [ -z "$EXISTING" ]; then |
| 34 | + gh api "repos/$REPO/issues/$PR_NUMBER/comments" \ |
| 35 | + --method POST \ |
| 36 | + --field body="${MARKER} |
| 37 | + **:warning: This PR needs to target a release branch before it can merge to \`master\`.** |
| 38 | +
|
| 39 | + All changes — including hotfixes, dependency bumps, and feature work — should flow through a \`rel/*\` branch rather than merging directly to \`master\`. This keeps commit history clean and ensures every change is tied to a release. |
| 40 | +
|
| 41 | + **To fix:** change the base branch of this PR to the appropriate \`rel/*\` branch. You can do this without closing the PR — use the base branch dropdown at the top of the PR. |
| 42 | +
|
| 43 | + - If the release version isn't decided yet, create a placeholder: \`rel/X.Y.Z\` |
| 44 | + - If this is CI/tooling work, rename your branch with a \`ci/\` prefix and this check will pass |
| 45 | +
|
| 46 | + _Enforced by the \`pr-target-check\` workflow._" |
| 47 | + fi |
| 48 | +
|
| 49 | + echo "Branch '$HEAD_REF' cannot target master directly. Must use rel/* or ci/*." |
| 50 | + exit 1 |
0 commit comments