feat(scripts): scaffold new resources from OpenAPI + drift-check existing ones#3
Open
timgl wants to merge 1 commit into
Open
feat(scripts): scaffold new resources from OpenAPI + drift-check existing ones#3timgl wants to merge 1 commit into
timgl wants to merge 1 commit into
Conversation
…ting ones Adds two devtools driven by the PostHog OpenAPI spec: - `pnpm scaffold-resource` generates the 5 files for a new resource (client/sdk/pipeline/pipeline.test/index), wires it into the resource registry, SDK index, and docs matrix, and inlines a deep Zod schema for the response component via `openapi-zod-client`. Recursive types and discriminated unions are rewritten to satisfy strict Zod 4. - `pnpm check-resources` compares each shipped resource's `ServerXSchema` (or `ServerX` TS type fallback) against the current OpenAPI component and reports added / removed / type-mismatched fields. Exits non-zero on drift so it can gate CI. ReadOnly server-set fields hidden by default; `--all` includes them. Skill updates: `add-resource` now opens with a "Start with the scaffolder" section listing what's auto-generated and the `TODO(human)` markers that require judgment, and gains a "Checking for API drift" section. The singleton skill clarifies the scaffolder is collection-only. Generated-By: PostHog Code Task-Id: 667f64df-ab33-400b-a081-8eb131ecccf7
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Two devtools, both driven by the PostHog OpenAPI spec (
../posthog/frontend/tmp/openapi.json):pnpm scaffold-resource --name <foo> --path <list-endpoint> --key-field <field>generates a new resource end-to-end:client.ts(deep Zod schema with all transitive component refs inlined viaopenapi-zod-client, plus CRUD wrappers),sdk.ts,pipeline.ts(skeleton withTODO(human)markers over the per-resource judgment calls — hash projection, identity mechanism, payload, validation, display),pipeline.test.ts,index.ts. Wires the resource intosrc/resources/index.ts,src/index.ts,scripts/lib/registry.ts, and flips the matching row indocs/resources.md.pnpm check-resourceswalks the resource registry, compares eachServerXSchema(ortype ServerX = {…}fallback for the older resources) against the current OpenAPI component, and reports added / removed / type-mismatched fields.readOnlyserver-set fields hidden by default;--allto include. Exits non-zero on drift so it can gate CI.Skill updates (
add-resource,add-singleton-resource) document the new entry point and theTODO(human)markers that still require human judgment.Why the openapi-zod-client path
A homegrown OpenAPI-to-Zod converter would punt on
$refresolution,allOfmerging, and recursive types — the cohort schema alone has 5 levels of$refcycles.openapi-zod-clienthandles all of that. The output is 1.6MB for the full spec, so we don't commit it: the extractor (scripts/lib/extract-zod.ts) runs the CLI to a tmp file (cached by openapi mtime), parses the prettier-formatted output into top-level declarations, transitively closes from a single component, topo-sorts, and inlines only what each resource needs.Two rewrites make the output satisfy strict Zod 4:
const X: z.ZodType<X> = z.lazy(...)→const X = (z.lazy(...)) as z.ZodType<X>z.discriminatedUnion("type", [...])→z.union([...])Test plan
pnpm typecheckpasses (pre-existingacceptance.tserror is unrelated to this PR).pnpm test— all 59 unit tests pass.pnpm check-resourcesagainst the 4 shipped resources surfaces 27 added user fields and 1 removed (data_freshness_secondson endpoint).cohortend-to-end.ServerCohortSchema.filtersisCohortFilters.nullish()with the full recursiveCohortFilterGroup;created_byisUserBasic;cohort_typeisz.union([CohortTypeEnum, BlankEnum, NullEnum]). Typechecks under strict Zod 4. Reverted before commit.client.tsfor one resource by runningpnpm scaffold-resource --name cohort --path /api/projects/{project_id}/cohorts/ --key-field nameagainst your local../posthog/frontend/tmp/openapi.json.Created with PostHog Code