Retrieve latest coverage details of particular file with buildstring information

Hi Team,
I am using Sonarqube Community Edition Version 9.8.
I want to retrieve the coverage details of the latest scanned build along with buildstring detail from sonarqube web api.
Using the below API, I’m able to retrieve the required details of a particular file from the latest build. But I could’nt get the buildstring details. I want to know the coverage details belongs to which build.

http://sonarqube/api/measures/component?component=prj-key:path&metricKeys=new_coverage,new_conditions_to_cover,new_uncovered_conditions,new_lines_to_cover,new_uncovered_lines

Is there any way to retrieve the buildstring detail along with the coverage details using sonarqube web api? Kindly suggest.

I’m not sure what you mean by “buildstring” – can you elaborate or give an example?

Hi @Colin

buildstring is a build id which will be passed as an argument ‘sonar.buildString’ to sonar-scanner cli. It is a unique id which is used to differentiate with other builds. It will be displayed in activity page as well.

My requirement is to retrieve the latest build’s coverage details using sonarqube web api. I would like to retrieve the coverage details along with its build id. Kindly suggest.

And one more doubt. If we pass the new_coverage as the parameter in the sonarqube measures/component web api , will it provide latest code coverage details ?

Thanks.

You should be able to retreive the build string for an analysis using GET api/project_analyses/search.

In any case, you’ll find this guide useful:

And one more doubt. If we pass the new_coverage as the parameter in the sonarqube measures/component web api , will it provide latest code coverage details ?

This will provide coverage details specifically for code in the New Code Period. coverage will provide overall coverage.

1 Like

Thanks @Colin for sharing the guide. It’s very useful.

New Code Period is set to ‘previous version’. But in my sonarqube dashboard, the coverage shown under ‘new code’ is different from the coverage which is shown in the graph in today’s latest build. Is new_code not related to latest build? plz clarify.

Take a look at the documentation on Defining New Code – typically, this won’t be only the last commit.

Thanks @Colin

Is there any way to retrieve the below details of the particular analysis id ?

http://sonarqube/api/measures/component?component=prj-key:path&metricKeys=new_coverage,new_conditions_to_cover,new_uncovered_conditions,new_lines_to_cover,new_uncovered_lines

overall code means is it containing the coverage details of all the files/folders of the latest build or it has the data of previous builds as well? plz clarify.

new_ metrics are only available on the latest analysis of a branch (once they’re historical, they aren’t new anymore).

Overall code refers to the entire codebase.