How to get LoC for all branches via API

Sonarqube version 8.9.6 (hosted)

As I understand the sonarqube uses the LoC of a project (largest loc across branches) to compute total LOC of the server.

I’m trying to figure out which projects are consuming lot of LoC space. I don’t want to do it manually and thought of using API to do it.

Though the below
https://sonarqube***com/api/measures/component?metricKeys=ncloc&component=ProjectKey
or
https://sonarqube***com/api/measures/search?metricKeys=ncloc&projectKeys=ProjectKey

provides LOC, I’m not sure whether it gives the Max. I want to know for every branch as total LOC is based on the sum(MAX(LOC of all branches given a project) LOC for all projects)

Also, I observed the value of Lines of Code as I observe in Measures->Size>Lines of Code is different from what I get from API. Ex. I see 5845 in UI vs 5468 is what I get from API.

Any help here is appreciated.

Hi,

By default those services are going to give you the values for the main branch. Add the branch parameter to get values for other branches. And you can use api/project_branches/list to get a list of the project branches.

AND… we’re working on reporting for 9.4 (E.T.A. end of March) to show you the license-LOC consumed by each project. So if you can wait that long, you may not have to bother with the web services.

 
HTH,
Ann

2 Likes

Thanks, and I could get the LoC now. But I feel there is something wrong with LoC computation by sonar when we have projects just initialized. My problem is related to Sonar - 983,206 lines currently analyzed (License Manager) - #6 by dmeneses. There is drastic difference in LoC in our deployment where sonar UI says ~20M lines vs when I compute is hardly 1 M. Is there any backend query that I can use to check the LoC compute behavior.

Hi,

I don’t know of one.

Have you checked all the branches for your projects? Typically we find that the “extra” lines of code are hiding in branches.

 
Ann

Hi again,

This may help:

 
Ann

Thanks Ann. Yes I did consider all branches. It is exactly the same APIs I used as in example. But, I have multiple projects which are just in initialized state as in the situation described here Sonar - 983,206 lines currently analyzed (License Manager) - #6 by dmeneses. I’m trying to get a ticket filed on this to investigate as the LoC keeps increasing.

Hi,

Since the projects are only initialized, can you delete them & see if that has an impact?

 
Ann

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.