jenkinsci / code-coverage-api-plugin

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

Document result access from within pipeline #756

Open stefan6419846 opened 1 year ago

stefan6419846 commented 1 year ago

Describe your use-case which is not covered by existing documentation.

I have to retrieve some coverage results from within a pipeline to do some custom handling with it. While this has been no real issue for the warnings-ng-plugin, it took me quite some time to figure out how to retrieve the desired values. Digging through the code, it seems like we need the following public calls:

import edu.hm.hafner.coverage.Metric
import io.jenkins.plugins.coverage.metrics.model.Baseline

def action = ...  // action of type io.jenkins.plugins.coverage.metrics.steps.CoverageBuildAction

def lineCoverage = action.getStatistics().getValue(Baseline.PROJECT, Metric.LINE)
def branchCoverage = action.getStatistics().getValue(Baseline.PROJECT, Metric.BRANCH)
def lineDelta = action.getStatistics().getValue(Baseline.PROJECT_DELTA, Metric.LINE)
def branchDelta = action.getStatistics().getValue(Baseline.PROJECT_DELTA, Metric.BRANCH)

Is this the desired public API? Or is there any "easier" alternative?

Nevertheless, it might make sense to actually document snippets like this to avoid having to dig through the whole code of both the code-coverage-api-plugin and coverage-model.

Reference any relevant documentation, other materials or issues/pull requests that can be used for inspiration.

No response

uhafner commented 1 year ago

Actually there is no public API yet, since nobody asked for it up to now. There is a remote API which covers all basic use cases, but no step API. And there is no additional documentation yet, on how to access all those values.

So using the public methods is a good approach. If you need more methods or a simpler access please let me know or provide a PR.

stefan6419846 commented 1 year ago

Thanks for the explanations. I agree that most users probably do not need to dig into these things and a documented external Groovy-based API has not the highest priority. Calling a JSON-based API might be feasible, but introduces the overhead for the HTTP requests and requires handling authentication etc.

As it turned out to take quite some time to understand how to get actual percentage strings and double values for the collected metrics (different class types for regular and delta values etc.), I would appreciate if at least some basic docs are provided. This might ensure that future changes at least take this into account as well.