Skip to content

Merge pull request #2336 from headlamp-k8s/something-went-wrong #702

Merge pull request #2336 from headlamp-k8s/something-went-wrong

Merge pull request #2336 from headlamp-k8s/something-went-wrong #702

Workflow file for this run

name: Backend test
on:
pull_request:
paths:
- 'backend/**'
- '.github/workflows/backend-test.yml'
push:
branches:
- main
- rc-*
- testing-rc-*
permissions:
contents: read
env:
HEADLAMP_RUN_INTEGRATION_TESTS: true
jobs:
build:
runs-on: ubuntu-latest
permissions:
contents: read
pull-requests: write # needed for commenting on PRs for coverage changes
steps:
- uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7
with:
fetch-depth: 0
- uses: actions/setup-go@cdcb36043654635271a94b9a6d1392de5bb323a7 # v5.0.1
with:
go-version: '1.22.*'
- name: Install dependencies
run: |
cd backend
go mod download
- name: Start cluster
uses: medyagh/setup-minikube@d8c0eb871f6f455542491d86a574477bd3894533 # latest
- name: Check cluster status and enable headlamp addon
run: |
minikube status
minikube addons enable headlamp
kubectl wait deployment -n headlamp headlamp --for condition=Available=True --timeout=30s
- name: setup and run golangci-lint
uses: golangci/golangci-lint-action@d6238b002a20823d52840fda27e2d4891c5952dc # v4.0.1
with:
version: v1.54
working-directory: backend
skip-cache: true
args: --timeout 3m
- name: Lint, Build & Check
run: |
cd $GITHUB_WORKSPACE
make backend
- name: Run tests and calculate code coverage
run: |
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: |
set -x
if [[ -z "${{ github.base_ref }}" ]]; then
echo "Base branch is empty. Skipping code coverage comparison."
exit 0
fi
cd backend
base_branch="${{ github.base_ref }}"
testcoverage="${{ env.coverage }}"
git fetch origin "$base_branch"
git checkout "origin/$base_branch"
go test ./... -coverprofile=base_coverage.out -covermode=atomic -coverpkg=./...
base_coverage=$(go tool cover -func=base_coverage.out | grep total | grep -Eo '[0-9]+\.[0-9]+')
echo "Base branch code coverage: $base_coverage"
echo "base_coverage=$base_coverage" >> $GITHUB_ENV
shell: bash
- name: Compare code coverage
if: ${{ github.event_name }} == 'pull_request'
run: |
set -x
if [[ -z "${{ github.base_ref }}" ]]; then
echo "Base branch is empty. Skipping code coverage comparison."
exit 0
fi
testcoverage="${{ env.coverage }}"
base_coverage="${{ env.base_coverage }}"
if [[ -z $testcoverage || -z $base_coverage ]]; then
echo "testcoverage or base_coverage is not set. Cannot calculate coverage difference."
exit 1
fi
echo "testcoverage=$testcoverage"
echo "base_coverage=$base_coverage"
echo "$testcoverage - $base_coverage"
coverage_diff=$(echo "$testcoverage - $base_coverage" | bc)
echo "Coverage change: $coverage_diff"
echo "coverage_diff=$coverage_diff" >> $GITHUB_ENV
shell: bash
- name: Comment on PR
if: ${{ github.event_name }} == 'pull_request'
run: |
set -x
if [[ -z "${{ github.base_ref }}" ]]; then
echo "Base branch is empty. Skipping code coverage comparison."
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
emoji="😃" # Increased or unchanged coverage
fi
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.
gh pr comment ${{github.event.number}} --body "${comment}" || true
else
echo "Pull request raised from a fork. Skipping comment."
fi
env:
GITHUB_TOKEN: ${{ github.token }}