SonarQube
SonarQube checks source code without running the application. It flags potential bugs, security issues, and duplicated code, and reports whether changes meet a team's quality requirements.
Also known as: Sonar
What SonarQube does
SonarQube, often called Sonar, is a platform for static code analysis. It looks for problems in source code without running the application. For example, an analyzer may find a path where a variable can be null when the code tries to access one of its fields. The project dashboard shows the finding, the rule behind it, and where to investigate. That gives reviewers a starting point; it does not tell them whether the code meets the business requirements.
From scanner to merge check
A CI job typically runs SonarScanner against the code it has checked out. The scanner analyzes the project and submits the results to the SonarQube server. The server processes the report and evaluates the project's Quality Gate. The CI job can be configured to wait for that result. Teams can also make the reported status a required check so that a pull request cannot merge if it fails the gate.
Two settings answer different questions:
- A Quality Profile answers “What should we look for?” It defines the active analysis rules for a language.
- A Quality Gate answers “Are these results acceptable?” It sets conditions on the reported results, such as a minimum level of test coverage on new code.
Example: rolling it out to an existing service
Suppose a Java service has years of code and hundreds of existing findings. Requiring every finding to be fixed before the next merge would stall unrelated work. The team can instead require, say, 80% coverage on new code and prioritize the existing backlog separately. For pull request analysis, new code is determined relative to the target branch.
In this setup, CI runs the tests and generates a JaCoCo coverage report before starting the analysis. SonarQube imports that report; it does not run the tests or measure coverage itself. If tests pass but reported coverage is unexpectedly low, first check that the report was generated, that the scanner found it, and that it corresponds to the sources being analyzed.
What a passing gate does not prove
A green gate means the configured conditions passed. It does not mean the application is bug-free, and high coverage does not mean the tests assert the right outcomes. A test can execute the code that calculates a discount without ever checking the amount. Static analysis, tests, and human review catch different kinds of problems.
Interview question: why did CI pass but merging is blocked?
A scanner step can succeed by submitting its report even before the server has evaluated the gate. The merge policy can still block the pull request if the resulting status is pending or failed. In an interview, explain which step produces the report, which component evaluates it, and which repository setting enforces the result.