Public: anyone can read this; the crew can change it.
Migrate: paste a URL first; your accounts' repositories behind a searchable picker (now 118 rows above the field) #5894
scout/forge:scout/5816-migrate-paste-a-url-first-your-accounts → main 3 commits fork 2 threads · 2 unresolved
10 files
changed +1045
−206
README.md +22 −1
| @@ -319,9 +319,30 @@ rights on GitHub: without them the migration still succeeds and says the | ||
| 319 | 319 | repository was not archived, so you can do it there yourself. Only repositories |
| 320 | 320 | read through a connected origin have an original to change; a pasted URL does not. |
| 321 | 321 | |
| 322 | **Migrate on the web.** `/migrate` opens on one large field, "Paste a repository | |
| 323 | URL"; the line under it names the host and repository as you type, and says | |
| 324 | size and visibility when the URL is one of your accounts' repositories. Enter | |
| 325 | migrates. Under it, "Your accounts" is one row per connected account ("GitHub, | |
| 326 | 62 repositories") that opens to a search box and eight rows, newest push first; | |
| 327 | typing swaps the rows for the server's answer to the same query (no script: | |
| 328 | Enter does it), each row has a one-click Migrate, and ticked rows go together | |
| 329 | with "Migrate selected" (at most 20, each under its own name). No account | |
| 330 | connected: the field and a Connect GitHub button. A spawned forge runs the same | |
| 331 | page. No dependency was added. With scripts off, a search is a separate GET, so | |
| 332 | ticks made before it are dropped: tick after searching. The Name field no longer | |
| 333 | follows the URL as you type, deliberately: the preview names what it will be | |
| 334 | called, and blank keeps the source's name. | |
| 335 | ||
| 336 | The terminal (`M` on the dashboard) does the same three things, over the same | |
| 337 | `migrateRepo`: its list is newest push first, a pasted URL (`u`) shows the same | |
| 338 | visibility and size line as the page (the pure `urlPreview` both read), and | |
| 339 | `space` ticks rows (at most 20) so Enter migrates them all with one set of | |
| 340 | answers, each under its own name. Without a tick, Enter picks a branch for the | |
| 341 | one repository, as before. | |
| 342 | ||
| 322 | 343 | **Migrate from the terminal.** Press `M` on the dashboard (or pick "Migrate a |
| 323 | 344 | repository" in the palette, `ctrl+k`). The list is your connected origins' |
| 324 | repositories, private ones marked; `/` filters it and `u` pastes a URL instead. | |
| 345 | repositories, private ones marked; `/` filters it, `space` ticks several (then Enter skips the branch step and the form's answers apply to all), and `u` pastes a URL instead. | |
| 325 | 346 | Enter on a repository lists its branches with their last commit, your own |
| 326 | 347 | newest first and marked "(suggested)", so Enter again takes it; a pasted URL |
| 327 | 348 | skips this step. The form asks for name, owner, visibility, agent, autonomy and afterwards |
docs/design/areas/forms.md +2
| @@ -4,3 +4,5 @@ A form's fields and its buttons sit in two rows, one for what you fill in and on | ||
| 4 | 4 | |
| 5 | 5 | - `.field-row` — a row of `.field`s side by side that wraps on a narrow screen, each at least 160px wide and aligned to the bottom so a labelled input and the button beside it share a baseline. Styled in `style/components.ts`; written by `createForm` in `pages/branches.ts` (the new-branch form), the graph filter in `pages/fleet-graph.ts`, the freeze form in `pages/frozen.ts` and the new-pull form in `pages/pulls.ts`. |
| 6 | 6 | - `.form-actions` — the row of a form's buttons under its fields: 16px above, 8px apart, wrapping. Styled in `style/components.ts`, and in `style/content.ts` for a thread's close reason and "close into" input and for `.comment-form` on a narrow screen; written by nearly every form in `src/web/pages/` (`account.ts`, `settings.ts`, `crew.ts`, `edit.ts`, `issues.ts`, `pulls.ts`, `oauth.ts`, `teams.ts`, `chain.ts`, `everyday.ts`) and by the review comment form in `diff-view.ts`; `client.ts` finds its `.muted` note to report a save. |
| 7 | ||
| 8 | - `.input[data-size="lg"]` — the one large field a page is for (`field({ size: "lg" })` in `ui.ts`; `/migrate`'s URL). `searchInput` in `ui.ts` is the search box of a list that filters as you type: `data-search` names the container a script swaps for the server's answer to the same query. |
src/tui/screens/migrate.ts +130 −21
| @@ -24,6 +24,12 @@ import { subscribe, type MirrorJob, type MirrorStage } from "../../origins/jobs. | ||
| 24 | 24 | import { providerForOrigin } from "../../origins/credential.ts"; |
| 25 | 25 | import { cachedRemoteTips, pickWorkBranch } from "../../origins/work-branch.ts"; |
| 26 | 26 | import { getOriginFor } from "../../core/origins.ts"; |
| 27 | import { | |
| 28 | byRecentPush, | |
| 29 | MULTI_MAX, | |
| 30 | urlPreview, | |
| 31 | type UrlPreview, | |
| 32 | } from "../../web/pages/migrate.ts"; | |
| 27 | 33 | import type { Ctx, Screen } from "../app/app.ts"; |
| 28 | 34 | import { binding } from "../app/keys.ts"; |
| 29 | 35 | import { G } from "../term/screen.ts"; |
| @@ -57,8 +63,31 @@ interface Picked { | ||
| 57 | 63 | readonly label: string; |
| 58 | 64 | readonly name: string; |
| 59 | 65 | readonly branch?: string; |
| 66 | /** Ticked rows: every one runs `migrateRepo`, each under its own name. */ | |
| 67 | readonly many?: readonly MigrateSource[]; | |
| 68 | /** The accounts' rows, to say what a pasted URL is. */ | |
| 69 | readonly known?: Known; | |
| 60 | 70 | } |
| 61 | 71 | |
| 72 | /** The origins' rows as the web's preview reads them. */ | |
| 73 | type Known = Parameters<typeof urlPreview>[1]; | |
| 74 | ||
| 75 | /** The preview as one terminal line; the page says the same in markup. */ | |
| 76 | function previewText(p: UrlPreview): string { | |
| 77 | switch (p.kind) { | |
| 78 | case "empty": | |
| 79 | return ""; | |
| 80 | case "invalid": | |
| 81 | return "That does not look like a repository URL yet."; | |
| 82 | case "account": | |
| 83 | return `${p.fullName} on ${p.host} · ${p.private ? "private" : "public"}${p.size === "" ? "" : ` · ${p.size}`}`; | |
| 84 | case "public": | |
| 85 | return `${p.name} on ${p.host} · cloned anonymously, so it must be public`; | |
| 86 | } | |
| 87 | } | |
| 88 | ||
| 89 | const markOf = (row: Row): string => `${row.originId}/${row.repo.fullName}`; | |
| 90 | ||
| 62 | 91 | const STAGES: readonly { stage: MirrorStage; label: string }[] = [ |
| 63 | 92 | { stage: "creating", label: "Creating the repository" }, |
| 64 | 93 | { stage: "fetching", label: "Cloning every branch and tag" }, |
| @@ -74,6 +103,9 @@ export function migrateScreen(f: Session, ctx: Ctx): Screen { | ||
| 74 | 103 | let loading = true; |
| 75 | 104 | // Origins that would not answer: said aloud, never read as "no repositories". |
| 76 | 105 | let unreachable: string[] = []; |
| 106 | let known: Known = []; | |
| 107 | // Ticked rows, by `<origin id>/<full name>`, in the order they were ticked. | |
| 108 | const marked = new Set<string>(); | |
| 77 | 109 | let self: Screen; |
| 78 | 110 | |
| 79 | 111 | const shown = (): Row[] => |
| @@ -83,6 +115,28 @@ export function migrateScreen(f: Session, ctx: Ctx): Screen { | ||
| 83 | 115 | if (user !== undefined) ctx.push(migrateFormScreen(f, ctx, user, picked)); |
| 84 | 116 | }; |
| 85 | 117 | |
| 118 | const tick = (row: Row): void => { | |
| 119 | const key = markOf(row); | |
| 120 | if (marked.has(key)) marked.delete(key); | |
| 121 | else if (marked.size >= MULTI_MAX) | |
| 122 | ctx.toast(`At most ${MULTI_MAX} at a time`, "danger"); | |
| 123 | else marked.add(key); | |
| 124 | ctx.invalidate(); | |
| 125 | }; | |
| 126 | ||
| 127 | const migrateTicked = (): void => { | |
| 128 | const picks = rows | |
| 129 | .filter((r) => marked.has(markOf(r))) | |
| 130 | .map((r) => ({ originId: r.originId, fullName: r.repo.fullName })); | |
| 131 | if (picks.length === 0) return; | |
| 132 | toForm({ | |
| 133 | source: picks[0]!, | |
| 134 | label: picks.length === 1 ? picks[0]!.fullName : `${picks.length} repositories`, | |
| 135 | name: "", | |
| 136 | many: picks, | |
| 137 | }); | |
| 138 | }; | |
| 139 | ||
| 86 | 140 | const choose = (row: Row): void => { |
| 87 | 141 | if (user === undefined) return; |
| 88 | 142 | runAsync(ctx, async () => { |
| @@ -153,7 +207,7 @@ export function migrateScreen(f: Session, ctx: Ctx): Screen { | ||
| 153 | 207 | (url.replace(/[?#].*$/, "").replace(/\/+$/, "").split("/").pop() ?? "") |
| 154 | 208 | .replace(/\.git$/i, ""); |
| 155 | 209 | return Promise.resolve().then(() => |
| 156 | toForm({ source: { url }, label: url, name }), | |
| 210 | toForm({ source: { url }, label: url, name, known }), | |
| 157 | 211 | ); |
| 158 | 212 | }, |
| 159 | 213 | }), |
| @@ -165,7 +219,8 @@ export function migrateScreen(f: Session, ctx: Ctx): Screen { | ||
| 165 | 219 | get keys() { |
| 166 | 220 | return [ |
| 167 | 221 | binding(["j", "k"], "move"), |
| 168 | binding(["enter"], "pick branch"), | |
| 222 | binding(["space"], "tick"), | |
| 223 | binding(["enter"], marked.size > 0 ? "migrate ticked" : "pick branch"), | |
| 169 | 224 | binding(["/"], "filter"), |
| 170 | 225 | binding(["u"], "paste a URL"), |
| 171 | 226 | binding(["esc"], "back"), |
| @@ -187,7 +242,13 @@ export function migrateScreen(f: Session, ctx: Ctx): Screen { | ||
| 187 | 242 | for (const repo of listed.value) |
| 188 | 243 | out.push({ originId: origin.id, origin: origin.login, repo }); |
| 189 | 244 | } |
| 190 | rows = out; | |
| 245 | rows = byRecentPush(out.map((r) => ({ pushedAt: r.repo.pushedAt, r }))).map( | |
| 246 | (x) => x.r, | |
| 247 | ); | |
| 248 | known = listUserOrigins(f.app, user).map((o) => ({ | |
| 249 | kind: o.kind, | |
| 250 | repos: rows.filter((r) => r.originId === o.id).map((r) => r.repo), | |
| 251 | })); | |
| 191 | 252 | unreachable = failed; |
| 192 | 253 | loading = false; |
| 193 | 254 | ctx.invalidate(); |
| @@ -231,7 +292,7 @@ export function migrateScreen(f: Session, ctx: Ctx): Screen { | ||
| 231 | 292 | c, |
| 232 | 293 | state, |
| 233 | 294 | items.map((r) => ({ |
| 234 | title: r.repo.fullName, | |
| 295 | title: `${marked.has(markOf(r)) ? "[x]" : "[ ]"} ${r.repo.fullName}`, | |
| 235 | 296 | secondary: r.repo.private ? "private" : "", |
| 236 | 297 | meta: [ |
| 237 | 298 | r.origin, |
| @@ -244,7 +305,9 @@ export function migrateScreen(f: Session, ctx: Ctx): Screen { | ||
| 244 | 305 | hitList(ctx, list!, state, items.length, { |
| 245 | 306 | open: (i) => { |
| 246 | 307 | const row = items[i]; |
| 247 | if (row !== undefined) choose(row); | |
| 308 | if (row === undefined) return; | |
| 309 | if (marked.size > 0) migrateTicked(); | |
| 310 | else choose(row); | |
| 248 | 311 | }, |
| 249 | 312 | }); |
| 250 | 313 | }, |
| @@ -262,9 +325,15 @@ export function migrateScreen(f: Session, ctx: Ctx): Screen { | ||
| 262 | 325 | return true; |
| 263 | 326 | } |
| 264 | 327 | if (state.key(e, shown().length)) return true; |
| 328 | if (e.key === " ") { | |
| 329 | const row = shown()[state.index]; | |
| 330 | if (row !== undefined) tick(row); | |
| 331 | return true; | |
| 332 | } | |
| 265 | 333 | if (e.key === "enter") { |
| 266 | 334 | const row = shown()[state.index]; |
| 267 | if (row !== undefined) choose(row); | |
| 335 | if (marked.size > 0) migrateTicked(); | |
| 336 | else if (row !== undefined) choose(row); | |
| 268 | 337 | return true; |
| 269 | 338 | } |
| 270 | 339 | return true; |
| @@ -312,25 +381,48 @@ function migrateFormScreen( | ||
| 312 | 381 | runAsync(ctx, async () => { |
| 313 | 382 | try { |
| 314 | 383 | const agent = crew[agentIndex] ?? ""; |
| 315 | const started = await migrateRepo(f.app, user, { | |
| 316 | source: picked.source, | |
| 384 | const sources = picked.many ?? [picked.source]; | |
| 385 | const jobs: MirrorJob[] = []; | |
| 386 | for (const source of sources.slice(0, MULTI_MAX)) { | |
| 387 | const started = await migrateRepo(f.app, user, { | |
| 388 | source, | |
| 317 | 389 | owner: owners[ownerIndex] ?? user.username, |
| 318 | name, | |
| 390 | name: picked.many === undefined ? name : "", | |
| 319 | 391 | visibility, |
| 320 | 392 | after: AFTER[afterIndex]?.id ?? "leave", |
| 321 | 393 | ...(picked.branch === undefined ? {} : { branch: picked.branch }), |
| 322 | 394 | ...(agent === "" ? {} : { crew: { agent, autonomous } }), |
| 323 | 395 | }); |
| 324 | if (!started.ok) { | |
| 325 | error = migrateMessage( | |
| 326 | started.error, | |
| 327 | repositoryLimitMessage(f.app, user), | |
| 328 | ); | |
| 329 | ctx.toast(error, "danger"); | |
| 330 | ctx.invalidate(); | |
| 396 | if (!started.ok) { | |
| 397 | const said = migrateMessage( | |
| 398 | started.error, | |
| 399 | repositoryLimitMessage(f.app, user), | |
| 400 | ); | |
| 401 | const at = "fullName" in source ? `${source.fullName}: ` : ""; | |
| 402 | if (jobs.length === 0) { | |
| 403 | error = `${at}${said}`; | |
| 404 | ctx.toast(error, "danger"); | |
| 405 | ctx.invalidate(); | |
| 406 | return; | |
| 407 | } | |
| 408 | ctx.toast( | |
| 409 | `Started ${jobs.length}; ${at}stopped the rest: ${said}`, | |
| 410 | "danger", | |
| 411 | ); | |
| 412 | ctx.pop(); | |
| 413 | return; | |
| 414 | } | |
| 415 | jobs.push(started.value.job); | |
| 416 | } | |
| 417 | if (jobs.length === 1) { | |
| 418 | ctx.replace(jobScreen(f, ctx, jobs[0]!, picked.label)); | |
| 331 | 419 | return; |
| 332 | 420 | } |
| 333 | ctx.replace(jobScreen(f, ctx, started.value.job, picked.label)); | |
| 421 | ctx.toast( | |
| 422 | `Started ${jobs.length} migrations — each repository appears as it lands.`, | |
| 423 | "success", | |
| 424 | ); | |
| 425 | ctx.pop(); | |
| 334 | 426 | } finally { |
| 335 | 427 | busy = false; |
| 336 | 428 | } |
| @@ -369,14 +461,30 @@ function migrateFormScreen( | ||
| 369 | 461 | ); |
| 370 | 462 | const cursor = field(s, { x: top!.x, y: top!.y + 1, w: top!.w, h: 3 }, c, { |
| 371 | 463 | label: `Name (under ${owners[ownerIndex] ?? "?"})`, |
| 372 | value: name, | |
| 464 | value: picked.many === undefined ? name : "", | |
| 373 | 465 | focused: focus === 0, |
| 374 | 466 | ...(focus === 0 ? { cursor: [...name].length } : {}), |
| 375 | 467 | ...(error === undefined |
| 376 | ? { hint: "Blank keeps the source's name." } | |
| 468 | ? { | |
| 469 | hint: | |
| 470 | picked.many === undefined | |
| 471 | ? "Blank keeps the source's name." | |
| 472 | : "Each keeps its own name.", | |
| 473 | } | |
| 377 | 474 | : { error }), |
| 378 | 475 | }); |
| 379 | ctx.setCursor(focus === 0 ? cursor : null); | |
| 476 | ctx.setCursor(focus === 0 && picked.many === undefined ? cursor : null); | |
| 477 | if (picked.known !== undefined) { | |
| 478 | const said = previewText(urlPreview(picked.label, picked.known)); | |
| 479 | if (said !== "") | |
| 480 | s.text( | |
| 481 | top!.x, | |
| 482 | top!.y + 4, | |
| 483 | truncate(said, top!.w), | |
| 484 | style(c.mutedFg, c.bg), | |
| 485 | right, | |
| 486 | ); | |
| 487 | } | |
| 380 | 488 | const lines = [ |
| 381 | 489 | `owner ${owners[ownerIndex] ?? "?"}`, |
| 382 | 490 | `visibility ${visibility}`, |
| @@ -395,7 +503,7 @@ function migrateFormScreen( | ||
| 395 | 503 | ); |
| 396 | 504 | }, |
| 397 | 505 | paste(text) { |
| 398 | if (focus === 0) name += text.replace(/\s+/g, ""); | |
| 506 | if (focus === 0 && picked.many === undefined) name += text.replace(/\s+/g, ""); | |
| 399 | 507 | return true; |
| 400 | 508 | }, |
| 401 | 509 | key(e) { |
| @@ -419,6 +527,7 @@ function migrateFormScreen( | ||
| 419 | 527 | submit(); |
| 420 | 528 | return true; |
| 421 | 529 | } |
| 530 | if (picked.many !== undefined && focus === 0) return true; | |
| 422 | 531 | if (focus === 0 && e.key === "backspace") { |
| 423 | 532 | name = [...name].slice(0, -1).join(""); |
| 424 | 533 | error = undefined; |
src/web/html.ts +20
| @@ -50,6 +50,26 @@ export function html(strings: TemplateStringsArray, ...values: Value[]): Html { | ||
| 50 | 50 | return new Html(out); |
| 51 | 51 | } |
| 52 | 52 | |
| 53 | const BLOCK = "div|li|ul|ol|p|form|details|summary|h[1-6]|option|select|section|header|footer"; | |
| 54 | ||
| 55 | /** | |
| 56 | * Markup with its indentation squeezed out, rendered the same: every run of | |
| 57 | * whitespace holding a newline becomes one space, a space before a tag's | |
| 58 | * closing `>` goes, and whitespace between block-level tags goes (between | |
| 59 | * inline ones it is a word space, and stays). For a page of rows whose | |
| 60 | * template indentation outweighs its content; never for markup holding | |
| 61 | * `<pre>`, `<textarea>` or `<script>`. | |
| 62 | */ | |
| 63 | export function tight(markup: Html): Html { | |
| 64 | return new Html( | |
| 65 | markup.text | |
| 66 | .replace(/\s*\n\s*/g, " ") | |
| 67 | .replace(/ +(\/?>)/g, "$1") | |
| 68 | .replace(new RegExp(`(<\\/?(?:${BLOCK})\\b[^>]*>) +(?=<)`, "g"), "$1") | |
| 69 | .replace(new RegExp(` +(?=<\\/?(?:${BLOCK})\\b)`, "g"), ""), | |
| 70 | ); | |
| 71 | } | |
| 72 | ||
| 53 | 73 | /** Conditional helper: `when(user, (u) => html`…`)` renders nothing on miss. */ |
| 54 | 74 | export function when<T>( |
| 55 | 75 | value: T | undefined | null | false, |
src/web/pages/migrate.ts +398 −164
| @@ -46,14 +46,16 @@ import type { Ctx } from "../../http/context.ts"; | ||
| 46 | 46 | import type { MirrorJob, MirrorStage } from "../../origins/jobs.ts"; |
| 47 | 47 | import { notFoundPage, pageHead, signInFirst } from "../chrome.ts"; |
| 48 | 48 | import { field, readForm } from "../form.ts"; |
| 49 | import { html, raw, type Html } from "../html.ts"; | |
| 49 | import { html, raw, tight, type Html } from "../html.ts"; | |
| 50 | 50 | import { flashTo, page, redirect } from "../layout.ts"; |
| 51 | 51 | import { |
| 52 | 52 | badge, |
| 53 | 53 | btn, |
| 54 | 54 | card, |
| 55 | disclosure, | |
| 55 | 56 | icon, |
| 56 | 57 | row, |
| 58 | searchInput, | |
| 57 | 59 | selectField, |
| 58 | 60 | field as formField, |
| 59 | 61 | } from "../ui.ts"; |
| @@ -63,8 +65,10 @@ import { githubConnectable, hostLabel } from "./origins.ts"; | ||
| 63 | 65 | |
| 64 | 66 | interface MigrateValues { |
| 65 | 67 | source: string; |
| 66 | /** The picker's filter: rows whose name or description holds it. */ | |
| 68 | /** The search: rows whose name or description holds it. */ | |
| 67 | 69 | q: string; |
| 70 | /** Rows ticked for "Migrate selected": `<origin id>/<owner>/<name>`. */ | |
| 71 | multi: string[]; | |
| 68 | 72 | url: string; |
| 69 | 73 | owner: string; |
| 70 | 74 | name: string; |
| @@ -191,8 +195,11 @@ function branchList(choices: BranchChoices): Html { | ||
| 191 | 195 | </div>`; |
| 192 | 196 | } |
| 193 | 197 | |
| 194 | /** Rows drawn per origin; past it, the filter narrows. */ | |
| 195 | export const PICKER_ROWS = 200; | |
| 198 | /** Rows an account shows before you search; the rest wait behind the filter. */ | |
| 199 | export const PICKER_ROWS = 8; | |
| 200 | ||
| 201 | /** The most repositories one "Migrate selected" starts. */ | |
| 202 | export const MULTI_MAX = 20; | |
| 196 | 203 | |
| 197 | 204 | /** |
| 198 | 205 | * A size an origin reported in KiB, in the unit that keeps it short: |
| @@ -221,115 +228,160 @@ export function matchesFilter( | ||
| 221 | 228 | ); |
| 222 | 229 | } |
| 223 | 230 | |
| 231 | /** A description as one short line: an origin allows several hundred characters. */ | |
| 232 | const clip = (text: string): string => | |
| 233 | text.length > 80 ? `${text.slice(0, 79)}…` : text; | |
| 234 | ||
| 235 | /** Most recently pushed first; a row with no push time goes last. */ | |
| 236 | export function byRecentPush<T extends Pick<OriginRepoRow, "pushedAt">>( | |
| 237 | repos: readonly T[], | |
| 238 | ): T[] { | |
| 239 | const at = (r: T): number => Date.parse(r.pushedAt ?? "") || 0; | |
| 240 | return [...repos].sort((a, b) => at(b) - at(a)); | |
| 241 | } | |
| 242 | ||
| 243 | /** A URL without its scheme's noise, to compare a pasted one with an origin's. */ | |
| 244 | const sameRepoUrl = (a: string, b: string): boolean => { | |
| 245 | const norm = (u: string): string => | |
| 246 | u | |
| 247 | .trim() | |
| 248 | .toLowerCase() | |
| 249 | .replace(/^[a-z+]+:\/\//, "") | |
| 250 | .replace(/[?#].*$/, "") | |
| 251 | .replace(/\/+$/, "") | |
| 252 | .replace(/\.git$/, ""); | |
| 253 | return norm(a) !== "" && norm(a) === norm(b); | |
| 254 | }; | |
| 255 | ||
| 256 | /** What a pasted URL is, as plain data: the page and the terminal each say it their way. */ | |
| 257 | export type UrlPreview = | |
| 258 | | { readonly kind: "empty" } | |
| 259 | | { readonly kind: "invalid" } | |
| 260 | | { | |
| 261 | readonly kind: "account"; | |
| 262 | readonly github: boolean; | |
| 263 | readonly fullName: string; | |
| 264 | readonly host: string; | |
| 265 | readonly private: boolean; | |
| 266 | readonly size: string; | |
| 267 | } | |
| 268 | | { readonly kind: "public"; readonly name: string; readonly host: string }; | |
| 269 | ||
| 224 | 270 | /** |
| 225 | * The accounts half, outside the migrate form (forms do not nest): each | |
| 226 | * connected origin with a Disconnect, and Connect GitHub when there is no | |
| 227 | * GitHub account yet. Both come back here (`then=migrate`). | |
| 271 | * What the pasted URL is, before Migrate: its host and name, and — when it | |
| 272 | * is one of your accounts' repositories — its size and visibility. Pure: the | |
| 273 | * URL and the rows in, data out. | |
| 228 | 274 | */ |
| 229 | function accounts(ctx: Ctx, origins: readonly OriginChoices[]): Html { | |
| 230 | const hasGithub = origins.some((o) => o.kind === "github"); | |
| 231 | const connect = hasGithub | |
| 232 | ? null | |
| 233 | : githubConnectable(ctx) | |
| 234 | ? html`<form method="post" action="/settings/origins/oauth" class="bar"> | |
| 235 | <input type="hidden" name="kind" value="github" /> | |
| 236 | <input type="hidden" name="then" value="migrate" /> | |
| 237 | ${btn("Connect GitHub", { variant: "outline", icon: "github", type: "submit" })} | |
| 238 | <span class="muted small" | |
| 239 | >A short code you type at github.com — no token to paste. Your | |
| 240 | private repositories and their issues come too.</span | |
| 241 | > | |
| 242 | </form>` | |
| 243 | : html`<p class="muted small"> | |
| 244 | ${icon("github")} Connect GitHub needs this forge's GitHub | |
| 245 | application, set up once under | |
| 246 | <a href="/settings/origins">Settings → Origins</a>; a token pasted | |
| 247 | there works until then. | |
| 248 | </p>`; | |
| 249 | return card( | |
| 250 | html`${origins.length === 0 | |
| 251 | ? null | |
| 252 | : html`<ul class="list" id="migrate-accounts"> | |
| 253 | ${origins.map((origin) => | |
| 254 | row({ | |
| 255 | lead: icon(origin.kind === "github" ? "github" : "globe"), | |
| 256 | title: origin.label, | |
| 257 | meta: html`${origin.repos.length} repositories`, | |
| 258 | trail: html`<form | |
| 259 | method="post" | |
| 260 | action="/settings/origins/${origin.id}/delete" | |
| 261 | > | |
| 262 | <input type="hidden" name="then" value="migrate" /> | |
| 263 | ${btn("Disconnect", { size: "sm", variant: "ghost", type: "submit" })} | |
| 264 | </form>`, | |
| 265 | }), | |
| 266 | )} | |
| 267 | </ul>`} | |
| 268 | ${connect}`, | |
| 269 | { | |
| 270 | head: html`${icon("globe")} | |
| 271 | <h2>Your accounts</h2>`, | |
| 272 | }, | |
| 273 | ); | |
| 275 | export function urlPreview( | |
| 276 | url: string, | |
| 277 | origins: readonly Pick<OriginChoices, "kind" | "repos">[], | |
| 278 | ): UrlPreview { | |
| 279 | const text = url.trim(); | |
| 280 | if (text === "") return { kind: "empty" }; | |
| 281 | const parsed = URL.parse(text); | |
| 282 | if ( | |
| 283 | parsed === null || | |
| 284 | !["https:", "http:", "git:"].includes(parsed.protocol) || | |
| 285 | parsed.pathname.replace(/\/+$/, "") === "" | |
| 286 | ) | |
| 287 | return { kind: "invalid" }; | |
| 288 | for (const origin of origins) { | |
| 289 | const hit = origin.repos.find( | |
| 290 | (r) => sameRepoUrl(r.htmlUrl, text) || sameRepoUrl(r.cloneUrl, text), | |
| 291 | ); | |
| 292 | if (hit === undefined) continue; | |
| 293 | return { | |
| 294 | kind: "account", | |
| 295 | github: origin.kind === "github", | |
| 296 | fullName: hit.fullName, | |
| 297 | host: hostLabel(parsed.href), | |
| 298 | private: hit.private, | |
| 299 | size: formatKib(hit.sizeKb), | |
| 300 | }; | |
| 301 | } | |
| 302 | const name = parsed.pathname | |
| 303 | .replace(/\/+$/, "") | |
| 304 | .replace(/\.git$/i, "") | |
| 305 | .split("/") | |
| 306 | .pop()!; | |
| 307 | return { kind: "public", name, host: parsed.host }; | |
| 308 | } | |
| 309 | ||
| 310 | /** The preview as the page's line. */ | |
| 311 | function preview(url: string, origins: readonly OriginChoices[]): Html { | |
| 312 | const p = urlPreview(url, origins); | |
| 313 | if (p.kind === "empty") | |
| 314 | return html`Public repositories clone anonymously; private ones come | |
| 315 | through Your accounts.`; | |
| 316 | if (p.kind === "invalid") | |
| 317 | return html`That does not look like a repository URL yet.`; | |
| 318 | if (p.kind === "account") | |
| 319 | return html`${icon(p.github ? "github" : "globe")} | |
| 320 | <strong class="mono">${p.fullName}</strong> on ${p.host} | |
| 321 | · ${p.private ? "private" : "public"}${p.size === "" ? null : html` · ${p.size}`}`; | |
| 322 | return html`${icon("globe")} <strong class="mono">${p.name}</strong> on | |
| 323 | ${p.host} · cloned anonymously, so it must be public`; | |
| 324 | } | |
| 325 | ||
| 326 | /** The Connect GitHub button, or why there is not one yet. */ | |
| 327 | function connectGithub(ctx: Ctx): Html { | |
| 328 | return githubConnectable(ctx) | |
| 329 | ? html`<form method="post" action="/settings/origins/oauth" class="bar"> | |
| 330 | <input type="hidden" name="kind" value="github" /> | |
| 331 | <input type="hidden" name="then" value="migrate" /> | |
| 332 | ${btn("Connect GitHub", { variant: "outline", icon: "github", type: "submit" })} | |
| 333 | <span class="muted small" | |
| 334 | >A short code you type at github.com — no token to paste. Your | |
| 335 | private repositories and their issues come too.</span | |
| 336 | > | |
| 337 | </form>` | |
| 338 | : html`<p class="muted small"> | |
| 339 | ${icon("github")} Connect GitHub needs this forge's GitHub | |
| 340 | application, set up once under | |
| 341 | <a href="/settings/origins">Settings → Origins</a>; a token pasted | |
| 342 | there works until then. | |
| 343 | </p>`; | |
| 274 | 344 | } |
| 275 | 345 | |
| 276 | 346 | /** |
| 277 | * One row per repository: name, private mark, size, description, and the | |
| 278 | * button that migrates it. The buttons are the form's submitters | |
| 279 | * (`name="pick"`), so one click carries the destination and crew fields | |
| 280 | * below with it. | |
| 347 | * One account's rows, newest push first: eight before a search, the rest | |
| 348 | * behind it. The list is its own container so a script can swap it for the | |
| 349 | * server's answer to the same query as you type. | |
| 281 | 350 | */ |
| 282 | function picker( | |
| 283 | origins: readonly OriginChoices[], | |
| 351 | function pickRows( | |
| 352 | origin: OriginChoices, | |
| 284 | 353 | values: MigrateValues, |
| 285 | 354 | withCrew: boolean, |
| 286 | branches: BranchChoices | undefined, | |
| 287 | 355 | ): Html { |
| 288 | const listed = origins.filter((o) => o.repos.length > 0); | |
| 289 | if (listed.length === 0) | |
| 290 | return html`<p class="muted small mb"> | |
| 291 | ${icon("globe")} Connect an account above — or another forge under | |
| 292 | <a href="/settings/origins">Settings → Origins</a> — to pick from your | |
| 293 | repositories there, private ones included. | |
| 294 | </p>`; | |
| 295 | return html`<div class="field" id="migrate-picker"> | |
| 296 | <label for="f-q">Your repositories</label> | |
| 297 | <input | |
| 298 | class="input" | |
| 299 | type="search" | |
| 300 | id="f-q" | |
| 301 | name="q" | |
| 302 | form="migrate-search" | |
| 303 | value="${values.q}" | |
| 304 | placeholder="Filter by name or description" | |
| 305 | autocomplete="off" | |
| 306 | /> | |
| 307 | ${values.source === "" | |
| 308 | ? null | |
| 309 | : html`<input type="hidden" name="source" value="${values.source}" />`} | |
| 310 | ${listed.map((origin) => { | |
| 311 | const rows = origin.repos.filter((r) => matchesFilter(r, values.q)); | |
| 312 | const shown = rows.slice(0, PICKER_ROWS); | |
| 313 | return html`<h4 class="mt small muted">${origin.label}</h4> | |
| 314 | <ul class="list" data-pick-rows> | |
| 356 | const q = values.q; | |
| 357 | const rows = byRecentPush(origin.repos.filter((r) => matchesFilter(r, q))); | |
| 358 | const shown = rows.slice(0, PICKER_ROWS); | |
| 359 | return html`<div id="pick-${origin.id}"> | |
| 360 | ${shown.length === 0 | |
| 361 | ? html`<p class="muted small">Nothing here matches.</p>` | |
| 362 | : html`<ul class="list" data-pick-rows> | |
| 315 | 363 | ${shown.map((remote) => { |
| 316 | 364 | const value = pickValue(origin.id, remote.fullName); |
| 317 | 365 | const size = formatKib(remote.sizeKb); |
| 318 | 366 | const branches = `/migrate?${new URLSearchParams({ |
| 319 | 367 | source: value, |
| 320 | 368 | branches: "1", |
| 321 | ...(values.q === "" ? {} : { q: values.q }), | |
| 369 | ...(q === "" ? {} : { q }), | |
| 322 | 370 | })}-branches`; |
| 323 | 371 | return row({ |
| 324 | data: { | |
| 325 | q: `${remote.fullName} ${remote.description}`.toLowerCase(), | |
| 326 | }, | |
| 327 | 372 | current: values.source === value, |
| 328 | title: html`<span class="mono">${remote.fullName}</span> | |
| 373 | title: html`<label class="check" | |
| 374 | ><input | |
| 375 | type="checkbox" | |
| 376 | name="multi" | |
| 377 | value="${value}" | |
| 378 | aria-label="Select ${remote.fullName}" | |
| 379 | ${values.multi.includes(value) ? raw("checked") : null} | |
| 380 | /></label> | |
| 381 | <a class="mono" href="${branches}">${remote.fullName}</a> | |
| 329 | 382 | ${remote.private ? badge("private") : null}`, |
| 330 | meta: html`${size === "" ? null : html`${size} · `}${remote.description}`, | |
| 331 | trail: html`${btn("Branches", { href: branches, size: "sm", variant: "ghost" })} | |
| 332 | ${btn(withCrew ? "Migrate with the crew" : "Migrate", { | |
| 383 | meta: html`${size === "" ? null : html`${size} · `}${clip(remote.description)}`, | |
| 384 | trail: html`${btn(withCrew ? "Migrate with the crew" : "Migrate", { | |
| 333 | 385 | size: "sm", |
| 334 | 386 | variant: "outline", |
| 335 | 387 | type: "submit", |
| @@ -338,21 +390,92 @@ function picker( | ||
| 338 | 390 | })}`, |
| 339 | 391 | }); |
| 340 | 392 | })} |
| 341 | </ul> | |
| 342 | ${rows.length > shown.length | |
| 343 | ? html`<p class="muted small"> | |
| 344 | ${rows.length - shown.length} more — filter to narrow. | |
| 345 | </p>` | |
| 346 | : null}`; | |
| 347 | })} | |
| 348 | ${branches === undefined ? null : branchList(branches)} | |
| 349 | <span class="hint" | |
| 350 | >Cloned with your account's credential, so private repositories come | |
| 351 | too — with their issues, comments, labels and open pull requests.</span | |
| 352 | > | |
| 393 | </ul>`} | |
| 394 | ${rows.length > shown.length | |
| 395 | ? html`<p class="muted small"> | |
| 396 | ${rows.length - shown.length} more — search to narrow. | |
| 397 | </p>` | |
| 398 | : null} | |
| 353 | 399 | </div>`; |
| 354 | 400 | } |
| 355 | 401 | |
| 402 | /** | |
| 403 | * Your accounts, inside the migrate form (its rows' buttons and ticks submit it; the | |
| 404 | * search and Disconnect belong to the small forms beside it): one row per connected | |
| 405 | * account that opens to its search and rows, and Connect GitHub when there | |
| 406 | * is no GitHub account yet. With no account at all it is that one button. | |
| 407 | */ | |
| 408 | function accounts( | |
| 409 | ctx: Ctx, | |
| 410 | origins: readonly OriginChoices[], | |
| 411 | values: MigrateValues, | |
| 412 | withCrew: boolean, | |
| 413 | branches: BranchChoices | undefined, | |
| 414 | ): Html { | |
| 415 | if (origins.length === 0) return html``; | |
| 416 | return html`<h3 class="mt mb">Your accounts</h3> | |
| 417 | <ul class="list" id="migrate-accounts"> | |
| 418 | ${origins.map((origin) => { | |
| 419 | const open = | |
| 420 | values.q !== "" || values.source.startsWith(`${origin.id}/`); | |
| 421 | return row({ | |
| 422 | lead: icon(origin.kind === "github" ? "github" : "globe"), | |
| 423 | title: origin.label, | |
outdated
The swap in migrateJs has no abort or staleness guard, so a slow earlier response can overwrite a newer one. Use an AbortController per target, or check that the response's query still matches the input's value. |
||
| 424 | meta: html`${origin.repos.length} repositories`, | |
| 425 | fold: { | |
| 426 | id: `acct-${origin.id}`, | |
| 427 | open, | |
| 428 | body: html`<div class="card-body"> | |
| 429 | ${origin.repos.length === 0 | |
| 430 | ? html`<p class="muted small"> | |
| 431 | Nothing listed for this account. | |
| 432 | </p>` | |
| 433 | : html`${searchInput({ | |
| 434 | name: "q", | |
| 435 | value: values.q, | |
| 436 | label: `Search ${origin.label}`, | |
| 437 | placeholder: `Search ${origin.repos.length} repositories`, | |
| 438 | target: `pick-${origin.id}`, | |
| 439 | form: "find", | |
| 440 | })} | |
| 441 | ${pickRows(origin, values, withCrew)} | |
| 442 | ${branches !== undefined && | |
| 443 | values.source.startsWith(`${origin.id}/`) | |
| 444 | ? branchList(branches) | |
| 445 | : null}`} | |
| 446 | <div class="bar mt"> | |
| 447 | ${origin.repos.length === 0 | |
| 448 | ? null | |
| 449 | : btn("Migrate selected", { | |
| 450 | name: "go", | |
| 451 | value: "multi", | |
| 452 | variant: "primary", | |
| 453 | size: "sm", | |
| 454 | })} | |
| 455 | ${btn("Disconnect", { | |
| 456 | size: "sm", | |
| 457 | variant: "ghost", | |
| 458 | type: "submit", | |
| 459 | form: "drop", | |
| 460 | formaction: `/settings/origins/${origin.id}/delete`, | |
| 461 | })} | |
| 462 | </div> | |
| 463 | </div>`, | |
| 464 | }, | |
| 465 | }); | |
| 466 | })} | |
| 467 | </ul>`; | |
| 468 | } | |
| 469 | ||
| 470 | /** | |
| 471 | * The small forms the account rows' controls belong to by `form=`: a form | |
| 472 | * cannot sit inside the migrate form, so they stand beside it, empty. | |
| 473 | */ | |
| 474 | function accountForms(): Html { | |
| 475 | return html`<form id="find" method="get" action="/migrate"></form> | |
| 476 | <form id="drop" method="post"><input type="hidden" name="then" value="migrate" /></form>`; | |
| 477 | } | |
| 478 | ||
| 356 | 479 | /** `<origin id>/<owner>/<name>` — an id never holds a slash, so the first one splits. */ |
| 357 | 480 | const pickValue = (originId: string, fullName: string): string => |
| 358 | 481 | `${originId}/${fullName}`; |
| @@ -378,15 +501,16 @@ function migrateForm( | ||
| 378 | 501 | const owners = [user.username, ...writeOrgs.map((o) => o.username)]; |
| 379 | 502 | const agents = listAllAgents(ctx.app, user); |
| 380 | 503 | const crew = agents.ok ? agents.value : []; |
| 504 | const picking = values.source !== "" || values.multi.length > 0; | |
| 381 | 505 | |
| 382 | 506 | return html`<div class="narrow-wide"> |
| 383 | 507 | ${pageHead("Migrate a repository", { |
| 384 | sub: "A one-time clone of the whole history — every branch, every tag — into a repository that lives here from now on. Nothing is written back to where it came from.", | |
| 508 | sub: "A one-time clone of every branch and tag. Nothing is written back to where it came from.", | |
| 385 | 509 | })} |
| 386 | 510 | ${ctx.url.searchParams.get("connected") === "1" |
| 387 | 511 | ? html`<p class="muted small mb" id="migrate-connected"> |
| 388 | ${icon("check")} Connected. Pick a repository below — one click | |
| 389 | migrates it. | |
| 512 | ${icon("check")} Connected. Open your account below — one click | |
| 513 | migrates a repository. | |
| 390 | 514 | </p>` |
| 391 | 515 | : null} |
| 392 | 516 | ${unmatched.length === 0 |
| @@ -395,25 +519,34 @@ function migrateForm( | ||
| 395 | 519 | ${icon("globe")} Ignored, nothing here matches: |
| 396 | 520 | ${unmatched.join(", ")} |
| 397 | 521 | </p>`} |
| 398 | ${accounts(ctx, origins)} | |
| 399 | <form method="get" action="/migrate" id="migrate-search"></form> | |
| 400 | ${card( | |
| 401 | html`<form method="post" action="/migrate"> | |
| 402 | <h3 class="mb">Source</h3> | |
| 403 | ${picker(origins, values, crew.length > 0, branches)} | |
| 404 | ${formField({ | |
| 405 | label: origins.every((o) => o.repos.length === 0) | |
| 406 | ? "Repository URL" | |
| 407 | : "Or a URL", | |
| 408 | name: "url", | |
| 409 | value: values.url, | |
| 410 | placeholder: "https://github.com/owner/name", | |
| 411 | autocomplete: "off", | |
| 412 | mono: true, | |
| 413 | autofocus: origins.every((o) => o.repos.length === 0), | |
| 414 | hint: "Any public repository over https, http or git. It is cloned anonymously.", | |
| 415 | })} | |
| 416 | <h3 class="mb mt">Destination</h3> | |
| 522 | ${error | |
| 523 | ? html`<p class="error mb" role="alert" id="migrate-error">${error}</p>` | |
| 524 | : null} | |
| 525 | <form method="post" action="/migrate" id="migrate-form"> | |
| 526 | ${values.source === "" | |
| 527 | ? null | |
| 528 | : html`<input type="hidden" name="source" value="${values.source}" />`} | |
| 529 | ${formField({ | |
| 530 | label: "Paste a repository URL (GitHub, GitLab, any git URL)", | |
| 531 | name: "url", | |
| 532 | value: values.url, | |
| 533 | placeholder: "https://github.com/owner/name", | |
| 534 | autocomplete: "off", | |
| 535 | mono: true, | |
| 536 | spellcheck: false, | |
| 537 | size: "lg", | |
| 538 | autofocus: !picking, | |
| 539 | })} | |
| 540 | <p class="muted small" id="migrate-preview" aria-live="polite"> | |
| 541 | ${preview(values.url, origins)} | |
| 542 | </p> | |
| 543 | <div class="form-actions"> | |
| 544 | ${btn("Migrate", { variant: "primary" })} | |
| 545 | </div> | |
| 546 | <div id="migrate-chosen"></div> | |
| 547 | ${disclosure( | |
| 548 | "Destination, crew and what happens afterwards", | |
| 549 | html`<h3 class="mb mt">Destination</h3> | |
| 417 | 550 | ${selectField({ |
| 418 | 551 | label: "Owner", |
| 419 | 552 | name: "owner", |
| @@ -429,7 +562,6 @@ function migrateForm( | ||
| 429 | 562 | value: values.name, |
| 430 | 563 | pattern: "[A-Za-z0-9][A-Za-z0-9._-]*", |
| 431 | 564 | hint: "Left blank, it keeps the source's name.", |
| 432 | ...(error ? { error } : {}), | |
| 433 | 565 | })} |
| 434 | 566 | ${selectField({ |
| 435 | 567 | label: "Visibility", |
| @@ -460,7 +592,7 @@ function migrateForm( | ||
| 460 | 592 | : html`${selectField({ |
| 461 | 593 | label: "Agent", |
| 462 | 594 | name: "agent", |
| 463 | hint: "Joins the crew, makes the repository an app, and is told to make it run here — adapting it, not rewriting it.", | |
| 595 | hint: "Makes the repository an app and gets it running here.", | |
| 464 | 596 | options: [ |
| 465 | 597 | { |
| 466 | 598 | value: "", |
| @@ -490,11 +622,11 @@ function migrateForm( | ||
| 490 | 622 | > |
| 491 | 623 | </label> |
| 492 | 624 | </div>`} |
| 493 | <h3 class="mb mt">What happens to the GitHub repository</h3> | |
| 625 | <h3 class="mb mt">What happens to the original</h3> | |
| 494 | 626 | ${selectField({ |
| 495 | 627 | label: "Afterwards", |
| 496 | 628 | name: "after", |
| 497 | hint: "Applies to a repository picked from a connected origin. Archiving needs admin rights there; without them the migration still succeeds and says so.", | |
| 629 | hint: "For a repository from a connected account. Archiving needs admin rights there.", | |
| 498 | 630 | options: [ |
| 499 | 631 | { |
| 500 | 632 | value: "leave", |
| @@ -512,45 +644,84 @@ function migrateForm( | ||
| 512 | 644 | selected: values.after === "mirror", |
| 513 | 645 | }, |
| 514 | 646 | ], |
| 515 | })} | |
| 516 | <div class="form-actions"> | |
| 517 | ${btn("Migrate", { variant: "primary", icon: "download" })} | |
| 518 | ${btn("Cancel", { href: "/", variant: "ghost" })} | |
| 519 | </div> | |
| 520 | </form>`, | |
| 521 | )} | |
| 647 | })}`, | |
| 648 | { extra: "mt" }, | |
| 649 | )} | |
| 650 | ${accounts(ctx, origins, values, crew.length > 0, branches)} | |
| 651 | </form> | |
| 652 | ${origins.length === 0 ? null : accountForms()} | |
| 653 | ${origins.some((o) => o.kind === "github") | |
| 654 | ? null | |
| 655 | : html`<div class="mt" id="migrate-connect">${connectGithub(ctx)}</div>`} | |
| 522 | 656 | </div>`; |
| 523 | 657 | } |
| 524 | 658 | |
| 525 | 659 | /** |
| 526 | * The name field follows the picked repository or the pasted URL until | |
| 527 | * somebody types in it — a preview; the server names it the same way. | |
| 660 | * The preview under the URL and an account's rows follow what you type by | |
| 661 | * asking the page itself (`/migrate?url=` and `?o=&q=`) and swapping in the | |
| 662 | * element that changed. Ticked rows are remembered as hidden fields, so a | |
| 663 | * search does not forget them. | |
| 528 | 664 | */ |
| 529 | const migrateJs = /* js */ ` | |
| 665 | export const migrateJs = new Bun.Transpiler({ minifyWhitespace: true }).transformSync(/* js */ ` | |
| 530 | 666 | (function () { |
| 531 | var name = document.getElementById('f-name'); | |
| 532 | 667 | var url = document.getElementById('f-url'); |
| 533 | var q = document.getElementById('f-q'); | |
| 534 | if (q) q.addEventListener('input', function () { | |
| 535 | var needle = q.value.trim().toLowerCase(); | |
| 536 | var rows = document.querySelectorAll('[data-pick-rows] [data-q]'); | |
| 537 | for (var i = 0; i < rows.length; i++) | |
| 538 | rows[i].hidden = needle !== '' && rows[i].getAttribute('data-q').indexOf(needle) < 0; | |
| 668 | var chosen = document.getElementById('migrate-chosen'); | |
| 669 | var flying = {}; | |
| 670 | function swap(query, ids, after) { | |
| 671 | var key = ids[0]; | |
| 672 | if (flying[key]) flying[key].abort(); | |
| 673 | var ctl = flying[key] = new AbortController(); | |
| 674 | fetch('/migrate?' + query, { credentials: 'same-origin', headers: { accept: 'text/html' }, signal: ctl.signal }) | |
| 675 | .then(function (r) { return r.ok ? r.text() : ''; }) | |
| 676 | .then(function (text) { | |
| 677 | if (!text || flying[key] !== ctl) return; | |
| 678 | var doc = new DOMParser().parseFromString(text, 'text/html'); | |
| 679 | ids.forEach(function (id) { | |
| 680 | var fresh = doc.getElementById(id), old = document.getElementById(id); | |
| 681 | if (fresh && old) old.innerHTML = fresh.innerHTML; | |
| 682 | }); | |
| 683 | if (after) after(); | |
| 684 | }, function () {}); | |
| 685 | } | |
| 686 | function ticked() { | |
| 687 | var boxes = document.querySelectorAll('input[type=checkbox][name=multi]'); | |
| 688 | var held = chosen ? chosen.querySelectorAll('input') : []; | |
| 689 | for (var i = 0; i < boxes.length; i++) | |
| 690 | for (var j = 0; j < held.length; j++) | |
| 691 | if (held[j].value === boxes[i].value) boxes[i].checked = true; | |
| 692 | } | |
| 693 | document.addEventListener('change', function (e) { | |
| 694 | var t = e.target; | |
| 695 | if (!chosen || t.name !== 'multi' || t.type !== 'checkbox') return; | |
| 696 | var held = chosen.querySelectorAll('input'); | |
| 697 | for (var i = 0; i < held.length; i++) if (held[i].value === t.value) chosen.removeChild(held[i]); | |
| 698 | if (t.checked) { | |
| 699 | var h = document.createElement('input'); | |
| 700 | h.type = 'hidden'; h.name = 'multi'; h.value = t.value; | |
| 701 | chosen.appendChild(h); | |
| 702 | } | |
| 539 | 703 | }); |
| 540 | if (!name) return; | |
| 541 | var touched = name.value.trim() !== ''; | |
| 542 | name.addEventListener('input', function () { touched = true; }); | |
| 543 | function fromUrl(v) { | |
| 544 | var path = v.trim().replace(/[?#].*$/, '').replace(/\\/+$/, ''); | |
| 545 | return (path.split('/').pop() || '').replace(/\\.git$/i, ''); | |
| 704 | var timer; | |
| 705 | document.addEventListener('input', function (e) { | |
| 706 | var t = e.target; | |
| 707 | var target = t.getAttribute && t.getAttribute('data-search'); | |
| 708 | if (!target) return; | |
| 709 | clearTimeout(timer); | |
| 710 | timer = setTimeout(function () { | |
| 711 | swap('q=' + encodeURIComponent(t.value.slice(0, 100)), [target], ticked); | |
| 712 | }, 120); | |
| 713 | }); | |
| 714 | if (url) { | |
| 715 | var seen; | |
| 716 | url.addEventListener('input', function () { | |
| 717 | clearTimeout(seen); | |
| 718 | seen = setTimeout(function () { | |
| 719 | swap('url=' + encodeURIComponent(url.value.slice(0, 500)), ['migrate-preview']); | |
| 720 | }, 150); | |
| 721 | }); | |
| 546 | 722 | } |
| 547 | function follow() { | |
| 548 | if (touched) return; | |
| 549 | name.value = url ? fromUrl(url.value) : ''; | |
| 550 | } | |
| 551 | if (url) url.addEventListener('input', follow); | |
| 552 | 723 | })(); |
| 553 | `; | |
| 724 | `); | |
| 554 | 725 | |
| 555 | 726 | async function show( |
| 556 | 727 | ctx: Ctx, |
| @@ -572,7 +743,7 @@ async function show( | ||
| 572 | 743 | { |
| 573 | 744 | ctx, |
| 574 | 745 | title: "Migrate", |
| 575 | body: migrateForm(ctx, values, origins, branches, error, unmatched), | |
| 746 | body: tight(migrateForm(ctx, values, origins, branches, error, unmatched)), | |
| 576 | 747 | scriptJs: migrateJs, |
| 577 | 748 | }, |
| 578 | 749 | status, |
| @@ -605,8 +776,10 @@ export async function migratePage(ctx: Ctx): Promise<Response> { | ||
| 605 | 776 | source: linked.repo |
| 606 | 777 | ? pickValue(linked.repo.originId, linked.repo.fullName) |
| 607 | 778 | : (q.get("source") ?? ""), |
| 608 | q: (q.get("q") ?? "").slice(0, 100), | |
| 609 | url: q.get("url") ?? "", | |
| 779 | // Every account's box shares one form, so the empty ones come too. | |
| 780 | q: (q.getAll("q").find((v) => v !== "") ?? "").slice(0, 100), | |
| 781 | multi: [], | |
| 782 | url: (q.get("url") ?? "").slice(0, 500), | |
| 610 | 783 | owner: q.get("owner") ?? ctx.auth.user.username, |
| 611 | 784 | name: q.get("name") ?? (linked.repo?.fullName.split("/").pop() ?? ""), |
| 612 | 785 | visibility: "private", |
| @@ -625,6 +798,46 @@ export async function migratePage(ctx: Ctx): Promise<Response> { | ||
| 625 | 798 | ); |
| 626 | 799 | } |
| 627 | 800 | |
| 801 | /** Every ticked row, each under its own name; one job goes to its page, several to the feed. */ | |
| 802 | async function migrateMany( | |
| 803 | ctx: Ctx, | |
| 804 | values: MigrateValues, | |
| 805 | again: (message: string) => Promise<Response>, | |
| 806 | ): Promise<Response> { | |
| 807 | const user = ctx.auth.user!; | |
| 808 | const jobs: string[] = []; | |
| 809 | for (const value of values.multi.slice(0, MULTI_MAX)) { | |
| 810 | const source = parsePick(value); | |
| 811 | if (source === undefined || !("fullName" in source)) continue; | |
| 812 | const started = await migrateRepo(ctx.app, user, { | |
| 813 | source, | |
| 814 | owner: values.owner, | |
| 815 | name: "", | |
| 816 | visibility: values.visibility === "public" ? "public" : "private", | |
| 817 | ...(values.agent === "" | |
| 818 | ? {} | |
| 819 | : { crew: { agent: values.agent, autonomous: values.autonomous } }), | |
| 820 | after: values.after, | |
| 821 | }); | |
| 822 | if (!started.ok) { | |
| 823 | if (started.error === "forbidden_owner" || started.error === "unauthorized") | |
| 824 | return new Response("forbidden", { status: 403 }); | |
| 825 | const said = migrateMessage(started.error, repositoryLimitMessage(ctx.app, user)); | |
| 826 | if (jobs.length === 0) return again(`${source.fullName}: ${said}`); | |
| 827 | return flashTo(ctx, "/", { | |
| 828 | tone: "attn", | |
| 829 | message: `Started ${jobs.length}; ${source.fullName} stopped the rest: ${said}`, | |
| 830 | }); | |
| 831 | } | |
| 832 | jobs.push(started.value.job.id); | |
| 833 | } | |
| 834 | if (jobs.length === 1) return redirect(`/migrate/jobs/${jobs[0]}`); | |
| 835 | if (jobs.length === 0) return again("Tick the repositories you want first."); | |
| 836 | return flashTo(ctx, "/", { | |
| 837 | message: `Started ${jobs.length} migrations — each repository appears as it lands.`, | |
| 838 | }); | |
| 839 | } | |
| 840 | ||
| 628 | 841 | // POST /migrate |
| 629 | 842 | export async function migrateSubmit(ctx: Ctx): Promise<Response> { |
| 630 | 843 | if (!ctx.auth.user) return signInFirst(ctx); |
| @@ -634,6 +847,13 @@ export async function migrateSubmit(ctx: Ctx): Promise<Response> { | ||
| 634 | 847 | // A row's button names its repository; it wins over a prefilled pick. |
| 635 | 848 | source: field(form, "pick") || field(form, "source"), |
| 636 | 849 | q: "", |
| 850 | multi: [ | |
| 851 | ...new Set( | |
| 852 | form | |
| 853 | .getAll("multi") | |
| 854 | .filter((v): v is string => typeof v === "string" && v.length <= 400), | |
| 855 | ), | |
| 856 | ], | |
| 637 | 857 | url: field(form, "url").trim(), |
| 638 | 858 | owner: field(form, "owner"), |
| 639 | 859 | name: field(form, "name").trim(), |
| @@ -653,6 +873,20 @@ export async function migrateSubmit(ctx: Ctx): Promise<Response> { | ||
| 653 | 873 | const again = async (message: string): Promise<Response> => |
| 654 | 874 | show(ctx, values, await originChoices(ctx), message, 422); |
| 655 | 875 | |
| 876 | const wantsMulti = | |
| 877 | field(form, "pick") === "" && | |
| 878 | values.multi.length > 0 && | |
| 879 | (field(form, "go") === "multi" || (values.source === "" && values.url === "")); | |
| 880 | if (field(form, "go") === "multi" && values.multi.length === 0) | |
| 881 | return again("Tick the repositories you want first."); | |
| 882 | if (wantsMulti) { | |
| 883 | if (values.url !== "") | |
| 884 | return again("Tick repositories or paste a URL, not both — clear one of them."); | |
| 885 | if (ctx.auth.scope === undefined || !scopeAllows(ctx.auth.scope, "admin")) | |
| 886 | return new Response("forbidden", { status: 403 }); | |
| 887 | return migrateMany(ctx, values, again); | |
| 888 | } | |
| 889 | ||
| 656 | 890 | const picked = values.source === "" ? undefined : parsePick(values.source); |
| 657 | 891 | if (picked !== undefined && values.url !== "") |
| 658 | 892 | return again( |
src/web/style/components.ts +6
| @@ -90,6 +90,7 @@ export const components = /* css */ ` | ||
| 90 | 90 | box-shadow: 0 0 0 3px color-mix(in srgb, var(--danger) 14%, transparent); |
| 91 | 91 | } |
| 92 | 92 | .input[readonly] { background: var(--muted); color: var(--muted-fg); } |
| 93 | .input[data-size="lg"] { min-height: 52px; padding: 12px 14px; font-size: 16px; } | |
| 93 | 94 | .input[data-size="sm"] { min-height: 28px; padding: 4px 8px; font-size: 13px; } |
| 94 | 95 | textarea.input { |
| 95 | 96 | resize: vertical; min-height: 88px; line-height: 1.55; font-family: inherit; |
| @@ -451,6 +452,11 @@ details[open] > summary.row > .row-lead { transform: rotate(90deg); } | ||
| 451 | 452 | .setting-list .row-trail { flex: 0 1 auto; max-width: 100%; } |
| 452 | 453 | @media (max-width: 640px) { .setting-list .row-trail { flex-basis: 100%; } } |
| 453 | 454 | @media (pointer: coarse) { .row-actions { opacity: 1; } .row { min-height: 44px; } } |
| 455 | /* A picker row's name is a link beside its tick: both keep a 44px target. */ | |
| 456 | @media (pointer: coarse) { | |
| 457 | [data-pick-rows] .row-title { display: flex; align-items: center; gap: 8px; } | |
| 458 | [data-pick-rows] .row-title a { display: inline-flex; align-items: center; min-height: 44px; } | |
| 459 | } | |
| 454 | 460 | .row[data-state="closed"] .row-title, .row[data-state="merged"] .row-title { color: var(--muted-fg); } |
| 455 | 461 | |
| 456 | 462 | /* ---- edit row: a list row whose body is a small form ---- */ |
src/web/ui.ts +36 −1
| @@ -162,6 +162,8 @@ export interface BtnOptions { | ||
| 162 | 162 | /** Submit a form this button does not sit inside — the way to offer two |
| 163 | 163 | * actions in one row without nesting forms, which HTML forbids. */ |
| 164 | 164 | form?: string; |
| 165 | /** Where this button submits its form instead of the form's own action. */ | |
| 166 | formaction?: string; | |
| 165 | 167 | /** A `g i`-style keyboard sequence the shell's shortcut layer picks up. */ |
| 166 | 168 | shortcut?: string; |
| 167 | 169 | /** Copy the text of the element with this id (enhancement; inert with JS off). */ |
| @@ -201,6 +203,7 @@ export function btn(content: Html | string, o: BtnOptions = {}): Html { | ||
| 201 | 203 | ${o.name ? html`name="${o.name}"` : null} |
| 202 | 204 | ${o.value !== undefined ? html`value="${o.value}"` : null} |
| 203 | 205 | ${o.form ? html`form="${o.form}"` : null} |
| 206 | ${o.formaction ? html`formaction="${o.formaction}"` : null} | |
| 204 | 207 | ${o.disabled ? raw("disabled") : null} |
| 205 | 208 | ${btnAttrs(o)} |
| 206 | 209 | > |
| @@ -1139,6 +1142,8 @@ export function menu(o: { | ||
| 1139 | 1142 | |
| 1140 | 1143 | export interface FieldOptions { |
| 1141 | 1144 | label: string; |
| 1145 | /** `lg`: the one large field a page is for. */ | |
| 1146 | size?: "lg"; | |
| 1142 | 1147 | name: string; |
| 1143 | 1148 | value?: string; |
| 1144 | 1149 | type?: string; |
| @@ -1159,6 +1164,35 @@ export interface FieldOptions { | ||
| 1159 | 1164 | id?: string; |
| 1160 | 1165 | } |
| 1161 | 1166 | |
| 1167 | /** | |
| 1168 | * A search box for a list that filters as you type: `data-search` names the | |
| 1169 | * list it narrows (the id of its container), and a script may swap that | |
| 1170 | * container for the server's answer to the same query. Without a script it | |
| 1171 | * is a plain GET field — Enter narrows on the server. | |
| 1172 | */ | |
| 1173 | export function searchInput(o: { | |
| 1174 | name: string; | |
| 1175 | value: string; | |
| 1176 | label: string; | |
| 1177 | placeholder: string; | |
| 1178 | /** The id of the list container this box narrows. */ | |
| 1179 | target: string; | |
| 1180 | /** The GET form it belongs to, when it sits outside one. */ | |
| 1181 | form?: string; | |
| 1182 | }): Html { | |
| 1183 | return html`<input | |
| 1184 | class="input" | |
| 1185 | type="search" | |
| 1186 | name="${o.name}" | |
| 1187 | value="${o.value}" | |
| 1188 | aria-label="${o.label}" | |
| 1189 | placeholder="${o.placeholder}" | |
| 1190 | autocomplete="off" | |
| 1191 | data-search="${o.target}" | |
| 1192 | ${o.form ? html`form="${o.form}"` : null} | |
| 1193 | />`; | |
| 1194 | } | |
| 1195 | ||
| 1162 | 1196 | /** Label, control, hint and error, wired together with `aria-describedby`. */ |
| 1163 | 1197 | export function field(o: FieldOptions): Html { |
| 1164 | 1198 | const id = o.id ?? `f-${o.name}`; |
| @@ -1173,7 +1207,8 @@ export function field(o: FieldOptions): Html { | ||
| 1173 | 1207 | ${o.autofocus ? raw("autofocus") : null} |
| 1174 | 1208 | ${o.readonly ? raw("readonly") : null} |
| 1175 | 1209 | ${o.placeholder ? html`placeholder="${o.placeholder}"` : null} |
| 1176 | ${o.spellcheck === false ? raw('spellcheck="false"') : null}`; | |
| 1210 | ${o.spellcheck === false ? raw('spellcheck="false"') : null} | |
| 1211 | ${o.size === "lg" ? raw('data-size="lg"') : null}`; | |
| 1177 | 1212 | const control = |
| 1178 | 1213 | o.type === "textarea" |
| 1179 | 1214 | ? html`<textarea ${shared} rows="${o.rows ?? 6}" data-grow> |
test/html-tight.test.ts added +25
| @@ -0,0 +1,25 @@ | ||
| 1 | import { expect, test } from "bun:test"; | |
| 2 | import { html, raw, tight } from "../src/web/html.ts"; | |
| 3 | ||
| 4 | test("tight drops indentation and whitespace between block tags", () => { | |
| 5 | const out = tight(html`<ul> | |
| 6 | <li> | |
| 7 | <div class="row" >a</div> | |
| 8 | </li> | |
| 9 | </ul>`).text; | |
| 10 | expect(out).toBe('<ul><li><div class="row">a</div></li></ul>'); | |
| 11 | }); | |
| 12 | ||
| 13 | test("tight keeps the space between inline tags, so words stay apart", () => { | |
| 14 | const out = tight(html`<p> | |
| 15 | <a href="/x">one</a> | |
| 16 | <span>two</span> | |
| 17 | </p>`).text; | |
| 18 | expect(out).toBe('<p><a href="/x">one</a> <span>two</span></p>'); | |
| 19 | }); | |
| 20 | ||
| 21 | test("tight leaves text and escaped angle brackets alone", () => { | |
| 22 | const out = tight(html`<p>${"a > b < c"} and more</p>`).text; | |
| 23 | expect(out).toBe("<p>a > b < c and more</p>"); | |
| 24 | expect(tight(raw("<b>x</b>")).text).toBe("<b>x</b>"); | |
| 25 | }); |
test/migrate-layout.test.ts added +297
| @@ -0,0 +1,297 @@ | ||
| 1 | /** `/migrate` leads with the URL field; accounts are compact and show eight rows (#5816). */ | |
| 2 | import { afterAll, beforeAll, expect, test } from "bun:test"; | |
| 3 | import { mkdtempSync, rmSync } from "node:fs"; | |
| 4 | import { tmpdir } from "node:os"; | |
| 5 | import { join } from "node:path"; | |
| 6 | import { createApp, type App } from "../src/app.ts"; | |
| 7 | import { cacheOriginRepos, createOrigin } from "../src/core/origins.ts"; | |
| 8 | import { createUser } from "../src/core/users.ts"; | |
| 9 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 10 | import { byRecentPush, migrateJs, MULTI_MAX, PICKER_ROWS } from "../src/web/pages/migrate.ts"; | |
| 11 | import { createToken } from "../src/core/tokens.ts"; | |
| 12 | import { searchInput, btn } from "../src/web/ui.ts"; | |
| 13 | import { connectOrigin } from "../src/actions/origins.ts"; | |
| 14 | import { forgetProviders } from "../src/origins/providers/index.ts"; | |
| 15 | import { newFakeState, serveFakeForge, type FakeForge } from "./fake-origin.ts"; | |
| 16 | import type { User } from "../src/core/users.ts"; | |
| 17 | import { quietLogs } from "./helpers/globals.ts"; | |
| 18 | ||
| 19 | quietLogs(); | |
| 20 | ||
| 21 | let root = ""; | |
| 22 | let app: App; | |
| 23 | let servers: Servers; | |
| 24 | let cookie = ""; | |
| 25 | let userId = ""; | |
| 26 | let alice: User; | |
| 27 | let fake: FakeForge | undefined; | |
| 28 | let fakeOrigin = ""; | |
| 29 | ||
| 30 | beforeAll(async () => { | |
| 31 | root = mkdtempSync(join(tmpdir(), "forge-migrate-layout-")); | |
| 32 | app = createApp({ dataDir: root, port: 0, forgeRepo: "alice/forge" }); | |
| 33 | const u = await createUser(app.db, { | |
| 34 | origin: "seed", | |
| 35 | trusted: true, | |
| 36 | username: "alice", | |
| 37 | email: "[email protected]", | |
| 38 | password: "a-good-passphrase", | |
| 39 | adminIfFirst: true, | |
| 40 | }); | |
| 41 | if (!u.ok) throw new Error(u.error); | |
| 42 | userId = u.value.id; | |
| 43 | alice = u.value; | |
| 44 | servers = await serve(app); | |
| 45 | const signin = await fetch(`${servers.url}/signin`, { | |
| 46 | method: "POST", | |
| 47 | headers: { origin: servers.url }, | |
| 48 | body: new URLSearchParams({ username: "alice", password: "a-good-passphrase" }), | |
| 49 | redirect: "manual", | |
| 50 | }); | |
| 51 | cookie = (signin.headers.get("set-cookie") ?? "").split(";")[0]!; | |
| 52 | }); | |
| 53 | afterAll(() => { | |
| 54 | fake?.stop(); | |
| 55 | servers.stop(); | |
| 56 | app.close(); | |
| 57 | rmSync(root, { recursive: true, force: true }); | |
| 58 | }); | |
| 59 | ||
| 60 | const get = async (path: string): Promise<string> => | |
| 61 | (await fetch(`${servers.url}${path}`, { headers: { cookie } })).text(); | |
| 62 | ||
| 63 | const main = (page: string): string => page.slice(page.indexOf("<main")); | |
| 64 | const rowCount = (page: string): number => page.split('name="pick"').length - 1; | |
| 65 | ||
| 66 | test("with no account the URL field leads and there is no list", async () => { | |
| 67 | const bare = await get("/migrate"); | |
| 68 | const page = main(bare); | |
| 69 | expect(page).toContain('name="url"'); | |
| 70 | expect(page).toContain("Paste a repository URL"); | |
| 71 | expect(page).not.toContain('name="pick"'); | |
| 72 | expect(page).not.toContain("Your accounts</h3>"); | |
| 73 | expect(page).toContain("Connect GitHub"); | |
| 74 | }); | |
| 75 | ||
| 76 | test("200 repositories: the URL field is the first input, the page is small, eight rows show until you search", async () => { | |
| 77 | const made = createOrigin(app.db, root, { | |
| 78 | userId, | |
| 79 | kind: "github", | |
| 80 | apiUrl: "https://api.github.com", | |
| 81 | webUrl: "https://github.com", | |
| 82 | login: "octo", | |
| 83 | token: "t0ken", | |
| 84 | }); | |
| 85 | if (!made.ok) throw new Error(made.error); | |
| 86 | const repos = Array.from({ length: 200 }, (_, i) => ({ | |
| 87 | remoteId: String(1000 + i), | |
| 88 | owner: "octo", | |
| 89 | name: `repo-${i}`, | |
| 90 | fullName: `octo/repo-${i}`, | |
| 91 | description: `Repository number ${i}, and a plain line about it`, | |
| 92 | private: i === 5, | |
| 93 | fork: false, | |
| 94 | defaultBranch: "main", | |
| 95 | cloneUrl: `https://github.com/octo/repo-${i}.git`, | |
| 96 | htmlUrl: `https://github.com/octo/repo-${i}`, | |
| 97 | pushedAt: new Date(Date.UTC(2026, 0, 1) + i * 86_400_000).toISOString(), | |
| 98 | sshUrl: "", | |
| 99 | permissions: { push: true, admin: false }, | |
| 100 | openIssues: 0, | |
| 101 | openPulls: 0, | |
| 102 | openIssuesIncludesPulls: false, | |
| 103 | sizeKb: 2048, | |
| 104 | parent: null, | |
| 105 | })); | |
| 106 | cacheOriginRepos(app.db, made.value.id, repos); | |
| 107 | ||
| 108 | const full = await get("/migrate"); | |
| 109 | const page = main(full); | |
| 110 | // The page's own part (the form and the rows) stays small; the shared | |
| 111 | // shell around it is not this page's to shrink. | |
| 112 | const own = page.slice(0, page.indexOf("</main>")); | |
| 113 | expect(new TextEncoder().encode(own).length).toBeLessThan(14 * 1024); | |
| 114 | expect(new TextEncoder().encode(full).length).toBeLessThan(40 * 1024); | |
| 115 | const firstInput = /<input[^>]*>/s.exec(page)![0]; | |
| 116 | expect(firstInput).toContain('name="url"'); | |
| 117 | expect(rowCount(full)).toBe(PICKER_ROWS); | |
| 118 | // Every tick box is named, or a screen reader hears eight unnamed boxes. | |
| 119 | const boxes = page.match(/<input[^>]*name="multi"[^>]*>/g) ?? []; | |
| 120 | expect(boxes.length).toBe(PICKER_ROWS); | |
| 121 | for (const box of boxes) expect(box).toMatch(/aria-label="Select octo\/repo-\d+"/); | |
| 122 | // Disconnect submits the shared drop form to this account's own route. | |
| 123 | expect(page).toMatch(/<button[^>]*form="drop"[^>]*formaction="\/settings\/origins\/[^"]+\/delete"/); | |
| 124 | expect(page).toContain('data-search="pick-'); | |
| 125 | expect(page).toContain("200 repositories"); | |
| 126 | expect(page).toContain("192 more"); | |
| 127 | // Newest push first. | |
| 128 | expect(page.indexOf("octo/repo-199")).toBeLessThan(page.indexOf("octo/repo-198")); | |
| 129 | expect(page).not.toContain("octo/repo-3<"); | |
| 130 | ||
| 131 | // Searching reaches rows beyond the eight, on the server too. | |
| 132 | const found = await get("/migrate?q=repo-0"); | |
| 133 | expect(found).toContain("octo/repo-0<"); | |
| 134 | expect(rowCount(found)).toBe(1); | |
| 135 | const many = await get("/migrate?q=repo-17"); | |
| 136 | expect(many).toContain("octo/repo-179<"); | |
| 137 | expect(many).not.toContain("octo/repo-170<"); | |
| 138 | expect(rowCount(many)).toBe(PICKER_ROWS); | |
| 139 | expect(many).toContain("3 more"); | |
| 140 | }); | |
| 141 | ||
| 142 | test("a pasted URL of one of your repositories shows its visibility and size", async () => { | |
| 143 | const known = await get("/migrate?url=" + encodeURIComponent("https://github.com/octo/repo-5.git")); | |
| 144 | const preview = /id="migrate-preview"[^>]*>([\s\S]*?)<\/p>/.exec(known)![1]!; | |
| 145 | expect(preview).toContain("octo/repo-5"); | |
| 146 | expect(preview).toContain("private"); | |
| 147 | expect(preview).toContain("2.0 MiB"); | |
| 148 | const other = await get("/migrate?url=" + encodeURIComponent("https://gitlab.com/x/y")); | |
| 149 | expect(/id="migrate-preview"[^>]*>([\s\S]*?)<\/p>/.exec(other)![1]).toContain("gitlab.com"); | |
| 150 | const junk = await get("/migrate?url=" + encodeURIComponent("<script>alert(1)</script>")); | |
| 151 | expect(junk).not.toContain("<script>alert(1)"); | |
| 152 | }); | |
| 153 | ||
| 154 | test("Migrate selected needs a tick, and a tick and a URL are not both", async () => { | |
| 155 | const post = (body: Record<string, string | string[]>) => { | |
| 156 | const form = new URLSearchParams(); | |
| 157 | for (const [k, v] of Object.entries(body)) | |
| 158 | for (const one of Array.isArray(v) ? v : [v]) form.append(k, one); | |
| 159 | return fetch(`${servers.url}/migrate`, { | |
| 160 | method: "POST", | |
| 161 | headers: { cookie, origin: servers.url }, | |
| 162 | body: form, | |
| 163 | redirect: "manual", | |
| 164 | }); | |
| 165 | }; | |
| 166 | const none = await post({ go: "multi", url: "", owner: "alice" }); | |
| 167 | expect(none.status).toBe(422); | |
| 168 | expect(await none.text()).toContain("Tick the repositories"); | |
| 169 | const both = await post({ multi: ["x/octo/repo-1"], url: "https://github.com/a/b", owner: "alice", go: "multi" }); | |
| 170 | expect(both.status).toBe(422); | |
| 171 | expect(await both.text()).toContain("not both"); | |
| 172 | }); | |
| 173 | ||
| 174 | test("recently pushed sorts first, unknown last", () => { | |
| 175 | const rows = [{ pushedAt: null }, { pushedAt: "2026-01-01T00:00:00Z" }, { pushedAt: "2026-02-01T00:00:00Z" }]; | |
| 176 | expect(byRecentPush(rows).map((r) => r.pushedAt)).toEqual(["2026-02-01T00:00:00Z", "2026-01-01T00:00:00Z", null]); | |
| 177 | }); | |
| 178 | ||
| 179 | /** A real (fake) forgejo behind one more account, so a started job can resolve its source. */ | |
| 180 | async function fakeAccount(): Promise<string> { | |
| 181 | if (fakeOrigin !== "") return fakeOrigin; | |
| 182 | const state = newFakeState("forgejo", "octo", "fake-secret"); | |
| 183 | for (let i = 0; i < 80; i++) | |
| 184 | state.repos.push({ | |
| 185 | id: 7000 + i, | |
| 186 | fullName: `octo/pick-${i}`, | |
| 187 | description: "", | |
| 188 | cloneUrl: `http://127.0.0.1:9/octo/pick-${i}.git`, | |
| 189 | htmlUrl: `http://127.0.0.1:9/octo/pick-${i}`, | |
| 190 | defaultBranch: "main", | |
| 191 | private: false, | |
| 192 | }); | |
| 193 | fake = serveFakeForge(state); | |
| 194 | forgetProviders(root); | |
| 195 | const connected = await connectOrigin(app, alice, { host: fake.url, kind: "forgejo", token: "fake-secret" }); | |
| 196 | if (!connected.ok) throw new Error(connected.error); | |
| 197 | fakeOrigin = connected.value.id; | |
| 198 | return fakeOrigin; | |
| 199 | } | |
| 200 | ||
| 201 | const pickOf = async (index: number): Promise<string> => `${await fakeAccount()}/octo/pick-${index}`; | |
| 202 | ||
| 203 | const postWith = (headers: Record<string, string>, body: URLSearchParams) => | |
| 204 | fetch(`${servers.url}/migrate`, { | |
| 205 | method: "POST", | |
| 206 | headers: { origin: servers.url, ...headers }, | |
| 207 | body, | |
| 208 | redirect: "manual", | |
| 209 | }); | |
| 210 | ||
| 211 | const ticks = (picks: string[]): URLSearchParams => { | |
| 212 | const form = new URLSearchParams({ go: "multi", url: "", owner: "alice", visibility: "private" }); | |
| 213 | for (const p of picks) form.append("multi", p); | |
| 214 | return form; | |
| 215 | }; | |
| 216 | ||
| 217 | test("one tick starts one job and lands on its page; duplicates collapse", async () => { | |
| 218 | const one = await postWith({ cookie }, ticks([await pickOf(1)])); | |
| 219 | expect(one.status).toBe(303); | |
| 220 | expect(one.headers.get("location")).toMatch(/^\/migrate\/jobs\/[A-Za-z0-9]+$/); | |
| 221 | const twice = await postWith({ cookie }, ticks([await pickOf(2), await pickOf(2)])); | |
| 222 | expect(twice.status).toBe(303); | |
| 223 | expect(twice.headers.get("location")).toMatch(/^\/migrate\/jobs\//); | |
| 224 | }); | |
| 225 | ||
| 226 | test("several ticks go to the feed with a flash saying how many", async () => { | |
| 227 | const several = await postWith({ cookie }, ticks([await pickOf(3), await pickOf(4), await pickOf(5)])); | |
| 228 | expect(several.status).toBe(303); | |
| 229 | expect(several.headers.get("location")).toBe("/"); | |
| 230 | expect(decodeURIComponent(several.headers.get("set-cookie") ?? "")).toContain("Started 3 migrations"); | |
| 231 | }); | |
| 232 | ||
| 233 | test(`Migrate selected starts at most ${MULTI_MAX}`, async () => { | |
| 234 | const picks: string[] = []; | |
| 235 | for (let i = 10; i < 10 + MULTI_MAX + 5; i++) picks.push(await pickOf(i)); | |
| 236 | const res = await postWith({ cookie }, ticks(picks)); | |
| 237 | expect(res.status).toBe(303); | |
| 238 | expect(decodeURIComponent(res.headers.get("set-cookie") ?? "")).toContain(`Started ${MULTI_MAX} migrations`); | |
| 239 | }); | |
| 240 | ||
| 241 | test("a token that cannot spend an account's credential is refused ticked rows", async () => { | |
| 242 | const rw = createToken(app.db, userId, "multi", "rw").token; | |
| 243 | const res = await postWith({ authorization: `Bearer ${rw}` }, ticks([await pickOf(60)])); | |
| 244 | expect(res.status).toBe(403); | |
| 245 | }); | |
| 246 | ||
| 247 | test("without a script, a search in any account's box filters, the empty boxes ignored", async () => { | |
| 248 | const second = createOrigin(app.db, root, { | |
| 249 | userId, | |
| 250 | kind: "forgejo", | |
| 251 | apiUrl: "https://git.example.test/api/v1", | |
| 252 | webUrl: "https://git.example.test", | |
| 253 | login: "alice", | |
| 254 | token: "t0ken", | |
| 255 | }); | |
| 256 | if (!second.ok) throw new Error(second.error); | |
| 257 | cacheOriginRepos(app.db, second.value.id, [ | |
| 258 | { | |
| 259 | remoteId: "9001", owner: "alice", name: "zebra", fullName: "alice/zebra", description: "", private: false, | |
| 260 | fork: false, defaultBranch: "main", cloneUrl: "https://git.example.test/alice/zebra.git", | |
| 261 | htmlUrl: "https://git.example.test/alice/zebra", sshUrl: "", pushedAt: null, | |
| 262 | permissions: { push: true, admin: false }, openIssues: 0, openPulls: 0, | |
| 263 | openIssuesIncludesPulls: false, sizeKb: 0, parent: null, | |
| 264 | }, | |
| 265 | ]); | |
| 266 | const full = await get("/migrate"); | |
| 267 | // One shared GET form, both boxes attached to it. | |
| 268 | expect(full.match(/data-search="pick-[^"]+"[^>]*form="find"|form="find"[^>]*data-search/g)?.length ?? 0).toBeGreaterThanOrEqual(2); | |
| 269 | expect(full).toContain('<form id="find" method="get" action="/migrate">'); | |
| 270 | // A browser sends every box; the empty one first must not win. | |
| 271 | const res = await get("/migrate?q=&q=zebra"); | |
| 272 | expect(res).toContain("alice/zebra<"); | |
| 273 | expect(res).not.toContain("octo/repo-199<"); | |
| 274 | }); | |
| 275 | ||
| 276 | test("searchInput names its list and its form, and escapes its words", () => { | |
| 277 | const out = searchInput({ | |
| 278 | name: "q", value: '"><x>', label: "Find", placeholder: "Search", target: "pick-1", form: "find", | |
| 279 | }).text; | |
| 280 | expect(out).toContain('type="search"'); | |
| 281 | expect(out).toContain('data-search="pick-1"'); | |
| 282 | expect(out).toContain('form="find"'); | |
| 283 | expect(out).not.toContain("<x>"); | |
| 284 | expect(btn("Go", { form: "drop", formaction: "/a/b" }).text).toContain('formaction="/a/b"'); | |
| 285 | }); | |
| 286 | ||
| 287 | test("the page holds no <pre> or <textarea>, which tight() would mangle", async () => { | |
| 288 | const page = await get("/migrate"); | |
| 289 | expect(page).not.toMatch(/<pre[\s>]/i); | |
| 290 | expect(page).not.toMatch(/<textarea[\s>]/i); | |
| 291 | }); | |
| 292 | ||
| 293 | test("a live swap aborts the one before it and drops an answer that is no longer the latest", async () => { | |
| 294 | expect(migrateJs).toContain("new AbortController"); | |
| 295 | expect(migrateJs).toContain(".abort()"); | |
| 296 | expect(migrateJs).toMatch(/flying\[\w+\]\s*!==\s*ctl/); | |
| 297 | }); |
test/tui-migrate.test.ts +109 −19
| @@ -14,6 +14,7 @@ import { connectOrigin } from "../src/actions/origins.ts"; | ||
| 14 | 14 | import { commitToBranch, createRepo } from "../src/actions/repos.ts"; |
| 15 | 15 | import { listIssues } from "../src/core/issues.ts"; |
| 16 | 16 | import { getRepo } from "../src/core/repos.ts"; |
| 17 | import { MULTI_MAX } from "../src/web/pages/migrate.ts"; | |
| 17 | 18 | import { createToken } from "../src/core/tokens.ts"; |
| 18 | 19 | import { createUser, type User } from "../src/core/users.ts"; |
| 19 | 20 | import { repoDir } from "../src/git/repos.ts"; |
| @@ -103,6 +104,8 @@ beforeAll(async () => { | ||
| 103 | 104 | htmlUrl: `${base}/octo/widgets`, |
| 104 | 105 | defaultBranch: "main", |
| 105 | 106 | private: true, |
| 107 | pushedAt: "2026-10-01T10:00:00Z", | |
| 108 | sizeKb: 2048, | |
| 106 | 109 | }); |
| 107 | 110 | state.branches.set("octo/widgets", [ |
| 108 | 111 | { name: "main", sha: await sha("main") }, |
| @@ -122,9 +125,16 @@ beforeAll(async () => { | ||
| 122 | 125 | files: [{ path: "README.md", content: "hi\n" }], |
| 123 | 126 | }); |
| 124 | 127 | if (!pubDone.ok) throw new Error(String(pubDone.error)); |
| 125 | // Repositories whose branches are none, or cannot be read. | |
| 128 | // Repositories whose branches are none, or cannot be read; flaky was pushed last. | |
| 126 | 129 | for (const [id, fullName] of [[7, "octo/empty"], [8, "octo/flaky"]] as const) |
| 127 | state.repos.push({ id, fullName, cloneUrl: `${base}/${fullName}.git`, defaultBranch: "main", private: false }); | |
| 130 | state.repos.push({ | |
| 131 | id, fullName, cloneUrl: `${base}/${fullName}.git`, defaultBranch: "main", private: false, | |
| 132 | ...(fullName === "octo/flaky" ? { pushedAt: "2026-10-20T10:00:00Z" } : {}), | |
| 133 | }); | |
| 134 | state.repos.push({ | |
| 135 | id: 9, fullName: "octo/pub", cloneUrl: `${base}/octo/pub.git`, htmlUrl: `${base}/octo/pub`, | |
| 136 | defaultBranch: "main", private: false, pushedAt: "2026-10-10T10:00:00Z", | |
| 137 | }); | |
| 128 | 138 | state.branches.set("octo/empty", []); |
| 129 | 139 | state.branches.set("octo/flaky", [{ name: "main", sha: "a".repeat(40) }]); |
| 130 | 140 | fake = serveFakeForge(state); |
| @@ -144,6 +154,23 @@ afterAll(async () => { | ||
| 144 | 154 | await rm(dir, { recursive: true, force: true }); |
| 145 | 155 | }); |
| 146 | 156 | |
| 157 | /** A terminal for `user` (or nobody), with `body` run and the terminal always stopped. */ | |
| 158 | async function withTui( | |
| 159 | user: User | undefined, | |
| 160 | body: (h: { tui: ReturnType<typeof createForgeApp>["app"]; terminal: ReturnType<typeof headlessTerminal> }) => Promise<void>, | |
| 161 | ): Promise<void> { | |
| 162 | const terminal = headlessTerminal(120, 40); | |
| 163 | const id = user === undefined ? { sshUsername: "git", remoteAddress: "127.0.0.1", fingerprint: "SHA256:test", user } : identity(user); | |
| 164 | const { app: tui } = createForgeApp(app, id, { terminal, exitHooks: false }); | |
| 165 | tui.start(); | |
| 166 | try { | |
| 167 | await settleApp(tui); | |
| 168 | await body({ tui, terminal }); | |
| 169 | } finally { | |
| 170 | tui.stop?.(); | |
| 171 | } | |
| 172 | } | |
| 173 | ||
| 147 | 174 | describe("migrate from the terminal", () => { |
| 148 | 175 | test("pick a repository, the person's own newest branch is first, a crew goes on it, and it ends on the repository", async () => { |
| 149 | 176 | const terminal = headlessTerminal(120, 40); |
| @@ -190,23 +217,6 @@ describe("migrate from the terminal", () => { | ||
| 190 | 217 | } |
| 191 | 218 | }, 60_000); |
| 192 | 219 | |
| 193 | /** A terminal for `user` (or nobody), with `body` run and the terminal always stopped. */ | |
| 194 | async function withTui( | |
| 195 | user: User | undefined, | |
| 196 | body: (h: { tui: ReturnType<typeof createForgeApp>["app"]; terminal: ReturnType<typeof headlessTerminal> }) => Promise<void>, | |
| 197 | ): Promise<void> { | |
| 198 | const terminal = headlessTerminal(120, 40); | |
| 199 | const id = user === undefined ? { sshUsername: "git", remoteAddress: "127.0.0.1", fingerprint: "SHA256:test", user } : identity(user); | |
| 200 | const { app: tui } = createForgeApp(app, id, { terminal, exitHooks: false }); | |
| 201 | tui.start(); | |
| 202 | try { | |
| 203 | await settleApp(tui); | |
| 204 | await body({ tui, terminal }); | |
| 205 | } finally { | |
| 206 | tui.stop?.(); | |
| 207 | } | |
| 208 | } | |
| 209 | ||
| 210 | 220 | test("u pastes a URL: an empty address is told so, the name comes from the URL, the branch step is skipped, and a blank name keeps the source's", () => |
| 211 | 221 | withTui(alice, async ({ tui, terminal }) => { |
| 212 | 222 | terminal.feed("Mu"); |
| @@ -305,6 +315,86 @@ describe("migrate from the terminal", () => { | ||
| 305 | 315 | }); |
| 306 | 316 | }); |
| 307 | 317 | |
| 318 | describe("migrate from the terminal: the page's conveniences", () => { | |
| 319 | test("the list is newest push first, as the page's is", () => | |
| 320 | withTui(alice, async ({ tui, terminal }) => { | |
| 321 | terminal.feed("M"); | |
| 322 | await settleApp(tui); | |
| 323 | const text = tui.screen.toText(); | |
| 324 | const at = (n: string) => text.indexOf(n); | |
| 325 | expect(at("octo/flaky")).toBeGreaterThan(-1); | |
| 326 | expect(at("octo/flaky")).toBeLessThan(at("octo/pub")); | |
| 327 | expect(at("octo/pub")).toBeLessThan(at("octo/widgets")); | |
| 328 | expect(at("octo/widgets")).toBeLessThan(at("octo/empty")); | |
| 329 | })); | |
| 330 | ||
| 331 | test("a pasted URL that is one of your repositories shows its visibility and size; a stranger's says it is cloned anonymously", () => | |
| 332 | withTui(alice, async ({ tui, terminal }) => { | |
| 333 | terminal.feed("Mu"); | |
| 334 | await settleApp(tui); | |
| 335 | terminal.feed(`${base}/octo/widgets\r`); | |
| 336 | await settleApp(tui); | |
| 337 | expect(tui.screen.toText()).toContain("octo/widgets on"); | |
| 338 | expect(tui.screen.toText()).toContain("private · 2.0 MiB"); | |
| 339 | terminal.feed("\x1b\x1b"); | |
| 340 | await settleApp(tui); | |
| 341 | terminal.feed("u"); | |
| 342 | await settleApp(tui); | |
| 343 | terminal.feed("https://example.test/someone/else\r"); | |
| 344 | await settleApp(tui); | |
| 345 | expect(tui.screen.toText()).toContain("else on example.test · cloned anonymously"); | |
| 346 | })); | |
| 347 | ||
| 348 | async function frank(name: string): Promise<User> { | |
| 349 | const made = await createUser(app.db, { origin: "seed", trusted: true, username: name, email: `${name}@forge.test` }); | |
| 350 | if (!made.ok) throw new Error(String(made.error)); | |
| 351 | promote(app.db, made.value); | |
| 352 | const connected = await connectOrigin(app, made.value, { host: fake.url, token: secretToken }); | |
| 353 | if (!connected.ok) throw new Error(connected.error); | |
| 354 | return made.value; | |
| 355 | } | |
| 356 | ||
| 357 | test("space ticks rows, Enter migrates every ticked one under its own name with one set of answers", async () => { | |
| 358 | const user = await frank("frank"); | |
| 359 | await withTui(user, async ({ tui, terminal }) => { | |
| 360 | terminal.feed("M"); | |
| 361 | await settleApp(tui); | |
| 362 | terminal.feed("j "); // flaky is first: down to pub, tick it | |
| 363 | terminal.feed("j "); // down to widgets, tick it | |
| 364 | await settleApp(tui); | |
| 365 | expect(tui.screen.toText()).toContain("[x]"); | |
| 366 | terminal.feed("\r"); // ticked rows → the form, no branch step | |
| 367 | await settleApp(tui); | |
| 368 | const form = tui.screen.toText(); | |
| 369 | expect(form).toContain("from 2 repositories"); | |
| 370 | expect(form).toContain("Each keeps its own name."); | |
| 371 | expect(form).not.toContain("Branch of"); | |
| 372 | terminal.feed("\x04"); | |
| 373 | await until(() => getRepo(app.db, "frank", "pub") !== undefined && getRepo(app.db, "frank", "widgets") !== undefined); | |
| 374 | expect(getRepo(app.db, "frank", "flaky")).toBeUndefined(); | |
| 375 | }); | |
| 376 | }, 60_000); | |
| 377 | ||
| 378 | test("ticking past the cap is refused in words", async () => { | |
| 379 | const before = fake.state.repos.length; | |
| 380 | for (let i = 0; i < MULTI_MAX + 1; i++) | |
| 381 | fake.state.repos.push({ id: 100 + i, fullName: `octo/bulk-${i}`, cloneUrl: `${base}/octo/bulk-${i}.git`, defaultBranch: "main", private: false }); | |
| 382 | try { | |
| 383 | // A new account's listing is read fresh. | |
| 384 | const late = await frank("gina"); | |
| 385 | await withTui(late, async ({ tui, terminal }) => { | |
| 386 | terminal.feed("M"); | |
| 387 | await settleApp(tui); | |
| 388 | for (let i = 0; i < MULTI_MAX + 1; i++) terminal.feed(" j"); | |
| 389 | await settleApp(tui); | |
| 390 | expect(tui.screen.toText()).toContain(`At most ${MULTI_MAX} at a time`); | |
| 391 | }); | |
| 392 | } finally { | |
| 393 | fake.state.repos.length = before; | |
| 394 | } | |
| 395 | }, 60_000); | |
| 396 | }); | |
| 397 | ||
| 308 | 398 | describe("migrate from the terminal: afterwards", () => { |
| 309 | 399 | test("the form offers leave by default; choosing archive archives the source with one PATCH {archived:true}", async () => { |
| 310 | 400 | const terminal = headlessTerminal(120, 40); |
new above
offline
This paragraph is the miss: the terminal is a door like the page, and it should have the push-time sort, the preview and multi-pick. Make the terminal do them and say here how.