-
Notifications
You must be signed in to change notification settings - Fork 313
Add post-PR metrics script for GitHub actions #688
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| const fs = require("fs") | ||
| const path = require("path") | ||
|
|
||
| const BASELINE_FILE = path.join(".metrics-baseline", "metrics.json") | ||
| const COMMENT_TAG = "<!-- pr-metrics -->" | ||
|
|
||
| // ---------- helpers ---------- | ||
|
|
||
| function readNumber(value) { | ||
| const n = Number(value) | ||
| return Number.isFinite(n) && n >= 0 ? n : null | ||
| } | ||
|
|
||
| function readCoverage(value) { | ||
| const n = Number(value) | ||
| return Number.isFinite(n) && n >= 0 && n <= 100 ? n : null | ||
| } | ||
|
|
||
| function safeReadBaseline() { | ||
| try { | ||
| if (!fs.existsSync(BASELINE_FILE)) return null | ||
| const raw = fs.readFileSync(BASELINE_FILE, "utf8") | ||
| return JSON.parse(raw) | ||
| } catch { | ||
| return null // corrupted file safe fallback | ||
| } | ||
| } | ||
|
|
||
| function formatBytes(bytes) { | ||
| if (bytes == null) return "—" | ||
| return `${(bytes / 1024).toFixed(1)} KB` | ||
| } | ||
|
|
||
| function diffText(current, baseline, formatter = v => v) { | ||
| if (current == null) return "—" | ||
| if (!baseline) return formatter(current) | ||
|
|
||
| const diff = current - baseline | ||
| if (diff === 0) return `${formatter(current)} (no change)` | ||
|
|
||
| const sign = diff > 0 ? "▲" : "▼" | ||
| return `${formatter(current)} (${sign} ${formatter(Math.abs(diff))})` | ||
| } | ||
|
|
||
| function buildComment({ bundle, baselineBundle, coverage, baselineCoverage }) { | ||
| const bundleRow = diffText(bundle, baselineBundle, formatBytes) | ||
| const coverageRow = diffText(coverage, baselineCoverage, v => `${v.toFixed(1)}%`) | ||
|
|
||
| return `${COMMENT_TAG} | ||
| ## 📊 PR Metrics | ||
|
|
||
| | Metric | Value | | ||
| |---|---| | ||
| | Bundle size (gzip) | ${bundleRow} | | ||
| | Test coverage | ${coverageRow} | | ||
| ` | ||
| } | ||
|
|
||
| // ---------- main function ---------- | ||
|
|
||
| async function postPrMetrics({ github, context }) { | ||
| // Skip conditions | ||
| if (process.env.BUILD_OUTCOME !== "success") return | ||
| if (process.env.PREVIEW_READY !== "true" && process.env.PREVIEW_READY !== "false") return | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unrelated PREVIEW_READY guard suppresses all metrics postingMedium Severity The Reviewed by Cursor Bugbot for commit 6193de5. Configure here. |
||
|
|
||
| const bundle = readNumber(process.env.BUNDLE_GZIP) | ||
| const coverage = readCoverage(process.env.COVERAGE) | ||
|
|
||
| const baseline = safeReadBaseline() | ||
| const baselineBundle = baseline?.bundleSize ?? null | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Baseline bundle size bypasses readNumber validationLow Severity
Reviewed by Cursor Bugbot for commit 6193de5. Configure here. |
||
| const baselineCoverage = readCoverage(baseline?.coverage) | ||
|
|
||
| const body = buildComment({ | ||
| bundle, | ||
| baselineBundle, | ||
| coverage, | ||
| baselineCoverage, | ||
| }) | ||
|
|
||
| const { owner, repo } = context.repo | ||
| const issue_number = context.issue.number | ||
|
|
||
| const comments = await github.rest.issues.listComments({ | ||
| owner, | ||
| repo, | ||
| issue_number, | ||
| }) | ||
|
|
||
| const existing = comments.data.find(c => c.body?.includes(COMMENT_TAG)) | ||
|
|
||
| if (existing) { | ||
| await github.rest.issues.updateComment({ | ||
| owner, | ||
| repo, | ||
| comment_id: existing.id, | ||
| body, | ||
| }) | ||
| } else { | ||
| await github.rest.issues.createComment({ | ||
| owner, | ||
| repo, | ||
| issue_number, | ||
| body, | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| module.exports = { postPrMetrics } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Production code in unreferenced file with test extensionMedium Severity This file appears to be accidentally committed. It has a Reviewed by Cursor Bugbot for commit 6193de5. Configure here. |
||


There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Falsy check skips diff when baseline is zero
Medium Severity
The
diffTextfunction usesif (!baseline)to check for a missing baseline, but this falsiness check also matches whenbaselineis0. A baseline bundle size of0or baseline coverage of0%would skip the diff calculation entirely and display the value without any change indicator, as if no baseline existed. The check needs to bebaseline == nullto properly distinguish "no baseline" from a zero-valued baseline.Reviewed by Cursor Bugbot for commit 6193de5. Configure here.