Build analytics using APIs

Hi,

We are currently using SonarQube version 6.7. We have a need to build analytics data store to report metrics out of Sonar especially how the projects have performed in the past scans etc. I have built the following API to get the project and its metics
api/measures/component?format=json&component=pb&metricKeys=ncloc,violations,new_vulnerabilities,critical_violations,major_violations,blocker_violations,tests,test_success_density,test_errors,test_failures,coverage,line_coverage,sqale_index,alert_status,quality_gate_details&includealerts=true

How do I change the API above to:

  • Get metrics information for all projects. I tried putting a comma but it did not work.

  • Add language and project scan date i,e, I need to report on multiple scans for a given project?

Thanks,
Vijay

Hi Vijay,

You can’t. You’ll need to iterate over the projects of interest.

For that you want api/measures/search_history. For language, you’ll want to request the value of ncloc_language_distribution. Note that this is a DATA format metric, so it returns something like this:

"value": "css=12191;java=244959;js=2150;ts=102716"

and you’ll need to untangle it after retrieval.

 
HTH,
Ann

Hi Ann,

Thank you for the response.

The API, api/measures/search_history would not return the project name attribute. How do I differentiate the projects in the result set when I iterate the projects I need. Please clarify.

Thanks,
Vijay

Hi Vijay,

If you’re iterating over the projects of interest, then you should already know which project the current request is for…

Explicitly, if you’re interested in projects A, B, and C, you already know that querying for &component=A,B,C doesn’t work. Instead, you need to do something like (pseudo code):

for (project in A,B,C) {
  api/measures/component?component=**${project}**&...

 
Ann