AI coding assistants and agents now write a significant share of code in production. The speed gained by leveraging agents only pays off when you can verify each change is production ready. SonarQube automates AI code verification to realize the productivity gains from agentic development. To verify AI-generated code with SonarQube, follow these seven steps:
- Prepare your project for pull request analysis in the CI pipeline.
- Configure a quality profile and a quality gate built for agent-generated code.
- Analyze the AI-generated change in your CI pipeline.
- Review the findings on the pull request.
- Fix the issues in the code.
- Reanalyze to confirm the fixes pass the quality gate.
- Enforce the quality gate as a required check before merging.
One boundary applies to everything below. A passing quality gate means the change meets the conditions you define. SonarQube is the independent verification layer in your pipeline that automates checks and works best alongside human review and functional testing.
1. Prepare your project for analysis
Confirm these prerequisites so your first AI-generated pull request gets analyzed without delays.
Repository and DevOps platform. Determine the projects you want to verify on GitHub, GitLab, Bitbucket, or Azure DevOps, and bind the DevOps platforms you use with SonarQube Cloud or SonarQube Server. Integrating directly lets SonarQube post verification results directly in pull request and branch comments.
Supported language. SonarQube analyzes more than 45 languages. This walkthrough uses Python. If you plan to use the agentic AI quality profile described in step 2, confirm your language appears in the profile's supported list in SonarQube.
Deployment and pull request analysis. Pull request analysis and health reporting in comments are available in SonarQube Cloud and in SonarQube Server. If your team runs SonarQube Community Build without branch and pull request analysis, you can analyze the main branch, but you cannot gate the merge of individual pull requests.
Permissions. You need permission to create or administer the project in SonarQube, create an analysis token, add secrets to your CI system, and edit branch protection or rule sets in your repository. In most enterprise organizations, a developer handles the first three and platform engineering owns the last one.
CI pipeline. Your CI pipeline needs to check out the full Git history (shallow clones break new-code detection and blame data), install your test dependencies, and store the SonarQube token as a secret.
Example environment for this guide:
- SonarQube Cloud connected to GitHub
- GitHub Actions in the CI pipeline to automate builds and analysis
- Python 3.12 with pytest and pytest-cov
You do not need to identify AI-written lines first. SonarQube analyzes code the same way regardless if written by a developer or LLM. LLMs generate code that reflects how humans write code, so the same issues that humans create can also be generated by the LLM. This consistency in verification is important: one standard applies to every change, so you never depend on accurate authorship labels to catch a problem.
2. Configure rules and a quality gate
Agent-generated changes arrive faster and in larger PRs than human-written ones. Clear, automated standards keep review effort proportional to risk, instead of proportional to volume. SonarQube separates verification standards into two controls.
Quality profiles versus quality gates
The quality profile defines what SonarQube looks for. The quality gate turns that into findings that guide the decision to merge. You need both: a strong profile without a quality gate produces findings nobody acts on, and a quality gate without a well-chosen profile enforces an incomplete standard.
Sonar way for agentic AI
SonarQube includes a quality gate called Sonar way for agentic AI, designed for most projects where AI assistants and agents produce the code. It focuses on new code, meaning the lines of code added or changed in the current pull request or branch. That focus matches how agent-generated changes enter your codebase and keeps the quality gate actionable. Developers fix what was just introduced, instead of inheriting years of legacy findings.
The quality gate defines the standard used to measure conditions across issues: bugs, maintainability problems, and security vulnerabilities, as well as test coverage and dependency risks for new code. Review the exact conditions and thresholds in your instance under quality gates before you assign the it.
Configure the quality gate with the agentic AI quality profile for each supported language in your project. This profile prioritizes rules for patterns more commonly found in agent-generated code, so the quality gate evaluates the right findings. Assign both under your project's Project Settings.
Dependency-risk checks need SonarQube Advanced Security
AI assistants frequently suggest third-party packages. Vulnerable or poorly licensed dependencies carry real risk to your software supply chain, even when your own code is sound. The dependency-risk condition in Sonar way for agentic AI relies on SonarQube Advanced Security, which adds software composition analysis and SBOM generation.
Without SonarQube Advanced Security, SonarQube does not perform the dependency-risk verification. The rest of the quality gate still evaluates normally. A skipped condition is not a passed condition, so use SonarQube Advanced Security to ensure your dependencies are fully covered.
3. Analyze an AI-generated change
Practical verification starts with a real change. The example below shows a small Flask endpoint an AI assistant might generate when asked to "add an endpoint that looks up a user by username."
The AI-generated code
# app/users.py
import sqlite3
from flask import Flask, jsonify, request
app = Flask(__name__)
DB_PATH = "app.db"
@app.route("/users")
def get_user():
username = request.args.get("username", "")
conn = sqlite3.connect(DB_PATH)
query = f"SELECT id, username, email FROM users WHERE username = '{username}'"
row = conn.execute(query).fetchone()
conn.close()
if row is None:
return jsonify({"error": "not found"}), 404
return jsonify({"id": row[0], "username": row[1], "email": row[2]})The code runs, returns the right user for normal input, and passes a quick visual review. It also builds a SQL statement directly from a query parameter, which opens the endpoint to SQL injection.
A test for the endpoint
# tests/test_users.py
import sqlite3
import pytest
from app import users
@pytest.fixture
def client(tmp_path, monkeypatch):
db = tmp_path / "test.db"
conn = sqlite3.connect(db)
conn.execute("CREATE TABLE users (id INTEGER, username TEXT, email TEXT)")
conn.execute("INSERT INTO users VALUES (1, 'ada', 'ada@example.com')")
conn.commit()
conn.close()
monkeypatch.setattr(users, "DB_PATH", str(db))
return users.app.test_client()
def test_get_user_found(client):
response = client.get("/users?username=ada")
assert response.status_code == 200
assert response.get_json()["email"] == "ada@example.com"
def test_get_user_not_found(client):
response = client.get("/users?username=nobody")
assert response.status_code == 404Both tests pass against the vulnerable code. That gap is exactly why static checks and tests work together: functional tests confirm expected behavior, while code security analysis catches what tests never exercise.
CI configuration
Add a sonar-project.properties file at the repository root:
Sonar.projectKey=my-org_ai-verification-demo
Sonar.organization=my-org
Sonar.sources=app
Sonar.tests=tests
Sonar.python.version=3.12
Sonar.python.coverage.reportPaths=coverage.xmlThen add a GitHub Actions workflow at .GitHub/workflows/sonarqube.yml:
name: Build and analyze
on:
push:
branches: [main]
pull_request:
types: [opened, synchronize, reopened]
jobs:
analyze:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0 # Full history for accurate new-code detection
- uses: actions/setup-python@v5
with:
python-version: "3.12"
- name: Install dependencies
run: pip install flask pytest pytest-cov
- name: Run tests with coverage
run: pytest --cov=app --cov-report=xml
- name: SonarQube scan
uses: SonarSource/sonarqube-scan-action@v5
env:
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}For SonarQube Server, remove Sonar.organization and add a SONAR_HOST_URL secret pointing to your instance.
SonarQube shows code test coverage
Pytest runs the tests. Pytest-cov produces coverage.xml. SonarQube imports that report and evaluates coverage on new code against your quality gate. If the coverage report is missing or the path is wrong, SonarQube shows 0% coverage, and a coverage condition fails. Always generate test and coverage reports in a CI step that runs before the scan.
Where results appear
Open a pull request with the change. After the workflow finishes, results appear in three places:
- The pull request comments, where SonarQube posts a summary comment with the quality gate status and counts of new issues: bugs, code smells, security vulnerabilities, coverage, and duplication.
- The pull request checks list, where SonarQube reports the quality gate as a passed or failed check.
- The SonarQube project, under the pull request's analysis, where you can open each finding with full context.
4. Review findings, fix issues, and rescan
Finding an issue is only half the job. The remediation loop (review, fix, rescan) turns a finding into verified production-ready code. Running automated verification inside the pull request keeps the fix cost low, because the context is still fresh and can be resolved quickly before the code is merged.
The finding
For the example above, SonarQube reports a new security vulnerability under the rule for database queries vulnerable to injection attacks. The quality gate fails because the change introduces a new issue on new code.
The finding highlights the conn.execute(query) line and traces the data flow: user input enters through request.args.get("username"), flows into the f-string, and reaches the SQL execution call without sanitization. That trace matters. It shows you the source, the path, and the sink, so you confirm the risk in seconds instead of reconstructing it by hand.
The impact
An attacker can send username=' OR '1'='1 and receive data the endpoint never meant to expose. Against a database with write access, crafted input can modify or delete records. One generated line puts customer data and your incident response budget at risk.
The fix: parameterized queries
Parameterized queries keep data and SQL separate. The database driver sends the value as a bound parameter, so user input can never change the structure of the statement.
@app.route("/users")
def get_user():
username = request.args.get("username", "")
conn = sqlite3.connect(DB_PATH)
row = conn.execute(
"SELECT id, username, email FROM users WHERE username = ?",
(username,),
).fetchone()
conn.close()
if row is None:
return jsonify({"error": "not found"}), 404
return jsonify({"id": row[0], "username": row[1], "email": row[2]})
Add a test that proves injection input no longer returns data:
def test_get_user_rejects_injection(client):
response = client.get("/users?username=' OR '1'='1")
assert response.status_code == 404The placeholder syntax varies by driver. SQLite uses ?, while psycopg uses %s. In every case, pass values as a separate argument and never format them into the query string. If you use an ORM such as SQLAlchemy, its query builder parameterizes values for you.
Security vulnerabilities
SonarQube reports security findings.
- Vulnerabilities are confirmed security problems that can be exploited and need a code fix. The SQL injection above is a vulnerability.
Re-analyze the changed code
Committing the fix to the same branch triggers the workflow to run again: pytest executes all three tests, pytest-cov regenerates coverage, and SonarQube reanalyzes the pull request. The vulnerability moves to Closed, the summary comment updates, and the quality gate check changes from Failed to Passed. Once passed, the code is ready to merge.
When an AI assistant or agent generates the fix, the same rule applies. The fix goes through the same scan as the original change. Never accept a fix because the model says it works; accept it because the analysis and tests confirm it.
5. Enforce checks before merging
Visibility without enforcement leaves the merge decision to whoever clicks the button. Required checks make the quality gate part of your release process, so every code change, human or agent-generated, meets the same bar before it reaches your main branch.
Status reporting does not block merging on its own
SonarQube reports the quality gate status to your DevOps platform. By default, GitHub still lets anyone with write access merge a pull request with a failed check. You need to make the check required.
Require passing the quality gate in GitHub
- Run at least one analysis on a pull request so GitHub registers the SonarQube check name.
- In your repository, go to Settings > Rules > Rulesets (or Settings > Branches for classic branch protection).
- Create or edit a rule that targets your default branch.
- Enable Require status checks to pass.
- Search for and add the SonarQube check, as it appears in your pull request's checks list.
- Save the rule. Consider also requiring pull request reviews and blocking force pushes on the same branch.
GitLab, Bitbucket, and Azure DevOps offer equivalent controls through merge checks, merge request approvals, or branch policies.
Optional: fail the CI job on a failed quality gate
Add Sonar.qualitygate.wait=true to your properties file to make the scan step wait for the quality gate result and fail the CI job when it fails. This surfaces the failure in the workflow itself, which helps when other jobs depend on the analysis. Keep the required status check as the merge control; the CI job failure complements it.
The enforcement loop in practice
For the example change, enforcement plays out in three states:
- Failure. The first push introduces the SQL injection. The quality gate fails, the required check turns red, and GitHub disables the merge button.
- Remediation. The developer replaces the f-string with a parameterized query and adds the injection test.
- Success. The rescan closes the vulnerability, the quality gate passes, and the pull request becomes mergeable once other required reviews complete.
That loop is what lets your engineering org scale AI-assisted development without scaling risk. Developers get fast, specific feedback. Leaders get a consistent, auditable standard applied to every merge.
6. Complete verification beyond the quality gate
A passing quality gate answers one question: does this code change meet the conditions you configured? Full confidence requires more layers. Verification works best when it is multilayered, consistent, and transparent, with each layer catching what the other layers miss.
Keep human review. Reviewers judge intent, design, and business logic. SonarQube confirms the code is free of coding issues. It cannot confirm the intent of the code change, that the endpoint should exist, that it returns the right fields for your privacy policy, or that it matches the ticket.
Run functional and integration tests. Coverage measures which lines your tests execute, not whether those tests assert the right behavior. A test that calls a function without checking the result adds coverage and proves nothing. Write assertions that reflect real requirements, and run integration tests against realistic data.
Match security checks to risk. Code security analysis and dependency scanning form a strong baseline. Changes that touch authentication, payments, or regulated data warrant threat modeling, secrets scanning, dynamic testing, or penetration testing, depending on your risk posture.
Remember the scope of new code. Sonar way for agentic AI evaluates new code. A passing quality gate on a pull request says nothing about issues in code written before your new-code period began. Track overall code health separately and reduce legacy issues over time, starting with the files agents modify most often.
Never let an LLM be the only check. AI review tools add useful context, but a model reviewing its own output, or output from a similar model, shares its blind spots. SonarQube combines deterministic and agentic analysis, so results stay explainable, consistent, and repeatable from one run to the next. Keep that independent layer in place no matter which assistant or agent writes the code.
