How would you set up branch protection rules and required status checks for a Node.js repository to prevent broken code from being merged?
Short answer
Branch protection rules (configured on the Git hosting platform, like GitHub) can require that a pull request pass specific CI checks (tests, linting, build) before it's allowed to merge, require at least one approving code review, and prevent direct pushes to the main branch…
Advertisement
AModel answer
Branch protection rules (configured on the Git hosting platform, like GitHub) can require that a pull request pass specific CI checks (tests, linting, build) before it's allowed to merge, require at least one approving code review, and prevent direct pushes to the main branch entirely -- combined with a CI pipeline that runs these checks automatically on every pull request, this ensures broken or unreviewed code can't reach the main branch regardless of who's making the change.
Example
# GitHub branch protection settings (conceptual, configured via the UI or API) required_status_checks: ['test', 'lint', 'build'] required_approving_review_count: 1 enforce_admins: true restrict_pushes: true # no direct pushes to main, only via pull request
Real-world example
A team enables branch protection on their main branch requiring the CI test suite and linter to pass along with at least one code review approval, preventing an incident where a developer had previously pushed directly to main with a change that broke production.
Likely follow-ups
- Should branch protection rules apply equally to repository administrators, or is an exception sometimes justified?
- How do you handle an urgent hotfix that needs to bypass the normal review process during an active incident?
Related topics