Skip to content

Commit aee9528

Browse files
authored
Merge pull request #130 from openSUSE/toms/gha-colorize-coverage
Colorize the coverage comment
2 parents 012e672 + b075940 commit aee9528

2 files changed

Lines changed: 42 additions & 4 deletions

File tree

.github/workflows/ci.yml

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,21 +104,58 @@ jobs:
104104
env:
105105
# Use the PR head if available, otherwise fallback to the workflow SHA
106106
COMMIT_SHA: ${{ github.event.pull_request.head.sha || github.sha }}
107+
COVERAGE_THRESHOLD: 90
107108
run: |
108109
# Get the short SHA (e.g., a1b2c3d)
109110
SHORT_SHA=${COMMIT_SHA::7}
110-
# Create the comment content with markdown formatting:
111+
112+
# 1. Header
111113
echo "## Coverage Report" > coverage_comment.txt
112114
echo "For commit $SHORT_SHA" >> coverage_comment.txt
113115
echo "" >> coverage_comment.txt
114116
117+
# 2. Open the collapsible section
115118
echo "<details><summary>Click to expand Coverage Report</summary>" >> coverage_comment.txt
116119
echo "" >> coverage_comment.txt
117-
echo "\`\`\`" >> coverage_comment.txt
118-
cat coverage.txt >> coverage_comment.txt
120+
121+
# 3. Open a diff code block
122+
echo "\`\`\`diff" >> coverage_comment.txt
123+
124+
# 4. Run awk to parse percentages and add colors
125+
awk -v threshold="$COVERAGE_THRESHOLD" '{
126+
# Flag to track if we found a percentage in this line
127+
is_coverage_row = 0
128+
129+
# 1. Scan all fields to find the one ending in "%"
130+
# (Crucial for "term-missing" where the last column might be line numbers "1-5")
131+
for (i=1; i<=NF; i++) {
132+
if ($i ~ /%$/) {
133+
# Remove "%" and force numeric type by adding 0
134+
val = $i
135+
gsub("%", "", val)
136+
val = val + 0 # Force numeric conversion
137+
is_coverage_row = 1
138+
break; # Stop once we find the percentage
139+
}
140+
}
141+
142+
# 2. Logic: Colorize rows with %, indent everything else
143+
if (is_coverage_row) {
144+
if (val < threshold) {
145+
print "- " $0; # Red
146+
} else {
147+
print "+ " $0; # Green
148+
}
149+
} else {
150+
# Headers and Separators (------) fall here.
151+
# We add two spaces so they appear Gray and aligned.
152+
print " " $0;
153+
}
154+
}' coverage.txt >> coverage_comment.txt
155+
156+
# 5. Close the block
119157
echo "\`\`\`" >> coverage_comment.txt
120158
echo "</details>" >> coverage_comment.txt
121-
cat coverage_comment.txt
122159
123160
- name: Post Coverage Comment
124161
if: success() && github.event_name == 'pull_request' && matrix.python-version == '3.12'

changelog.d/130.infra.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Colorize the coverage report in the GitHub Actions comment based on a threshold.

0 commit comments

Comments
 (0)