Update README.md #47
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
| name: Test | |
| on: | |
| push: | |
| branches: [main] | |
| pull_request: | |
| branches: [main] | |
| jobs: | |
| test-and-build: | |
| runs-on: ubuntu-latest | |
| permissions: | |
| contents: read | |
| pull-requests: write | |
| steps: | |
| - uses: actions/checkout@v4 | |
| - name: Set up Go | |
| uses: actions/setup-go@v5 | |
| with: | |
| go-version: "1.23" | |
| - name: Install dependencies | |
| run: go mod download | |
| - name: Run golangci-lint | |
| uses: golangci/golangci-lint-action@v6 | |
| with: | |
| version: latest | |
| - name: Run tests with coverage | |
| run: | | |
| go test -v -race -coverprofile=coverage.out -covermode=atomic ./... | |
| go tool cover -html=coverage.out -o coverage.html | |
| - name: Calculate coverage | |
| id: coverage | |
| run: | | |
| COVERAGE=$(go tool cover -func=coverage.out | grep total | awk '{print $3}') | |
| echo "coverage=$COVERAGE" >> $GITHUB_OUTPUT | |
| echo "Coverage: $COVERAGE" | |
| - name: Get main branch coverage | |
| if: github.event_name == 'pull_request' | |
| id: main-coverage | |
| run: | | |
| # Store current working directory | |
| CURRENT_DIR=$(pwd) | |
| # Fetch main branch | |
| git fetch origin main:main | |
| # Checkout main branch in a temporary directory | |
| git worktree add /tmp/main-branch main | |
| # Run tests on main branch to get coverage | |
| cd /tmp/main-branch | |
| go test -coverprofile=main-coverage.out -covermode=atomic ./... > /dev/null 2>&1 || echo "Main branch tests failed" | |
| if [ -f main-coverage.out ]; then | |
| MAIN_COVERAGE=$(go tool cover -func=main-coverage.out | grep total | awk '{print $3}' || echo "0.0%") | |
| echo "main-coverage=$MAIN_COVERAGE" >> $GITHUB_OUTPUT | |
| echo "Main branch coverage: $MAIN_COVERAGE" | |
| # Copy main coverage file back to current directory | |
| cp main-coverage.out "$CURRENT_DIR/main-coverage.out" | |
| else | |
| echo "main-coverage=0.0%" >> $GITHUB_OUTPUT | |
| echo "Could not get main branch coverage" | |
| fi | |
| # Return to original directory | |
| cd "$CURRENT_DIR" | |
| # Clean up worktree (force removal to handle modified files) | |
| git worktree remove --force /tmp/main-branch || rm -rf /tmp/main-branch | |
| - name: Generate coverage summary | |
| id: coverage-summary | |
| run: | | |
| echo "## 📊 Test Coverage Report" > coverage-summary.md | |
| echo "" >> coverage-summary.md | |
| # Current coverage | |
| CURRENT_COV="${{ steps.coverage.outputs.coverage }}" | |
| echo "**Current Coverage:** \`$CURRENT_COV\`" >> coverage-summary.md | |
| # Compare with main if this is a PR | |
| if [ "${{ github.event_name }}" = "pull_request" ] && [ "${{ steps.main-coverage.outputs.main-coverage }}" != "" ]; then | |
| MAIN_COV="${{ steps.main-coverage.outputs.main-coverage }}" | |
| echo "**Main Branch Coverage:** \`$MAIN_COV\`" >> coverage-summary.md | |
| echo "" >> coverage-summary.md | |
| # Calculate difference | |
| CURRENT_NUM=$(echo $CURRENT_COV | sed 's/%//') | |
| MAIN_NUM=$(echo $MAIN_COV | sed 's/%//') | |
| if [ "$CURRENT_NUM" != "" ] && [ "$MAIN_NUM" != "" ]; then | |
| DIFF=$(echo "$CURRENT_NUM - $MAIN_NUM" | bc -l 2>/dev/null || echo "0") | |
| if [ "$(echo "$DIFF > 0" | bc -l 2>/dev/null)" = "1" ]; then | |
| echo "**Coverage Change:** 📈 +${DIFF}% (improved)" >> coverage-summary.md | |
| elif [ "$(echo "$DIFF < 0" | bc -l 2>/dev/null)" = "1" ]; then | |
| DIFF_ABS=$(echo "$DIFF * -1" | bc -l 2>/dev/null || echo "${DIFF#-}") | |
| echo "**Coverage Change:** 📉 -${DIFF_ABS}% (decreased)" >> coverage-summary.md | |
| else | |
| echo "**Coverage Change:** ✅ No change" >> coverage-summary.md | |
| fi | |
| fi | |
| fi | |
| echo "" >> coverage-summary.md | |
| echo "### Coverage by Package" >> coverage-summary.md | |
| echo "\`\`\`" >> coverage-summary.md | |
| go tool cover -func=coverage.out >> coverage-summary.md | |
| echo "\`\`\`" >> coverage-summary.md | |
| echo "" >> coverage-summary.md | |
| echo "- 🧪 All tests passed" >> coverage-summary.md | |
| echo "- 📈 Full coverage report available in workflow artifacts" >> coverage-summary.md | |
| echo "" >> coverage-summary.md | |
| echo "_Generated by GitHub Actions_" >> coverage-summary.md | |
| - name: Comment PR with coverage | |
| if: github.event_name == 'pull_request' | |
| uses: actions/github-script@v7 | |
| with: | |
| script: | | |
| const fs = require('fs'); | |
| const coverageSummary = fs.readFileSync('coverage-summary.md', 'utf8'); | |
| // Look for existing coverage comment | |
| const comments = await github.rest.issues.listComments({ | |
| issue_number: context.issue.number, | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| }); | |
| const botComment = comments.data.find(comment => | |
| comment.user.type === 'Bot' && | |
| comment.body.includes('📊 Test Coverage Report') | |
| ); | |
| if (botComment) { | |
| // Update existing comment | |
| await github.rest.issues.updateComment({ | |
| comment_id: botComment.id, | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| body: coverageSummary | |
| }); | |
| } else { | |
| // Create new comment | |
| await github.rest.issues.createComment({ | |
| issue_number: context.issue.number, | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| body: coverageSummary | |
| }); | |
| } | |
| - name: Upload coverage artifact | |
| uses: actions/upload-artifact@v4 | |
| with: | |
| name: coverage-report | |
| path: | | |
| coverage.out | |
| coverage.html | |
| - name: Build | |
| run: go build -v ./... |