How to use embeddable-build-status-plugin to display code coverage

Hi,

I’m trying to display badges from embeddable-build-status-plugin but that display the code coverage (Like codecov.io). Assuming ‘coverage-plugin’ is also installed and correctly display coverage (column or graph). Typically I want to get a badge similar to the column definition

Even after reading embeddable-build-status-plugin I don’t get exactly how we can extends the plugin to also display such badges (Perhaps we should implement a plugin implementing some extension point ?).

Thanks!

Thanks for your interest! The embeddable build status plugin documentation is weak or non-existent for this scenario. I’m not sure that it currently can handle the feature that you need.

There is an example that shows how to create an additional embeddable status, but even that example is missing a lot of description.

Let’s use this topic to better describe the creation of an additional embeddable status configuration and explore the techniques needed to describe it better, then the plugin documentation can be updated. If that helps with your scenario, that is great. If it doesn’t help with your scenario, then we will have at least explored a scenario for others.

If you are willing to adapt the Pipeline job definition rather than extend the plugin, you can define a new embeddable build status configuration in the Pipeline job.

Here is a Pipeline job that reports a fake coverage number based on the build number:

pipeline {
    agent any
    stages {
        stage('Fake coverage') {
            steps {
                script {
                    int buildNumber = env.BUILD_ID as int
                    // rotate coveragePercent from 20% to 100%
                    coveragePercent = 20 * (1 + buildNumber % 5) 
                    // set coverageColor based on coveragePercent
                    if (coveragePercent > 30) {
                        coverageColor = "yellow"
                    }
                    if (coveragePercent > 50) {
                        coverageColor = "yellowgreen"
                    }
                    if (coveragePercent > 70) {
                        coverageColor = "green"
                    }
                    if (coveragePercent > 90) {
                        coverageColor = "brightgreen"
                    }
                    echo "Coverage is ${coveragePercent}%"
                    // Use embeddable build status badge to report coverage
                    coverageBadge = addEmbeddableBadgeConfiguration(id: "coverage", subject: "Coverage:")
                    coverageBadge.setStatus(coveragePercent + "%")
                    coverageBadge.setColor(coverageColor)
                    echo "Coverage badge URL is ${currentBuild.absoluteUrl}/badge/icon?config=coverage"
                    // Use groovy postbuild plugin to add a badge to the build history entry
                    textColor = "black"
                    border = coverageColor
                    borderColor = "transparent"
                    backgroundColor = coverageColor
                    manager.addShortText(coveragePercent + "%", textColor, backgroundColor, border, borderColor)
                }
            }
        }
    }
}

That job will report the added embeddable build status URL in the build log.

However, that technique requires that every Pipeline job must be modified to include that embeddable build status configuration.

Thanks you! But I think I will go ahead with plugin code. I don’t want to implement this logic on every pipeline (even encapsulated inside a jenkins library). I have already hundreds of pipeline already using the recordCoverage step.

By reading the code I think I can implement the extension point ParameterResolverExtensionPoint embeddable-build-status-plugin/src/main/java/org/jenkinsci/plugins/badge/extensionpoints/ParameterResolverExtensionPoint.java at 4a954796e45d9bba8602ff102565cad1668f65d5 · jenkinsci/embeddable-build-status-plugin · GitHub

And resolve 2 new value (coveragePercentage and coverageColor) and could be resolved in URL like <instanceUrl>/buildStatus/icon?job=job&status=${coveragePercentage}%25&color=${coverageColor}

Could be a new plugin or an optional extension of embeddable-build-status-plugin activated when coverage plugin is also installed

1 Like

I did a small plugin GitHub - jonesbusy/coverage-badges-extension-plugin

See README to understand how it works. I will publish it in the next few days

I originally open a PR on the Implement extension for code coverage badges by jonesbusy · Pull Request #280 · jenkinsci/embeddable-build-status-plugin · GitHub but I think it’s much better to create a new plugin to provide this extension. Specially if we need to provide more metrics.

Would be good it the plugin can also be installed on ci.jenkins.io