Report coverage in pull requests #4
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 | |
| services: | |
| dind: | |
| image: docker:23.0-rc-dind-rootless | |
| ports: | |
| - 2375:2375 | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v4 | |
| - name: Set up Go | |
| uses: actions/setup-go@v5 | |
| with: | |
| go-version: 1.24.2 | |
| - name: Run tests and calculate coverage | |
| run: | | |
| POSTGRES_CONTAINER=1 VERNEMQ_CONTAINER=1 go test -v -coverpkg=./... -coverprofile=profile.cov ./... | |
| CURRENT_COVERAGE=$(go tool cover -func profile.cov | grep total | awk '{print $3}' | 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: Delete old coverage comments | |
| uses: actions/github-script@v7 | |
| with: | |
| script: | | |
| const { data: comments } = await github.rest.issues.listComments({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| issue_number: context.issue.number, | |
| }); | |
| const coverageCommentTag = '<!-- coverage-comment -->'; | |
| for (const comment of comments) { | |
| if (comment.body.includes(coverageCommentTag)) { | |
| await github.rest.issues.deleteComment({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| comment_id: comment.id, | |
| }); | |
| } | |
| } | |
| - 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-comment --> | |
| 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, | |
| }); |