CI / Rules
Require reviews before a merge
Your branch rule makes the checks pass, but a person can still merge a pull request that nobody read. Add review rules to the branch rule. Then a pull request merges only after people with write access approve it, and code owners approve the files that they own.
4 min read
You need admin access to the repository. The review rules are options of a branch rule.
1. Turn on the review rules
- Open the repository, then Settings, then Branches under Code (
/@<owner>/<repo>/settings/branches). - Click Edit on the rule of
main, or start a new rule under Add a rule. - Under Reviews, set Required approvals. It is a number from 0 to 10.
- Select the other review rules that you want. The table below says what each one does.
- Click Save rule, or Add rule for a new rule.
| Rule | What it does |
|---|---|
| Required approvals | A pull request needs this many approvals. |
| Require a code owner's approval | Each changed file that CODEOWNERS names needs an approval of one of its owners. |
| Dismiss stale approvals | An approval counts only when it was given on the head commit. After a push, the pull request needs new approvals. |
| Require a linear history | Pull requests merge by squash or rebase, never with a merge commit. A direct push that adds a merge commit is refused. |
With the src CLI:
src ci rule set main --require-pull --check ci/test --approvals 2 --code-owners --dismiss-stale --linear-history
Over REST:
curl -X PUT "$TSS_FORGE_URL/repos/acme/app/ci/rules/main" \
-H "Authorization: Bearer $TSS_TOKEN" \
-H "Content-Type: application/json" \
-d '{"requirePull":true,"requiredChecks":["ci/test"],"requiredApprovals":2,"requireCodeOwnerApproval":true,"dismissStaleApprovals":true,"requireLinearHistory":true}'
Over MCP, call the tool set_branch_rule with the same fields.
src ci rule setandPUTreplace the whole rule. An option that you leave out is off, and the approvals are 0.- If more than one rule matches a branch, the highest number of approvals applies. A review rule that any matching rule turns on applies too.
src ci rule listshows the review rules in the columnsAPPROVALS,CODE-OWNERS,DISMISS-STALEandLINEAR-HISTORY.
2. Read the merge box
Open a pull request into the branch. The merge box lists each review rule that blocks the merge:
- Approvals: 1 of 2: the pull request has 1 of the 2 approvals that it needs.
, 1 staleafter it counts the approvals that were given on an older commit. - Changes requested by @carol: a person with write access asked for changes.
- Needs approval from @acme/docs or @ben for docs/setup.md: no code owner of these files approved yet.
While one of them blocks, the merge button is off. The CLI gives the same reasons:
src ci checks 7
May merge: no. This pull request can't merge into main yet. It needs 1 more approval. @carol requested changes.
3. Get the approvals
Ask people with write access to review the pull request. Each one approves on the web, or with src pr review 7 --approve. See Review a pull request.
Done
A pull request into main merges only with the approvals that the rule needs. When it has them and its checks passed, the merge box says Ready to merge.
Which reviews count
- The latest review of each person that approves or requests changes counts. A review that only comments changes nothing.
- Only people with write access count.
- The author of the pull request never counts, also when they approve.
- Agents and apps never approve.
- A request for changes of a person with write access blocks the merge. It stops when that person approves.
Code owners
With Require a code owner's approval, the forge groups the changed files by their owners in CODEOWNERS.
- Each group needs an approval of one of its owners. A team owner counts each member of the team.
- A changed file that no rule of CODEOWNERS owns needs no owner.
- The merge box lists at most 3 files of a group, then
and N more files.
Stale approvals
An approval is stale when it was given on an older commit than the head of the pull request.
- With Dismiss stale approvals, a stale approval does not count. The merge box still shows it, as
, 1 stale. - Without it, a stale approval counts.
- An approval that the forge recorded with no commit counts as stale.
Linear history
With Require a linear history, the branch keeps one line of commits:
- The merge box offers only Squash and merge and Rebase and merge. Merge when ready and the merge queue squash in place of a merge commit, and keep a rebase.
- A merge commit is refused:
This branch takes no merge commits: main keeps a linear history. Merge it with squash or rebase. - A direct push that adds a merge commit is refused, for admins and CI runs too. Git shows the reason:
branch rule "main" requires a linear history: commit abc1234 is a merge commit. Rebase or squash your changes, then push again.
Merge past the review rules
An admin can merge past the rules with Override checks. It skips the review rules too.
In a monorepo, the review rules apply to the pull request of each part, in its own repository.