Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

6.5 KB
Raw Blame
#!/usr/bin/env node
// Posts the frontend layers section of the CI report. It compares the PR's layer report with the base's,
// not with layer-baseline.txt, so an upward import that a PR also adds to the baseline still shows.
// CI copies this file before it resets the workspace to the base branch, so it may import only modules
// that already exist on the base: no layers.mjs, and the layer order comes from the report itself.
import { formatBytes, formatDelta } from './ci-report/format.mjs'

const MAX_LISTED = 20

export function compareLayerReports(report, baseReport) {
    const baseUpward = new Set(baseReport?.upward ?? [])
    const prUpward = new Set(report.upward)
    return {
        added: baseReport ? report.upward.filter((edge) => !baseUpward.has(edge)) : [],
        removed: baseReport ? baseReport.upward.filter((edge) => !prUpward.has(edge)) : [],
        layers: report.layers.map((layer) => ({
            layer,
            bytes: report.eagerBytes[layer],
            baseBytes: baseReport?.eagerBytes?.[layer],
        })),
    }
}

// Edge and chain paths come from the PR's own build output, so a crafted file name could carry
// a newline, an HTML comment delimiter or a backtick, and forge the CI report's markers or markdown.
const sanitizeForReport = (text) =>
    text
        .replace(/[\r\n]/g, ' ')
        .replace(/`/g, "'")
        .replace(/<!--/g, '< !--')
        .replace(/-->/g, '-- >')
const shortName = (file) => sanitizeForReport(file.replace(/^frontend\/src\//, ''))
const formatEdge = (edge) => `\`${edge.split(' -> ').map(shortName).join(' -> ')}\``

function listEdges(lines, edges) {
    lines.push(...edges.slice(0, MAX_LISTED).map((edge) => `- ${formatEdge(edge)}`))
    if (edges.length > MAX_LISTED) {
        lines.push(`- …and ${edges.length - MAX_LISTED} more.`)
    }
    lines.push('')
}

// A report from before that field existed has no verdict, so it renders as a comparison with the base only.
function renderBaselineVerdict(lines, verdict) {
    if (verdict.added.length) {
        lines.push(
            `❌ ${verdict.added.length} upward import(s) are not in \`layer-baseline.txt\`, so the layer check fails:`,
            ''
        )
        listEdges(lines, verdict.added)
        lines.push(
            'Move the shared code down a layer, or load the higher layer with `lazyWithRetry()` or `retryImport(() => import(...))`.',
            ''
        )
    }
    if (verdict.fixed.length) {
        lines.push(
            `❌ ${verdict.fixed.length} fixed entry(ies) are still in \`layer-baseline.txt\`. Run \`hogli lint:frontend-layers --prune-baseline\` to drop them:`,
            ''
        )
        listEdges(lines, verdict.fixed)
    }
}

export function renderLayerSection(report, baseReport, graphUrl) {
    const { added, removed, layers } = compareLayerReports(report, baseReport)
    const lines = [
        'Static imports from a lower frontend layer into a higher one (layers, lowest first: ' +
            `${report.layers.join(', ')}). Compared with the base branch, so an import that a PR also adds to ` +
            '`layer-baseline.txt` still shows here. Sizes are the source bytes on the eager path of every logged-in page.',
        '',
        '| Layer | Eager source bytes | Δ vs base |',
        '| --- | --- | --- |',
        ...layers.map(
            ({ layer, bytes, baseBytes }) =>
                `| ${layer} | ${formatBytes(bytes)} | ${formatDelta(bytes, baseBytes, { noBaseline: '_(no base measurement)_' })} |`
        ),
        '',
    ]
    const verdict = report.baseline
    const verdictFails = Boolean(verdict && (verdict.added.length || verdict.fixed.length))
    if (verdictFails) {
        renderBaselineVerdict(lines, verdict)
    }
    if (!baseReport) {
        lines.push('ℹ️ No base report, so new and removed imports are not listed.', '')
    }
    for (const edge of added.slice(0, MAX_LISTED)) {
        const [from] = edge.split(' -> ')
        const chain = report.eagerChains?.[from]
        lines.push(`🟡 New upward import: ${formatEdge(edge)}`)
        if (chain) {
            lines.push(
                '',
                'It is on the eager path of every logged-in page:',
                '',
                '```',
                chain.map(shortName).join('\n  -> '),
                '```'
            )
        }
        lines.push('')
    }
    if (added.length > MAX_LISTED) {
        lines.push(`…and ${added.length - MAX_LISTED} more.`, '')
    }
    if (added.length) {
        lines.push(
            'Load the higher layer with `lazyWithRetry()` or `retryImport(() => import(...))`, or move the shared code down a layer.',
            ''
        )
    }
    if (removed.length) {
        lines.push(
            `🟢 ${removed.length} upward import(s) removed. Run \`hogli lint:frontend-layers --prune-baseline\` if CI asks.`,
            ''
        )
    }
    if (graphUrl) {
        lines.push(`[Download the interactive layer graph](${graphUrl}) for this PR.`, '')
    }
    lines.push(
        '<sub>Posted automatically by [check-layers](https://github.com/PostHog/posthog/blob/master/frontend/bin/check-layers.mjs)</sub>'
    )
    if (verdictFails) {
        const summary = `${verdict.added.length} import(s) not in the baseline, ${verdict.fixed.length} fixed baseline entry(ies)`
        return { status: 'fail', summary, body: lines.join('\n') }
    }
    // Without a base there is no comparison, so a new upward import must not read as a pass.
    if (!baseReport) {
        return { status: 'info', summary: 'no base comparison available', body: lines.join('\n') }
    }
    const summary = added.length
        ? `${added.length} new upward import(s)`
        : `no new upward imports, ${removed.length} removed`
    return { status: added.length ? 'warn' : 'ok', summary, body: lines.join('\n') }
}

async function main() {
    const [{ resolvePrAndBaseReport }, { postSection }] = await Promise.all([
        import('./ci-report/report-files.mjs'),
        import('./ci-report/update-ci-report.mjs'),
    ])
    const resolved = resolvePrAndBaseReport('layer-report', 'frontend layers')
    if (resolved) {
        await postSection({
            id: 'frontend-layers',
            ...renderLayerSection(resolved.report, resolved.baseReport, process.env.LAYER_GRAPH_URL),
        })
    }
}

// The CI helpers load only when this runs as a script, so jest can import the functions above.
if (/post-layer-comment(-pr)?\.mjs$/.test(process.argv[1] ?? '')) {
    main().catch((error) => {
        console.error(error)
        process.exit(1)
    })
}