Skip to content
Tutorials Center
My learning

Topics

Lesson 19 of 37 ยท Scanning and Integrations

Pull Request Analysis and IDEs

Bring SonarQube feedback to reviewers and developers: DevOps platform integrations, pull request decoration and status checks, and SonarQube for IDE in connected mode.

A quality report only helps if people see it at the right moment. Two integrations put results where work happens: pull request analysis, which comments on a change while it is reviewed, and the IDE plugin, which flags problems while you type. Together they implement shift-left quality: the earlier a problem is found, the cheaper it is to fix.

Pull request analysis with decoration needs Developer Edition or above on SonarQube Server, or SonarQube Cloud. The Community Build analyzes the main branch only. Menu names below are from the real product and may differ slightly between versions; the code here is a stand-in.

Connecting your DevOps platform

SonarQube integrates with GitHub, GitLab, Bitbucket (Cloud and Server) and Azure DevOps. The setup has two halves:

  1. Server side (Administration > Configuration > DevOps Platform Integrations): create a binding to the platform. For GitHub you register a GitHub App and give the server its ID, private key and the instance URL; for GitLab or Bitbucket you provide a personal access token or app credentials; for Azure DevOps a token. The server uses it to read repository information and to post results.
  2. Project side: bind a project to a repository (Project Settings > General Settings > DevOps Platform Integration) so SonarQube knows which repository's pull requests belong to it. Importing a project from the platform does this for you.

Developers can also log into SonarQube with their platform account (single sign-on), which makes permission management easier.

What happens on a pull request

With the binding in place, the pipeline runs a scan for each pull request:

  1. CI starts on the pull request event.
  2. The scanner is called with the pull request properties (many CI templates and the platform-aware scanners detect them automatically): the request key, source branch and target branch.
  3. The server analyzes the change, compares it with the target branch and evaluates the quality gate against the new code of the request.
  4. It posts back to the platform: a status check (success or failure, which you can make required for merging) and a decoration: a summary comment or inline comments on the changed lines with the new issues, coverage on new code and duplication.
sequenceDiagram
  participant Dev as Developer
  participant Host as Code host
  participant CI as CI job
  participant SQ as SonarQube
  Dev->>Host: open pull request
  Host->>CI: trigger pipeline
  CI->>SQ: scan with PR key + branches
  SQ->>SQ: compare with target, evaluate gate
  SQ->>Host: status check + decoration
  Host->>Dev: shows Passed or Failed

The merge button can be tied to the status check, so a failing gate blocks the merge.

In GitHub, mark the SonarQube check as required in the branch protection rules; in GitLab use merge checks and an approval rule; in Azure DevOps use branch policies. Without that, the check is only a hint.

Formatting the decoration

The stand-in below builds a short pull request summary from a gate result, like the comment reviewers see. It shows how new-code measures and a verdict combine into a message, and why a short message helps.

Python Format a PR summary comment (stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Choosing which lines to comment on

A decoration comments only on lines the pull request changed. Existing problems in untouched lines stay out of the review, which is Clean as You Code in action. The next stand-in filters a list of issues by the changed line ranges of a diff.

Python Keep only issues on changed lines (stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

SonarQube for IDE (formerly SonarLint)

SonarQube for IDE is a free extension for VS Code, the JetBrains IDEs, Visual Studio and Eclipse. It analyzes the file you are editing as you type and underlines issues with the rule explanation, with no server needed. It is the fastest feedback loop: a problem is flagged seconds after it is written.

In connected mode the extension links to your SonarQube Server or Cloud project:

  • It uses the project's quality profile, so the IDE and the server agree on what is a problem.
  • It shows issues already on the server and respects the status set there (accepted and false positives are hidden).
  • In recent versions you can see new code issues only, and review security hotspots from the IDE.
  • Some features can suggest an automatic fix for supported rules.

To bind, install the extension, add a connection (server URL and a user token), and select your project. Keep the connection token private, because it is a user token with your permissions.

A good routine

  1. Fix IDE warnings as you code.
  2. Open the pull request; CI scans it.
  3. Read the decoration and fix remaining issues before asking for review.
  4. Merge only when the gate is green; the main branch scan then updates the dashboard.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does a pull request analysis compare?

    The server compares the request with its target branch and applies the gate to the new code.
  2. 2. How can a failed SonarQube gate block merging?

    A required status check on the code host prevents the merge until the gate passes.
  3. 3. What does connected mode in the IDE add?

    The IDE then applies the same rules as your project and shows server-side decisions.
  4. 4. Which edition feature is pull request decoration?

    Branch and pull request analysis with decoration starts with Developer Edition and is part of the cloud service.

Summary

  • Bind SonarQube to GitHub, GitLab, Bitbucket or Azure DevOps to analyze and decorate pull requests.
  • The server compares the request with the target branch and posts a status check and comments.
  • Make the status check required so a failed gate blocks the merge.
  • SonarQube for IDE gives instant feedback, and connected mode aligns it with the server profile.
  • Fix in the IDE first, then in the pull request, and merge on green.

Next: Security Hotspots vs Vulnerabilities in SonarQube