Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SonarQube REST APIs : Read Metrics for individual projects

Tags:

sonarqube

My question:

I am using SonarQube version 7.1 and trying to extract the metrics and quality gate related to individual projects.

What we have tried

We were using Python SonarQube API to extract these data before our company upgraded to version 7.1. "api/resources" web service Deprecated since sonarqube5.4, so we cannot use it anymore.

I have also tried using getting data using CURL command via Web API using curl -i -H "Content-Type: application/json" -H "x-api-key:token" -X GET 'http://MY_HOST/api/measures/component?metricKeys=key&component=project_key'
We are able to get a json payload for individual metrics, but involves tedious task of creating the URL every single time.

But I wanted to know if there is a better/smarter way to access these "measures", be it any language or implementation.

like image 391
Ananya krishna Avatar asked Jul 25 '18 19:07

Ananya krishna


1 Answers

You could do this:

Call the API api/metrics/search first to get a (json) list of all the metrics and then iterate over that list and create a comma separated string of all the metric keys.

For example something like this: ncloc,complexity,violations .. as mentioned in the parameters example value in the API documentation here.

Then you could just add this comma separated list to the url as a parameter something like: http://MY_HOST/api/measures/component?metricKeys=ncloc,complexity,violations&component=project_key

and call it once to get the response for all metrics.

Also, I haven't tried this, but as per the latest documentation, the parameter component is optional. So if you omit that, ideally you should get a response with metrics of all the projects.

like image 109
Sumit Avatar answered Oct 29 '22 17:10

Sumit