Report coverage in pull requests #1
Workflow file for this run
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: Coverage Check | |
| "on": | |
| pull_request: | |
| branches: | |
| - '*' | |
| workflow_dispatch: {} | |
| jobs: | |
| coverage: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v3 | |
| - name: Install Go | |
| uses: actions/setup-go@v4 | |
| with: | |
| go-version: '1.20' | |
| - name: Run tests and calculate coverage | |
| run: | | |
| go test -cover ./... > coverage.txt | |
| CURRENT_COVERAGE=$(grep -oP '\d+\.\d+%' coverage.txt | tail -1 | tr -d '%') | |
| echo "CURRENT_COVERAGE=$CURRENT_COVERAGE" >> $GITHUB_ENV | |
| if [ -f previous_coverage.txt ]; then | |
| PREVIOUS_COVERAGE=$(cat previous_coverage.txt) | |
| else | |
| PREVIOUS_COVERAGE=0 | |
| fi | |
| echo "PREVIOUS_COVERAGE=$PREVIOUS_COVERAGE" >> $GITHUB_ENV | |
| CHANGE=$(echo "$CURRENT_COVERAGE - $PREVIOUS_COVERAGE" | bc) | |
| echo "CHANGE=$CHANGE" >> $GITHUB_ENV | |
| echo "$CURRENT_COVERAGE" > previous_coverage.txt | |
| - name: Post coverage comment | |
| uses: actions/github-script@v7 | |
| with: | |
| script: | | |
| const currentCoverage = process.env.CURRENT_COVERAGE || 'unknown'; | |
| const previousCoverage = process.env.PREVIOUS_COVERAGE || 'unknown'; | |
| const change = process.env.CHANGE || 'unknown'; | |
| const commentBody = `Coverage: **${currentCoverage}%** (${change >= 0 ? '+' : ''}${change}%)`; | |
| await github.rest.issues.createComment({ | |
| issue_number: context.issue.number, | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| body: commentBody | |
| }) |