what are you trying to achieve: I want to get a correct analysis of new code.
what have you tried so far to achieve this: I configured SonarQube to use my main branch as reference branch, but it still identifies classes that were not changed in a pull request as New Code.
I have a pull request that changed 359 lines. These lines contain changes in source code and tests files as well as some meta files.
However, in the latest update of the pull request, the coverage was detected to be nothing as SonarQube detected no new lines to cover. SonarQube is configured to detect new code against the main branch.
The changes in the pull request contain several new classes and coverable code. SonarQube even detected coverable code in the analysis before.
My guess is that SonarQube only analyzed only the latest commit which did not have any coverable changes. However, we configured the new code rule to be branch-based in the SonarQube server as well as in the analysis directly:
dotnet sonarscanner begin /k:"..." \
/d:sonar.host.url="sonarqube" \
/d:sonar.cs.vscoveragexml.reportsPaths=coverage.xml \
/d:sonar.scm.provider=git \
/d:sonar.newCode.referenceBranch=main \
/d:sonar.token=$SONAR_TOKEN
dotnet publish -c Release --no-restore my.sln
dotnet-coverage collect 'dotnet test --logger:"junit;LogFilePath=dotnet-test-result.xml"' -f xml -o 'coverage.xml'
dotnet sonarscanner end /d:sonar.token=$SONAR_TOKEN
In the logs, it detected the difference correctly:
Your screenshot shows a PR. What’s considered new code in a PR is based entirely on SCM data and any sonar.newCode.referenceBranch value is entirely irrelevant.
Were there 359 lines changed in the latest commit?
At any rate, I suspect this is about your checkout. The docs may help.
Based on the SCM data, this file is not part of the PR. The 359 lines were changed in multiple commits inside this PR. However, as defined in the docs, any change in the PR is considered New Code. As shown in the screenshot, it does not detect the coverage correctly as the PR contains coverable code and new tests.
What that last screenshot shows is the entire file is marked as ‘new’. This brings us back to the question of your SCM data.
What the first screenshot shows is that every file except the very last seems to be understood by analysis to be a test file. I’m saying that based on the icons. You should review your configuration.
I didn’t find any documentation about which icon has which meaning and there is no other indication about how SonarQube categorizes a file. This should be indicated in the UI.