Sonarcloud Pull request analysis code coverage showing empty -coverage

Bitbucket Cloud

CI: Jenkins

Scanner command used within the pipeline script in Jenkins: script {

        withSonarQubeEnv('sonarcloud') {
            sh """
                 mvn -s settings.xml clean verify sonar:sonar \
                -Dsonar.organization=projectname \
                -Dsonar.projectKey=key \
                -Pcoverage
                
                ${SCANNER_HOME}/bin/sonar-scanner \
                -Dsonar.projectKey=projectname \
                -Dsonar.organization=name \
                -Dsonar.host.url=https://sonarcloud.io \
                -Dsonar.token=${env.SONAR_TOKEN} \
                -Dsonar.java.coveragePlugin=jacoco \
                -Dsonar.java.binaries=target/classes \
                -Dsonar.jacoco.reportPaths=target/site/jacoco/jacoco.xml \
                -Dsonar.pullrequest.key=${env.CHANGE_ID} \
                -Dsonar.pullrequest.branch=${env.CHANGE_BRANCH} \
                -Dsonar.pullrequest.base=${env.CHANGE_TARGET}
                """
                }

The code is private

Problem report:

The analysis is coming to zero, I need help identifying the problem and using the same steps i can abe to see code coverage percentage in branch analysis and for PR analsysis its -coverage and below to that i can able to see estimated percentage to merge like below screen shot

Hi,

Welcome to the community!

You seem to run a Maven analysis and immediately after a second analysis using the SonarScanner CLI? I don’t understand why you would do that. What’s your build technology?

 
Ann

why im doing 2 analysis means with above only im getting code coverage for branch analsysis and its not happening for PR analysis and build techonology using Maven as the build technology for my Java backend project

Hi,

Can you share your full job log, starting from the checkout?

 
Thx,
Ann

deleted

Hi,

Thanks for the logs.

I have a few general observations:

First,

[e[1;34mINFOe[m] 2 languages detected in 19 preprocessed files

INFO: 4 languages detected in 28 preprocessed files

Your two analyses have different initial scopes. I say ‘initial’ because the scope for the PR is subsequently narrowed to only those files changed in the PR.

Your Maven analysis uses sonar-maven-plugin:3.9.1.2184, which is one version behind. The latest version includes

MSONAR-190 - Default sources to “src/main” instead of “src/main/java”

which may be helpful in this regard. However, you seem to be letting sonar.sources default to . in the SonarScanner CLI analysis, so it may still end up with some additional scope.

Second, this parameter, which you pass to the 2nd analysis, does nothing:

-Dsonar.java.coveragePlugin=jacoco

Similarly,

-Dsonar.jacoco.reportPaths=target/site/jacoco/jacoco.xml

is also a noop, as noted in your log:

WARN: Property 'sonar.jacoco.reportPaths' is no longer supported. Use JaCoCo's xml report and sonar-jacoco plugin.

Now the good news is that the location of your coverage report is automatically checked:

INFO: Sensor JaCoCo XML Report Importer [jacoco]
INFO: 'sonar.coverage.jacoco.xmlReportPaths' is not defined. Using default locations: target/site/jacoco/jacoco.xml,target/site/jacoco-it/jacoco.xml,build/reports/jacoco/test/jacocoTestReport.xml
INFO: Importing 1 report(s). Turn your logs in debug mode in order to see the exhaustive list.
INFO: Sensor JaCoCo XML Report Importer [jacoco] (done) | time=78ms

So it seems that your coverage report is read.

Which brings me back to your original post, where you complain that coverage is “coming to zero”. But actually, the screenshot you provided shows it as -. The distinction is significant. A 0% coverage means there was coverage to measure and it came to 0%. A - coverage means there was no measurement. The question is “Why?”. You passed in a coverage report and the log says it was read.

So let’s come back to what I said earlier:

the scope for the PR is subsequently narrowed to only those files changed in the PR.

If no code files were changed in the PR, then it’s quite legitimate to have 0 issues and - for coverage and duplications.

So, what files were changed?
 

And with all that said, I’m still not understanding why you’re doing two analyses. Is the end goal to have branch analysis or PR analysis?

If it’s PR analysis that you’re truly after, then run the one, Maven analysis (i.e. use the scanner that aligns with your build technology) and pass in your PR parameters on the command line. (The fact that you need to pass them manually is another topic, related to how you’re doing your checkout, I believe.)

 
Ann

Thanks ann, But, answer to your question
My require ment need to run PR analysis in sonarcloud with code coverage
build tech: maven
Tried below steps with same build technlogy already dnt get code coverage after that added sonar stepd also, for that got code coverage in branch analysis and but my requirment need to run sonarcloud analysis only for all PR -->PR analysis need to run , any suggestion
withSonarQubeEnv(‘###’) {
sh “mvn -s settings.xml verify sonar:sonar -Dsonar.projectKey=projectkey -Dsonar.pullrequest.key=${env.CHANGE_ID} -Dsonar.pullrequest.branch=${env.CHANGE_BRANCH} -Dsonar.pullrequest.base=${env.CHANGE_TARGET}”

              }

My issue still not resolved

Hi,

I still need to know:

 
Ann

13 files we changed and raised PR for that and attached screenshot for your reference
Below screen shot from bitbucket cloud

Hi,

This looks like a mismatch to me. Your earlier screenshot showed

  • 0 Bugs
  • 0 Code Smells
  • 0 Vulnerabilities
  • 0 Security Hotspots

This one shows

  • 6 issues

 
Ann

yes its not reflect too, simple can you provided me steps to run PR analaysis with code coverage enabled for maven builds and jacoco has been added in pom.xml also

Hi,

Per the docs

mvn -Dsonar.coverage.jacoco.xmlReportPaths=../app-it/target/site/jacoco-aggregate/jacoco.xml sonar:sonar -Pcoverage

And make sure to manually add your PR parameters if needed.

 
Ann

As per reply, added the path also but still is not showing the coverage and if no files changed means its hould show 100% right, why its not showing that also let me attach latest changes made after your reply and attaching the logs for your reference

Note: I’m having issue only in PR analysis sonarcloud using the same syntax below its showing coverage for branch analysis
newsonarlogs.txt (46.4 KB)

Jenkinsfile:
script {

        withSonarQubeEnv('sonarcloud') {
            sh """
                mvn -s settings.xml clean verify sonar:sonar \
                -Dsonar.organization=projectname \
                -Dsonar.projectKey=projectname_dev \
                -Dsonar.host.url=https://sonarcloud.io \
                -Dsonar.token=${SONAR_TOKEN} \
                -Dsonar.java.coveragePlugin=jacoco \
                -Dsonar.java.binaries=target/classes \
                -Dsonar.coverage.jacoco.xmlReportPaths=target/site/jacoco/jacoco.xml sonar:sonar -Pcoverage \
                -Dsonar.pullrequest.branch=${env.CHANGE_BRANCH} \
                -Dsonar.pullrequest.key=${env.CHANGE_ID} \
                -Dsonar.pullrequest.base=${env.CHANGE_TARGET}
                 """
                 }

Hi,

No. I think it would probably show ‘-’.

Okay, then everything is configured correctly, and (apparently) what’s changed in your PR is not cover-able, I.E. not something that can be covered by unit tests.

 
Ann

If its not covered by unit tests means , included in exclusions , we are not able to any coverage its blank

Hi,

Sorry, I don’t understand.

 
Ann