Sub topic 6
Requiring pull requests, reviews and status checks
This chapter assumes you know Git branching and pull requests. It also assumes you can read JSON and know what a GitHub Actions status check is. Here you will add the two rules that decide whether a change can merge: pull_request and required_status_checks.
What the two rules control
The pull_request rule blocks direct pushes to the targeted branch. Changes must arrive through a pull request, and the rule's parameters set the review requirements.
The required_status_checks rule lists checks that must report success on the pull request's head commit before it can merge.
Pull request parameters
These are the parameters you will use most.
| Parameter | Effect |
|---|---|
| `required_approving_review_count` | Approvals needed, from 0 to 10. |
| `dismiss_stale_reviews_on_push` | New commits discard earlier approvals. |
| `require_code_owner_review` | An owner named in `CODEOWNERS` must approve. |
| `require_last_push_approval` | Someone other than the last pusher must approve. |
| `required_review_thread_resolution` | All review conversations must be resolved. |
Code owner review does nothing useful without a CODEOWNERS file in the repository. A path with no matching owner adds no requirement.
Status check parameters
Each entry in required_status_checks names a check by its context. The context is the check's reported name. For a GitHub Actions job, that is the job name, not the workflow name. You can also set integration_id to accept the check only from a specific GitHub App. Setting strict_required_status_checks_policy to true requires the branch to be up to date with the base branch before merging.
A combined rule set
This fragment is the rules array of a ruleset. It extends the earlier protect-default-branch example with a status check and linear history.
{
"rules": [
{
"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
}
},
{
"type": "required_status_checks",
"parameters": {
"strict_required_status_checks_policy": true,
"do_not_enforce_on_create": false,
"required_status_checks": [
{ "context": "build" },
{ "context": "unit-tests" }
]
}
},
{ "type": "required_linear_history" }
]
}The required_linear_history rule rejects merge commits, so pull requests must use squash or rebase merging. Check that your teams expect this before you turn it on.
Common errors
- A pull request waits forever on a check. The name in
contextdoes not match any job name, or the workflow never runs for that pull request, for example because of a path filter. GitHub treats a missing check as pending, not as passed. - Approvals vanish after a push. This is
dismiss_stale_reviews_on_pushworking as intended. Turn it off only if your team accepts approving code that has since changed. - An author cannot get their change approved. With
require_last_push_approval, a reviewer who pushed the last commit cannot approve it. A second reviewer is needed. - Many pull requests suddenly show "out of date". The strict policy forces each one to update the branch after another merges. On busy repositories, consider a merge queue.
Applying the change
You can send the full ruleset JSON with the gh api command from the earlier example. Use PUT /orgs/YOUR_ORG/rulesets/RULESET_ID to update an existing ruleset. Start with enforcement set to evaluate if you want to see the effects first. That mode is covered in a later chapter.
Exercise
A required check named CI never appears on a pull request, and the pull request cannot merge. The ruleset lists { "context": "CI" }, and the workflow's job is named build. What is the likely cause, and how do you fix it?
Show answer
The context must match the name the check reports. For GitHub Actions, that is the job name build, not the workflow name. Change the context to build, or rename the job to CI. The check is then reported under the name the ruleset expects.