railyard/apps/ritgram/web/services/githubImportService.ts
caelum0x 6801c08af9
Some checks failed
ci / cargo test (abacus) (push) Has been cancelled
ci / cargo test (agent-runtime) (push) Has been cancelled
ci / cargo test (context-passport) (push) Has been cancelled
ci / cargo test (game-economy) (push) Has been cancelled
ci / cargo test (ledgerline) (push) Has been cancelled
ci / cargo test (mint) (push) Has been cancelled
ci / cargo test (rago) (push) Has been cancelled
ci / cargo test (relay) (push) Has been cancelled
ci / node suites (push) Has been cancelled
ci / cargo test (toolgate) (push) Has been cancelled
ci / cargo test (turnstile) (push) Has been cancelled
ci / cargo test (vend) (push) Has been cancelled
feat(RIT-OPS,RIT-BRD): rename gitea -> rit everywhere we control, deployed + live-verified
apps/gitea -> apps/rithub-server; compose services/containers renamed
(rithub-server-app/postgres/act-runner) with explicit project name and
volume names pinned to the exact existing Docker volumes (no data loss);
our own GITEA_* compose/env var names -> RIT_*; new rit-admin CLI wrapper
on railyard-box; giteaSession.mjs/giteaOAuth.mjs/giteaInfo.mjs ->
ritSession/ritOAuth/ritInfo across 6 apps; GiteaForgeAdapter -> RitForgeAdapter
+ giteaSyncService/importGiteaRepos/syncGiteaRepos -> rit* in ritgram
(tsc clean); apps/console/lib/gitea.ts -> lib/ritProvision.ts; Go
internal/giteaclient -> internal/ritclient in rithub-async-import (go
build/test clean); new /auth/rit + /forges/rit/connect routes with
/gitea back-compat aliases; locale/swagger/footer/captcha template text
scrubbed of remaining "Gitea" strings.

Deployed to railyard-box: old containers stopped/removed, /srv/railyard-gitea
renamed to /srv/rithub-server with a symlink kept, docker compose up -d from
the new path, act_runner network-alias fix for its persisted instance URL.
Live-verified: docker compose config diff showed zero unintended value
changes, DB row counts unchanged (19 users, 635 repos), full route
regression 200/expected, 0 "gitea" in headers/bodies on every checked page,
real git clone+push over HTTPS and SSH with a disposable account (deleted
after). Deferred on purpose (documented in plans/railyard.md): the forge
discriminator / DB column names / Postgres role+volume names (data
migrations, not a naming change) and the upstream GITEA__ env-to-ini keys
and X-Gitea-* wire headers (hardcoded in the vendored engine binary).

See plans/railyard.md RIT-OPS/RIT-BRD entry for the full old->new map and
live evidence.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-12-01 16:34:39 +03:00

103 lines
3.4 KiB
TypeScript

import api from './api'
// ---------------------------------------------------------------------------
// "Move from GitHub" — paste a username (public repos, no token) or an
// optional fine-grained read token (private repos), tick repos, Rithub
// mirrors them via Rithub's built-in migration API with live progress.
// Mirrors apps/ritgram/backend/src/controllers/githubImportController.ts.
// ---------------------------------------------------------------------------
export interface GithubImportRepo {
fullName: string
name: string
description: string | null
isPrivate: boolean
stars: number
language: string | null
updatedAt: string | null
}
export interface GithubImportPreview {
username: string
repos: GithubImportRepo[]
hasToken: boolean
}
export type GithubImportEvent =
| { type: 'start'; total: number }
| { type: 'migrating'; repo: string }
| { type: 'migrated'; repo: string; htmlUrl: string }
| { type: 'skipped'; repo: string; reason: string; htmlUrl: string }
| { type: 'failed'; repo: string; reason: string }
| { type: 'done'; migrated: number; skipped: number; failed: number; profileUrl: string }
function apiBase(): string {
const base = process.env.NEXT_PUBLIC_API_URL || 'http://localhost:4000/api'
return `${base.replace(/\/+$/, '')}/v1`
}
export const githubImportService = {
/** Public — no Ritgram login required. Never stores the token server-side. */
preview: async (username: string, token?: string): Promise<GithubImportPreview> => {
const { data } = await api.post('/github-import/preview', { username, token })
return data.data
},
/**
* Requires Ritgram login + a connected Rithub account. Streams
* Server-Sent Events over a fetch() POST (EventSource has no POST body
* support) and invokes `onEvent` for each one as it arrives.
*/
start: async (
params: { username: string; token?: string; repos: string[] },
onEvent: (event: GithubImportEvent) => void,
signal?: AbortSignal
): Promise<void> => {
const response = await fetch(`${apiBase()}/github-import/start`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
credentials: 'include',
body: JSON.stringify(params),
signal,
})
if (!response.ok || !response.body) {
let message = `Import failed (${response.status})`
try {
const body = await response.json()
message = body?.error?.message || body?.message || message
} catch {
// non-JSON error body; keep default message
}
throw new Error(message)
}
const reader = response.body.getReader()
const decoder = new TextDecoder()
let buffer = ''
for (;;) {
const { value, done } = await reader.read()
if (done) break
buffer += decoder.decode(value, { stream: true })
// SSE frames are separated by a blank line; each frame may carry
// multiple "data: ..." lines, but this server only ever sends one.
let boundary = buffer.indexOf('\n\n')
while (boundary !== -1) {
const frame = buffer.slice(0, boundary)
buffer = buffer.slice(boundary + 2)
const line = frame.split('\n').find((l) => l.startsWith('data:'))
if (line) {
try {
onEvent(JSON.parse(line.slice(5).trim()) as GithubImportEvent)
} catch {
// ignore a malformed frame rather than aborting the whole import
}
}
boundary = buffer.indexOf('\n\n')
}
}
},
}