-
Notifications
You must be signed in to change notification settings - Fork 96
feat(ci): Add PR coverage check workflow #8667
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,264 @@ | ||
| name: PR Coverage Check | ||
|
|
||
| on: | ||
| pull_request: | ||
| branches: [main, dev/*, hotfix/*] | ||
|
|
||
| jobs: | ||
| coverage: | ||
| runs-on: ubuntu-latest | ||
| permissions: | ||
| contents: read | ||
| pull-requests: write | ||
|
|
||
| steps: | ||
| - name: Checkout PR branch | ||
| uses: actions/checkout@v4 | ||
| with: | ||
| fetch-depth: 0 | ||
|
|
||
| - name: Cache turbo build setup | ||
| uses: actions/cache@v4 | ||
| with: | ||
| path: .turbo | ||
| key: ${{ runner.os }}-turbo-${{ github.sha }} | ||
| restore-keys: | | ||
| ${{ runner.os }}-turbo- | ||
|
|
||
| - name: Set up Node.js | ||
| uses: actions/setup-node@v4 | ||
| with: | ||
| node-version: 20.x | ||
|
|
||
| - name: Install pnpm | ||
| uses: pnpm/action-setup@v3 | ||
| with: | ||
| version: 9.1.3 | ||
| run_install: | | ||
| - recursive: true | ||
| args: [--frozen-lockfile, --strict-peer-dependencies] | ||
|
|
||
| - name: Build | ||
| run: pnpm turbo run build --cache-dir=.turbo | ||
|
|
||
| - name: Run tests with coverage | ||
| run: pnpm turbo run test:lib --cache-dir=.turbo | ||
|
|
||
| - name: Merge coverage reports | ||
| run: | | ||
| mkdir -p coverage | ||
| # Find all lcov.info files and merge them | ||
| find . -path ./node_modules -prune -o -name 'lcov.info' -print 2>/dev/null | head -20 > lcov-files.txt | ||
takyyon marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if [ -s lcov-files.txt ]; then | ||
| npx lcov-result-merger './libs/**/coverage/lcov.info' './apps/**/coverage/lcov.info' ./coverage/lcov.info || true | ||
hartra344 marked this conversation as resolved.
Show resolved
Hide resolved
hartra344 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| fi | ||
|
|
||
| - name: Get changed files | ||
| id: changed-files | ||
| uses: tj-actions/changed-files@v44 | ||
| with: | ||
| files: | | ||
| **/*.ts | ||
| **/*.tsx | ||
| files_ignore: | | ||
| # Test files | ||
| **/*.test.ts | ||
| **/*.test.tsx | ||
| **/*.spec.ts | ||
| **/*.spec.tsx | ||
| **/__test__/** | ||
| **/__tests__/** | ||
| **/__mocks__/** | ||
| **/test-setup.ts | ||
| **/test-globals.ts | ||
| **/setup.ts | ||
| # Type definitions and models | ||
| **/*.d.ts | ||
| **/models/** | ||
| **/types/** | ||
| **/interfaces/** | ||
| **/*Types.ts | ||
| **/*Types.tsx | ||
| **/*Model.ts | ||
| **/*Models.ts | ||
| **/*Interface.ts | ||
| **/*Interfaces.ts | ||
| **/types.ts | ||
| **/types.tsx | ||
| **/constants.ts | ||
| **/constants/** | ||
takyyon marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| # Config files | ||
| **/*.config.ts | ||
| **/*.config.tsx | ||
| **/vite.config.* | ||
| **/vitest.config.* | ||
| **/tsconfig.* | ||
| # Index/barrel files (just re-exports) | ||
| **/index.ts | ||
| **/index.tsx | ||
| # Node modules and build output | ||
| **/node_modules/** | ||
| **/dist/** | ||
| **/build/** | ||
|
|
||
| - name: Check coverage on changed files | ||
| id: coverage-check | ||
| run: | | ||
| echo "## 📊 Coverage Report for Changed Files" >> $GITHUB_STEP_SUMMARY | ||
| echo "" >> $GITHUB_STEP_SUMMARY | ||
|
|
||
| # Get the list of changed source files | ||
| CHANGED_FILES="${{ steps.changed-files.outputs.all_changed_files }}" | ||
|
|
||
| if [ -z "$CHANGED_FILES" ]; then | ||
| echo "No source files changed in this PR." >> $GITHUB_STEP_SUMMARY | ||
| echo "coverage_status=skip" >> $GITHUB_OUTPUT | ||
| exit 0 | ||
| fi | ||
|
|
||
| echo "### Changed Files:" >> $GITHUB_STEP_SUMMARY | ||
| for file in $CHANGED_FILES; do | ||
| echo "- \`$file\`" >> $GITHUB_STEP_SUMMARY | ||
| done | ||
| echo "" >> $GITHUB_STEP_SUMMARY | ||
|
|
||
| # Check if coverage file exists | ||
| if [ ! -f "./coverage/lcov.info" ]; then | ||
| echo "⚠️ No merged coverage report found. Checking individual coverage files..." >> $GITHUB_STEP_SUMMARY | ||
|
|
||
| # Look for individual coverage files | ||
| COVERAGE_FILES=$(find . -path ./node_modules -prune -o -name 'lcov.info' -print 2>/dev/null | grep -v node_modules || true) | ||
takyyon marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| if [ -z "$COVERAGE_FILES" ]; then | ||
| echo "" >> $GITHUB_STEP_SUMMARY | ||
| echo "❌ **No coverage data found.** Please ensure tests generate coverage reports." >> $GITHUB_STEP_SUMMARY | ||
| echo "coverage_status=no_data" >> $GITHUB_OUTPUT | ||
| exit 0 | ||
| fi | ||
| fi | ||
|
|
||
| # Parse coverage and check changed files | ||
| UNCOVERED_FILES="" | ||
| COVERED_FILES="" | ||
| PARTIAL_FILES="" | ||
|
|
||
| for file in $CHANGED_FILES; do | ||
takyyon marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| # Skip if file doesn't exist (might have been deleted) | ||
| if [ ! -f "$file" ]; then | ||
| continue | ||
| fi | ||
|
|
||
| # Find the corresponding coverage file for this source file | ||
| # Look in the package's coverage directory | ||
| PKG_DIR=$(echo "$file" | sed 's|/src/.*||') | ||
takyyon marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| LCOV_FILE="$PKG_DIR/coverage/lcov.info" | ||
|
|
||
| if [ -f "$LCOV_FILE" ]; then | ||
| # Check if this file is mentioned in the coverage report | ||
| if grep -q "SF:.*$(basename $file)" "$LCOV_FILE" 2>/dev/null; then | ||
takyyon marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| # Get line coverage for this file | ||
| FILE_SECTION=$(awk "/SF:.*$(basename $file)/,/end_of_record/" "$LCOV_FILE" 2>/dev/null || true) | ||
takyyon marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| if [ -n "$FILE_SECTION" ]; then | ||
| LINES_FOUND=$(echo "$FILE_SECTION" | grep "^LF:" | cut -d: -f2 || echo "0") | ||
| LINES_HIT=$(echo "$FILE_SECTION" | grep "^LH:" | cut -d: -f2 || echo "0") | ||
|
|
||
| if [ "$LINES_FOUND" -gt 0 ] 2>/dev/null; then | ||
| COVERAGE_PCT=$((LINES_HIT * 100 / LINES_FOUND)) | ||
hartra344 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if [ "$COVERAGE_PCT" -ge 80 ]; then | ||
| COVERED_FILES="$COVERED_FILES\n✅ \`$file\` - ${COVERAGE_PCT}% covered" | ||
hartra344 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| elif [ "$COVERAGE_PCT" -gt 0 ]; then | ||
| PARTIAL_FILES="$PARTIAL_FILES\n⚠️ \`$file\` - ${COVERAGE_PCT}% covered (needs improvement)" | ||
| else | ||
| UNCOVERED_FILES="$UNCOVERED_FILES\n❌ \`$file\` - 0% covered" | ||
| fi | ||
| fi | ||
| fi | ||
| else | ||
| UNCOVERED_FILES="$UNCOVERED_FILES\n❌ \`$file\` - Not covered by tests" | ||
takyyon marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| fi | ||
| else | ||
| # No coverage file found for this package - might be acceptable | ||
| echo "ℹ️ No coverage data for package: $PKG_DIR" >> $GITHUB_STEP_SUMMARY | ||
| fi | ||
| done | ||
|
|
||
| echo "### Coverage Results:" >> $GITHUB_STEP_SUMMARY | ||
| echo "" >> $GITHUB_STEP_SUMMARY | ||
|
|
||
| if [ -n "$COVERED_FILES" ]; then | ||
| echo "#### ✅ Well Covered:" >> $GITHUB_STEP_SUMMARY | ||
| echo -e "$COVERED_FILES" >> $GITHUB_STEP_SUMMARY | ||
| echo "" >> $GITHUB_STEP_SUMMARY | ||
| fi | ||
|
|
||
| if [ -n "$PARTIAL_FILES" ]; then | ||
| echo "#### ⚠️ Partially Covered (< 80%):" >> $GITHUB_STEP_SUMMARY | ||
| echo -e "$PARTIAL_FILES" >> $GITHUB_STEP_SUMMARY | ||
| echo "" >> $GITHUB_STEP_SUMMARY | ||
| fi | ||
|
|
||
| if [ -n "$UNCOVERED_FILES" ]; then | ||
| echo "#### ❌ Not Covered:" >> $GITHUB_STEP_SUMMARY | ||
| echo -e "$UNCOVERED_FILES" >> $GITHUB_STEP_SUMMARY | ||
| echo "" >> $GITHUB_STEP_SUMMARY | ||
| echo "" >> $GITHUB_STEP_SUMMARY | ||
| echo "**Please add tests for the uncovered files before merging.**" >> $GITHUB_STEP_SUMMARY | ||
| echo "coverage_status=fail" >> $GITHUB_OUTPUT | ||
| else | ||
| echo "" >> $GITHUB_STEP_SUMMARY | ||
| echo "🎉 **All changed files have adequate test coverage!**" >> $GITHUB_STEP_SUMMARY | ||
| echo "coverage_status=pass" >> $GITHUB_OUTPUT | ||
| fi | ||
|
|
||
| - name: Post coverage comment on PR | ||
| if: always() | ||
| uses: actions/github-script@v7 | ||
| with: | ||
| script: | | ||
| const fs = require('fs'); | ||
|
|
||
| // Read the step summary | ||
| const summaryPath = process.env.GITHUB_STEP_SUMMARY; | ||
| let summary = ''; | ||
| try { | ||
| summary = fs.readFileSync(summaryPath, 'utf8'); | ||
takyyon marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } catch (e) { | ||
| summary = '📊 Coverage check completed. See workflow run for details.'; | ||
| } | ||
|
|
||
| // Find existing comment | ||
| const { data: comments } = await github.rest.issues.listComments({ | ||
| owner: context.repo.owner, | ||
| repo: context.repo.repo, | ||
| issue_number: context.issue.number, | ||
| }); | ||
|
|
||
| const botComment = comments.find(comment => | ||
| comment.user.type === 'Bot' && | ||
| comment.body.includes('📊 Coverage Report for Changed Files') | ||
| ); | ||
|
|
||
| const commentBody = summary || '📊 Coverage check completed. See workflow run for details.'; | ||
|
|
||
| if (botComment) { | ||
| await github.rest.issues.updateComment({ | ||
| owner: context.repo.owner, | ||
| repo: context.repo.repo, | ||
| comment_id: botComment.id, | ||
| body: commentBody | ||
| }); | ||
| } else { | ||
| await github.rest.issues.createComment({ | ||
| owner: context.repo.owner, | ||
| repo: context.repo.repo, | ||
| issue_number: context.issue.number, | ||
| body: commentBody | ||
| }); | ||
| } | ||
|
|
||
| - name: Fail if coverage is insufficient | ||
| if: steps.coverage-check.outputs.coverage_status == 'fail' | ||
| run: | | ||
| echo "❌ Some changed files lack test coverage. Please add tests before merging." | ||
| exit 1 | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.