Skip to content

Commit

Permalink
👷 Extract Kover coverage badge to README
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 29, 2025
1 parent 6b85873 commit a05f01b
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 1 deletion.
46 changes: 45 additions & 1 deletion .github/workflows/kover.main.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@
@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


Expand All @@ -26,5 +29,46 @@ workflow(
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
)
)
}
}
36 changes: 36 additions & 0 deletions .github/workflows/kover.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,39 @@ 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 "Raw Coverage: $COVERAGE"
COVERAGE=$(printf "%.0f" "$COVERAGE") # Round to integer
echo "Rounded Coverage: $COVERAGE"
echo "COVERAGE=$COVERAGE" >> $GITHUB_ENV
- id: 'step-6'
name: 'Generate Coverage Badge'
run: |-
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
- id: 'step-7'
name: 'Deploy Badge to GitHub Pages'
uses: 'peaceiris/actions-gh-pages@v3'
with:
github_token: '${{ secrets.GITHUB_TOKEN }}'
publish_branch: 'gh-pages'
publish_dir: 'badge'
allow_empty_commit: 'false'
force_orphan: 'true'
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 a05f01b

Please sign in to comment.