Enforce "Squash and Merge" depending on PR label rather than specific branch. New feature request: Label protection rules #191858
-
🏷️ Discussion TypeQuestion 💬 Feature/Topic AreaWorkflow Configuration Discussion DetailsIn a PR, one could choose freely between "Merge" or "Squash and Merge". This is different from configuring a specific branch to have only one of the two (https://github.com/orgs/community/discussions/10809). It's done dynamically depending on the PR of the contributor to which I would add a label "squash" or "not-squash", eg. I see it's possible to fully block the merge using https://bgenc.net/2023.02.18.github-actions-do-not-merge-label/ but I don't know if it's doable to only block one of the two variants? If that's impossible, is it doable to switch the default enabled button to one or the other depending on that label? I only found this very generic related answer: https://github.com/orgs/community/discussions/148702 If not, I'd would recommend this as a new feature for GitHub called "Label protection rules" on top of the "Branch protection rules"! Thanks in advance for your advice. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
|
Hey @ferdymercury! Unfortunately, GitHub doesn't currently offer a native way to restrict which merge button (Merge vs Squash and Merge) is available based on PR labels — the merge method enforcement in branch/repository rules applies globally to a branch, not conditionally per-label. That said, here's a practical workaround using GitHub Actions that achieves the same goal: label-driven auto-merge via the API, so the merge method is enforced programmatically rather than through the UI. Approach: Label-triggered auto-merge with the correct method
name: Label-driven merge
on:
pull_request:
types: [labeled]
jobs:
merge:
runs-on: ubuntu-latest
permissions:
pull-requests: write
contents: write
steps:
- name: Determine merge method from label
id: method
run: |
LABELS='${{ toJson(github.event.pull_request.labels.*.name) }}'
if echo "$LABELS" | grep -q '"automerge-squash"'; then
echo "method=squash" >> $GITHUB_OUTPUT
elif echo "$LABELS" | grep -q '"automerge-merge"'; then
echo "method=merge" >> $GITHUB_OUTPUT
else
echo "method=none" >> $GITHUB_OUTPUT
fi
- name: Merge PR with correct method
if: steps.method.outputs.method != 'none'
uses: actions/github-script@v7
with:
script: |
await github.rest.pulls.merge({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: context.payload.pull_request.number,
merge_method: '${{ steps.method.outputs.method }}'
});If you only want to block a specific merge method (not auto-merge)This is harder because GitHub Actions can't intercept a UI merge button click before it happens. The closest you can get is:
Unfortunately that still doesn't technically restrict the UI button — it just blocks merging entirely, which isn't quite what you're after. Feature requestAgreed this would be a great addition as "Label protection rules" — essentially making merge method a per-PR ruleset condition. You might want to upvote or reference the existing feature discussion at https://github.com/orgs/community/discussions/148702 to help get traction on it. Hope that helps! |
Beta Was this translation helpful? Give feedback.
-
|
Hi @ferdymercury! Currently, GitHub doesn't support dynamic UI changes (like hiding specific merge buttons) based on PR labels. Merge options are managed globally in the repo settings. However, you can achieve the same enforcement by using a GitHub Action as a Required Status Check:
While this won't hide the button, it will stay grayed out and prevent anyone from merging until the conditions of your label-logic are met. Hope this works. |
Beta Was this translation helpful? Give feedback.
Hey @ferdymercury! Unfortunately, GitHub doesn't currently offer a native way to restrict which merge button (Merge vs Squash and Merge) is available based on PR labels — the merge method enforcement in branch/repository rules applies globally to a branch, not conditionally per-label.
That said, here's a practical workaround using GitHub Actions that achieves the same goal: label-driven auto-merge via the API, so the merge method is enforced programmatically rather than through the UI.
Approach: Label-triggered auto-merge with the correct method