-
Notifications
You must be signed in to change notification settings - Fork 347
417 lines (346 loc) · 16.3 KB
/
pr-metrics.yml
File metadata and controls
417 lines (346 loc) · 16.3 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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
name: PR Build Metrics
on:
pull_request:
types: [opened, synchronize, reopened]
concurrency:
group: pr-metrics-${{ github.event.pull_request.number }}
cancel-in-progress: true
permissions:
contents: read
pull-requests: write
jobs:
build-base:
runs-on: ubuntu-latest
steps:
- name: Checkout base branch
uses: actions/checkout@v4
with:
ref: ${{ github.event.pull_request.base.sha }}
- name: Set up Go
id: setup-go
uses: actions/setup-go@v5
with:
go-version-file: go.mod
- name: Cache base binary
id: cache-base
uses: actions/cache@v4
with:
path: litestream-base
key: pr-metrics-base-${{ github.event.pull_request.base.sha }}-${{ steps.setup-go.outputs.go-version }}
- name: Build base binary
if: steps.cache-base.outputs.cache-hit != 'true'
run: CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags "-s -w" -o litestream-base ./cmd/litestream
- name: Record base binary size
run: stat --format=%s litestream-base > base-size.txt
- name: Upload base size
uses: actions/upload-artifact@v4
with:
name: base-size
path: base-size.txt
build-pr:
runs-on: ubuntu-latest
steps:
- name: Checkout PR branch
uses: actions/checkout@v4
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version-file: go.mod
- name: Build PR binary
run: |
START=$SECONDS
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags "-s -w" -o litestream-pr ./cmd/litestream
echo "$((SECONDS - START))" > build-time.txt
go version | awk '{print $3}' > go-version.txt
- name: Record PR binary size
run: stat --format=%s litestream-pr > pr-size.txt
- name: Upload PR size
uses: actions/upload-artifact@v4
with:
name: pr-size
path: pr-size.txt
- name: Upload build info
uses: actions/upload-artifact@v4
with:
name: build-info
path: |
build-time.txt
go-version.txt
analyze-deps:
runs-on: ubuntu-latest
steps:
- name: Checkout PR branch
uses: actions/checkout@v4
with:
path: pr
- name: Checkout base branch
uses: actions/checkout@v4
with:
ref: ${{ github.event.pull_request.base.sha }}
path: base
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version-file: pr/go.mod
- name: Diff go.mod dependencies
run: |
extract_require_deps() {
local gomod="$1"
awk '/^require \(/{found=1; next} found && /^\)/{found=0} found && /^\t/' "$gomod" | sed 's/^\t//' | sort
}
extract_require_deps base/go.mod > /tmp/base-deps.txt
extract_require_deps pr/go.mod > /tmp/pr-deps.txt
{
echo "## Added"
comm -13 /tmp/base-deps.txt /tmp/pr-deps.txt
echo "## Removed"
comm -23 /tmp/base-deps.txt /tmp/pr-deps.txt
} > deps-diff.txt
- name: Module graph size
run: |
cd base && go mod graph | wc -l | tr -d ' ' > ../base-graph-size.txt && cd ..
cd pr && go mod graph | wc -l | tr -d ' ' > ../pr-graph-size.txt && cd ..
- name: Run govulncheck
run: |
go install golang.org/x/vuln/cmd/govulncheck@latest
cd pr
govulncheck ./... > ../vulncheck-results.txt 2>&1 || true
- name: Check Go toolchain freshness
run: |
CURRENT=$(grep '^toolchain ' pr/go.mod | awk '{print $2}' | sed 's/^go//')
if [ -z "$CURRENT" ]; then
CURRENT=$(grep '^go ' pr/go.mod | awk '{print $2}')
fi
MINOR=$(echo "$CURRENT" | grep -oE '^[0-9]+\.[0-9]+')
LATEST=$(curl -sf 'https://go.dev/dl/?mode=json&include=all' | \
python3 -c "import sys,json; releases=json.load(sys.stdin); print(next(r['version'] for r in releases if r['stable'] and r['version'].startswith('go${MINOR}.')))" 2>/dev/null | sed 's/^go//')
if [ -z "$LATEST" ]; then
echo "current=${CURRENT}" > go-toolchain.txt
echo "latest=unknown" >> go-toolchain.txt
echo "stale=false" >> go-toolchain.txt
elif [ "$CURRENT" = "$LATEST" ]; then
echo "current=${CURRENT}" > go-toolchain.txt
echo "latest=${LATEST}" >> go-toolchain.txt
echo "stale=false" >> go-toolchain.txt
else
echo "current=${CURRENT}" > go-toolchain.txt
echo "latest=${LATEST}" >> go-toolchain.txt
echo "stale=true" >> go-toolchain.txt
fi
- name: Upload dependency analysis
uses: actions/upload-artifact@v4
with:
name: dep-analysis
path: |
deps-diff.txt
base-graph-size.txt
pr-graph-size.txt
vulncheck-results.txt
go-toolchain.txt
post-comment:
if: github.event.pull_request.head.repo.full_name == github.repository
runs-on: ubuntu-latest
needs: [build-base, build-pr, analyze-deps]
steps:
- name: Download all artifacts
uses: actions/download-artifact@v4
- name: Post PR comment and manage labels
uses: actions/github-script@v7
with:
script: |
const fs = require('fs');
const owner = context.repo.owner;
const repo = context.repo.repo;
const issue_number = context.issue.number;
const baseSize = parseInt(fs.readFileSync('base-size/base-size.txt', 'utf8').trim());
const prSize = parseInt(fs.readFileSync('pr-size/pr-size.txt', 'utf8').trim());
const buildTime = fs.readFileSync('build-info/build-time.txt', 'utf8').trim();
const goVersion = fs.readFileSync('build-info/go-version.txt', 'utf8').trim();
const depsDiff = fs.readFileSync('dep-analysis/deps-diff.txt', 'utf8').trim();
const baseGraphSize = fs.readFileSync('dep-analysis/base-graph-size.txt', 'utf8').trim();
const prGraphSize = fs.readFileSync('dep-analysis/pr-graph-size.txt', 'utf8').trim();
let vulncheck = fs.readFileSync('dep-analysis/vulncheck-results.txt', 'utf8').trim();
const toolchainData = fs.readFileSync('dep-analysis/go-toolchain.txt', 'utf8').trim();
const MAX_VULNCHECK_LEN = 10000;
if (vulncheck.length > MAX_VULNCHECK_LEN) {
vulncheck = vulncheck.substring(0, MAX_VULNCHECK_LEN) + '\n... (truncated, see workflow run for full output)';
}
// --- Parse Go toolchain freshness ---
const tcLines = Object.fromEntries(toolchainData.split('\n').map(l => l.split('=')));
const goCurrent = tcLines.current || 'unknown';
const goLatest = tcLines.latest || 'unknown';
const goStale = tcLines.stale === 'true';
// --- Compute metrics ---
const diff = prSize - baseSize;
const absPct = baseSize > 0 ? Math.abs((diff / baseSize) * 100) : 0;
const pct = baseSize > 0 ? ((diff / baseSize) * 100).toFixed(2) : 'N/A';
const sign = diff > 0 ? '+' : '';
const fmt = (bytes) => (bytes / 1024 / 1024).toFixed(2) + ' MB';
const addedSection = depsDiff.split('## Removed')[0].replace('## Added', '').trim();
const removedSection = depsDiff.split('## Removed')[1]?.trim() || '';
const addedDeps = addedSection ? addedSection.split('\n').filter(l => l.trim()) : [];
const removedDeps = removedSection ? removedSection.split('\n').filter(l => l.trim()) : [];
const depsChanged = addedDeps.length + removedDeps.length;
const graphDiff = parseInt(prGraphSize) - parseInt(baseGraphSize);
const graphSign = graphDiff > 0 ? '+' : '';
const hasVulns = vulncheck.includes('Vulnerability #') || vulncheck.includes('GO-');
// --- Determine status ---
const flags = [];
let sizeIcon = '✅';
if (diff > 0 && absPct >= 10) { sizeIcon = '🚨'; flags.push('binary size >10%'); }
else if (diff > 0 && absPct >= 5) { sizeIcon = '⚠️'; flags.push('binary size >5%'); }
else if (diff < 0) { sizeIcon = '📉'; }
const vulnIcon = hasVulns ? '⚠️' : '✅';
if (hasVulns) flags.push('vulnerabilities found');
const goIcon = goStale ? '⚠️' : '✅';
if (goStale) flags.push(`Go toolchain outdated (${goCurrent} → ${goLatest})`);
const depsIcon = depsChanged > 0 ? 'ℹ️' : '✅';
const overallIcon = flags.length > 0 ? '⚠️' : '✅';
const overallText = flags.length > 0
? `**Attention needed** — ${flags.join(', ')}`
: '**All clear** — no issues detected';
// --- Build compact comment ---
let depsDetail = 'No dependency changes.';
if (depsChanged > 0) {
const parts = [];
if (addedDeps.length > 0) parts.push('**Added:**\n' + addedDeps.map(d => `- \`${d}\``).join('\n'));
if (removedDeps.length > 0) parts.push('**Removed:**\n' + removedDeps.map(d => `- \`${d}\``).join('\n'));
depsDetail = parts.join('\n\n');
}
const body = `## PR Build Metrics
${overallIcon} ${overallText}
| Check | Status | Summary |
|:------|:------:|:--------|
| Binary size | ${sizeIcon} | ${fmt(prSize)} (${sign}${(diff / 1024).toFixed(1)} KB / ${sign}${pct}%) |
| Dependencies | ${depsIcon} | ${depsChanged > 0 ? `${addedDeps.length} added, ${removedDeps.length} removed` : 'No changes'} |
| Vulnerabilities | ${vulnIcon} | ${hasVulns ? 'Issues found — expand details below' : 'None detected'} |
| Go toolchain | ${goIcon} | ${goCurrent}${goStale ? ` → ${goLatest} available` : ' (latest)'} |
| Module graph | ✅ | ${prGraphSize} edges (${graphSign}${graphDiff}) |
<details>
<summary>Binary size details</summary>
| | Size | Change |
|---|---:|---:|
| Base (\`${context.payload.pull_request.base.sha.substring(0, 7)}\`) | ${fmt(baseSize)} | |
| PR (\`${context.sha.substring(0, 7)}\`) | ${fmt(prSize)} | ${sign}${(diff / 1024).toFixed(1)} KB (${sign}${pct}%) |
</details>
<details>
<summary>Dependency changes</summary>
${depsDetail}
</details>
<details>
<summary>govulncheck output</summary>
\`\`\`
${vulncheck}
\`\`\`
</details>
<details>
<summary>Build info</summary>
| Metric | Value |
|---|---|
| Build time | ${buildTime}s |
| Go version | \`${goVersion}\` |
| Commit | \`${context.sha.substring(0, 7)}\` |
</details>
<!-- history -->
---
<sub>🤖 Updated on each push.</sub>`.replace(/^ /gm, '');
// --- Post or update comment (with history) ---
const marker = '## PR Build Metrics';
const historyMarker = '<!-- history -->';
let existing = null;
for await (const response of github.paginate.iterator(
github.rest.issues.listComments,
{ owner, repo, issue_number, per_page: 100 }
)) {
existing = response.data.find(c => c.body?.startsWith(marker));
if (existing) break;
}
if (existing) {
// Extract previous summary line and history from existing comment
const prevBody = existing.body;
const prevStatusMatch = prevBody.match(/\| Binary size \|[^\n]+/);
const prevCommitMatch = prevBody.match(/\| Commit \| `([^`]+)` \|/);
const prevSummary = prevStatusMatch ? prevStatusMatch[0] : null;
const prevCommit = prevCommitMatch ? prevCommitMatch[1] : '?';
// Extract existing history entries
let historyEntries = '';
const historyIdx = prevBody.indexOf(historyMarker);
if (historyIdx !== -1) {
const afterMarker = prevBody.substring(historyIdx + historyMarker.length);
const detailsMatch = afterMarker.match(/<details>[\s\S]*?<\/details>/);
if (detailsMatch) {
const innerMatch = detailsMatch[0].match(/<summary>[^<]*<\/summary>([\s\S]*?)<\/details>/);
if (innerMatch) historyEntries = innerMatch[1].trim();
}
}
// Build new history (most recent first, cap at 10)
const now = new Date().toISOString().replace('T', ' ').substring(0, 16) + ' UTC';
const newEntry = prevSummary
? `| \`${prevCommit}\` | ${now} | ${prevSummary.replace(/\| Binary size \|/, '').trim().replace(/^\||\|$/g, '').trim()} |`
: null;
let historyRows = '';
if (newEntry || historyEntries) {
const existingRows = historyEntries
.split('\n')
.filter(l => l.startsWith('|') && !l.startsWith('| Commit') && !l.startsWith('|:'))
.slice(0, 9);
const allRows = newEntry ? [newEntry, ...existingRows] : existingRows;
if (allRows.length > 0) {
historyRows = `\n<details>\n<summary>History (${allRows.length} previous)</summary>\n\n| Commit | Updated | Status | Summary |\n|:-------|:--------|:------:|:--------|\n${allRows.join('\n')}\n\n</details>`;
}
}
// Insert history into body
const finalBody = body.replace(historyMarker, historyMarker + historyRows);
await github.rest.issues.updateComment({ owner, repo, comment_id: existing.id, body: finalBody });
} else {
await github.rest.issues.createComment({ owner, repo, issue_number, body });
}
// --- Manage labels ---
const LABELS = {
SIZE_WARNING: 'metrics: size-warning',
SIZE_ALERT: 'metrics: size-alert',
VULNS: 'metrics: vulns-found',
GO_UPDATE: 'metrics: go-update',
};
const ensureLabel = async (name, color, description) => {
try {
await github.rest.issues.getLabel({ owner, repo, name });
} catch {
await github.rest.issues.createLabel({ owner, repo, name, color, description });
}
};
const addLabel = async (name) => {
await github.rest.issues.addLabels({ owner, repo, issue_number, labels: [name] });
};
const removeLabel = async (name) => {
try {
await github.rest.issues.removeLabel({ owner, repo, issue_number, name });
} catch { /* label not present, ignore */ }
};
// Size labels
await ensureLabel(LABELS.SIZE_WARNING, 'fbca04', 'Binary size increased 5-10%');
await ensureLabel(LABELS.SIZE_ALERT, 'e11d48', 'Binary size increased >10%');
await ensureLabel(LABELS.VULNS, 'e11d48', 'govulncheck found vulnerabilities');
await ensureLabel(LABELS.GO_UPDATE, 'fbca04', 'Go toolchain has a newer patch release');
if (diff > 0 && absPct >= 10) {
await addLabel(LABELS.SIZE_ALERT);
await removeLabel(LABELS.SIZE_WARNING);
} else if (diff > 0 && absPct >= 5) {
await addLabel(LABELS.SIZE_WARNING);
await removeLabel(LABELS.SIZE_ALERT);
} else {
await removeLabel(LABELS.SIZE_WARNING);
await removeLabel(LABELS.SIZE_ALERT);
}
// Vuln label
if (hasVulns) {
await addLabel(LABELS.VULNS);
} else {
await removeLabel(LABELS.VULNS);
}
// Go toolchain label
if (goStale) {
await addLabel(LABELS.GO_UPDATE);
} else {
await removeLabel(LABELS.GO_UPDATE);
}