Skip to content

Commit 7a990bf

Browse files
authored
feat: add test coverage comment (#802)
* generate test coverage in CI * add test coverage as comment in PR
1 parent 793ae0f commit 7a990bf

File tree

2 files changed

+34
-3
lines changed

2 files changed

+34
-3
lines changed

.github/workflows/ci.yaml

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
name: CI
22

3-
on: [pull_request, workflow_dispatch]
3+
on:
4+
pull_request:
5+
workflow_dispatch:
6+
push:
7+
branches:
8+
- main
9+
10+
env:
11+
CODE_COVERAGE_FILE_NAME: "coverage.out" # must be the same as in Makefile
12+
CODE_COVERAGE_ARTIFACT_NAME: "code-coverage"
413

514
jobs:
615
main:
@@ -26,8 +35,15 @@ jobs:
2635
- name: Test
2736
run: make test
2837

38+
- name: Archive code coverage results
39+
uses: actions/upload-artifact@v4
40+
with:
41+
name: ${{ env.CODE_COVERAGE_ARTIFACT_NAME }}
42+
path: ${{ env.CODE_COVERAGE_FILE_NAME }}
43+
2944
config:
3045
name: Check GoReleaser config
46+
if: github.event_name == 'pull_request'
3147
runs-on: ubuntu-latest
3248
steps:
3349
- name: Checkout
@@ -36,4 +52,19 @@ jobs:
3652
- name: Check GoReleaser
3753
uses: goreleaser/goreleaser-action@v6
3854
with:
39-
args: check
55+
args: check
56+
57+
code_coverage:
58+
name: "Code coverage report"
59+
if: github.event_name == 'pull_request' # Do not run when workflow is triggered by push to main branch
60+
runs-on: ubuntu-latest
61+
needs: main
62+
permissions:
63+
contents: read
64+
actions: read # to download code coverage results from "main" job
65+
pull-requests: write # write permission needed to comment on PR
66+
steps:
67+
- uses: fgrosse/[email protected]
68+
with:
69+
coverage-artifact-name: ${{ env.CODE_COVERAGE_ARTIFACT_NAME }}
70+
coverage-file-name: ${{ env.CODE_COVERAGE_FILE_NAME }}

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ lint: lint-golangci-lint lint-yamllint
2525
# Test
2626
test:
2727
@echo ">> Running tests for the CLI application"
28-
@go test ./... -count=1
28+
@go test ./... -count=1 -coverprofile=coverage.out
2929

3030
# Test coverage
3131
coverage:

0 commit comments

Comments
 (0)