Something went wrong. Try again.
move github sites
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153// Apply a plan against Tangled.//// Deliberately conservative: it only touches repos that already exist on Tangled// (repo sync is a separate concern), it never picks an index for you, and it// refuses to overwrite an existing site config unless asked.
import * as t from './tangled.mjs'
/** * Find the Tangled repo corresponding to a GitHub repo name. * * `name` on the sh.tangled.repo record is the right key, but records written by * some clients leave it null, so the rkey is the only handle they have. The rkey * is not a reliable stand-in — it is sometimes a TID, and when it is a name it * can differ from the GitHub one — so an rkey hit is reported as a guess and a * plural match is never resolved silently. */export function matchRepo(repos, name) { const want = name.toLowerCase()
const named = repos.filter((r) => r.name?.toLowerCase() === want) if (named.length === 1) return { target: named[0] } if (named.length > 1) { return { status: 'ambiguous', detail: `${named.length} Tangled repos are named ${name} (rkeys ${named.map((r) => r.rkey).join(', ')})`, } }
const keyed = repos.filter((r) => !r.name && r.rkey.toLowerCase() === want) if (keyed.length === 1) { return { target: keyed[0], detail: `matched on rkey; the record has no name`, } } if (keyed.length > 1) { return { status: 'ambiguous', detail: `${keyed.length} unnamed records share the rkey ${name}` } }
const unnamed = repos.filter((r) => !r.name).length return { status: 'missing', detail: unnamed ? `no repo of that name on Tangled; ${unnamed} records have no name and could not be matched` : 'no repo of that name on Tangled yet', }}
export async function migrate(plan, { handle, password, apply = false, force = false } = {}) { if (!(await t.xrpcAvailable())) throw new Error(t.XRPC_UNAVAILABLE)
const session = await t.login(handle, password)
const claim = await t.getDomainClaim(session) if (!claim.domain) { throw new Error( 'this account has no Tangled sites domain claimed — claim one in settings, or run `entangle claim <subdomain>`' ) } if (plan.domain && plan.domain !== claim.domain) { throw new Error( `plan targets ${plan.domain} but the account has claimed ${claim.domain}; re-run \`entangle plan --domain ${claim.domain}\`` ) }
const tangledRepos = await t.listRepos(session)
const results = [] for (const entry of plan.entries) { const { target, status, detail } = matchRepo(tangledRepos, entry.name)
if (!target) { results.push({ ...entry, status, detail }) continue } // Carried onto every downstream result so a guessed match stays visible. const matchNote = detail ? { matchedBy: detail } : {} if (!target.repoDid) { results.push({ ...entry, status: 'no-repo-did', detail: 'repo record has no repoDid; it predates repo DIDs and cannot host a site', }) continue }
// Only a 404 means "no site yet". Any other failure (auth, a server error) // leaves the current config unknown, and writing over an unknown config is // exactly what --force exists to gate. let existing try { existing = await t.getSiteConfig(session, target.repoDid) } catch (e) { if (e.status !== 404) { results.push({ ...entry, status: 'failed', detail: `could not read the current site config: ${e.message}`, }) continue } existing = {} } if (existing?.config && !force) { results.push({ ...entry, status: 'exists', detail: `already serving ${existing.config.branch} @ ${existing.config.dir}`, }) continue }
if (!apply) { results.push({ ...entry, ...matchNote, status: 'would-deploy', repoDid: target.repoDid }) continue }
try { await t.updateSiteConfig(session, { repoDid: target.repoDid, branch: entry.branch, dir: entry.dir, isIndex: entry.isIndex, }) results.push({ ...entry, ...matchNote, status: 'deployed', repoDid: target.repoDid }) } catch (e) { results.push({ ...entry, status: 'failed', detail: String(e.message || e) }) } }
return { domain: claim.domain, applied: apply, results }}
export function migrateSummary(r) { const counts = {} for (const x of r.results) counts[x.status] = (counts[x.status] ?? 0) + 1 const L = [ `${r.applied ? 'Applied' : 'Dry run'} against ${r.domain}`, ...Object.entries(counts).map(([k, v]) => ` ${k}: ${v}`), '', ] for (const x of r.results) { if (x.status === 'deployed' || x.status === 'would-deploy') { const note = x.matchedBy ? ` [${x.matchedBy}]` : '' L.push(` ${x.status.padEnd(13)} ${x.name} → ${x.targetUrl} (${x.branch} @ ${x.dir})${note}`) } else { L.push(` ${x.status.padEnd(13)} ${x.name} — ${x.detail}`) } } return L.join('\n')}