Retrieving project attributes using SonarQube Web Api

Hi there,

I am currently working on a project that requires the retrieval of several attributes (reliability rating, maintainability rating, quality gate) associated with a project.

However, I have not found a way in the Web Api to do this efficiently since api/components/search_projects does not provide any of the above attributes. Currently, I am looping over every single project (with its component key) using the api/measures/component endpoint to obtain metric data and the api/qualitygates/get_by_project endpoint to obtain quality gate data. This method is very inefficient and time consuming since our company has a thousand over projects and I am making a few thousand API calls just to obtain these data.

I’m wondering if there is an endpoint I could use to obtain my required data with a single API call. Thanks.

Hello @yuheng222,

There is just one slight “mistake” in the endpoints you have identified to achieve your use case. api/components/search_projects is not the right API. It’s an internal (not officially supported) API, and my guess is that it will be deprecated soon.
api/projects/search is a better replacement of if, for 2 reasons:

  • It’s a public (supported) API
  • It has an extra field that should allow you a nice optimization (hopefully). This field is lastAnalysisDate. By looking at this field you can verify if the project was changed since you last ran your script, and if no change happened (ie the project was not re-analyzed) you don’t need to invoke api/measures/components and api/qualitygates/get_by_project on the projects

If you want to further optimize the number of API calls, there’s a more sophisticated solution: Web hooks. Every time a project is analyzed and has its quality gate computed, you can configure SonarQube to fire a web hook to any external software (your script), that would collect the “attributes” for that project only. So you run API calls only for what is needed, when need, and your external attributes “database” is always up to date.
See https://docs.sonarqube.org/latest/project-administration/webhooks/ if that’s of interest to you.

Olivier

2 Likes

Hi Olivier,

Thanks for your prompt reply! :slightly_smiling_face:

I will definitely use the api/projects/search endpoint then once I have administrative rights. Will be trying out the Web Hook solution too.

Cheers!

Xavier