Skip to content

Commit 0b5e34d

Browse files
enoodleArmedGuy
andauthored
chore: add pr coverage report (#154)
Co-authored-by: Johan Jatko <[email protected]>
1 parent 6039d73 commit 0b5e34d

File tree

4 files changed

+109
-2
lines changed

4 files changed

+109
-2
lines changed

.github/workflows/on-pr.yaml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
name: KAI Scheduler - Pull Request
55
on:
66
pull_request:
7+
types: [opened, reopened, synchronize]
78

89
concurrency:
910
group: ${{ github.head_ref || github.ref || github.run_id }}
@@ -28,6 +29,29 @@ jobs:
2829
- name: Run tests
2930
run: make test
3031

32+
- name: Archive code coverage results
33+
uses: actions/upload-artifact@v4
34+
with:
35+
name: code-coverage
36+
path: coverage/coverage.out
37+
38+
code-coverage-report:
39+
name: Code Coverage Report
40+
if: github.event_name == 'pull_request'
41+
runs-on: ubuntu-latest
42+
needs: validate-and-test
43+
permissions:
44+
contents: read
45+
actions: read
46+
pull-requests: write
47+
steps:
48+
- uses: fgrosse/go-coverage-report@8c1d1a09864211d258937b1b1a5b849f7e4f2682
49+
with:
50+
coverage-artifact-name: "code-coverage"
51+
coverage-file-name: "coverage.out"
52+
root-package: "github.com/NVIDIA/KAI-Scheduler"
53+
github-baseline-workflow-ref: update-coverage-badge.yaml
54+
3155
build:
3256
name: Build
3357
runs-on: ubuntu-latest
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# Copyright 2025 NVIDIA CORPORATION
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
name: Update Coverage Badge
5+
6+
on:
7+
push:
8+
branches:
9+
- "main"
10+
pull_request:
11+
paths:
12+
- .github/workflows/update-coverage-badge.yaml
13+
workflow_dispatch:
14+
15+
jobs:
16+
update-coverage:
17+
name: Update Coverage Badge
18+
runs-on: ubuntu-latest
19+
permissions:
20+
contents: write
21+
22+
steps:
23+
- name: Checkout code
24+
uses: actions/checkout@v4
25+
26+
- name: Set up Go
27+
uses: actions/setup-go@v5
28+
with:
29+
go-version: '1.23.4'
30+
31+
- name: Run tests with coverage
32+
run: make test
33+
34+
- name: Archive code coverage results
35+
uses: actions/upload-artifact@v4
36+
with:
37+
name: code-coverage
38+
path: coverage/coverage.out
39+
40+
- name: Calculate coverage percentage
41+
id: coverage
42+
run: |
43+
COVERAGE=$(go tool cover -func=coverage/coverage.out | grep total | grep -Eo '[0-9]+\.[0-9]+')
44+
echo "percentage=$COVERAGE" >> $GITHUB_OUTPUT
45+
ROUNDED_COVERAGE=$(printf "%.0f" $COVERAGE)
46+
echo "rounded=$ROUNDED_COVERAGE" >> $GITHUB_OUTPUT
47+
echo "Total coverage: $COVERAGE%"
48+
49+
# Determine badge color based on coverage percentage
50+
if (( $(echo "$COVERAGE >= 80" | bc -l) )); then
51+
echo "color=brightgreen" >> $GITHUB_OUTPUT
52+
elif (( $(echo "$COVERAGE >= 70" | bc -l) )); then
53+
echo "color=green" >> $GITHUB_OUTPUT
54+
elif (( $(echo "$COVERAGE >= 60" | bc -l) )); then
55+
echo "color=yellowgreen" >> $GITHUB_OUTPUT
56+
elif (( $(echo "$COVERAGE >= 50" | bc -l) )); then
57+
echo "color=yellow" >> $GITHUB_OUTPUT
58+
elif (( $(echo "$COVERAGE >= 40" | bc -l) )); then
59+
echo "color=orange" >> $GITHUB_OUTPUT
60+
else
61+
echo "color=red" >> $GITHUB_OUTPUT
62+
fi
63+
64+
- name: Update README.md with Coverage Badge
65+
if: github.event_name != 'pull_request'
66+
run: |
67+
# Create the shields.io URL with the coverage data
68+
BADGE_URL="https://img.shields.io/badge/coverage-${{ steps.coverage.outputs.percentage }}%25-${{ steps.coverage.outputs.color }}"
69+
70+
# Update the README.md file
71+
sed -i -E "s|(\!\[License\]\([^)]+\))|\1 \![Coverage]($BADGE_URL)|" README.md
72+
73+
# Check if the README has been modified
74+
if git diff --exit-code README.md; then
75+
echo "No changes to README.md"
76+
else
77+
git config --local user.email "[email protected]"
78+
git config --local user.name "Update Coverage Badge Action"
79+
git add README.md
80+
git commit -m "docs: update test coverage badge [skip ci]"
81+
git push
82+
fi

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ bin/
33
charts/
44
cover.out
55
.DS_Store
6+
coverage/

build/makefile/testenv.mk

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
ENVTEST_K8S_VERSION = 1.32.0
32
ENVTEST_VERSION=release-0.20
43

@@ -12,8 +11,9 @@ envtest-docker-go: builder gocache
1211

1312
envtest-go: envtest
1413
@ ${ECHO_COMMAND} ${GREEN_CONSOLE} "${CONSOLE_PREFIX} Running unit-tests" ${BASE_CONSOLE}
14+
mkdir -p coverage
1515
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) -p path --bin-dir $(LOCALBIN))" \
16-
go test ${TEST_TARGETS} -timeout 30m || ${FAILURE_MESSAGE_HANDLER}
16+
go test ${TEST_TARGETS} -timeout 30m -coverprofile=coverage/coverage.out || ${FAILURE_MESSAGE_HANDLER}
1717
${SUCCESS_MESSAGE_HANDLER}
1818

1919
ENVTEST = $(LOCALBIN)/setup-envtest

0 commit comments

Comments
 (0)