Files
supabase/apps/kb/scripts/federated-content/sync-troubleshooting-updates.ts
Jeremias Menichelli 30833db87e chore: Refactor sync troubleshooting scripts after data reconciliation (#52713)
## Problem

We solved manually some `troubleshooting_entries` table missing data and
mismatches.

- **Rows without slugs.** Some `troubleshooting_entries` rows had no
slug, so no guide could match them and the guides looked "new".
- **Duplicate discussions.** A guide that didn't match its row got a
fresh GitHub discussion, leaving the original behind (sometimes with
extras).
- **Slug mismatch.** The fetch stripped the hash suffix from file names
while the table keeps it, so those guides never matched their rows.
- **Overlapping runs.** The workflow had no concurrency control, so two
runs could both create a discussion for the same guide, and `upsert`
silently overwrote the first run's row.
- **Missing checksums.** Rows without a checksum counted as "changed"
and were re-pushed to GitHub on every run.
- **Weak dry run.** `--dry-run` only printed slugs, so it was hard to
see what a run would actually do.
- **Fragile lookup and duplicated code.** Rows were fetched with every
slug in the URL (`.in('slug', …)`), and both scripts carried their own
copy of the guide reader and checksum.

## Solution

After those steps we applied successfully to the database, we revamp our
existing sync scripts to allow dry runs to make sure the changes we
apply are going to generate the correct impact.

A new improvement to prevent bulk discussion errors on GitHub API.

<!--
## Preview links

If relevant, include links to changed pages for easy review access.

Copy the preview base URL from the Vercel bot comment on this PR. Use
the following table as an example template.

| Site | Live | Preview | Search for |
| -------------- |
-------------------------------------------------------------------------
|
------------------------------------------------------------------------------------------------------------
| ----------------------------- |
| WWW | [/blog/your-post](https://supabase.com/blog/your-post) |
[/blog/your-post](https://zone-www-dot-com-git-branch-name-supabase.vercel.app/blog/your-post)
| unique phrase from the change |
| Docs |
[/docs/guides/your-page](https://supabase.com/docs/guides/your-page) |
[/docs/guides/your-page](https://docs-git-branch-name-supabase.vercel.app/docs/guides/your-page)
| unique phrase from the change |
| Studio | [/dashboard](https://supabase.com/dashboard) |
[/dashboard](https://studio-git-branch-name-supabase.vercel.app/dashboard)
| unique phrase from the change |
| Design system | [/design-system](https://supabase.com/design-system) |
[/design-system](https://design-system-git-branch-name-supabase.vercel.app/design-system)
| unique phrase from the change |
| UI library | [/library](https://supabase.com/library) |
[/library](https://ui-library-git-branch-name-supabase.vercel.app/library)
| unique phrase from the change |
| Knowledge base |
[/kb/guides/your-page](https://supabase.com/kb/guides/your-page) |
[/kb/guides/your-page](https://kb-git-branch-name-supabase.vercel.app/kb/guides/your-page)
| unique phrase from the change |
-->

<!-- ## Additional context

Optionally add any other context or screenshots.

-->

## Review instructions
_Sadly, we need to merge, and pray..._

## Checklist

Check all before review:

- [x] I have read
[CONTRIBUTING.md](https://github.com/supabase/supabase/blob/master/CONTRIBUTING.md)
- [x] If I wrote a new docs topic or edited an existing topic, I used
the `/write-the-docs` or `/edit-the-docs` skill, which applies the docs
[style
guide](https://github.com/supabase/supabase/tree/master/apps/docs/style-guide)
2026-11-04 10:24:19 +01:00

154 lines
4.9 KiB
TypeScript

// Pushes title/content updates to already-synced GitHub Discussions when the
// local guide's checksum differs from the one stored on its row (a null
// checksum counts as changed). Guides are matched to rows by exact slug; a
// guide with no row is skipped (sync-troubleshooting-entries.ts creates it).
// The discussion is updated first and the checksum stored second, so a failed
// DB write only means the next run pushes the same content again.
//
// Run after sync-troubleshooting-entries.ts, from CI only.
//
// --dry-run is read-only: prints each discussion it would update (url, id,
// old -> new checksum) and updates nothing.
import '../utils/dotenv.js'
import { createPacer, isRateLimitError, readMinIntervalMs } from './github-throttle.ts'
import {
computeChecksum,
createDb,
createOctokit,
fetchAllEntries,
readLocalGuides,
type EntryRow,
type RetryOctokitInstance,
} from './troubleshooting-sync-shared.ts'
const DRY_RUN = process.argv.includes('--dry-run')
const LOG = '[sync-troubleshooting-updates]'
async function updateDiscussion(
octokit: RetryOctokitInstance,
discussionId: string,
title: string,
body: string
): Promise<void> {
const mutation = `
mutation UpdateDiscussionMutation($discussionId: ID!, $title: String!, $body: String!) {
updateDiscussion(input: { discussionId: $discussionId, title: $title, body: $body }) {
discussion { id }
}
}
`
// retries: 0 disables @octokit/plugin-retry; mutations are sent once.
await octokit.graphql(mutation, { discussionId, title, body, request: { retries: 0 } })
}
async function syncDiscussionUpdates() {
const guides = await readLocalGuides()
const db = createDb()
const octokit = createOctokit()
const entries = await fetchAllEntries(db)
const entryBySlug = new Map<string, EntryRow>()
for (const entry of entries) if (entry.slug) entryBySlug.set(entry.slug, entry)
let unchanged = 0
let withoutRow = 0
let updated = 0
let noStoredChecksum = 0
const failures: string[] = []
const notAttempted: string[] = []
const minIntervalMs = readMinIntervalMs()
const pace = createPacer(minIntervalMs)
let rateLimited = false
console.log(
`${LOG} ${guides.length} local guide(s), ${entries.length} row(s)${DRY_RUN ? ' (dry run)' : ''}.`
)
if (DRY_RUN) {
console.log(
` (pacing: at least ${minIntervalMs}ms between updates; a failed update is not retried)`
)
}
for (const guide of guides) {
const row = entryBySlug.get(guide.slug)
if (!row) {
withoutRow++
continue
}
const newChecksum = computeChecksum(guide.title, guide.body)
if (row.checksum && row.checksum === newChecksum) {
unchanged++
continue
}
if (!row.github_id) {
console.error(`${LOG} ${guide.slug}: row ${row.id} has no github_id, cannot update.`)
failures.push(guide.slug)
continue
}
const reason = row.checksum ? 'content changed' : 'no stored checksum'
if (!row.checksum) noStoredChecksum++
if (DRY_RUN) {
console.log(` ~ ${guide.slug}`)
console.log(` reason: ${reason}`)
console.log(` discussion: UPDATE ${row.github_url} (${row.github_id})`)
console.log(` title: ${guide.title}`)
console.log(` body: ${guide.body.length} chars`)
console.log(
` row: UPDATE row ${row.id} { checksum: ${row.checksum ?? 'null'} -> ${newChecksum} }`
)
updated++
continue
}
// After a rate limit the next calls would fail too: stop, next run resumes.
if (rateLimited) {
notAttempted.push(guide.slug)
continue
}
try {
await pace()
await updateDiscussion(octokit, row.github_id, guide.title, guide.body)
const { error } = await db
.from('troubleshooting_entries')
.update({ checksum: newChecksum })
.eq('id', row.id)
if (error) throw error
console.log(` - updated ${guide.slug} (${reason}) -> ${row.github_url}`)
updated++
} catch (err) {
console.error(`${LOG} Failed to update ${guide.slug}:`, err)
failures.push(guide.slug)
if (isRateLimitError(err)) rateLimited = true
}
}
console.log(
`\n${LOG} ${DRY_RUN ? 'Would update' : 'Updated'} ${updated} discussion(s) ` +
`(${updated - noStoredChecksum} changed, ${noStoredChecksum} with no stored checksum); ` +
`${unchanged} unchanged, ${withoutRow} without a row yet.`
)
if (notAttempted.length > 0) {
console.error(
`${LOG} Stopped early: GitHub rate-limited the request. ${notAttempted.length} discussion(s) not ` +
`attempted, the next run will pick them up (or raise --delay-ms): ${notAttempted.join(', ')}`
)
process.exitCode = 1
}
if (failures.length > 0) {
console.error(`${LOG} Failed: ${failures.join(', ')}`)
process.exitCode = 1
}
}
syncDiscussionUpdates().catch((error) => {
console.error(error)
process.exit(1)
})