Skip to content

Commit

Permalink
♻️ Refactor Kover Coverage Collection
Browse files Browse the repository at this point in the history
Signed-off-by: Leonardo Colman Lopes <[email protected]>
  • Loading branch information
LeoColman committed Feb 13, 2025
1 parent a479820 commit 4053c4c
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 49 deletions.
76 changes: 44 additions & 32 deletions .github/workflows/kover.main.kts
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
#!/usr/bin/env kotlin
@file:Repository("https://repo1.maven.org/maven2/")
@file:CompilerOptions("-Xmulti-dollar-interpolation")
@file:DependsOn("io.github.typesafegithub:github-workflows-kt:3.2.0")

@file:Repository("https://bindings.krzeminski.it")
@file:DependsOn("actions:checkout:v4")
@file:DependsOn("actions:setup-java:v4")
@file:DependsOn("gradle:gradle-build-action:v3")
@file:DependsOn("actions:upload-artifact:v4")
@file:DependsOn("gradle:actions__setup-gradle:v3")
@file:DependsOn("peaceiris:actions-gh-pages:v4")


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.actions.UploadArtifact
import io.github.typesafegithub.workflows.actions.gradle.ActionsSetupGradle
import io.github.typesafegithub.workflows.actions.peaceiris.ActionsGhPages
import io.github.typesafegithub.workflows.domain.RunnerType
import io.github.typesafegithub.workflows.domain.triggers.Push
Expand All @@ -21,50 +24,59 @@ import io.github.typesafegithub.workflows.dsl.workflow

workflow(
name = "Kover Coverage",
on = listOf(Push(branches = listOf("main"))),
on = listOf(Push(branches = listOf("main", "tweak-gha"))),
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"))
uses(name = "Checkout", action = Checkout())
uses(name = "Generate Kover Reports", action = ActionsSetupGradle(arguments = "koverHtmlReport koverXmlReport"))
uses(
name = "Upload Kover Reports",
action = UploadArtifact(name = "coverage-reports", path = listOf("app/build/reports/kover"))
)
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
name = "Calculate Coverage",
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 = "Update GitHub Summary",
command = $$"""
echo "## Code Coverage Report" >> $GITHUB_STEP_SUMMARY
echo "**Total Coverage:** $COVERAGE%" >> $GITHUB_STEP_SUMMARY
""".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()
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",
name = "Deploy Coverage Report & Badge",
action = ActionsGhPages(
githubToken = expr { secrets.GITHUB_TOKEN },
publishDir = "badge",
githubToken = expr { "secrets.GITHUB_TOKEN" },
publishDir = "app/build/reports/kover/html",
publishBranch = "gh-pages",
allowEmptyCommit = false,
forceOrphan = true
Expand Down
42 changes: 25 additions & 17 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'
- 'tweak-gha'
jobs:
check_yaml_consistency:
name: 'Check YAML consistency'
Expand All @@ -33,46 +34,53 @@ jobs:
java-version: '17'
distribution: 'adopt'
- id: 'step-1'
name: 'Checkout'
uses: 'actions/checkout@v4'
- id: 'step-2'
uses: 'gradle/gradle-build-action@v3'
name: 'Generate Kover Reports'
uses: 'gradle/actions/setup-gradle@v3'
with:
arguments: 'koverHtmlReport'
arguments: 'koverHtmlReport koverXmlReport'
- id: 'step-3'
run: 'cat app/build/reports/kover/html/index.html >> $GITHUB_STEP_SUMMARY'
- id: 'step-4'
uses: 'gradle/gradle-build-action@v3'
name: 'Upload Kover Reports'
uses: 'actions/upload-artifact@v4'
with:
arguments: 'koverXmlReport'
- id: 'step-5'
name: 'Generate coverage output'
name: 'coverage-reports'
path: 'app/build/reports/kover'
- id: 'step-4'
name: 'Calculate Coverage'
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-5'
name: 'Update GitHub Summary'
run: |-
echo "## Code Coverage Report" >> $GITHUB_STEP_SUMMARY
echo "**Total Coverage:** $COVERAGE%" >> $GITHUB_STEP_SUMMARY
- 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
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'
name: 'Deploy Coverage Report & Badge'
uses: 'peaceiris/actions-gh-pages@v4'
with:
github_token: '${{ secrets.GITHUB_TOKEN }}'
publish_branch: 'gh-pages'
publish_dir: 'badge'
publish_dir: 'app/build/reports/kover/html'
allow_empty_commit: 'false'
force_orphan: 'true'

0 comments on commit 4053c4c

Please sign in to comment.