A class library and CLI to ease interaction with SonarQube API.
Although this project was written from scratch, it was made possible by understanding the previous work of https://github.com/kako-nawao/python-sonarqube-api
This script library uses Poetry to set up Python and the required libraries.
Instructions to install Poetry here:
https://python-poetry.org/docs/
To help development it is useful to run SonarQube as a docker instance.
Use this command:
docker run -d --name sonarqube -p 9000:9000 sonarqube
SonarQube API docs are not so easy to find: They are available from a running instance of SonarQube.
If you are running sonarqube on localhost try this link:
http://localhost:9000/web_api/
If you have access to sonarcloud, try this:
Access to SonarQube API will require authentication by default. You should use a Personal Access Token generated from the SonarQube UI
You can run a CLI that has been designed to support rudimentary management of a project within SonarQube.
The CLI relies on a configuration file to define the project key, name and which quality profiles and gate to apply.
---
project:
key: sonarqube-py
name: sonarqube-py
quality-gate: "Sonar way"
quality-profiles:
py: "Sonar way"
css: "Sonar way"
You can configure behaviour using environment variables:
env-var | meaning | default |
---|---|---|
SONAR_URL | SonarQube URL | http://localhost:9000 |
SONAR_HOST | SonarQube host | http://localhost |
SONAR_PORT | SonarQube port | 9000 |
SONAR_TOKEN | SonarQube personal access token |
You can also configure behaviour using command line arguments. Command line arguments will override environment variables.
flag | meaning | default |
---|---|---|
-c | Configuration file | .sonarqube-ci.yml |
-s | Suffix to add to key and name when managing SonarQube projects | |
-u | SonarQube url | see environment variables |
-h | SonarQube host | see environment variables |
-p | SonarQube port | see environment variables |
-t | SonarQube personal access token | |
-l | Logging level (ERROR/WARNING/INFO/DEBUG) | INFO |
--help | Show help |
command | meaning |
---|---|
create | create or update a project with specified quality gate and profiles |
delete | delete a project |
Command line args MUST be placed before the command.
e.g.
python -m sonarqube.cli -c my-sonarqube-config.yml create
You can also use the API in code. Import the SonarQube class from the sonarqube.api module:
from sonarqube.api import SonarQube
instantiate the class, and execute any of the supported endpoints:
sq = SonarQube(host=host, port=port, token=token)
sonarqube-py supports the following endpoints:
- get_authentication_validate
- get_projects_search
- get_issues
- get_measures
- get_rule
- get_qualitygates_project_status
- get_qualitygates_get_by_project
- get_qualityprofiles_search
All endpoints support parameters as defined in the sonarqube wep-api documentation. I.e. the python client simply passes through any arguments you provide through to the web service API.
python -m examples.measures
python -m examples.projects