Sub topic 10
Protecting main and reviewing changes
This chapter assumes you have the pull request validation from the previous chapter in place. It also assumes you use GitHub and have admin rights on the repository. Other Git hosts have similar settings under different names.
Why main needs rules
In this flow, main is the source of truth and every promotion to UAT and production starts from it. A direct push to main skips validation and review. A force push can rewrite the history that your release tags point to. Branch protection makes the host enforce the process, so it does not depend on everyone remembering it.
The rules to turn on
| Rule | What it prevents |
|---|---|
| Require a pull request before merging | Direct pushes to `main` |
| Require approving reviews | Merging your own unreviewed work |
| Require status checks to pass | Merging a change whose check-only deploy failed |
| Require branches to be up to date | Merging a change validated against an older `main` |
| Block force pushes and deletions | Rewriting or losing release history |
| Apply rules to administrators | Admins bypassing the process under pressure |
The "up to date" rule matters more on Salesforce than on many stacks. A check-only deploy proves the change works against the code at the time it ran. If another pull request merged since then, you validated against a state of main that no longer exists.
Setting the rules as code
You can click through the settings, but a script is repeatable and easy to review. Save this as protection.json. The value in contexts must match the job name in your workflow, which is validate in the previous chapter's example.
{
"required_status_checks": { "strict": true, "contexts": ["validate"] },
"enforce_admins": true,
"required_pull_request_reviews": {
"required_approving_review_count": 1,
"require_code_owner_reviews": true,
"dismiss_stale_reviews": true
},
"restrictions": null,
"required_linear_history": true,
"allow_force_pushes": false,
"allow_deletions": false
}Then apply it with the GitHub CLI. Replace OWNER/REPO with your repository.
gh api --method PUT repos/OWNER/REPO/branches/main/protection --input protection.jsonstrict: true is the "up to date" rule. dismiss_stale_reviews removes approvals when new commits arrive, so a reviewer never approves code that later changed.
Routing reviews with CODEOWNERS
A CODEOWNERS file asks the right people to review risky areas. With require_code_owner_reviews on, their approval is mandatory. Put the file at .github/CODEOWNERS. When several patterns match a file, the last one wins, so list general rules first.
* @acme/platform-team
/force-app/main/default/permissionsets/ @acme/security-admins
/force-app/main/default/profiles/ @acme/security-admins
/force-app/main/default/classes/ @acme/apex-developers
/sfdx-project.json @acme/platform-teamWhat reviewers should check
Reviewers see metadata XML, which is hard to read. Direct their attention to these points:
- Permission changes: new object, field, or system permissions in profiles and permission sets.
- Destructive changes: any file listed in a
destructiveChanges.xml. - Test coverage: new Apex has tests that assert behavior, not tests that only raise coverage.
- Hard-coded IDs or URLs that differ between orgs.
- Unrelated files, such as a profile that picked up dozens of reordered lines after a retrieve.
Common errors
- The required check never reports, so the pull request waits forever. The name in
contextsdoes not match the job name. - Admins merge around the rules. Enable
enforce_admins. - Everything is owned by one person in
CODEOWNERS. That person becomes a bottleneck. Assign teams, not individuals.
Exercise
A pull request passed its check-only deploy on Monday. On Tuesday another pull request merged into main. Which protection setting forces the first pull request to be validated again, and why does that matter?
Show answer
The "require branches to be up to date" setting (strict: true in the status check rule). The Monday validation ran against the old main. The merge on Tuesday may conflict in behavior with it, for example by changing the same Apex class or a shared field. Re-running the check validates the combined result before it reaches main.