Skip to content

Add media usage repair admin API - #1867

Merged
khoinguyenpham04 merged 6 commits into
mainfrom
media-usage-repair-admin-api
Jul 8, 2026
Merged

khoinguyenpham04 merged 6 commits into
mainfrom
media-usage-repair-admin-api

Conversation

@khoinguyenpham04

@khoinguyenpham04 khoinguyenpham04 commented Jul 7, 2026 •

Copy link
Copy Markdown
Collaborator

What does this PR do?

Adds Patch 4C's admin/operator API surface for repairing content media usage indexes.

  • Adds POST /_emdash/api/admin/media-usage/repair with explicit { "scope": "collection" } and { "scope": "all" } request shapes.
  • Adds strict request/response schemas, handler mapping, route registration, and route/auth coverage.
  • Adds EmDashClient.mediaRepairUsage() and exported client-side request/response types.
  • Documents the endpoint in OpenAPI and the REST reference.
  • Adds a changeset for the published emdash package.

No linked issue. Implements the maintainer-directed Patch 4C media usage repair plan.

Type of change

  • Bug fix
  • Feature (requires maintainer-approved Discussion)
  • Refactor (no behavior change)
  • Translation
  • Documentation
  • Performance improvement
  • Tests
  • Chore (dependencies, CI, tooling)

Checklist

  • I have read CONTRIBUTING.md
  • pnpm typecheck passes
  • pnpm lint passes
  • pnpm test passes (or targeted tests for my change)
  • pnpm format has been run
  • I have added/updated tests for my changes (if applicable)
  • User-visible strings in the admin UI are wrapped for translation (if applicable). Do not include messages.po changes except in translation PRs — a workflow extracts catalogs on merge to main. N/a: no admin UI strings were added.
  • I have added a changeset (if this PR changes a published package)
  • New features link to an approved Discussion: n/a, maintainer-directed Patch 4C implementation with no public Discussion URL.

AI-generated code disclosure

  • This PR includes AI-generated code — model/tool: OpenCode with GPT-5.5

Screenshots / test output

No screenshots: API/client/docs change only.

Verified:

pnpm format
pnpm lint
pnpm lint:quick
pnpm lint:json | jq '.diagnostics | length' # 0
pnpm typecheck
pnpm exec vitest run tests/unit/client/client.test.ts tests/unit/api/openapi.test.ts tests/unit/api/media-usage-repair-route.test.ts tests/unit/astro/media-usage-repair-auth.test.ts

Focused Vitest result: 77 tests passed.

Additional review:

  • Iterative second-opinion adversarial review converged with no remaining code/spec findings.

Try this PR

Open a fresh playground →

A full working EmDash site, deployed from this branch. Each visit gets its own session-scoped sandbox: no login needed and no shared state. Try the admin, edit content, hit the public site.

Tracks media-usage-repair-admin-api. Updated automatically when the playground redeploys.

Copilot AI review requested due to automatic review settings July 7, 2026 23:12
@changeset-bot

changeset-bot Bot commented Jul 7, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: ae2e421

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 16 packages
Name Type
emdash Minor
@emdash-cms/cloudflare Minor
@emdash-cms/sandbox-workerd Patch
@emdash-cms/fixture-perf-site Patch
@emdash-cms/perf-demo-site Patch
@emdash-cms/cache-demo-site Patch
@emdash-cms/do-demo-site Patch
@emdash-cms/do-solo-demo-site Patch
@emdash-cms/admin Minor
@emdash-cms/auth Minor
@emdash-cms/blocks Minor
@emdash-cms/gutenberg-to-portable-text Minor
@emdash-cms/x402 Minor
create-emdash Minor
@emdash-cms/auth-atproto Patch
@emdash-cms/plugin-embeds Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@github-actions

github-actions Bot commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

Scope check

This PR changes 1,025 lines across 15 files. Large PRs are harder to review and more likely to be closed without review.

If this scope is intentional, no action needed. A maintainer will review it. If not, please consider splitting this into smaller PRs.

See CONTRIBUTING.md for contribution guidelines.

@github-actions github-actions Bot added the review/awaiting-author Reviewed; waiting on the author to respond label Jul 7, 2026
@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Jul 7, 2026 •

Copy link
Copy Markdown

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Updated (UTC)
✅ Deployment successful!
View logs
emdash-playground ae2e421 Jul 08 2026, 09:27 AM

@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Jul 7, 2026 •

Copy link
Copy Markdown

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Updated (UTC)
✅ Deployment successful!
View logs
emdash-demo-cache ae2e421 Jul 08 2026, 09:27 AM

@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Jul 7, 2026 •

Copy link
Copy Markdown

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Updated (UTC)
✅ Deployment successful!
View logs
emdash-demo-do ae2e421 Jul 08 2026, 09:27 AM

@pkg-pr-new

pkg-pr-new Bot commented Jul 7, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

@emdash-cms/admin

npm i https://pkg.pr.new/@emdash-cms/admin@1867

@emdash-cms/auth

npm i https://pkg.pr.new/@emdash-cms/auth@1867

@emdash-cms/auth-atproto

npm i https://pkg.pr.new/@emdash-cms/auth-atproto@1867

@emdash-cms/blocks

npm i https://pkg.pr.new/@emdash-cms/blocks@1867

@emdash-cms/cloudflare

npm i https://pkg.pr.new/@emdash-cms/cloudflare@1867

@emdash-cms/contentful-to-portable-text

npm i https://pkg.pr.new/@emdash-cms/contentful-to-portable-text@1867

emdash

npm i https://pkg.pr.new/emdash@1867

create-emdash

npm i https://pkg.pr.new/create-emdash@1867

@emdash-cms/gutenberg-to-portable-text

npm i https://pkg.pr.new/@emdash-cms/gutenberg-to-portable-text@1867

@emdash-cms/plugin-cli

npm i https://pkg.pr.new/@emdash-cms/plugin-cli@1867

@emdash-cms/plugin-types

npm i https://pkg.pr.new/@emdash-cms/plugin-types@1867

@emdash-cms/registry-client

npm i https://pkg.pr.new/@emdash-cms/registry-client@1867

@emdash-cms/registry-lexicons

npm i https://pkg.pr.new/@emdash-cms/registry-lexicons@1867

@emdash-cms/sandbox-workerd

npm i https://pkg.pr.new/@emdash-cms/sandbox-workerd@1867

@emdash-cms/x402

npm i https://pkg.pr.new/@emdash-cms/x402@1867

@emdash-cms/plugin-ai-moderation

npm i https://pkg.pr.new/@emdash-cms/plugin-ai-moderation@1867

@emdash-cms/plugin-atproto

npm i https://pkg.pr.new/@emdash-cms/plugin-atproto@1867

@emdash-cms/plugin-audit-log

npm i https://pkg.pr.new/@emdash-cms/plugin-audit-log@1867

@emdash-cms/plugin-color

npm i https://pkg.pr.new/@emdash-cms/plugin-color@1867

@emdash-cms/plugin-embeds

npm i https://pkg.pr.new/@emdash-cms/plugin-embeds@1867

@emdash-cms/plugin-field-kit

npm i https://pkg.pr.new/@emdash-cms/plugin-field-kit@1867

@emdash-cms/plugin-forms

npm i https://pkg.pr.new/@emdash-cms/plugin-forms@1867

@emdash-cms/plugin-webhook-notifier

npm i https://pkg.pr.new/@emdash-cms/plugin-webhook-notifier@1867

commit: ae2e421

@github-actions github-actions Bot added review/needs-rereview New commits since the last review area/templates cla: signed and removed review/awaiting-author Reviewed; waiting on the author to respond labels Jul 7, 2026

@emdashbot emdashbot Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR adds the admin/operator REST surface for the existing content media-usage repair machinery: a new POST /_emdash/api/admin/media-usage/repair route, handler, strict Zod schemas, client method, OpenAPI docs, REST reference docs, tests, and a changeset.

Approach assessment

Assuming the maintainer-directed “Patch 4C” framing is accurate, the change is a sensible, additive API surface. It does not introduce new DB queries or repair logic; it exposes the already-existing repairContentMediaUsageCollection / repairContentMediaUsageAll functions with proper auth, CSRF, request validation, and a shaped response envelope. The permission choice (schema:manage) and the token-scope requirement (admin, because the path is under /_emdash/api/admin) match the stated operator-only intent.

I cannot verify the maintainer direction or an approved Discussion from the diff alone, so I’m treating that as a process note rather than a code blocker.

What I checked

  • Auth / CSRF: The route lives under /_emdash/api/admin, so the middleware enforces the admin token scope; the route itself checks schema:manage via requirePerm. Non-ADMIN users and missing/bad tokens are rejected with the expected 401/403. Session-authenticated POSTs still require X-EmDash-Request: 1 (verified in tests and middleware).
  • Request/response shape: The mediaUsageRepairBody is a strict discriminated union with explicit scope: "collection" and scope: "all" shapes; there is no silent default to all-content repair. Unknown collections return a structured 200 with status: "failed" and lastErrorCode: "COLLECTION_NOT_FOUND", consistent with the docs.
  • API conventions: The route uses requireDb, requirePerm, parseBody, unwrapResult, and the standard ApiResult envelope. The new MEDIA_USAGE_REPAIR_ERROR maps to 500 via the existing mapErrorStatus fallback.
  • Client: EmDashClient.mediaRepairUsage() POSTs to the correct prefixed path and accepts the same explicit input union.
  • OpenAPI: The new path is registered in buildMediaPaths, uses the Zod schemas as source of truth, and documents the expected 200/400/401/403/413/500 responses.
  • AGENTS.md: No new admin UI strings (Lingui/RTL n/a), no dynamic SQL added, no new permission string invented, collection slug validation aligns with existing schemas, and a minor changeset is present.

Headline conclusion

The diff is clean and well-tested. I found no blocking issues, regressions, or convention violations.

@github-actions github-actions Bot added review/approved Approved; no new commits since and removed review/needs-rereview New commits since the last review labels Jul 7, 2026
@github-actions

github-actions Bot commented Jul 8, 2026

Copy link
Copy Markdown
Contributor

Overlapping PRs

This PR modifies files that are also changed by other open PRs:

This may cause merge conflicts or duplicated work. A maintainer will coordinate.

@khoinguyenpham04
khoinguyenpham04 requested a review from ascorbic July 8, 2026 09:02

@ascorbic ascorbic left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What will this be used by?

@github-actions github-actions Bot added review/needs-rereview New commits since the last review and removed review/approved Approved; no new commits since labels Jul 8, 2026
@khoinguyenpham04

Copy link
Copy Markdown
Collaborator Author

What will this be used by?

It will be used by the next patch:

  • Patch 4.5 adds CLI/MCP commands that call this API.
  • Operators can run repair after imports, migrations, or bugs that leave media Used In data out of sync.

The overall plan here is repair service exists already. This patch exposes it safely over REST and EmDashClient. The next PR will add CLI/MCP commands for operators.

@ascorbic ascorbic left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@github-actions github-actions Bot added review/approved Approved; no new commits since and removed review/needs-rereview New commits since the last review labels Jul 8, 2026
@khoinguyenpham04
khoinguyenpham04 merged commit c57b12b into main Jul 8, 2026
52 checks passed
@khoinguyenpham04
khoinguyenpham04 deleted the media-usage-repair-admin-api branch July 8, 2026 10:43
@emdashbot emdashbot Bot mentioned this pull request Jul 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants