GitHub OAuth app approval is blocked, so this ships the next-best onboarding path: paste a GitHub username (public repos, no token) or an optional fine-grained read token (private repos), tick repos, and Rithub mirrors each one with Gitea's built-in /repos/migrate, streamed as live per-repo progress over SSE, landing on the new Ritgram profile when done. - GiteaForgeAdapter.migrateRepo(): calls the real Gitea migrate API, treats 409 as "already there", and self-heals a false failure (proxy timeout or a post-clone step erroring after the repo already landed — both observed live against git.ereboslab.com) by re-checking existence with backoff before reporting a real failure. - New public POST /api/v1/github-import/preview and auth'd POST /api/v1/github-import/start (requires an existing Rithub/Gitea connection); the GitHub token, when provided, is used once in-memory for that request and is never stored or logged. Both routes are rate limited. - New /import-from-github page (public route) with username/token form, a tickable repo list, and a live per-repo progress list fed by SSE. Verified end to end against the live git.ereboslab.com instance: created a disposable Gitea test account, ran preview + start through the real HTTP routes against real octocat.github.com repos, confirmed migrated repos landing on Gitea, then deleted the test repos, the test Gitea account, and the matching Ritgram test user + DB rows. Found and noted a real, unrelated blocker: railyard-box is at 99% disk (for reference, this backend's dev/db config lives in apps/ritgram/backend/.env, pointed at the project's Neon dev DB; no change needed here). Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
277 lines
10 KiB
TypeScript
277 lines
10 KiB
TypeScript
'use client'
|
|
|
|
/**
|
|
* /import-from-github — "Move from GitHub" without GitHub OAuth.
|
|
*
|
|
* One-click "Sign in with GitHub" needs a GitHub OAuth app we don't have
|
|
* approved yet (see /docs/import-from-github for the manual fallback). This
|
|
* page is the real next-best thing: paste a GitHub username to see public
|
|
* repos (no token needed), or an optional fine-grained *read* token to also
|
|
* see private ones, tick the repos to move, and Rithub mirrors them via
|
|
* Gitea's own migration API with a live per-repo progress list. On success
|
|
* it lands on the new Ritgram profile.
|
|
*
|
|
* The token (if any) is sent once to our server for this request and is
|
|
* never stored or logged — see githubImportController.ts.
|
|
*/
|
|
|
|
import { useCallback, useMemo, useState } from 'react'
|
|
import { useRouter } from 'next/navigation'
|
|
import Link from 'next/link'
|
|
import { useAuthStore } from '@/store/authStore'
|
|
import { forgeService } from '@/services/forgeService'
|
|
import {
|
|
githubImportService,
|
|
GithubImportEvent,
|
|
GithubImportRepo,
|
|
} from '@/services/githubImportService'
|
|
import { useToast } from '@/components/ui/toast'
|
|
import { getErrorMessage } from '@/utils/error'
|
|
import { Button } from '@/components/ui/button'
|
|
import { Input } from '@/components/ui/input'
|
|
|
|
type RepoStatus = 'pending' | 'migrating' | 'migrated' | 'skipped' | 'failed'
|
|
|
|
export default function ImportFromGitHubPage() {
|
|
const router = useRouter()
|
|
const { isAuthenticated } = useAuthStore()
|
|
const { showToast } = useToast()
|
|
|
|
const [username, setUsername] = useState('')
|
|
const [token, setToken] = useState('')
|
|
const [loadingPreview, setLoadingPreview] = useState(false)
|
|
const [previewError, setPreviewError] = useState<string | null>(null)
|
|
const [repos, setRepos] = useState<GithubImportRepo[]>([])
|
|
const [resolvedUsername, setResolvedUsername] = useState<string | null>(null)
|
|
const [selected, setSelected] = useState<Record<string, boolean>>({})
|
|
const [statuses, setStatuses] = useState<Record<string, RepoStatus>>({})
|
|
const [importing, setImporting] = useState(false)
|
|
const [importDone, setImportDone] = useState<{ migrated: number; skipped: number; failed: number; profileUrl: string } | null>(null)
|
|
const [giteaConnected, setGiteaConnected] = useState<boolean | null>(null)
|
|
|
|
const selectedCount = useMemo(() => Object.values(selected).filter(Boolean).length, [selected])
|
|
|
|
const checkGiteaConnection = useCallback(async () => {
|
|
if (!isAuthenticated) return
|
|
try {
|
|
const { available } = await forgeService.list()
|
|
setGiteaConnected(available.some((f) => f.forge === 'gitea' && f.connected))
|
|
} catch {
|
|
setGiteaConnected(null)
|
|
}
|
|
}, [isAuthenticated])
|
|
|
|
const handlePreview = async (e: React.FormEvent) => {
|
|
e.preventDefault()
|
|
if (!username.trim()) return
|
|
setLoadingPreview(true)
|
|
setPreviewError(null)
|
|
setImportDone(null)
|
|
try {
|
|
const result = await githubImportService.preview(username.trim(), token.trim() || undefined)
|
|
setRepos(result.repos)
|
|
setResolvedUsername(result.username)
|
|
const initial: Record<string, boolean> = {}
|
|
result.repos.forEach((r) => (initial[r.fullName] = true))
|
|
setSelected(initial)
|
|
setStatuses({})
|
|
if (result.repos.length === 0) {
|
|
setPreviewError('No repos found for that account.')
|
|
}
|
|
await checkGiteaConnection()
|
|
} catch (error: unknown) {
|
|
setPreviewError(getErrorMessage(error))
|
|
setRepos([])
|
|
} finally {
|
|
setLoadingPreview(false)
|
|
}
|
|
}
|
|
|
|
const toggleRepo = (fullName: string) => {
|
|
setSelected((prev) => ({ ...prev, [fullName]: !prev[fullName] }))
|
|
}
|
|
|
|
const handleImport = async () => {
|
|
const repoFullNames = Object.keys(selected).filter((k) => selected[k])
|
|
if (repoFullNames.length === 0) return
|
|
|
|
if (!isAuthenticated) {
|
|
showToast('Create a free Ritgram account first, then come back here.', 'error')
|
|
router.push(`/register?next=/import-from-github`)
|
|
return
|
|
}
|
|
if (giteaConnected === false) {
|
|
showToast('Connect Rithub in Settings → Account first.', 'error')
|
|
router.push('/settings/account')
|
|
return
|
|
}
|
|
|
|
setImporting(true)
|
|
setImportDone(null)
|
|
const next: Record<string, RepoStatus> = {}
|
|
repoFullNames.forEach((name) => (next[name] = 'pending'))
|
|
setStatuses(next)
|
|
|
|
try {
|
|
await githubImportService.start(
|
|
{ username: resolvedUsername || username.trim(), token: token.trim() || undefined, repos: repoFullNames },
|
|
(event: GithubImportEvent) => {
|
|
if (event.type === 'migrating') {
|
|
setStatuses((prev) => ({ ...prev, [event.repo]: 'migrating' }))
|
|
} else if (event.type === 'migrated') {
|
|
setStatuses((prev) => ({ ...prev, [event.repo]: 'migrated' }))
|
|
} else if (event.type === 'skipped') {
|
|
setStatuses((prev) => ({ ...prev, [event.repo]: 'skipped' }))
|
|
} else if (event.type === 'failed') {
|
|
setStatuses((prev) => ({ ...prev, [event.repo]: 'failed' }))
|
|
} else if (event.type === 'done') {
|
|
setImportDone(event)
|
|
}
|
|
}
|
|
)
|
|
} catch (error: unknown) {
|
|
showToast(getErrorMessage(error), 'error')
|
|
} finally {
|
|
setImporting(false)
|
|
}
|
|
}
|
|
|
|
return (
|
|
<div className="max-w-3xl mx-auto py-16 px-4">
|
|
<h1 className="text-4xl font-bold mb-3">Move from GitHub</h1>
|
|
<p className="text-lg text-foreground-secondary mb-10">
|
|
Paste your GitHub username to see your public repos — no token needed. Add a
|
|
fine-grained read token if you also want to bring over private repos. Rithub
|
|
mirrors each one with its own built-in import, full history included.
|
|
</p>
|
|
|
|
<form onSubmit={handlePreview} className="space-y-4 mb-8">
|
|
<Input
|
|
label="GitHub username"
|
|
placeholder="octocat"
|
|
value={username}
|
|
onChange={(e) => setUsername(e.target.value)}
|
|
disabled={loadingPreview}
|
|
autoComplete="off"
|
|
/>
|
|
<Input
|
|
label="Fine-grained token (optional, for private repos)"
|
|
placeholder="github_pat_..."
|
|
type="password"
|
|
value={token}
|
|
onChange={(e) => setToken(e.target.value)}
|
|
disabled={loadingPreview}
|
|
autoComplete="off"
|
|
/>
|
|
<p className="text-xs text-foreground-secondary">
|
|
Used once to list and import your repos, then discarded — never stored, never logged.
|
|
Create a read-only token at{' '}
|
|
<a
|
|
href="https://github.com/settings/personal-access-tokens/new"
|
|
target="_blank"
|
|
rel="noreferrer"
|
|
className="underline"
|
|
>
|
|
github.com/settings/personal-access-tokens
|
|
</a>
|
|
.
|
|
</p>
|
|
<Button type="submit" loading={loadingPreview} disabled={!username.trim()}>
|
|
Find repos
|
|
</Button>
|
|
</form>
|
|
|
|
{previewError && <p className="text-error mb-6">{previewError}</p>}
|
|
|
|
{repos.length > 0 && (
|
|
<div className="space-y-3 mb-8">
|
|
<div className="flex items-center justify-between">
|
|
<h2 className="text-xl font-semibold">
|
|
{repos.length} repo{repos.length === 1 ? '' : 's'} found for {resolvedUsername}
|
|
</h2>
|
|
<span className="text-sm text-foreground-secondary">{selectedCount} selected</span>
|
|
</div>
|
|
<div className="border border-border rounded-xl divide-y divide-border max-h-[420px] overflow-y-auto">
|
|
{repos.map((repo) => {
|
|
const status = statuses[repo.fullName]
|
|
return (
|
|
<label
|
|
key={repo.fullName}
|
|
className="flex items-center gap-3 p-3 cursor-pointer hover:bg-surface-secondary"
|
|
>
|
|
<input
|
|
type="checkbox"
|
|
checked={Boolean(selected[repo.fullName])}
|
|
onChange={() => toggleRepo(repo.fullName)}
|
|
disabled={importing}
|
|
className="h-4 w-4"
|
|
/>
|
|
<div className="flex-1 min-w-0">
|
|
<div className="font-medium truncate">
|
|
{repo.name}
|
|
{repo.isPrivate && (
|
|
<span className="ml-2 text-xs text-foreground-secondary border border-border rounded px-1">
|
|
private
|
|
</span>
|
|
)}
|
|
</div>
|
|
{repo.description && (
|
|
<div className="text-sm text-foreground-secondary truncate">{repo.description}</div>
|
|
)}
|
|
</div>
|
|
<div className="text-xs text-foreground-secondary w-24 text-right">
|
|
{statusLabel(status)}
|
|
</div>
|
|
</label>
|
|
)
|
|
})}
|
|
</div>
|
|
|
|
<Button onClick={handleImport} loading={importing} disabled={selectedCount === 0}>
|
|
Move {selectedCount} repo{selectedCount === 1 ? '' : 's'} to Rithub
|
|
</Button>
|
|
|
|
{!isAuthenticated && (
|
|
<p className="text-sm text-foreground-secondary">
|
|
You'll need a free Ritgram account and a connected Rithub account to finish —
|
|
we'll walk you through it.
|
|
</p>
|
|
)}
|
|
</div>
|
|
)}
|
|
|
|
{importDone && (
|
|
<div className="border border-border rounded-xl p-6 bg-surface space-y-3">
|
|
<p className="font-semibold">
|
|
Done — {importDone.migrated} moved, {importDone.skipped} already there, {importDone.failed} failed.
|
|
</p>
|
|
<Button onClick={() => router.push(importDone.profileUrl)}>Go to your Ritgram profile</Button>
|
|
</div>
|
|
)}
|
|
|
|
<p className="text-sm text-foreground-secondary mt-10">
|
|
Prefer the command line, or moving a huge repo?{' '}
|
|
<Link href="/docs/import-from-github" className="underline">
|
|
See the manual steps
|
|
</Link>
|
|
.
|
|
</p>
|
|
</div>
|
|
)
|
|
}
|
|
|
|
function statusLabel(status: RepoStatus | undefined): string {
|
|
switch (status) {
|
|
case 'migrating':
|
|
return 'Moving…'
|
|
case 'migrated':
|
|
return 'Done'
|
|
case 'skipped':
|
|
return 'Already there'
|
|
case 'failed':
|
|
return 'Failed'
|
|
default:
|
|
return ''
|
|
}
|
|
}
|