From 413f669128bc2bfc536b0de89a427b622f3eb9ef Mon Sep 17 00:00:00 2001 From: Leonardo Colman Lopes Date: Mon, 27 Jan 2025 18:56:17 -0300 Subject: [PATCH] =?UTF-8?q?=E2=9A=97=EF=B8=8F=20First=20attempt=20on=20ext?= =?UTF-8?q?racting=20Kover=20coverage=20badge?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Leonardo Colman Lopes --- .github/workflows/kover.main.kts | 43 +++++++++++++++++++++++++++++++- .github/workflows/kover.yaml | 20 +++++++++++++++ app/build.gradle.kts | 38 ++++++++++++++++++++++++++++ 3 files changed, 100 insertions(+), 1 deletion(-) diff --git a/.github/workflows/kover.main.kts b/.github/workflows/kover.main.kts index e1d17b21..06693683 100755 --- a/.github/workflows/kover.main.kts +++ b/.github/workflows/kover.main.kts @@ -6,19 +6,23 @@ @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) { @@ -26,5 +30,42 @@ 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 "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", + )) } } diff --git a/.github/workflows/kover.yaml b/.github/workflows/kover.yaml index c403d343..c5f31bc6 100644 --- a/.github/workflows/kover.yaml +++ b/.github/workflows/kover.yaml @@ -7,6 +7,7 @@ on: push: branches: - 'main' + pull_request: {} jobs: check_yaml_consistency: name: 'Check YAML consistency' @@ -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' diff --git a/app/build.gradle.kts b/app/build.gradle.kts index 93c0014f..057f0a85 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -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 @@ -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)) + } +}