Skip to content

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.

You need a SonarQube server that has analysed your project, and a user token.

  1. 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:latest

    Open http://localhost:9000 and log in with admin / admin. To install SonarQube from the zip file instead, follow the SonarQube documentation.

  2. 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.

  3. 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.

    SonarQube security settings with the form to generate a user token

Terminal window
ccsh sonarimport http://localhost:9000 my-project-key -u squ_12345 -o sonar.cc.json

URL and PROJECT_ID are the sonar.host.url and sonar.projectKey your scanner uses.

OptionDescription
URLURL of the SonarQube server
PROJECT_IDkey 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-modulesmerge the modules of a multi-module project into one folder structure
-o, --output-file=<outputFile>output file; prints to stdout when omitted
-nc, --not-compressedwrite plain JSON instead of a gzip file
-h, --helpshow the help and exit
Usage: ccsh sonarimport [-h] [--merge-modules] [-nc] [-o=<outputFile>]
[-u=<userToken>] [-m=<metrics>]... URL PROJECT_ID

In PowerShell, write the --metrics list without spaces or put it in quotes.

Import only a few metrics, from a multi-module project:

Terminal window
ccsh sonarimport https://sonar.example.com de.example:shop -u squ_12345 -m ncloc,complexity,coverage --merge-modules -o shop.cc.json

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 keyCodeCharta metric
linesloc
nclocrloc
comment_linescomment_lines
commented_out_code_linescommented_out_loc
function_complexityaverage_function_mcc
functionsfunctions
statementsstatements
accessorsaccessors
public_apipublic_api
line_coverageline_coverage
branch_coveragebranch_coverage

--metrics takes the SonarQube keys, not the CodeCharta names. Each file links to its code page in SonarQube.

A project piped into ccsh sonarimport is merged with the SonarQube metrics:

Terminal window
ccsh unifiedparser . | ccsh sonarimport http://localhost:9000 my-project-key -u squ_12345 -o combined.cc.json

The merged map has an empty project name.