Sub topic 6
Requiring pull requests, reviews and status checks
Two rules do most of the work in a branch ruleset: pull_request and required_status_checks. The first makes people review changes before they land. The second makes automation prove the changes are safe. This chapter covers what each rule enforces, how its parameters behave, and the mistakes that cause stuck or unprotected merges.
Prerequisites
This chapter assumes you can:
- Create a branch and open a pull request.
- Read a ruleset JSON body like the one in the earlier chapters.
- Describe a GitHub Actions workflow with jobs and triggers.
- Follow the difference between a repository ruleset and an organisation ruleset.
What the two rules enforce
When a ruleset targets a branch and includes the pull_request rule, nobody can push directly to that branch. Changes must arrive through a pull request. The rule's parameters then decide what has to happen on that pull request before the merge button works.
The required_status_checks rule adds a second gate. Named checks must report success on the pull request's head commit. Both rules must pass, so a pull request can have every approval it needs and still be blocked by a failing test.
The pull request rule
The pull_request rule takes these parameters. The first example in this tutorial already uses most of them.
| Parameter | Effect |
|---|---|
| `required_approving_review_count` | Number of approving reviews needed, from 0 to 10. |
| `dismiss_stale_reviews_on_push` | New commits discard existing approvals. |
| `require_code_owner_review` | At least one owner named in `CODEOWNERS` must approve for the files changed. |
| `require_last_push_approval` | The most recent pusher cannot supply the approval that counts. |
| `required_review_thread_resolution` | Every review conversation must be resolved. |
| `allowed_merge_methods` | Restricts merging to `merge`, `squash`, `rebase`, or a subset. |
Choosing a review count
A count of 1 is the usual floor. Setting it to 0 still forces changes through a pull request, which is useful when you want an audit trail and status checks but not a human gate. A higher count adds friction on small teams. If a repository has two active maintainers, a count of 2 means the author can never merge without help from both others, and that stalls work.
Stale reviews and last-push approval
These two parameters solve different problems.
dismiss_stale_reviews_on_pushhandles the case where a reviewer approves, then the author pushes new code. Without it, the old approval still counts for code the reviewer never saw.require_last_push_approvalhandles the case where two people collaborate on a branch. Person A pushes the final commit, and person B, who also pushed earlier, approves. Without the parameter, B's approval counts even though B contributed code. With it, someone who did not push the latest commit must approve.
Use dismissal for most repositories. Add last-push approval where you want a strict four-eyes rule. Note that the pull request author cannot approve their own pull request in either case.
Code owner review
require_code_owner_review only works if a CODEOWNERS file exists. GitHub looks for it in .github/, the repository root, or docs/. If the file is missing, the parameter has no owners to require, so the rule is effectively off for that repository. If you apply the rule across an organisation, either add a CODEOWNERS file to each repository or use a different rule for repositories that lack one.
A minimal file looks like this:
# Everything needs the platform team unless a later line overrides it
* @YOUR_ORG/platform
/docs/ @YOUR_ORG/docs-writers
/.github/ @YOUR_ORG/securityThe last matching pattern wins. Owners must be users or teams with write access to the repository, or their review will not satisfy the rule.
Thread resolution
required_review_thread_resolution blocks the merge while any review comment thread is unresolved. It is cheap to enable and prevents "approved with comments" pull requests from landing with open questions. The cost is that authors must click "Resolve conversation" on trivial threads.
The required status checks rule
Here is a rule entry for the rules array of a ruleset:
{
"type": "required_status_checks",
"parameters": {
"strict_required_status_checks_policy": true,
"do_not_enforce_on_create": false,
"required_status_checks": [
{ "context": "test", "integration_id": 15368 },
{ "context": "lint" }
]
}
}Each item in required_status_checks names a check by its context. The optional integration_id pins the check to one GitHub App. The ID 15368 is the GitHub Actions app. Without it, any app or user that posts a status named test can satisfy the rule.
Strict policy
strict_required_status_checks_policy means the branch must be up to date with the base branch before merging. If someone else merges first, your pull request is out of date. You must update it and wait for the checks to run again. This prevents two individually green pull requests from breaking the base branch when combined. On busy repositories it causes repeated rebases, so consider a merge queue instead.
Check names
A check's context is the name that appears in the pull request's checks list. For GitHub Actions, that is the job name, not the workflow name. Given this workflow:
name: CI
on:
pull_request:
branches: [main]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: npm ci
- run: npm testthe check name to require is test. If the job has a name: key, use that value. For matrix jobs, each combination reports its own name, such as test (18) and test (20), so you must list each one you want to require.
Gotchas with status checks
- Checks that never run. A required check that is not triggered stays "Expected" forever and blocks the merge. This happens when a workflow uses
paths:filters, or when the job has anif:that skips it on some pull requests. A job skipped byif:reports success, but a workflow skipped byon.pull_request.pathsreports nothing. - A typo in the name. GitHub does not verify that the context exists when you save it through the API. A wrong name creates a permanent blocker.
- Repositories without the workflow. In an organisation ruleset, requiring a check named
testblocks every targeted repository that has no job reportingtest. The next chapter covers required workflows, which solve this by running the workflow for you. - Picking checks in the UI. The web interface only offers checks that ran in the repository within the last seven days. The API has no such limit.
Creating the ruleset branch for new branches
do_not_enforce_on_create lets someone create a new matching branch and push to it without the checks having already passed. Leave it false unless you have a workflow that creates branches and pushes immediately.
Combining the rules with bypass
A bypass actor with mode pull_request, as in the first example, can skip the review and check requirements only by opening a pull request and merging it with the bypass option. The bypass is visible on the pull request. Mode always allows direct pushes that skip every rule in the ruleset. For a ruleset whose job is to require reviews and checks, pull_request is the narrower choice.
Putting it together
Here is the rules array for a ruleset that requires two approvals from code owners and passing test and lint checks. It also blocks deletion and force pushes, and allows only squash merges:
[
{ "type": "deletion" },
{ "type": "non_fast_forward" },
{
"type": "pull_request",
"parameters": {
"required_approving_review_count": 2,
"dismiss_stale_reviews_on_push": true,
"require_code_owner_review": true,
"require_last_push_approval": true,
"required_review_thread_resolution": true,
"allowed_merge_methods": ["squash"]
}
},
{
"type": "required_status_checks",
"parameters": {
"strict_required_status_checks_policy": true,
"do_not_enforce_on_create": false,
"required_status_checks": [
{ "context": "test", "integration_id": 15368 },
{ "context": "lint", "integration_id": 15368 }
]
}
}
]Place this array in the rules field of the body you send with gh api, as shown earlier in the tutorial. If you update an existing ruleset with PUT, send the complete rules array, because the call replaces the old one.
Common errors
| Symptom | Likely cause |
|---|---|
| Merge button says "Expected" for a check | Workflow skipped by `paths:` filter, or wrong `context` name |
| Code owner review never satisfied | No `CODEOWNERS` file, or the owner lacks write access |
| Approval disappears after a small fix | `dismiss_stale_reviews_on_push` is on, as intended |
| Pull request must be updated again and again | `strict_required_status_checks_policy` on a busy branch |
| Admin cannot merge a red pull request | Bypass actor missing, or mode is not `pull_request` |
Exercise
A ruleset requires the check build, but your workflow job is declared as build-and-test: with no name: key, and the workflow is called CI. Pull requests stay blocked on "Expected: build". What do you change, and where?
Show answer
The check name comes from the job, so it is build-and-test. Either change context in the ruleset's required_status_checks to build-and-test, or add name: build to the job in the workflow. Workflow names such as CI are not used as check names.
Exercise
You want a stricter review process where a person who pushed commits to a pull request cannot be the one whose approval satisfies the rule, and any approval must be discarded when new commits arrive. Which two pull_request parameters do you set, and to what?
Show answer
Set require_last_push_approval to true and dismiss_stale_reviews_on_push to true.