Skip to content

Commit

Permalink
⚗️ First attempt on extracting Kover coverage badge
Browse files Browse the repository at this point in the history
Signed-off-by: Leonardo Colman Lopes <[email protected]>
  • Loading branch information
LeoColman committed Jan 28, 2025
1 parent 5abb37d commit 413f669
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 1 deletion.
43 changes: 42 additions & 1 deletion .github/workflows/kover.main.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,66 @@
@file:DependsOn("actions:checkout:v4")
@file:DependsOn("actions:setup-java:v4")
@file:DependsOn("gradle:gradle-build-action:v3")
@file:DependsOn("peaceiris:actions-gh-pages:v3")


import io.github.typesafegithub.workflows.actions.actions.Checkout
import io.github.typesafegithub.workflows.actions.actions.SetupJava
import io.github.typesafegithub.workflows.actions.gradle.GradleBuildAction
import io.github.typesafegithub.workflows.actions.peaceiris.ActionsGhPages
import io.github.typesafegithub.workflows.domain.RunnerType
import io.github.typesafegithub.workflows.domain.triggers.PullRequest
import io.github.typesafegithub.workflows.domain.triggers.Push
import io.github.typesafegithub.workflows.dsl.expressions.expr
import io.github.typesafegithub.workflows.dsl.workflow


workflow(
name = "Kover Coverage",
on = listOf(Push(branches = listOf("main"))),
on = listOf(Push(branches = listOf("main")), PullRequest()),
sourceFile = __FILE__
) {
job(id = "analyse", runsOn = RunnerType.UbuntuLatest) {
uses(name = "Set up JDK", action = SetupJava(javaVersion = "17", distribution = SetupJava.Distribution.Adopt))
uses(action = Checkout())
uses(action = GradleBuildAction(arguments = "koverHtmlReport"))
run(command = "cat app/build/reports/kover/html/index.html >> \$GITHUB_STEP_SUMMARY")

uses(action = GradleBuildAction(arguments = "koverXmlReport"))
run(
name = "Generate coverage output",
command = """
COVERAGE=$(${expr { github.workspace }}/gradlew -q printLineCoverage)
echo "Extracted Coverage: ${'$'}COVERAGE"
echo "COVERAGE=${'$'}COVERAGE" >> ${'$'}GITHUB_ENV
""".trimIndent()
)

run(
name = "Generate Coverage Badge",
command = """
echo "COVERAGE=${'$'}COVERAGE"
curl "https://img.shields.io/badge/Coverage-${'$'}COVERAGE%25-brightgreen" -o coverage-badge.svg
mkdir -p badge
mv coverage-badge.svg badge/
ls -l badge/
# Configure Git
git config --global user.name "github-actions[bot]"
git config --global user.email "41898282+github-actions[bot]@users.noreply.github.com"
# Add and commit changes
git add badge/
git commit -m "Update coverage badge"
git branch -M gh-pages
git push origin gh-pages
"""
)



uses(name = "Deploy Badge to GitHub Pages", action = ActionsGhPages(
githubToken = expr { secrets.GITHUB_TOKEN },
publishDir = "badge",
))
}
}
20 changes: 20 additions & 0 deletions .github/workflows/kover.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ on:
push:
branches:
- 'main'
pull_request: {}
jobs:
check_yaml_consistency:
name: 'Check YAML consistency'
Expand Down Expand Up @@ -40,3 +41,22 @@ jobs:
arguments: 'koverHtmlReport'
- id: 'step-3'
run: 'cat app/build/reports/kover/html/index.html >> $GITHUB_STEP_SUMMARY'
- id: 'step-4'
uses: 'gradle/gradle-build-action@v3'
with:
arguments: 'koverXmlReport'
- id: 'step-5'
name: 'Generate coverage output'
run: |-
COVERAGE=$(${{ github.workspace }}/gradlew -q printLineCoverage)
echo "Extracted Coverage: $COVERAGE"
echo "COVERAGE=$COVERAGE" >> $GITHUB_ENV
- id: 'step-6'
name: 'Generate Coverage Badge'
run: "\n echo \"COVERAGE=$COVERAGE\"\n curl \"https://img.shields.io/badge/Coverage-$COVERAGE%25-brightgreen\" -o coverage-badge.svg\n mkdir -p badge\n mv coverage-badge.svg badge/\n ls -l badge/\n # Configure Git\n git config --global user.name \"github-actions[bot]\"\n git config --global user.email \"41898282+github-actions[bot]@users.noreply.github.com\"\n\n # Add and commit changes\n git add badge/\n git commit -m \"Update coverage badge\"\n git branch -M gh-pages\n git push origin gh-pages\n "
- id: 'step-7'
name: 'Deploy Badge to GitHub Pages'
uses: 'peaceiris/actions-gh-pages@v3'
with:
github_token: '${{ secrets.GITHUB_TOKEN }}'
publish_dir: 'badge'
38 changes: 38 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/

import java.util.Properties
import javax.xml.parsers.DocumentBuilderFactory
import org.gradle.api.JavaVersion.VERSION_17

@Suppress("DSL_SCOPE_VIOLATION") //KTIJ-19369
Expand Down Expand Up @@ -280,3 +281,40 @@ licensee {
because("Google Play License Agreement.")
}
}

/**
* Parse kover coverage report for badge creation
*/
tasks.register("printLineCoverage") {
group = "verification" // Put into the same group as the `kover` tasks
dependsOn("koverXmlReport")
doLast {
val report = file("${layout.buildDirectory.get()}/reports/kover/report.xml")

val doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(report)
val rootNode = doc.firstChild
var childNode = rootNode.firstChild

var coveragePercent = 0.0

while (childNode != null) {
if (childNode.nodeName == "counter") {
val typeAttr = childNode.attributes.getNamedItem("type")
if (typeAttr.textContent == "LINE") {
val missedAttr = childNode.attributes.getNamedItem("missed")
val coveredAttr = childNode.attributes.getNamedItem("covered")

val missed = missedAttr.textContent.toLong()
val covered = coveredAttr.textContent.toLong()

coveragePercent = (covered * 100.0) / (missed + covered)

break
}
}
childNode = childNode.nextSibling
}

println("%.1f".format(coveragePercent))
}
}

0 comments on commit 413f669

Please sign in to comment.