code-coverage-api-plugin icon indicating copy to clipboard operation
code-coverage-api-plugin copied to clipboard

Get results in Pipeline

Open rhys-rant opened this issue 5 years ago • 4 comments

Hi there,

I know this has been raised in the past, but other people seem to have solved this yet I can't find sample code.

I'm looking for a way to get the results from this plugin into GitHub, and compare them against the change branch.

It seems like the only way to get the data is via the REST API. So far, I can get a blob of JSON with the Line data that I need with this endpoint coverage/result/api/json?depth=1. However, I literally just want to get the data from results.elements[Line].ratio. I just want to get that Line coverage ratio out.

I'd love if this was added directly to the plugin - would be far simpler than calling the REST API. However, if this is currently accessible via the REST API alone, could someone provide some guidance on how to get this data?

Much appreciated!

rhys-rant avatar Feb 27 '20 17:02 rhys-rant

@rhys-rantmedia: did you try https://github.com/rguenthe/pipeline-coverage-results-plugin?

electriquo avatar Apr 13 '20 09:04 electriquo

@rhys-rantmedia: did you try https://github.com/rguenthe/pipeline-coverage-results-plugin?

No, I didn't get round to actually installing that on our Jenkins server yet, and now... well...

rhys-rant avatar Apr 16 '20 07:04 rhys-rant

@rhys-rantmedia I know I'm probably late, but I could not build the pipeline-coverage-results-plugin . Instead I used Embeddable Build Status to generate a custom badge. I had to extract the line coverage by hand but it work good even with private repository, jenkins server and multi-branch pipeline.

here is a sample of the Jenkinsfile:

def coverageBadge = addEmbeddableBadgeConfiguration(id: "coverage", subject: "Coverage",status: "0 %")

pipeline
{
// ....
        stage('Report Code coverage')
        {
                //the code coverage was save in coverage.xml
                publishCoverage adapters: [cobertura('build/Coverage/coverage.xml')], calculateDiffForChangeRequests: true, sourceFileResolver: sourceFiles('STORE_LAST_BUILD')

               //Here is the real work 
               script
               {
                   //start by extracting the total line coverage then cut everything before the character "
                   def double ratio = sh(script: ' echo $( grep -oE \'^.*coverage line-rate=\"([0-9]+.[0-9]+)\' build/Coverage/coverage.xml |  cut -d\"\\"\" -f2- )', returnStdout:true)
                     ratio = ratio * 100
                     coverageBadge.setStatus(" ${ratio} %")
// ....
               }
// ....

with that you could work around the lack of token or element to do a better badge. I'm pretty sure that with something like that you could even calculate more than one coverage result.

I hope it can help anyone needed a badge for the code coverage in Jenkins.

alexandreSalconiDenis avatar Dec 08 '21 21:12 alexandreSalconiDenis

It would be much simpler if we would report the coverage using a token, see #178 for the corresponding feature request. It shouldn't be hard to implement.

uhafner avatar Dec 08 '21 21:12 uhafner