ci: add coverage report #3249
Workflow file for this run
This file contains 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: | |
pull_request: | |
push: | |
branches: | |
- main | |
jobs: | |
tests: | |
runs-on: ubuntu-latest | |
timeout-minutes: 10 | |
permissions: | |
# permissions are required for the fgrosse/go-coverage-report action | |
contents: read | |
actions: read | |
pull-requests: write | |
strategy: | |
fail-fast: false | |
steps: | |
- uses: actions/checkout@v4 | |
- uses: actions/setup-go@v5 | |
with: | |
go-version-file: 'go.mod' | |
- uses: tj-actions/changed-files@c65cd883420fd2eb864698a825fc4162dd94482c # v44.5.7 | |
id: changed-files | |
with: | |
files: | | |
**/**.go | |
"!test/" | |
go.mod | |
go.sum | |
# run tests on PR only if there are code changes and always on main | |
- name: Run Go Tests | |
if: ${{ steps.changed-files.outputs.any_changed == 'true' && github.event_name == 'pull_request' || github.ref == 'refs/heads/main'}} | |
run: | | |
make test | |
# upload raw coverage report always if tests ran | |
- name: Archive code coverage results | |
uses: actions/upload-artifact@v4 | |
if: ${{ steps.changed-files.outputs.any_changed == 'true' && github.event_name == 'pull_request' || github.ref == 'refs/heads/main'}} | |
with: | |
name: code-coverage | |
path: cover.out | |
# upload html coverage report only on PR and if tests ran | |
- name: Upload html coverage report | |
uses: actions/upload-artifact@v4 | |
if: ${{ steps.changed-files.outputs.any_changed == 'true' && github.event_name == 'pull_request'}} | |
id: html-artifact | |
with: | |
name: code-coverage-html | |
path: cover.html | |
# create comment with coverage html report url on PR and if tests ran | |
# create comment with coverage changes digest on PR and if tests ran | |
- name: "Code coverage report" | |
uses: fgrosse/[email protected] | |
if: ${{ steps.changed-files.outputs.any_changed == 'true' && github.event_name == 'pull_request'}} | |
with: | |
coverage-artifact-name: "code-coverage" | |
coverage-file-name: "cover.out" | |
- uses: actions/github-script@v6 | |
name: Create comment with html report | |
if: ${{ steps.changed-files.outputs.any_changed == 'true' && github.event_name == 'pull_request'}} | |
with: | |
script: | | |
const commentIdentifier = "Coverage Δ"; // the same marker as in the fgrosse/go-coverage-report | |
const htmlCoverageTitle = "\n ### HTML Report" | |
// Get all comments on the pull request | |
const { data: comments } = await github.rest.issues.listComments({ | |
issue_number: context.issue.number, | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
}); | |
// Find the comment that contains the identifier | |
const existingComment = comments.find(comment => comment.body.includes(commentIdentifier)); | |
const htmlCoverageTitleIdx = existingComment.body.indexOf(htmlCoverageTitle) | |
var originalCommentBody = existingComment.body | |
if (htmlCoverageTitleIdx != -1) { | |
originalCommentBody = existingComment.body.slice(0, htmlCoverageTitleIdx) | |
} | |
const newBody = `${originalCommentBody}${htmlCoverageTitle} \n ${{ steps.html-artifact.outputs.artifact-url }}`; | |
if (existingComment) { | |
// Update the existing comment | |
await github.rest.issues.updateComment({ | |
comment_id: existingComment.id, | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
body: newBody, | |
}); | |
} else { | |
// Create a new comment | |
await github.rest.issues.createComment({ | |
issue_number: context.issue.number, | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
body: newBody, | |
}); | |
} |