SonarQube counting LOCs for empty branches

Must-share information (formatted with Markdown):

  • SonarQube 9.9 LTS (issue noticed first in 9.4)
  • Docker

Issue Detail:

We recently upgraded to 9.9LTS and ran the license_usage endpoint to get an overview.

In the above image you can see that the branch shows only 2 LOCs for the project.
From the endpoint, we get the following information:

        {
            "projectKey": "…",
            "projectName": "…",
            "pullRequest": "995",
            "linesOfCode": 676315,
            "licenseUsagePercentage": 13.526
        }

This is also contributing a major percentage to our license usage.

We also see the master branch has no analysis at all and only 2 branches are part of the project:

image

image

Has someone else experienced something similar? Could it be a configuration issue from our side?

Hi @anujl

The measures page on the screenshot shows the count of new lines of code on that branch/PR.

As for the licence_usage endpoint and lines of code that count towards the license:

For a given project, the LOCs that count towards License Usage are those of the largest branch (or pull request). Lines of test code do not count toward your License Usage.

Since we there are only 2 branches on the project and the PR is the only one that has the analysis, the total LoC count of the PR will be used for the license usage calculation.
Does the LoC from the license_usage endpoint look correct for total lines of code in the branch for that PR?

More details in the SonarQube documentation..

Hope that helped.

Hi, thanks for your input. The problem is we don’t see any LOC in the Overview page of the PR:

How can we find the real LOC being used for this particular branch? The GUI doesn’t help much here.

We replicated the issue in our instance:

  1. Created a new SQ project manually
  2. Only scanned a PR branch, without scanning the master at all
  3. The end point for license_usage shows:
{
  "projectKey": "LOC-Testing-Project",
  "projectName": "LOC Testing Project",
  "pullRequest": "16",
  "linesOfCode": 37,
  "licenseUsagePercentage": 0.001
} 

So the issue seems to be related to the PR not being cleaned up in SQ. We see from the first image having the overview of the PR branch that the last analysis was Apr 2021.

We already checked the Global Settings → Housekeeping are all set to default values.

We would like to know why the PR branches are not cleaning up automatically?

Hi @anujl

The housekeeping for the project is only performed after a new analysis is run for the project.

Alternatively, you can manually remove the PR in Project settings → Branches and pull requests.

Thanks for pointing it out. Then it seems to be working as expected.

Is there any other automated mechanism that we could use for deleting these kind of stale branches?

Unfortunately, there’s no such automated mechanism in SonarQube itself.

It should be possible to automate it using the Web API endpoints to find stale branches/PRs and remove them.