SonarQube does not detect New Code as expected after merge

Versions:

  • SonarQube 9.4 (build 54424)
  • SonarQube Gradle plugin 3.2.0

Deployment option: Helm

Trying to achieve: new code correctly detected in main branch after the development branch is merged and some of its commits are older than the latest analysis on main.

Steps to reproduce:

  • have a main branch with some code in it, let’s say the last commit is commit1
  • have a development branch created from main using git checkout -b development_branch
  • in SonarQube have New Code setting set to “Reference branch: main” for Project, and to “Previous version” for main branch
  • make some changes in the development branch and commit them (creating commit2)
  • make some changes in main and commit them (creating commit3)
  • trigger analysis on main using gradle sonarqube -Dsonar.projectVersion=main@commit3
  • made some more changes in the development branch and commit them (creating commit4)
  • merge development branch into main (creating commit5), have a following commit history:
    * commit5 Merge branch 'development_branch` into main
    |\
    | * commit4
    * | commit3
    | * commit2
    |/
    * commit1
    
  • trigger analysis on main using gradle sonarqube -Dsonar.projectVersion=main@commit5
  • observe that only the changes from the commit4 are considered New Code while commit2 is ignored, even though it is a new code for main branch

After reading this topic and a bit of SonarQube source code, I now know that the New Code detection logic does not use git diff but instead compares previous version timestamp with the modification timestamps of the code lines being scanned. However, I’m nor sure I understand the motivation behind this approach. Yes, it is (relatively) simple but it also seems to be not complicated enough to handle relatively trivial use cases.

Also, I still don’t see what I can do to make New Code detection work as expected in my situation. I would appreciate if anyone could suggest a solution.

Hi,

Your version is past EOL. You should upgrade to either the latest version or the current LTS at your earliest convenience. Your upgrade path is:

9.4 → 9.9 → 10.1 (last step optional)

You may find these resources helpful:

If you have questions about upgrading, feel free to open a new thread for that here.

If your issue persists after upgrade, please come back to us.