: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(devex): verify fork PR snapshots offline in storybook ci (#105948)
Julian Bez82e6efbb6d99
diff --git a/.github/workflows/ci-storybook.yml b/.github/workflows/ci-storybook.ymlindex 400c4d984660..316822fd215d 100644--- a/.github/workflows/ci-storybook.yml+++ b/.github/workflows/ci-storybook.yml@@ -648,10 +648,14 @@ jobs:pnpm --filter=@posthog/storybook test:visual:ci:update --browsers ${{ matrix.browser }} --shard ${{ matrix.shard }}/${{ matrix.shard_count }}fi- # Upload snapshots to Visual Review (runs in parallel across shards).- # Build the CLI from the base branch (see vr-setup for the rationale).+ # Upload snapshots to Visual Review (runs in parallel across shards), or verify+ # them offline on a fork PR. Fork PRs get no VR_API_TOKEN, so vr-setup skips and+ # leaves run_id empty. The fork arm reads the event, not env, because the fork's+ # test code runs earlier in this job and can write to $GITHUB_ENV. Build the CLI+ # from the base branch (see vr-setup for the rationale), which also keeps fork+ # code out of the CLI.- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2- if: always() && needs.vr-setup.outputs.run_id != ''+ if: always() && (needs.vr-setup.outputs.run_id != '' || (github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name != github.repository))with:ref: ${{ github.event.pull_request.base.ref || github.sha }}path: vr-cli@@ -665,7 +669,7 @@ jobs:# the cache version. The save below writes the container version.- name: Restore VR CLI buildid: restore-vr-cli- if: always() && needs.vr-setup.outputs.run_id != ''+ if: always() && (needs.vr-setup.outputs.run_id != '' || (github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name != github.repository))uses: actions/cache/restore@cdf6c1fa76f9f475f3d7449005a359c84ca0f306 # v5.0.3with:path: |@@ -676,7 +680,7 @@ jobs:# Runs only on a miss, and retries the install for the reason given at vr-setup.- name: Build VR CLIid: build-vr-cli- if: always() && needs.vr-setup.outputs.run_id != '' && steps.restore-vr-cli.outputs.cache-hit != 'true'+ if: always() && (needs.vr-setup.outputs.run_id != '' || (github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name != github.repository)) && steps.restore-vr-cli.outputs.cache-hit != 'true'timeout-minutes: 8shell: bashworking-directory: vr-cli/products/visual_review/cli@@ -711,6 +715,45 @@ jobs:finode "$VR_CLI" run upload "$@"+ # Without this step a fork PR's gate reads the skipped Visual Review jobs as a pass,+ # and the merge queue is the first run to compare its screenshots. verify compares+ # exact pixel hashes with no API call. It does not know the tolerated alternate hashes+ # or the quarantine that Visual Review applies, so a flaky story can fail a fork PR+ # that changes nothing visible. --partial because a shard renders only part of the+ # baseline, so a baseline missing from the shard is not a removal.+ # The PR controls frontend/snapshots.yml and verify cannot check the HMAC offline, so+ # this catches honest drift, not a forged baseline. Visual Review drops entries with a+ # bad signature, so the merge queue still catches a forged one.+ # !cancelled() keeps the check after a test-runner failure. A failed CLI build+ # fails this step too, so a fork PR never passes unverified.+ - name: Verify snapshots against the baseline offline+ if: ${{ !cancelled() && (github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name != github.repository) }}+ shell: bash+ run: |+ set +e+ node vr-cli/products/visual_review/cli/dist/cli/src/index.js verify --partial \+ --dir frontend/__snapshots__/ \+ --baseline frontend/snapshots.yml > /tmp/vr-verify.log 2>&1+ status=$?+ set -e+ cat /tmp/vr-verify.log+ {+ echo "### Offline snapshot check (${{ matrix.browser }} ${{ matrix.shard }}/${{ matrix.shard_count }})"+ echo+ echo "Fork PRs get no Visual Review run, so this shard compares its screenshots with \`frontend/snapshots.yml\` offline."+ echo "This fallback accepts only an exact pixel match. It does not know the tolerated hashes or the quarantine of Visual Review, so a flaky story can also fail it."+ echo+ echo '```'+ cat /tmp/vr-verify.log+ echo '```'+ } >> "$GITHUB_STEP_SUMMARY"+ if [ "$status" -ne 0 ] && grep -q 'Baseline mismatch' /tmp/vr-verify.log; then+ echo "::error title=Snapshots differ from the baseline::This shard's screenshots do not match frontend/snapshots.yml. Fork PRs get no Visual Review run, so this offline fallback accepts only an exact pixel match, without the tolerated hashes or the quarantine of Visual Review. A maintainer must review the change and update the baseline before this PR can merge. The job log lists the snapshots that differ."+ elif [ "$status" -ne 0 ]; then+ echo "::error title=Offline snapshot check failed::The offline snapshot check could not compare this shard's screenshots. The job log shows the error."+ fi+ exit "$status"+# Saves the container version of the entry from one shard, master only. Runs# after the upload so packing never delays a snapshot. !cancelled() because the# tests run before the build, and a red test still warms a correct cache. Thediff --git a/docs/published/handbook/engineering/fork-pull-requests.md b/docs/published/handbook/engineering/fork-pull-requests.mdindex 3908472490cc..209f0af739fd 100644--- a/docs/published/handbook/engineering/fork-pull-requests.md+++ b/docs/published/handbook/engineering/fork-pull-requests.md@@ -12,6 +12,7 @@ What a fork contributor sees:- Backend CI runs on GitHub Actions, as it always has, and selects the same tests it selects for any other pull request.- Depot's optional checks show as skipped. They are not required and nothing waits on them.- Steps that need a secret skip, on both engines. The same-repo `if:` guard that makes that safe is in the [CI authoring skill](https://github.com/PostHog/posthog/blob/master/.agents/skills/authoring-ci-workflows/SKILL.md).+- Visual Review needs a secret, so a fork pull request gets no Visual Review run. Each Storybook shard instead compares its screenshots with `frontend/snapshots.yml` offline. This fallback accepts only an exact pixel match. It does not know the tolerated hashes or the quarantine of Visual Review, so a flaky story can fail it. A snapshot that differs fails `Visual regression tests pass` until a maintainer updates the baseline. Without the check, the merge queue would be the first run to compare the screenshots.Never push a fork's head to a branch inside this repository to get it a Depot run.An in-repo branch is trusted: every secret-gated step runs, with the fork's code in control of the job.diff --git a/products/visual_review/cli/src/index.ts b/products/visual_review/cli/src/index.tsindex a91ded7fbabb..173a84095b5b 100644--- a/products/visual_review/cli/src/index.ts+++ b/products/visual_review/cli/src/index.ts@@ -60,6 +60,7 @@ program.description('Compare local screenshots against baseline (no API calls)').requiredOption('--dir <path>', 'Directory containing PNG screenshots').requiredOption('--baseline <path>', 'Path to snapshots.yml baseline file')+ .option('--partial', 'Screenshots cover only part of the baseline (a shard); skip the removed check').action(async (options: VerifyOptions) => {try {const exitCode = await runVerify(options)@@ -171,6 +172,7 @@ program.parse()interface VerifyOptions {dir: stringbaseline: string+ partial?: boolean}interface SubmitOptions {@@ -496,17 +498,22 @@ async function runVerify(options: VerifyOptions): Promise<number> {const scanned = scanDirectory(dirPath)- if (scanned.length === 0) {- console.error('No PNGs found in directory')- return 1- }-const baselineHashes = readBaselineHashes(baselinePath)if (Object.keys(baselineHashes).length === 0) {console.error('No baseline hashes found — run `vr submit` on a PR first')return 1}+ if (scanned.length === 0) {+ // A shard whose story files all skip their screenshot writes no PNG, and that is not a mismatch.+ if (options.partial) {+ log('No PNGs found in directory, nothing to verify')+ return 0+ }+ console.error('No PNGs found in directory')+ return 1+ }+log(`Found ${scanned.length} snapshots, verifying against ${Object.keys(baselineHashes).length} baselines...`)const changed: string[] = []@@ -527,7 +534,7 @@ async function runVerify(options: VerifyOptions): Promise<number> {}const currentIds = new Set(scanned.map((s) => s.identifier))- const removed = Object.keys(baselineHashes).filter((id) => !currentIds.has(id))+ const removed = options.partial ? [] : Object.keys(baselineHashes).filter((id) => !currentIds.has(id))const unchanged = scanned.length - changed.length - added.lengthlog(