Skip to content

Commit 2ab6b5f

Browse files
committed
Fix CI wrt code coverage
1 parent d275d7c commit 2ab6b5f

File tree

2 files changed

+88
-60
lines changed

2 files changed

+88
-60
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
name: Post Coverage Comment
2+
on:
3+
workflow_run:
4+
workflows: ["Build and Deploy Snapshot"]
5+
types: [completed]
6+
7+
permissions:
8+
pull-requests: write
9+
10+
jobs:
11+
comment:
12+
runs-on: ubuntu-24.04
13+
if: github.event.workflow_run.event == 'pull_request' && github.event.workflow_run.conclusion == 'success'
14+
steps:
15+
- name: Download comment artifact
16+
uses: dawidd6/action-download-artifact@v6
17+
with:
18+
name: pr-comment
19+
path: pr-comment/
20+
run_id: ${{ github.event.workflow_run.id }}
21+
github_token: ${{ secrets.GITHUB_TOKEN }}
22+
23+
- name: Post coverage comment
24+
run: |
25+
PR_NUMBER=$(cat pr-comment/pr-number.txt)
26+
COMMENT_BODY=$(cat pr-comment/comment-body.txt)
27+
28+
# Find and delete existing comment
29+
COMMENT_ID=$(gh pr view $PR_NUMBER --json comments \
30+
--jq '.comments[] | select(.body | contains("<!-- jacoco-coverage-comment -->")) | .id' | head -1)
31+
32+
if [ -n "$COMMENT_ID" ]; then
33+
gh api -X DELETE "repos/${{ github.repository }}/issues/comments/$COMMENT_ID" || true
34+
fi
35+
36+
# Post new comment
37+
gh pr comment $PR_NUMBER --body "$COMMENT_BODY"
38+
env:
39+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

.github/workflows/main.yml

Lines changed: 49 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ on:
1111
- "release-notes/*"
1212
permissions:
1313
contents: read
14-
pull-requests: write
1514

1615
jobs:
1716
build:
@@ -107,72 +106,62 @@ jobs:
107106
generate-coverage-badge: false
108107
generate-branches-badge: false
109108
generate-summary: true
110-
- name: Add coverage comment to PR
109+
- name: Generate coverage comment
111110
if: ${{ matrix.release_build && github.event_name == 'pull_request' }}
112111
run: |
112+
# Helper functions
113+
parse_coverage() {
114+
local csv_file=$1 col_missed=$2 col_covered=$3
115+
awk -F',' -v m="$col_missed" -v c="$col_covered" \
116+
'NR>1 && $1=="Jackson-core" {
117+
missed=$m; covered=$c;
118+
if (missed + covered > 0) printf "%.1f", (covered * 100.0) / (missed + covered)
119+
}' "$csv_file" | head -1
120+
}
121+
122+
format_delta() {
123+
local delta=$1
124+
if awk -v d="$delta" 'BEGIN { exit (d >= 0) ? 0 : 1 }'; then
125+
echo "+${delta}%|📈"
126+
else
127+
echo "${delta}%|📉"
128+
fi
129+
}
130+
131+
get_color() {
132+
awk -v v="$1" 'BEGIN {
133+
if (v >= 80) print "brightgreen"
134+
else if (v >= 60) print "green"
135+
else if (v >= 40) print "yellow"
136+
else print "red"
137+
}'
138+
}
139+
113140
# Convert decimal to percentage and round to 1 decimal place
114141
COVERAGE=$(awk -v cov="${{ steps.jacoco.outputs.coverage }}" 'BEGIN { printf "%.1f", cov * 100 }')
115142
BRANCHES=$(awk -v br="${{ steps.jacoco.outputs.branches }}" 'BEGIN { printf "%.1f", br * 100 }')
116143
117-
# Check if base coverage artifact was downloaded
144+
# Check if base coverage artifact was downloaded and calculate deltas
145+
HAS_DELTA=false
118146
if [ -f "base-coverage/jacoco.csv" ]; then
119147
echo "Found base branch coverage from artifact"
120-
BASE_CSV="base-coverage/jacoco.csv"
121-
122-
# Parse base branch coverage
123-
BASE_COVERAGE=$(awk -F',' 'NR>1 && $1=="Jackson-core" {
124-
missed=$4; covered=$5;
125-
if (missed + covered > 0) printf "%.1f", (covered * 100.0) / (missed + covered)
126-
}' "$BASE_CSV" | head -1)
127-
BASE_BRANCHES=$(awk -F',' 'NR>1 && $1=="Jackson-core" {
128-
missed=$6; covered=$7;
129-
if (missed + covered > 0) printf "%.1f", (covered * 100.0) / (missed + covered)
130-
}' "$BASE_CSV" | head -1)
148+
BASE_COVERAGE=$(parse_coverage "base-coverage/jacoco.csv" 4 5)
149+
BASE_BRANCHES=$(parse_coverage "base-coverage/jacoco.csv" 6 7)
131150
132151
if [ -n "$BASE_COVERAGE" ] && [ -n "$BASE_BRANCHES" ]; then
133-
# Calculate deltas
134152
COV_DELTA=$(awk -v curr="$COVERAGE" -v base="$BASE_COVERAGE" 'BEGIN { printf "%.1f", curr - base }')
135153
BR_DELTA=$(awk -v curr="$BRANCHES" -v base="$BASE_BRANCHES" 'BEGIN { printf "%.1f", curr - base }')
136154
137-
# Format delta strings with + or - signs
138-
if awk -v delta="$COV_DELTA" 'BEGIN { exit (delta >= 0) ? 0 : 1 }'; then
139-
COV_DELTA_STR="+${COV_DELTA}%"
140-
COV_DELTA_EMOJI="📈"
141-
else
142-
COV_DELTA_STR="${COV_DELTA}%"
143-
COV_DELTA_EMOJI="📉"
144-
fi
145-
146-
if awk -v delta="$BR_DELTA" 'BEGIN { exit (delta >= 0) ? 0 : 1 }'; then
147-
BR_DELTA_STR="+${BR_DELTA}%"
148-
BR_DELTA_EMOJI="📈"
149-
else
150-
BR_DELTA_STR="${BR_DELTA}%"
151-
BR_DELTA_EMOJI="📉"
152-
fi
155+
IFS='|' read -r COV_DELTA_STR COV_DELTA_EMOJI <<< "$(format_delta "$COV_DELTA")"
156+
IFS='|' read -r BR_DELTA_STR BR_DELTA_EMOJI <<< "$(format_delta "$BR_DELTA")"
153157
154158
HAS_DELTA=true
155-
else
156-
HAS_DELTA=false
157159
fi
158-
else
159-
HAS_DELTA=false
160160
fi
161161
162-
# Determine color for coverage badge using awk (more portable than bc)
163-
COV_COLOR=$(awk -v cov="$COVERAGE" 'BEGIN {
164-
if (cov >= 80) print "brightgreen"
165-
else if (cov >= 60) print "green"
166-
else if (cov >= 40) print "yellow"
167-
else print "red"
168-
}')
169-
170-
BR_COLOR=$(awk -v br="$BRANCHES" 'BEGIN {
171-
if (br >= 80) print "brightgreen"
172-
else if (br >= 60) print "green"
173-
else if (br >= 40) print "yellow"
174-
else print "red"
175-
}')
162+
# Determine badge colors
163+
COV_COLOR=$(get_color "$COVERAGE")
164+
BR_COLOR=$(get_color "$BRANCHES")
176165
177166
# Build coverage table with or without deltas
178167
if [ "$HAS_DELTA" = "true" ]; then
@@ -195,17 +184,17 @@ jobs:
195184
196185
<!-- jacoco-coverage-comment -->"
197186
198-
# Find and delete existing coverage comment
199-
COMMENT_ID=$(gh pr view ${{ github.event.pull_request.number }} --json comments --jq '.comments[] | select(.body | contains("<!-- jacoco-coverage-comment -->")) | .id' | head -1) || true
200-
201-
if [ -n "$COMMENT_ID" ]; then
202-
gh api -X DELETE "repos/${{ github.repository }}/issues/comments/$COMMENT_ID" || true
203-
fi
204-
205-
# Post new comment
206-
gh pr comment ${{ github.event.pull_request.number }} --body "$COMMENT_BODY"
207-
env:
208-
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
187+
# Save comment for the workflow_run
188+
mkdir -p pr-comment
189+
echo "${{ github.event.pull_request.number }}" > pr-comment/pr-number.txt
190+
echo "$COMMENT_BODY" > pr-comment/comment-body.txt
191+
- name: Upload PR comment
192+
if: ${{ matrix.release_build && github.event_name == 'pull_request' }}
193+
uses: actions/upload-artifact@v4
194+
with:
195+
name: pr-comment
196+
path: pr-comment/
197+
retention-days: 1
209198

210199
trigger-dep-builds-v2:
211200
name: Trigger downstream builds for v2

0 commit comments

Comments
 (0)