Repository navigation
272 lines (231 loc) · 8.74 KB
/
Copy pathcheck-code.yml
File metadata and controls
272 lines (231 loc) · 8.74 KB
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
name: Check Code
on:
pull_request:
branches: [main]
permissions:
contents: write
pull-requests: write
jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
go-version: ['1.22', '1.23', '1.24', '1.25', '1.26']
steps:
- uses: actions/checkout@v4
- name: Set up Go ${{ matrix.go-version }}
uses: actions/setup-go@v5
with:
go-version: ${{ matrix.go-version }}
cache: false
- name: Run tests
run: go test -v -race -count=1 ./...
- name: Run example tests
run: cd _examples && go test -v -race -count=1 ./...
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: '1.26'
cache: false
- name: golangci-lint
uses: golangci/golangci-lint-action@v7
with:
version: v2.10.1
code-quality:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
ref: ${{ github.head_ref }}
- uses: actions/setup-go@v5
with:
go-version: '1.26'
cache: false
- name: Run gofmt
run: gofmt -s -w .
- name: Push gofmt changes
run: |
if git diff --quiet; then
echo "No gofmt changes needed"
else
git config user.name "github-actions[bot]"
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
git add -A
git commit -m "style: gofmt -s"
git push origin HEAD:${{ github.head_ref }}
fi
- name: Check gocyclo
run: |
go install github.com/fzipp/gocyclo/cmd/gocyclo@latest
RESULT=$(gocyclo -over 15 . | grep -v '_test.go' || true)
if [ -n "$RESULT" ]; then
echo "$RESULT"
exit 1
fi
version-sync:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
ref: ${{ github.head_ref }}
- name: Extract version from branch name
id: extract
run: |
BRANCH="${{ github.head_ref }}"
if [[ "$BRANCH" =~ ^v/([0-9]+\.[0-9]+\.[0-9]+)$ ]]; then
echo "version=${BASH_REMATCH[1]}" >> "$GITHUB_OUTPUT"
echo "Extracted version: ${BASH_REMATCH[1]}"
else
echo "Branch '$BRANCH' does not match v/X.Y.Z pattern, skipping"
fi
- name: Update version.go
if: steps.extract.outputs.version != ''
run: |
VERSION="${{ steps.extract.outputs.version }}"
CURRENT=$(sed -n 's/^const Version = "\(.*\)"/\1/p' internal/buildinfo/version.go)
if [ "$CURRENT" = "$VERSION" ]; then
echo "version.go already has version $VERSION"
else
sed -i "s/const Version = \".*\"/const Version = \"${VERSION}\"/" internal/buildinfo/version.go
git config user.name "github-actions[bot]"
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
git add internal/buildinfo/version.go
git commit -m "chore: bump version to v${VERSION}"
git push origin HEAD:${{ github.head_ref }}
fi
coverage:
needs: [test]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
ref: ${{ github.head_ref }}
- uses: actions/setup-go@v5
with:
go-version: '1.25'
cache: false
- name: Run coverage
id: coverage
run: |
go test -coverprofile=coverage.out -covermode=atomic ./...
REPORT=$(go tool cover -func=coverage.out)
TOTAL=$(echo "$REPORT" | grep total | awk '{print $3}' | sed 's/%//')
echo "$REPORT"
echo ""
echo "Total coverage: ${TOTAL}%"
# Build function table (exclude total line)
TABLE="| File | Function | Coverage |\n|---|---|---|"
while IFS= read -r line; do
FILE=$(echo "$line" | awk '{print $1}')
FUNC=$(echo "$line" | awk '{print $2}')
COV=$(echo "$line" | awk '{print $NF}')
TABLE="${TABLE}\n| \`${FILE}\` | \`${FUNC}\` | ${COV} |"
done <<< "$(echo "$REPORT" | grep -v total)"
# Build uncovered table (< 100%)
UNCOVERED_TABLE="| File | Function | Coverage |\n|---|---|---|"
UNCOVERED_COUNT=0
while IFS= read -r line; do
COV=$(echo "$line" | awk '{print $NF}' | sed 's/%//')
if (( $(echo "$COV < 100.0" | bc -l) )); then
FILE=$(echo "$line" | awk '{print $1}')
FUNC=$(echo "$line" | awk '{print $2}')
UNCOVERED_TABLE="${UNCOVERED_TABLE}\n| \`${FILE}\` | \`${FUNC}\` | ${COV}% |"
UNCOVERED_COUNT=$((UNCOVERED_COUNT + 1))
fi
done <<< "$(echo "$REPORT" | grep -v total)"
# Threshold check
PASS="true"
if (( $(echo "$TOTAL < 80.0" | bc -l) )); then
PASS="false"
fi
# Status emoji
if [ "$PASS" = "true" ]; then
STATUS="✅"
else
STATUS="❌"
fi
# Save outputs
{
echo "total=${TOTAL}"
echo "pass=${PASS}"
echo "status=${STATUS}"
echo "uncovered_count=${UNCOVERED_COUNT}"
} >> "$GITHUB_OUTPUT"
# Save tables to files (multiline)
echo -e "$TABLE" > /tmp/coverage_table.md
echo -e "$UNCOVERED_TABLE" > /tmp/uncovered_table.md
if [ "$PASS" = "false" ]; then
echo "::error::Coverage ${TOTAL}% is below 80% threshold"
fi
- name: Comment on PR
uses: actions/github-script@v7
with:
script: |
const fs = require('fs');
const total = '${{ steps.coverage.outputs.total }}';
const pass = '${{ steps.coverage.outputs.pass }}';
const status = '${{ steps.coverage.outputs.status }}';
const uncoveredCount = '${{ steps.coverage.outputs.uncovered_count }}';
const fullTable = fs.readFileSync('/tmp/coverage_table.md', 'utf8').trim();
const uncoveredTable = fs.readFileSync('/tmp/uncovered_table.md', 'utf8').trim();
let body = `## 📊 Coverage Report\n\n`;
body += `**Total: ${total}%** (threshold: 80%) ${status}\n\n`;
if (parseInt(uncoveredCount) > 0) {
body += `<details>\n<summary>⚠️ Uncovered functions (${uncoveredCount})</summary>\n\n`;
body += uncoveredTable + '\n\n</details>\n\n';
}
body += `<details>\n<summary>All functions</summary>\n\n`;
body += fullTable + '\n\n</details>';
const { data: comments } = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
});
const marker = '## 📊 Coverage Report';
const existing = comments.find(c => c.body.startsWith(marker));
if (existing) {
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: existing.id,
body,
});
} else {
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
body,
});
}
- name: Fail if below threshold
if: steps.coverage.outputs.pass == 'false'
run: exit 1
- name: Update coverage badge
if: steps.coverage.outputs.pass == 'true'
run: |
TOTAL=${{ steps.coverage.outputs.total }}
if (( $(echo "$TOTAL >= 90" | bc -l) )); then
COLOR="brightgreen"
elif (( $(echo "$TOTAL >= 80" | bc -l) )); then
COLOR="green"
elif (( $(echo "$TOTAL >= 70" | bc -l) )); then
COLOR="yellowgreen"
elif (( $(echo "$TOTAL >= 60" | bc -l) )); then
COLOR="yellow"
else
COLOR="red"
fi
sed -i "s|!\[coverage\](https://img.shields.io/badge/coverage-[^)]*)||g" README.md
if git diff --quiet README.md; then
echo "No badge change needed"
else
git config user.name "github-actions[bot]"
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
git add README.md
git commit -m "update coverage badge to ${TOTAL}%"
git push origin HEAD:${{ github.head_ref }}
fi