Skip to content

Commit f5cc607

Browse files
authored
Merge pull request #219 from LerianStudio/fix/vitest3-coverage-reporter-flag
fix(frontend-pr-analysis): detect vitest version for coverage reporter flag
2 parents 8439c02 + 21a8667 commit f5cc607

1 file changed

Lines changed: 77 additions & 49 deletions

File tree

.github/workflows/frontend-pr-analysis.yml

Lines changed: 77 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -101,28 +101,32 @@ jobs:
101101
has_changes: ${{ steps.set-matrix.outputs.has_changes }}
102102
steps:
103103
- name: Checkout code
104-
uses: actions/checkout@v6
104+
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6
105105
with:
106106
fetch-depth: 0
107107

108108
- name: Get changed files
109109
id: changed
110110
shell: bash
111+
env:
112+
EVENT_NAME: ${{ github.event_name }}
113+
PR_BASE_SHA: ${{ github.event.pull_request.base.sha }}
114+
PR_HEAD_SHA: ${{ github.event.pull_request.head.sha }}
115+
BASE_REF: ${{ github.base_ref }}
116+
EVENT_BEFORE: ${{ github.event.before }}
117+
CURRENT_SHA: ${{ github.sha }}
111118
run: |
112-
if [[ "${{ github.event_name }}" == "pull_request" ]]; then
113-
BASE_SHA="${{ github.event.pull_request.base.sha }}"
114-
HEAD_SHA="${{ github.event.pull_request.head.sha }}"
115-
git fetch origin $BASE_SHA --depth=1 2>/dev/null || true
116-
FILES=$(git diff --name-only $BASE_SHA $HEAD_SHA 2>/dev/null || git diff --name-only origin/${{ github.base_ref }}...HEAD)
117-
elif [[ "${{ github.event.before }}" == "0000000000000000000000000000000000000000" ]] || [[ -z "${{ github.event.before }}" ]]; then
118-
PREV_COMMIT=$(git rev-parse HEAD^)
119-
if [[ $? -eq 0 ]]; then
120-
FILES=$(git diff --name-only $PREV_COMMIT HEAD)
119+
if [[ "$EVENT_NAME" == "pull_request" ]]; then
120+
git fetch origin "$PR_BASE_SHA" --depth=1 2>/dev/null || true
121+
FILES=$(git diff --name-only "$PR_BASE_SHA" "$PR_HEAD_SHA" 2>/dev/null || git diff --name-only "origin/${BASE_REF}...HEAD")
122+
elif [[ "$EVENT_BEFORE" == "0000000000000000000000000000000000000000" ]] || [[ -z "$EVENT_BEFORE" ]]; then
123+
if PREV_COMMIT=$(git rev-parse HEAD^); then
124+
FILES=$(git diff --name-only "$PREV_COMMIT" HEAD)
121125
else
122126
FILES=$(git ls-tree -r --name-only HEAD)
123127
fi
124128
else
125-
FILES=$(git diff --name-only ${{ github.event.before }} ${{ github.sha }})
129+
FILES=$(git diff --name-only "$EVENT_BEFORE" "$CURRENT_SHA")
126130
fi
127131
printf "files<<EOF\n%s\nEOF\n" "$FILES" >> "$GITHUB_OUTPUT"
128132
@@ -225,10 +229,10 @@ jobs:
225229
app: ${{ fromJson(needs.detect-changes.outputs.matrix) }}
226230
steps:
227231
- name: Checkout code
228-
uses: actions/checkout@v6
232+
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6
229233

230234
- name: Setup Node.js
231-
uses: actions/setup-node@v6
235+
uses: actions/setup-node@53b83947a5a98c8d113130e565377fae1a50d02f # v6
232236
with:
233237
node-version: ${{ inputs.node_version }}
234238
cache: ${{ inputs.package_manager }}
@@ -266,10 +270,10 @@ jobs:
266270
app: ${{ fromJson(needs.detect-changes.outputs.matrix) }}
267271
steps:
268272
- name: Checkout code
269-
uses: actions/checkout@v6
273+
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6
270274

271275
- name: Setup Node.js
272-
uses: actions/setup-node@v6
276+
uses: actions/setup-node@53b83947a5a98c8d113130e565377fae1a50d02f # v6
273277
with:
274278
node-version: ${{ inputs.node_version }}
275279
cache: ${{ inputs.package_manager }}
@@ -302,10 +306,10 @@ jobs:
302306
app: ${{ fromJson(needs.detect-changes.outputs.matrix) }}
303307
steps:
304308
- name: Checkout code
305-
uses: actions/checkout@v6
309+
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6
306310

307311
- name: Setup Node.js
308-
uses: actions/setup-node@v6
312+
uses: actions/setup-node@53b83947a5a98c8d113130e565377fae1a50d02f # v6
309313
with:
310314
node-version: ${{ inputs.node_version }}
311315
cache: ${{ inputs.package_manager }}
@@ -343,10 +347,10 @@ jobs:
343347
app: ${{ fromJson(needs.detect-changes.outputs.matrix) }}
344348
steps:
345349
- name: Checkout code
346-
uses: actions/checkout@v6
350+
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6
347351

348352
- name: Setup Node.js
349-
uses: actions/setup-node@v6
353+
uses: actions/setup-node@53b83947a5a98c8d113130e565377fae1a50d02f # v6
350354
with:
351355
node-version: ${{ inputs.node_version }}
352356
cache: ${{ inputs.package_manager }}
@@ -364,14 +368,24 @@ jobs:
364368
- name: Run tests with coverage
365369
working-directory: ${{ matrix.app.working_dir }}
366370
run: |
371+
# Vitest 3+ removed --coverageReporters CLI flag (use --coverage.reporter instead)
372+
# Jest and Vitest 2 still support --coverageReporters
373+
VITEST_MAJOR=$(node -e "try{console.log(require('vitest/package.json').version.split('.')[0])}catch{console.log('0')}" 2>/dev/null)
374+
375+
if [[ "$VITEST_MAJOR" -ge 3 ]]; then
376+
COVERAGE_FLAGS=(--coverage.reporter=text --coverage.reporter=json-summary --coverage.reporter=lcov)
377+
else
378+
COVERAGE_FLAGS=(--coverageReporters=text --coverageReporters=json-summary --coverageReporters=lcov)
379+
fi
380+
367381
case "${{ inputs.package_manager }}" in
368-
yarn) yarn test --coverage --coverageReporters=text --coverageReporters=json-summary --coverageReporters=lcov ;;
369-
pnpm) pnpm test --coverage --coverageReporters=text --coverageReporters=json-summary --coverageReporters=lcov ;;
370-
*) npm test -- --coverage --coverageReporters=text --coverageReporters=json-summary --coverageReporters=lcov ;;
382+
yarn) yarn test --coverage "${COVERAGE_FLAGS[@]}" ;;
383+
pnpm) pnpm test --coverage "${COVERAGE_FLAGS[@]}" ;;
384+
*) npm test -- --coverage "${COVERAGE_FLAGS[@]}" ;;
371385
esac
372386
373387
- name: Upload coverage artifact
374-
uses: actions/upload-artifact@v7
388+
uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7
375389
with:
376390
name: coverage-${{ matrix.app.name }}
377391
path: |
@@ -392,10 +406,10 @@ jobs:
392406
app: ${{ fromJson(needs.detect-changes.outputs.matrix) }}
393407
steps:
394408
- name: Checkout code
395-
uses: actions/checkout@v6
409+
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6
396410

397411
- name: Download coverage artifact
398-
uses: actions/download-artifact@v8
412+
uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8
399413
with:
400414
name: coverage-${{ matrix.app.name }}
401415
path: ${{ matrix.app.working_dir }}/coverage
@@ -409,23 +423,27 @@ jobs:
409423
STATEMENTS=$(jq '.total.statements.pct' coverage/coverage-summary.json)
410424
BRANCHES=$(jq '.total.branches.pct' coverage/coverage-summary.json)
411425
FUNCTIONS=$(jq '.total.functions.pct' coverage/coverage-summary.json)
412-
413-
echo "coverage=$COVERAGE" >> $GITHUB_OUTPUT
414-
echo "statements=$STATEMENTS" >> $GITHUB_OUTPUT
415-
echo "branches=$BRANCHES" >> $GITHUB_OUTPUT
416-
echo "functions=$FUNCTIONS" >> $GITHUB_OUTPUT
426+
427+
{
428+
echo "coverage=$COVERAGE"
429+
echo "statements=$STATEMENTS"
430+
echo "branches=$BRANCHES"
431+
echo "functions=$FUNCTIONS"
432+
} >> "$GITHUB_OUTPUT"
417433
echo "Total line coverage: $COVERAGE%"
418434
else
419-
echo "coverage=0" >> $GITHUB_OUTPUT
420-
echo "statements=0" >> $GITHUB_OUTPUT
421-
echo "branches=0" >> $GITHUB_OUTPUT
422-
echo "functions=0" >> $GITHUB_OUTPUT
435+
{
436+
echo "coverage=0"
437+
echo "statements=0"
438+
echo "branches=0"
439+
echo "functions=0"
440+
} >> "$GITHUB_OUTPUT"
423441
echo "No coverage file found"
424442
fi
425443
426444
- name: Post coverage comment
427445
if: github.event_name == 'pull_request'
428-
uses: actions/github-script@v8
446+
uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8
429447
with:
430448
github-token: ${{ secrets.MANAGE_TOKEN || github.token }}
431449
script: |
@@ -478,25 +496,35 @@ jobs:
478496
479497
- name: Check coverage threshold
480498
if: inputs.fail_on_coverage_threshold
499+
env:
500+
COVERAGE: ${{ steps.coverage.outputs.coverage }}
501+
THRESHOLD: ${{ inputs.coverage_threshold }}
481502
run: |
482-
COVERAGE=${{ steps.coverage.outputs.coverage }}
483-
THRESHOLD=${{ inputs.coverage_threshold }}
484503
if (( $(echo "$COVERAGE < $THRESHOLD" | bc -l) )); then
485-
echo "::error::Coverage $COVERAGE% is below threshold $THRESHOLD%"
504+
echo "::error::Coverage ${COVERAGE}% is below threshold ${THRESHOLD}%"
486505
exit 1
487506
fi
488507
489508
- name: Coverage summary
509+
env:
510+
APP_NAME: ${{ matrix.app.name }}
511+
COV_LINES: ${{ steps.coverage.outputs.coverage }}
512+
COV_STATEMENTS: ${{ steps.coverage.outputs.statements }}
513+
COV_BRANCHES: ${{ steps.coverage.outputs.branches }}
514+
COV_FUNCTIONS: ${{ steps.coverage.outputs.functions }}
515+
COV_THRESHOLD: ${{ inputs.coverage_threshold }}
490516
run: |
491-
echo "## Coverage Summary: ${{ matrix.app.name }}" >> $GITHUB_STEP_SUMMARY
492-
echo "" >> $GITHUB_STEP_SUMMARY
493-
echo "| Metric | Value |" >> $GITHUB_STEP_SUMMARY
494-
echo "|--------|-------|" >> $GITHUB_STEP_SUMMARY
495-
echo "| Lines | ${{ steps.coverage.outputs.coverage }}% |" >> $GITHUB_STEP_SUMMARY
496-
echo "| Statements | ${{ steps.coverage.outputs.statements }}% |" >> $GITHUB_STEP_SUMMARY
497-
echo "| Branches | ${{ steps.coverage.outputs.branches }}% |" >> $GITHUB_STEP_SUMMARY
498-
echo "| Functions | ${{ steps.coverage.outputs.functions }}% |" >> $GITHUB_STEP_SUMMARY
499-
echo "| Threshold | ${{ inputs.coverage_threshold }}% |" >> $GITHUB_STEP_SUMMARY
517+
{
518+
echo "## Coverage Summary: ${APP_NAME}"
519+
echo ""
520+
echo "| Metric | Value |"
521+
echo "|--------|-------|"
522+
echo "| Lines | ${COV_LINES}% |"
523+
echo "| Statements | ${COV_STATEMENTS}% |"
524+
echo "| Branches | ${COV_BRANCHES}% |"
525+
echo "| Functions | ${COV_FUNCTIONS}% |"
526+
echo "| Threshold | ${COV_THRESHOLD}% |"
527+
} >> "$GITHUB_STEP_SUMMARY"
500528
501529
# ============================================
502530
# BUILD VERIFICATION
@@ -512,10 +540,10 @@ jobs:
512540
app: ${{ fromJson(needs.detect-changes.outputs.matrix) }}
513541
steps:
514542
- name: Checkout code
515-
uses: actions/checkout@v6
543+
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6
516544

517545
- name: Setup Node.js
518-
uses: actions/setup-node@v6
546+
uses: actions/setup-node@53b83947a5a98c8d113130e565377fae1a50d02f # v6
519547
with:
520548
node-version: ${{ inputs.node_version }}
521549
cache: ${{ inputs.package_manager }}

0 commit comments

Comments
 (0)