SonarQube REST APIs : Read Metrics for individual

2020-06-18 09:19发布

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.

标签: sonarqube
1条回答
家丑人穷心不美
2楼-- · 2020-06-18 10:02

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.

查看更多
登录 后发表回答