Quality Gates as a Merge Gate: Using SonarQube New Code to Protect Forward Progress
Use SonarQube Quality Gates scoped to New Code to turn code quality metrics into an enforced merge decision without blocking delivery on legacy debt.
28 Sept 2025, 09:34 UTC

Pull requests pass unit tests, CI is green, and the code still ships a new blocker bug and a security hotspot that will sit in the main branch for months. The problem is not missing analysis, it is missing an enforced decision point. A SonarQube Quality Gate turns aggregated metrics into a pass/fail signal that can block a merge, and when it is scoped to New Code it lets teams protect forward progress without being held hostage by legacy debt.
The problem with “good enough” merges
Code review catches intent. Tests catch regressions. Static analysis catches patterns that are easy to miss in review. Without a gate, those findings are advisory. Teams see issues in the SonarQube UI after merge and create tickets later, which means remediation cost rises and risk accumulates.
A Quality Gate is a named set of conditions evaluated after analysis. It does not fix code, it decides whether the current analysis is acceptable for the branch or pull request. When branch analysis and pull request decoration are enabled, the gate status appears in the code review tool and can be wired to fail the CI job.
What a gate actually decides
Conditions are metric thresholds, e.g., bugs, vulnerabilities, code smells, coverage, technical debt ratio, and security rating. The gate is evaluated against the scope you choose: overall project or New Code.
New Code is a time or reference based definition, typically recent changes since the last version or since a specific analysis date. Focusing the gate on New Code is the practical engineering decision that makes gates usable on mature codebases. Legacy issues remain visible but do not block delivery, while every change is held to a current standard.
Custom conditions and webhooks let you tailor thresholds per project type and trigger downstream automation, such as notifications or ticket creation, when a gate fails.
New Code focus as the compromise
Applying strict thresholds to the whole project often blocks delivery on legacy systems. Applying them only to New Code reduces friction while protecting forward progress.
Branch analysis runs the scanner on feature branches and compares results to the main branch. Pull request decoration posts findings inline in the review tool and surfaces the gate status. Together they move quality feedback earlier in the workflow.
Worked example: a gate for new changes
Define a gate named Team-Standard-New-Code with conditions evaluated on New Code:
- Coverage on New Code is greater than or equal to 80
- Blocker Issues on New Code is equal to 0
- Security Rating on New Code is A
Bind the gate to a project in Administration > Quality Gates, then set it as the default for the project. In CI, run analysis with the SonarQube scanner on the pull request branch. The scanner is executed on the CI runner with read access to source and test reports and write access to the SonarQube server via a project token. A typical invocation uses placeholders for project key and branch name:
sonar-scanner -Dsonar.projectKey=<project-key> -Dsonar.branch.name=<branch-name>The pipeline should treat a failing gate as a failure. The scanner exit code and the webhook payload can be checked in CI logs when the gate is not passed. In the SonarQube UI, Project > Overview shows the gate status, and the Measures tab shows New Code metrics for comparison against the conditions.
Verification steps:
- Open Administration > Quality Gates and inspect conditions and the New Code definition for the project.
- Run analysis on a test branch with known issues and confirm the gate status changes in Project > Overview.
- Compare the New Code metrics shown in Measures against the gate conditions to validate evaluation logic.
Trade-offs to watch
Thresholds can create perverse incentives. Teams may increase coverage without improving test quality, or suppress issues to pass the gate. Gates should be paired with review norms and regular audits of rule relevance.
Strict gates on legacy code can block delivery if New Code scope is misconfigured. Verify the New Code period definition matches the team’s release cadence.
Analysis time and compute cost grow with project size and history depth, which impacts CI pipeline duration. Consider incremental analysis and selective rule sets for large monorepos.
Quality Gate behavior is edition and version sensitive, especially for branch and pull request features and security rules. Confirm capabilities in your environment before relying on them for merge blocking.
Use gates as a decision aid, not a substitute for judgment. Start with a small set of high-signal conditions on New Code, bind them to critical projects, and iterate thresholds based on observed signal and developer feedback.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.