Skip to content

Commit

Permalink
Merge pull request #2352 from headlamp-k8s/backend-coverage
Browse files Browse the repository at this point in the history
backend: Add test coverage reports into the PR coverage bot comment
  • Loading branch information
illume authored Sep 25, 2024
2 parents b6be9da + b14a896 commit 63f6229
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
35 changes: 35 additions & 0 deletions .github/workflows/backend-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,25 @@ jobs:
set -x
cd backend
go test ./... -coverprofile=coverage.out -covermode=atomic -coverpkg=./...
go tool cover -html=coverage.out -o backend_coverage.html
testcoverage_full=$(go tool cover -func=coverage.out)
testcoverage=$(go tool cover -func=coverage.out | grep total | grep -Eo '[0-9]+\.[0-9]+')
testcoverage_full_base64=$(echo "$testcoverage_full" | base64 -w 0)
echo "Code coverage: $testcoverage"
echo "$testcoverage_full"
echo "coverage=$testcoverage" >> $GITHUB_ENV
echo "testcoverage_full_base64=$testcoverage_full_base64" >> $GITHUB_ENV
echo "cleaning up..."
rm ~/.config/Headlamp/kubeconfigs/config
shell: bash

- name: Upload coverage report as artifact
id: upload-artifact
uses: actions/upload-artifact@v4
with:
name: backend-coverage-report
path: ./backend/backend_coverage.html

- name: Get base branch code coverage
if: ${{ github.event_name }} == 'pull_request'
run: |
Expand Down Expand Up @@ -125,8 +137,13 @@ jobs:
exit 0
fi
testcoverage="${{ env.coverage }}"
testcoverage_full_base64="${{ env.testcoverage_full_base64 }}"
testcoverage_full=$(echo "$testcoverage_full_base64" | base64 --decode)
base_coverage="${{ env.base_coverage }}"
coverage_diff="${{ env.coverage_diff }}"
artifact_url=${{ steps.upload-artifact.outputs.artifact-url }}
if (( $(echo "$coverage_diff < 0" | bc -l) )); then
emoji="😞" # Decreased coverage
else
Expand All @@ -135,6 +152,24 @@ jobs:
comment="Backend Code coverage changed from $base_coverage% to $testcoverage%. Change: $coverage_diff% $emoji."
echo "$comment"
# Add the full coverage report as a collapsible section
comment="${comment}
<details>
<summary>Coverage report</summary>
\`\`\`
$testcoverage_full
\`\`\`
</details>
[Html coverage report download]($artifact_url)
"
echo "$comment"
if [[ "${{github.event.pull_request.head.repo.full_name}}" == "${{github.repository}}" ]]; then
# Forks (like dependabot ones) do not have permission to comment on the PR,
# so do not fail the action if this fails.
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
backend/headlamp-server
backend/headlamp-server.exe
backend/tools
backend/coverage.out
app/electron/src/*
docs/development/storybook/
.plugins
Expand Down
10 changes: 10 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,16 @@ backend:
backend-test:
cd backend && go test -v -p 1 ./...

.PHONY: backend-coverage
backend-coverage:
cd backend && go test -v -p 1 -coverprofile=coverage.out ./...
cd backend && go tool cover -func=coverage.out

.PHONY: backend-coverage-html
backend-coverage-html:
cd backend && go test -v -p 1 -coverprofile=coverage.out ./...
cd backend && go tool cover -html=coverage.out

.PHONY: backend-format
backend-format:
cd backend && go fmt ./cmd/ ./pkg/**
Expand Down

0 comments on commit 63f6229

Please sign in to comment.