Sub topic 10
Protecting main and reviewing changes
This chapter covers the rules that keep main trustworthy and the review habits that catch Salesforce-specific problems. It assumes you have read the previous chapter on check-only deployments and that you can run the pull request validation workflow shown there. It also assumes you have admin access to a GitHub repository. The examples use GitHub. GitLab, Bitbucket, and Azure DevOps offer the same ideas under different names.
Why main needs protection
In this flow, main is the source of truth. Every promotion to UAT and production starts from it, and every hotfix branches from it. If someone pushes a broken Apex class straight to main, the next release inherits the problem. If someone force-pushes, history that your release tags point to can disappear.
Salesforce adds a second reason. An org will accept a deployment that is technically valid but harmful, such as a profile change that removes access for a whole department. Git cannot detect that kind of mistake. A reviewer who knows the org can.
Branch protection therefore does two jobs:
- It makes the automated checks mandatory, so no one can skip validation.
- It makes human review mandatory, so no change reaches
mainunseen.
The gates a change passes
A protected main turns a merge into a sequence of gates. A pull request cannot merge until each one passes.
What to protect
Apply these rules to main. Each one closes a specific gap.
| Rule | What it prevents |
|---|---|
| Require a pull request before merging | Direct pushes that skip validation and review |
| Require status checks to pass | Merging a change whose check-only deployment failed |
| Require branches to be up to date | Merging a change that was validated against an old copy of `main` |
| Require approving reviews | A change merging with no second pair of eyes |
| Dismiss stale approvals on new commits | Approving a small change, then pushing a large one |
| Require code owner review | Sensitive metadata changing without the right person approving |
| Block force pushes and deletions | Rewriting or removing the history that release tags point to |
| Require linear history | Merge commits that make `git bisect` and reverts harder |
| Apply rules to administrators | The people with the most power skipping the rules |
The "up to date" rule matters more for Salesforce than for most stacks. A check-only deployment proves that your branch deploys on its own. It does not prove that your branch deploys after another pull request has merged. Two branches can each pass and still conflict at the metadata level, for example when both add a field to the same page layout. Requiring an up-to-date branch forces the check to re-run against the current main. The cost is that busy teams re-run validation often. If that becomes a bottleneck, a merge queue is the usual answer, because it tests each pull request on top of the ones queued ahead of it.
Configuring the rules
You can set these rules in the repository settings, but a script is easier to review and repeat. This one uses the GitHub CLI and the REST API. It assumes the validation job from the previous chapter is named validate, because the job name is what GitHub treats as the status check name.
cat > protection.json <<'EOF'
{
"required_status_checks": {
"strict": true,
"contexts": ["validate"]
},
"enforce_admins": true,
"required_pull_request_reviews": {
"required_approving_review_count": 1,
"dismiss_stale_reviews": true,
"require_code_owner_reviews": true
},
"restrictions": null,
"required_linear_history": true,
"allow_force_pushes": false,
"allow_deletions": false,
"required_conversation_resolution": true
}
EOF
gh api --method PUT repos/OWNER/REPO/branches/main/protection --input protection.jsonReplace OWNER/REPO with your repository. strict: true is the "require branches to be up to date" setting. restrictions: null means any user who passes the other rules may merge, which suits most teams. The API requires all four top-level fields (required_status_checks, enforce_admins, required_pull_request_reviews, and restrictions), even when you set one to null.
Newer GitHub repositories can use rulesets instead of classic branch protection. Rulesets express the same rules and add features such as organization-wide scope. The choices in the table apply either way.
Code owners for sensitive metadata
A single approval from anyone is a weak control on a project where some files carry far more risk than others. A CODEOWNERS file assigns paths to people or teams. With "require code owner review" on, a pull request that touches a path needs approval from its owner.
# .github/CODEOWNERS
# Default reviewers for everything
* @acme/salesforce-developers
# Access and security metadata
/force-app/main/default/profiles/ @acme/salesforce-security
/force-app/main/default/permissionsets/ @acme/salesforce-security
/force-app/main/default/sharingRules/ @acme/salesforce-security
# Automation that can touch many records
/force-app/main/default/flows/ @acme/salesforce-architects
/force-app/main/default/triggers/ @acme/salesforce-architects
# Pipeline and project configuration
/.github/ @acme/platform-leads
/sfdx-project.json @acme/platform-leadsWhen several patterns match a file, the last matching line wins. Keep the broad * rule first and the specific rules after it. Use teams, not individuals, so a holiday does not block a release. Make sure each team has at least two members with write access, or the rule will make some pull requests impossible to merge.
Protect the pipeline files too. If anyone can edit .github/workflows/, they can remove the validation step in the same pull request that needs it. Listing /.github/ in CODEOWNERS means a change to the checks needs approval from the people who own them.
What reviewers should look for
Reviewing Salesforce metadata differs from reviewing application code. A reviewer cannot rely on the diff alone, because XML diffs are noisy and some risks are not visible in them.
Check these points on each pull request:
- Scope. Does the diff contain only what the description says? Retrieved metadata often carries unrelated changes, such as reordered elements or other people's field updates.
- Access. Do profile or permission set changes grant more than the feature needs? Look for
modifyAllData,viewAllData, and broad object permissions. - Deletions. Is a file or component deleted? Deleting a field in production removes its data. Confirm that a destructive change was intended and that someone has checked dependencies.
- Tests. Do new Apex classes have tests that assert behavior, not only enough lines to meet coverage?
- Flows. Does a new record-triggered flow run on an object that already has triggers? Check the order of execution and the entry conditions.
- Hard-coded values. Look for record IDs, usernames, and URLs that differ between orgs. They work in the scratch org and fail in UAT.
- Pipeline files. Does the pull request touch
sfdx-project.jsonor workflow files? That deserves extra attention.
Reviewers should also read the validation output. A green check means the deployment and tests passed. It says nothing about whether the change does what the ticket asked for.
Making reviews fast enough to keep
Protection rules only work if people do not resent them. A few habits keep the process light:
- Keep pull requests small. A change under a few hundred lines of real code gets read. A change of several thousand lines gets approved without being read.
- Write a description that says what changed, why, and how you tested it. Link the work item.
- Set a team expectation for review time, such as one working day.
- Use squash merges so each pull request becomes one commit on
main. That gives release tags clean boundaries and makes a revert a single operation. - Delete the branch after merging. The branch naming chapter covers lifecycles, and most platforms can delete merged branches automatically.
Common errors
- The required check never appears. GitHub matches the name you configured to the job name. If you rename the job, the old name stays required, and every pull request waits forever. Update the rule when you rename the job.
- The check is required but never runs. If the workflow has path filters and a pull request touches none of those paths, the check does not run and cannot pass. Either remove the filters or add a job that always reports a result.
- Administrators merge around the rules. With
enforce_adminsoff, an admin can bypass the rules on any pull request. Keep it on, and give a small group a documented emergency path instead. - A hotfix feels blocked by the rules. It should not be. A hotfix goes through the same gates. If validation is too slow for emergencies, fix the validation speed. Do not weaken the protection.
- Review is a formality. If every pull request gets approved within minutes with no comments, the rule is satisfied and the control is gone. Track review comments or approval times occasionally to see whether people are reading.
Exercise
Your team renames the validation job in the workflow from validate to check-only-deploy. Afterward, every pull request shows "Expected — Waiting for status to be reported" and cannot merge, even though the new job passes. What is wrong, and what should you do?
Show answer
The branch protection rule still lists validate as the required status check. No job reports that name any more, so the requirement never completes. Update the rule to require check-only-deploy (or rename the job back). When you rename jobs in the future, change the protection rule in the same piece of work.
Exercise
A pull request changes only force-app/main/default/permissionsets/Sales_Manager.permissionset-meta.xml. The check-only deployment passes and one developer approves it. The repository has the CODEOWNERS file shown in this chapter with "require code owner review" enabled. Can the pull request merge? Why?
Show answer
No. The path /force-app/main/default/permissionsets/ belongs to @acme/salesforce-security, and the last matching rule wins over the * rule. Code owner review is required, so someone from the security team must approve. A developer who is not on that team does not satisfy the rule, even if the required approval count is met.