Sonar Importer
The Sonar importer fetches the metrics of a project from a SonarQube server and writes them as a cc.json map. You get every metric SonarQube computes, for every language it analyses, such as complexity, coverage, duplications and code smells, on every file. Use it when your project already runs through SonarQube, or when you need a metric only SonarQube provides.
Before you start
Section titled “Before you start”You need a SonarQube server that has analysed your project, and a user token.
-
Start a server, for example locally with Docker:
Terminal window docker run -d --name sonarqube -e SONAR_ES_BOOTSTRAP_CHECKS_DISABLE=true -p 9000:9000 sonarqube:latestOpen
http://localhost:9000and log in withadmin/admin. To install SonarQube from the zip file instead, follow the SonarQube documentation. -
Create a project in SonarQube and analyse your code with the SonarScanner as SonarQube’s instructions describe. You need the project key from this step.
-
Create a user token under your account’s security settings. Its value starts with
squ_. Project and global analysis tokens can fail with an “Insufficient privileges” error, so the importer warns when the token does not look like a user token.
Quick start
Section titled “Quick start”ccsh sonarimport http://localhost:9000 my-project-key -u squ_12345 -o sonar.cc.jsonURL and PROJECT_ID are the sonar.host.url and sonar.projectKey your scanner uses.
Options
Section titled “Options”| Option | Description |
|---|---|
URL | URL of the SonarQube server |
PROJECT_ID | key of the SonarQube project |
-u, --user-token=<userToken> | user token to connect to the server |
-m, --metrics=<metrics> | comma-separated list of SonarQube metric keys to import; all metrics when omitted |
--merge-modules | merge the modules of a multi-module project into one folder structure |
-o, --output-file=<outputFile> | output file; prints to stdout when omitted |
-nc, --not-compressed | write plain JSON instead of a gzip file |
-h, --help | show the help and exit |
Usage: ccsh sonarimport [-h] [--merge-modules] [-nc] [-o=<outputFile>] [-u=<userToken>] [-m=<metrics>]... URL PROJECT_IDIn PowerShell, write the --metrics list without spaces or put it in quotes.
Import only a few metrics, from a multi-module project:
ccsh sonarimport https://sonar.example.com de.example:shop -u squ_12345 -m ncloc,complexity,coverage --merge-modules -o shop.cc.jsonMetrics
Section titled “Metrics”The importer takes every metric SonarQube reports for a file. See the
SonarQube metric definitions
for the full list. Most metrics keep their SonarQube key with the prefix sonar_, for example sonar_complexity or
sonar_code_smells. These metrics get the CodeCharta name instead:
| SonarQube key | CodeCharta metric |
|---|---|
lines | loc |
ncloc | rloc |
comment_lines | comment_lines |
commented_out_code_lines | commented_out_loc |
function_complexity | average_function_mcc |
functions | functions |
statements | statements |
accessors | accessors |
public_api | public_api |
line_coverage | line_coverage |
branch_coverage | branch_coverage |
--metrics takes the SonarQube keys, not the CodeCharta names. Each file links to its code page in SonarQube.
Piped input
Section titled “Piped input”A project piped into ccsh sonarimport is merged with the SonarQube metrics:
ccsh unifiedparser . | ccsh sonarimport http://localhost:9000 my-project-key -u squ_12345 -o combined.cc.jsonThe merged map has an empty project name.
Related
Section titled “Related”- Analyze a project with SonarQube: the full walkthrough from server to map
- Merge Filter: combine SonarQube metrics with other maps
- Structure Modifier: rename an
mccmetric tosonar_complexity