-
Notifications
You must be signed in to change notification settings - Fork 4
336 lines (289 loc) · 11.5 KB
/
Copy pathcoverage.yml
File metadata and controls
336 lines (289 loc) · 11.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
name: Generate Tekton Coverage Dashboard
on:
push:
branches:
- main
pull_request:
branches:
- main
schedule:
- cron: '0 3 * * *' # Daily at 03:00 UTC
workflow_dispatch:
concurrency:
group: coverage-${{ github.ref }}
cancel-in-progress: false
jobs:
prepare:
runs-on: ubuntu-latest
outputs:
matrix: ${{ steps.set-matrix.outputs.matrix }}
steps:
- name: Checkout this repo (dashboard)
uses: actions/checkout@v4
- name: Install yq
run: pip install yq
- name: Generate matrix from repos.yaml
id: set-matrix
run: |
REPOS=$(yq -c '[.repositories[] | {
name: .name,
exclude_dirs: (.exclude_dirs // []),
exclude_files: (.exclude_files // []),
upstream_coverage_workflow: (.upstream_coverage_workflow // "")
}]' repos.yaml)
echo "matrix=$REPOS" >> $GITHUB_OUTPUT
coverage:
needs: prepare
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
repo: ${{ fromJson(needs.prepare.outputs.matrix) }}
steps:
- name: Checkout this repo (dashboard)
uses: actions/checkout@v4
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: '1.25'
- name: Install tools
run: |
sudo apt-get update
sudo apt-get install -y jq
pip install yq
go install github.com/chmouel/go-better-html-coverage@latest
- name: Checkout gh-pages to access old coverage.json
uses: actions/checkout@v4
with:
ref: gh-pages
path: gh-pages
- name: Calculate coverage for ${{ matrix.repo.name }}
env:
GH_TOKEN: ${{ github.token }}
REPO: ${{ matrix.repo.name }}
EXCLUDE_DIRS: ${{ toJson(matrix.repo.exclude_dirs) }}
EXCLUDE_FILES: ${{ toJson(matrix.repo.exclude_files) }}
UPSTREAM_WORKFLOW: ${{ matrix.repo.upstream_coverage_workflow }}
run: |
REPO_NAME=$(basename "$REPO")
echo "→ Cloning $REPO..."
git clone --depth 1 "https://github.com/$REPO.git"
cd "$REPO_NAME"
# Build the exclude pattern first (needed for both local and upstream coverage)
EXCLUDE_PATTERN=""
if [ "$EXCLUDE_DIRS" != "[]" ]; then
PATTERN_COUNT=$(echo "$EXCLUDE_DIRS" | jq 'length')
PATTERNS=""
for k in $(seq 0 $((PATTERN_COUNT - 1))); do
PATTERN=$(echo "$EXCLUDE_DIRS" | jq -r ".[$k]")
echo " Processing exclude pattern: '$PATTERN'"
case "$PATTERN" in
*/*)
if [[ "$PATTERN" == /* ]]; then
ESCAPED_PATTERN=$(echo "$PATTERN" | sed 's/[[\.*^$()+{}|]/\\&/g')
else
if [[ "$PATTERN" == */ ]]; then
DIR_NAME=${PATTERN%/}
ESCAPED_PATTERN="/${DIR_NAME}(/|$)"
else
ESCAPED_PATTERN="/$(echo "$PATTERN" | sed 's/[[\.*^$()+{}|]/\\&/g')"
fi
fi
;;
*)
ESCAPED_PATTERN="/${PATTERN}(/|$)"
;;
esac
echo " Escaped pattern: '$ESCAPED_PATTERN'"
if [ -z "$PATTERNS" ]; then
PATTERNS="$ESCAPED_PATTERN"
else
PATTERNS="$PATTERNS|$ESCAPED_PATTERN"
fi
done
EXCLUDE_PATTERN="$PATTERNS"
echo " Final exclude pattern: '$EXCLUDE_PATTERN'"
fi
# Get packages and apply exclusions
ALL_PACKAGES=$(go list ./... 2>/dev/null || true)
echo " All packages found:"
echo "$ALL_PACKAGES" | while read pkg; do echo " $pkg"; done
if [ -n "$EXCLUDE_PATTERN" ] && [ -n "$ALL_PACKAGES" ]; then
echo " Applying exclusion pattern: $EXCLUDE_PATTERN"
INCLUDED_PACKAGES=$(echo "$ALL_PACKAGES" | grep -vE "$EXCLUDE_PATTERN" || true)
echo " Packages after exclusion:"
echo "$INCLUDED_PACKAGES" | while read pkg; do [ -n "$pkg" ] && echo " $pkg"; done
echo " Original packages: $(echo "$ALL_PACKAGES" | wc -l)"
echo " Included packages: $(echo "$INCLUDED_PACKAGES" | wc -l)"
else
INCLUDED_PACKAGES="$ALL_PACKAGES"
echo " No exclusion pattern applied"
fi
# Try to fetch coverage from upstream workflow artifact if configured
COVERAGE_FROM_UPSTREAM=false
if [ -n "$UPSTREAM_WORKFLOW" ]; then
echo " Checking for upstream coverage from workflow: $UPSTREAM_WORKFLOW"
RUN_ID=$(gh run list \
--repo "$REPO" \
--workflow "$UPSTREAM_WORKFLOW" \
--branch main \
--status success \
--limit 1 \
--json databaseId \
--jq '.[0].databaseId' 2>/dev/null || echo "")
if [ -n "$RUN_ID" ]; then
echo " Found workflow run: $RUN_ID"
if gh run download "$RUN_ID" --repo "$REPO" --name code-coverage 2>/dev/null; then
if [ -f "coverage.txt" ]; then
echo " ✓ Successfully downloaded coverage.txt from upstream artifact"
cp coverage.txt coverage_raw.out
COVERAGE_FROM_UPSTREAM=true
else
echo " ⚠ Artifact downloaded but coverage.txt not found"
fi
else
echo " ⚠ Could not download artifact from run $RUN_ID"
fi
else
echo " ⚠ No successful workflow runs found for $UPSTREAM_WORKFLOW"
fi
fi
STATUS="ok"
COVERAGE="0.0"
# If we didn't get coverage from upstream, run tests locally
if [ "$COVERAGE_FROM_UPSTREAM" = false ]; then
echo " Running tests locally..."
if [ -n "$INCLUDED_PACKAGES" ] && go test -coverprofile=coverage_raw.out $INCLUDED_PACKAGES; then
echo " ✓ Tests completed successfully"
else
echo " ❌ Coverage failed or no testable packages"
STATUS="failed"
fi
fi
# Apply exclusions to coverage data (for both upstream and local coverage)
if [ -f "coverage_raw.out" ] && [ "$STATUS" = "ok" ]; then
cp coverage_raw.out coverage.out
# Remove excluded directories from coverage
if [ -n "$EXCLUDE_PATTERN" ]; then
echo " Applying exclusion pattern to coverage data: $EXCLUDE_PATTERN"
# Filter out lines matching excluded patterns
grep -vE "$EXCLUDE_PATTERN" coverage.out > coverage_filtered.out || cp coverage.out coverage_filtered.out
cp coverage_filtered.out coverage.out
fi
# Remove excluded files from coverage
if [ "$EXCLUDE_FILES" != "[]" ]; then
FILE_COUNT=$(echo "$EXCLUDE_FILES" | jq 'length')
for j in $(seq 0 $((FILE_COUNT - 1))); do
FILE=$(echo "$EXCLUDE_FILES" | jq -r ".[$j]")
echo " Removing $FILE from coverage"
sed -i "/$FILE/d" coverage.out || true
done
fi
fi
# Calculate coverage if we have coverage.out
if [ -f "coverage.out" ] && [ "$STATUS" = "ok" ]; then
COVERAGE=$(go tool cover -func=coverage.out | grep total | awk '{print $3}' | tr -d '%')
# Use go-better-html-coverage for single-file HTML with better UI
go-better-html-coverage -profile coverage.out -o coverage.html -n
mkdir -p ../coverage-html
cp coverage.html ../coverage-html/index.html
elif [ "$STATUS" = "failed" ]; then
OLD_COVERAGE_JSON=$(cat ../gh-pages/coverage.json 2>/dev/null || echo '{"data":[]}')
OLD_ENTRY=$(echo "$OLD_COVERAGE_JSON" | jq -r --arg repo "$REPO" '.data[]? | select(.repo == $repo)')
if [ -n "$OLD_ENTRY" ]; then
COVERAGE=$(echo "$OLD_ENTRY" | jq -r '.coverage')
else
COVERAGE=null
fi
fi
echo " → $REPO coverage: $COVERAGE% (status: $STATUS)"
# Create result JSON
cd ..
jq -n \
--arg repo "$REPO" \
--argjson coverage "$COVERAGE" \
--arg status "$STATUS" \
'{repo: $repo, coverage: $coverage, status: $status}' > result.json
- name: Upload coverage result
uses: actions/upload-artifact@v4
with:
name: coverage-result-${{ strategy.job-index }}
path: |
result.json
coverage-html/
retention-days: 1
aggregate:
needs: [prepare, coverage]
runs-on: ubuntu-latest
if: always()
permissions:
contents: write
steps:
- name: Checkout this repo (dashboard)
uses: actions/checkout@v4
- name: Install jq
run: sudo apt-get update && sudo apt-get install -y jq
- name: Checkout gh-pages
uses: actions/checkout@v4
with:
ref: gh-pages
path: gh-pages
- name: Download all coverage results
uses: actions/download-artifact@v4
with:
path: artifacts
pattern: coverage-result-*
- name: Aggregate results
run: |
echo "Aggregating coverage results..."
echo "[" > _temp_coverage.json
FIRST=true
for dir in artifacts/coverage-result-*/; do
if [ -f "$dir/result.json" ]; then
if [ "$FIRST" = true ]; then
FIRST=false
else
echo "," >> _temp_coverage.json
fi
cat "$dir/result.json" >> _temp_coverage.json
fi
done
echo "]" >> _temp_coverage.json
RUN_URL="${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}"
jq -n \
--arg run_url "$RUN_URL" \
--argjson data "$(cat _temp_coverage.json)" \
'{run_url: $run_url, data: $data}' > coverage.json
# Copy HTML reports to gh-pages
for dir in artifacts/coverage-result-*/; do
if [ -f "$dir/result.json" ]; then
REPO=$(jq -r '.repo' "$dir/result.json")
if [ -f "$dir/coverage-html/index.html" ]; then
mkdir -p "gh-pages/coverage/$REPO"
cp "$dir/coverage-html/index.html" "gh-pages/coverage/$REPO/index.html"
fi
fi
done
- name: Upload coverage artifacts (PR only)
if: github.event_name == 'pull_request'
uses: actions/upload-artifact@v4
with:
name: coverage-reports
path: |
coverage.json
gh-pages/coverage/
retention-days: 30
- name: Commit and push updated coverage.json
if: github.event_name != 'pull_request'
run: |
cp coverage.json gh-pages/coverage.json
cd gh-pages
git config user.name "github-actions"
git config user.email "github-actions@github.com"
git add coverage.json coverage/
if git diff --cached --quiet; then
echo "No changes to commit"
else
git commit -m "Update coverage data on $(date --utc)"
git push origin gh-pages
fi