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 f92b94c
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 1 deletion.
47 changes: 46 additions & 1 deletion .github/workflows/kover.main.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,70 @@
@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 "Raw Coverage: ${'$'}COVERAGE"
COVERAGE=${'$'}(printf "%.0f" "${'$'}COVERAGE") # Round to integer
echo "Rounded Coverage: ${'$'}COVERAGE"
echo "COVERAGE=${'$'}COVERAGE" >> ${'$'}GITHUB_ENV
""".trimIndent()
)

run(
name = "Generate Coverage Badge",
command = """
mkdir -p badge
COLOR=$(
if [ "${'$'}COVERAGE" -ge 90 ]; then
echo "brightgreen"
elif [ "${'$'}COVERAGE" -ge 70 ]; then
echo "yellow"
else
echo "red"
fi
)
URL="https://img.shields.io/badge/Coverage-${'$'}COVERAGE%25-${'$'}COLOR"
curl -sS "${'$'}URL" -o badge/coverage-badge.svg
""".trimIndent()
)

uses(
name = "Deploy Badge to GitHub Pages",
action = ActionsGhPages(
githubToken = expr { secrets.GITHUB_TOKEN },
publishDir = "badge",
publishBranch = "gh-pages",
allowEmptyCommit = false,
forceOrphan = true
)
)
}
}
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 git fetch origin gh-pages || true\n git checkout gh-pages 2>/dev/null || git checkout --orphan gh-pages\n\n echo \"COVERAGE=$COVERAGE\"\n mkdir -p badge\n curl \"https://img.shields.io/badge/Coverage-${COVERAGE}%25-$( [ \"$COVERAGE\" -ge 90 ] && echo brightgreen || ( [ \"$COVERAGE\" -ge 70 ] && echo yellow ) || echo red )\" -o badge/coverage-badge.svg\n mkdir -p badge\n\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 git add badge/\n if ! git diff --cached --quiet; then\n git commit -m \"Update coverage badge\"\n git push \"https://github-actions:${GITHUB_TOKEN}@github.com/${GITHUB_REPOSITORY}.git\" gh-pages\n else\n echo \"No coverage changes to commit\"\n fi\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 f92b94c

Please sign in to comment.