Sub topic 3
Requiring pull requests and approvals
This chapter assumes you know Git branches, commits, and pushes, and have opened and merged a GitHub pull request. You also need admin access to a repository you can experiment in.
What the rule does
A pull request requirement stops anyone from pushing commits directly to a protected branch. Changes must arrive through a pull request (PR), and the PR must gather a set number of approving reviews before the merge button works.
In a classic branch protection rule, this is the setting "Require a pull request before merging". In the API it is the required_pull_request_reviews object. In a ruleset, it is a rule of type pull_request.
Common settings
| Setting | Effect |
|---|---|
| Required approving reviews | Number of approvals needed, from 1 to 6. |
| Dismiss stale approvals | New commits pushed to the PR branch remove existing approvals. |
| Require code owner review | Owners listed in CODEOWNERS must approve changes to their files. |
| Require approval of the most recent push | Someone other than the last pusher must approve. |
| Require conversation resolution | All review threads must be resolved before merging. |
Only reviews from people with write access count toward the required number.
Classic rule versus ruleset
The two systems use different names for the same ideas. This ruleset rule requires one approval, dismisses stale approvals, and requires resolved threads:
{
"type": "pull_request",
"parameters": {
"required_approving_review_count": 1,
"dismiss_stale_reviews_on_push": true,
"require_code_owner_review": false,
"require_last_push_approval": false,
"required_review_thread_resolution": true
}
}The classic equivalent uses required_approving_review_count, dismiss_stale_reviews, and require_code_owner_reviews. Note the plural in the last one. Copying a field name between the two payloads is a common cause of rejected requests.
How a change flows
Points to watch
- Author approval does not count. You cannot approve your own PR toward the required total. In a one-person repository, a required review of 1 blocks you unless an administrator bypasses the rule.
- Stale approval dismissal trades speed for safety. It stops a reviewer's approval from covering code they never saw. It also means every fixup push needs a fresh review.
- Required reviews do not run tests. Approvals only cover human review. Use required status checks, covered in a later chapter, to demand a passing build.
- Administrators may skip the rule. Unless the rule applies to administrators, they can merge without approvals. That is covered in the chapter on administrator enforcement.
Exercise
A branch requires 2 approving reviews with "dismiss stale approvals" turned on. A PR has 2 approvals, and the author then pushes one more commit. Can the PR be merged right away? Why or why not?
Show answer
No. Pushing a new commit dismisses the existing approvals, so the count drops to 0. Reviewers must approve the updated PR again until 2 approvals are present.