Python SonarQube API Wrapper

2.0.5 · active · verified Thu Apr 16

python-sonarqube-api is a Python wrapper for the SonarQube and SonarCloud API, providing a convenient interface to interact with SonarQube Community, Enterprise, and SonarCloud instances. It supports Python 2.7 and 3.3+ (though modern usage typically implies Python 3.6+). The library is actively maintained, with version 2.0.5 currently available, facilitating integration of SonarQube's static analysis capabilities into Python applications and CI/CD pipelines.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to connect to a SonarQube instance using a token and retrieve a list of projects. It uses environment variables for sensitive connection details. For SonarCloud, `SonarCloudClient` should be used with `sonarcloud_url` instead of `sonarqube_url`.

import os
from sonarqube import SonarQubeClient

# Configure connection details using environment variables for security
SONARQUBE_URL = os.environ.get('SONARQUBE_URL', 'http://localhost:9000')
SONARQUBE_TOKEN = os.environ.get('SONARQUBE_TOKEN', 'YOUR_SONARQUBE_TOKEN') # Or use user/password

# Initialize the SonarQube client
try:
    client = SonarQubeClient(sonarqube_url=SONARQUBE_URL, token=SONARQUBE_TOKEN)

    # Example: Fetch all projects
    print(f"Connected to SonarQube at {SONARQUBE_URL}")
    print("Fetching projects...")
    projects = list(client.projects.search_projects())

    if projects:
        print(f"Found {len(projects)} projects:")
        for project in projects[:3]: # Print first 3 projects
            print(f"  - {project.get('name')} (Key: {project.get('key')})")
    else:
        print("No projects found or accessible.")

except Exception as e:
    print(f"An error occurred: {e}")
    print("Please ensure SonarQube is running and accessible, and your token/credentials are correct.")
    print("For SonarCloud, use SonarCloudClient and 'sonarcloud_url' parameter.")

view raw JSON →