Brandon McConnell
Brandon McConnell

Reputation: 6119

Prevent merging of branch conditionally based on label

My team uses private branches which do not have access to Draft Pull Requests, but I would still at least like a way to prevent merging pull requests while they have a custom label applied to them such as "Draft" or "WIP".

Is there a way to establish a custom required status check that checks for that label or uses a GitHub action to invalidate the PR until the label is removed or something like that?

Upvotes: 5

Views: 6858

Answers (3)

Rim
Rim

Reputation: 269

Just want to add something to this

In case someone has a bug when we update new things to the pull request, the checks will run again but it is stuck at

Expected — Waiting for status to be reported

We may have to add some extra events

---
name: 'Check Mergable by label ✅'

on:
  pull_request:
    types:
      - opened
      - reopened
      - synchronize
      - edited
      - labeled
      - unlabeled

jobs:
  fail-by-label:
    if: contains(github.event.pull_request.labels.*.name, 'do not merge')
    runs-on: ubuntu-latest
    steps:
      - name: Fail if PR is labeled "do not merge"
        run: |
          echo "This PR is labeled as do not merge!"
          exit 1

Upvotes: 2

yuki
yuki

Reputation: 31

Moving the condition inside the steps will show green check✅ instead of being shown as skipped

name: Check Draft

on:
  pull_request:
    types:
      - opened
      - labeled
      - unlabeled

jobs:
  fail-for-draft:
    runs-on: ubuntu-latest
    steps:
      - name: Fail if PR is a draft
        if: contains(github.event.pull_request.labels.*.name, 'draft') # Move the condition to here
        run: |
          echo "This PR is currently a draft."
          exit 1

Upvotes: 3

Matt
Matt

Reputation: 13943

Since you don't have draft pull requests available, I assume you don't have branch protection rules as well. Therefore, you cannot really enforce what you describe in your question. However, it is possible to give some graphical hints to make the users aware that the PR is a draft.

This can be done with a workflow in GitHub Actions. You need to add the opened, labeled and unlabeled type on the pull_request trigger in order to run the workflow when the PR gets created or when labels get changed. Afterwards, you can define a job which only runs when the custom label (e.g. draft) is present and just run exit 1 to fail the check.

Here is the complete workflow (e.g. check-draft.yml):

name: Check Draft

on:
  pull_request:
#    branches:
#      - main
    types:
      - opened
      - labeled
      - unlabeled

jobs:
  fail-for-draft:
    if: contains(github.event.pull_request.labels.*.name, 'draft')
    runs-on: ubuntu-latest
    steps:
      - name: Fail if PR is a draft
        run: |
          echo "This PR is currently a draft."
          exit 1

Failed check with draft label on PR: (Note that even though the merge button is grey, the PR can actually be merged if no branch protection rules are set.)

fail

Successful run with no draft label on PR:

success

Upvotes: 9

Related Questions