-
Notifications
You must be signed in to change notification settings - Fork 160
191 lines (161 loc) · 6.64 KB
/
backend-test.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
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
uses: actions/upload-artifact@v3
with:
name: backend-coverage-report
path: ./backend/backend_coverage.html
- name: Get backend_coverage artifact URL
id: get-artifact-url
env:
GH_TOKEN: ${{ github.token }}
run: |
ARTIFACT_ID=$(gh api -H "Accept: application/vnd.github.v3+json" /repos/${{ github.repository }}/actions/artifacts | jq -r '.artifacts[] | select(.name=="backend-coverage-report") | .id')
ARTIFACT_URL="https://github.com/${{ github.repository }}/suites/${{ github.run_id }}/artifacts/$ARTIFACT_ID"
artifact_url_base64=$(echo "$ARTIFACT_URL" | base64 -w 0)
echo "artifact_url_base64=$artifact_url_base64" >> $GITHUB_ENV
- 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_base64="${{ env.artifact_url_base64 }}"
artifact_url=$(echo "$artifact_url_base64" | base64 --decode)
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 }}