jenkinsci / code-coverage-api-plugin

Deprecated Jenkins Code Coverage Plugin
https://plugins.jenkins.io/code-coverage-api/
MIT License
112 stars 75 forks source link

Add `@Whitelisted` API to access results of coverage recorder #664

Closed medianick closed 1 year ago

medianick commented 1 year ago

Jenkins and plugins versions report

Environment ```text Paste the output here ```

What Operating System are you using (both controller, and any agents involved in the problem)?

Amazon Linux 2 for controller and agents

Reproduction steps

Previously with publishCoverage, I was able to call currentBuild.rawBuild.getAction(CoverageAction.class) to return a CoverageAction object, from which I could access coverage details via its getResult() method (which then returned a CoverageResult object with a getCoverage(CoverageElement) method, etc.). My mechanism is very similar to the implementation in https://github.com/rguenthe/pipeline-coverage-results-plugin/blob/master/src/main/java/io/jenkins/plugins/pipelinecoverageresults/GetCoverageStep.java#L104, but with a slightly different way to access the CoverageAction object. In my case, this is run as a Groovy file via a Shared Library method.

Is there a different syntax to use that would allow this CoverageAction object to be retrieved after recordCoverage has been called, as previously worked with publishCoverage?

Expected Results

Expected to retrieve the CoverageAction object the same way as with publishCoverage

Actual Results

Got a null object instead

Anything else?

No response

medianick commented 1 year ago

Relates to https://github.com/jenkinsci/code-coverage-api-plugin/issues/145

uhafner commented 1 year ago

You can use

def action = build.getAction(io.jenkins.plugins.coverage.metrics.steps.CoverageBuildAction.class)

to get the action from a build.

Note, that Jenkins security system does not provide access to internal objects. So in order to use the action we need to whitelist the required methods.

Which results do you need? We already have a remote API that provides a lot of those results. Is there missing something? Maybe it would be simpler to expose those results as tokens, see #178.

medianick commented 1 year ago

What I was previously relying on was being able to fetch the CoverageResult object, whose getCoverage method I could then call to get the different kinds of coverage (e.g., CoverageElement.LINE) as ints or floats. I have tooling that takes this and does things like renders build badges with the numbers.

And yes, if I were doing this directly within Jenkinsfiles it would require more whitelisting but this is being done through a shared library, so I can just call, e.g., def coverage = getCoverage() in my Jenkinsfile to get the object in question.

It seems like the change here with recordCoverage is a different action class -- CoverageBuildAction rather than CoverageAction (as with publishCoverage) -- so I'll have to rework the logic to see if I can still extract compatible structure from it.

I had noted the token macro issue (#178) and knew of the API possibility but did not want to deal with the additional overhead of authenticating; the internal objects are accessible directly from my pipeline builds so were the most convenient (but at risk of changes, as this shows).

medianick commented 1 year ago

I've managed to resolve this for the statistics I previously retrieved from CoverageAction to obtain them from CoverageBuildAction (and CoverageStatistics, etc.) instead. I'll close this ticket and await #178 as a more supported way to obtain this data inside a pipeline. Thanks for the assistance!