Public: anyone can read this; the crew can change it.
A connection to another forge shows what it may do and what follows it, and widens in place #16485
Check · Review · Land · Live done — Landed and live in production.
sold/connection-settings → main 0 commits
5 files
changed +158
−6
src/actions/origins.ts +9 −4
| @@ -308,6 +308,7 @@ export async function replaceOriginToken( | ||
| 308 | 308 | actor: User | undefined, |
| 309 | 309 | originId: string, |
| 310 | 310 | token: string, |
| 311 | write = false, | |
| 311 | 312 | ): Promise<Result<Origin, ReplaceTokenError>> { |
| 312 | 313 | const user = requireUser(actor); |
| 313 | 314 | if (!user.ok) return user; |
| @@ -341,6 +342,7 @@ export async function replaceOriginToken( | ||
| 341 | 342 | renewOrigin(app.db, app.config.dataDir, origin.id, { |
| 342 | 343 | token: secret, |
| 343 | 344 | authKind: "pat", |
| 345 | ...(write ? { readOnly: false } : {}), | |
| 344 | 346 | }); |
| 345 | 347 | forgetProvider({ dataDir: app.config.dataDir, originId: origin.id }); |
| 346 | 348 | forgetTokenReach(app.config.dataDir, origin.id); |
| @@ -387,7 +389,7 @@ export type Connected = Origin & { | ||
| 387 | 389 | readonly reattached: readonly Reattachment[]; |
| 388 | 390 | }; |
| 389 | 391 | |
| 390 | function storeOrigin( | |
| 392 | export function storeOrigin( | |
| 391 | 393 | app: App, |
| 392 | 394 | user: User, |
| 393 | 395 | input: { |
| @@ -405,11 +407,14 @@ function storeOrigin( | ||
| 405 | 407 | ): Result<Origin, "already_connected"> { |
| 406 | 408 | // Connecting an account whose credential died is re-authorizing it: the |
| 407 | 409 | // row keeps its id, so its mirrors never detach and nothing freezes |
| 408 | // longer than the dead token already made it. A healthy account is still | |
| 409 | // `already_connected` — a second token for it is a mistake, not a fix. | |
| 410 | // longer than the dead token already made it. So is a read-only account | |
| 411 | // connected again to write: a wider token replaces the credential in place. | |
| 412 | // A healthy account otherwise is still `already_connected`. | |
| 410 | 413 | const existing = findOrigin(app.db, user.id, input.apiUrl, input.login); |
| 411 | if (existing?.needsReauth) { | |
| 414 | const widens = existing?.readOnly === true && input.readOnly !== true; | |
| 415 | if (existing && (existing.needsReauth || widens)) { | |
| 412 | 416 | renewOrigin(app.db, app.config.dataDir, existing.id, { |
| 417 | ...(widens ? { readOnly: false } : {}), | |
| 413 | 418 | token: input.token, |
| 414 | 419 | authKind: input.authKind, |
| 415 | 420 | ...(input.refreshToken === undefined |
src/core/origins.ts +5 −1
| @@ -281,11 +281,14 @@ export function renewOrigin( | ||
| 281 | 281 | authKind: "pat" | "oauth"; |
| 282 | 282 | refreshToken?: string; |
| 283 | 283 | expiresAt?: string | null; |
| 284 | /** false lets a read-only origin write; omitted leaves it as it was. */ | |
| 285 | readOnly?: boolean; | |
| 284 | 286 | }, |
| 285 | 287 | ): void { |
| 286 | 288 | db.query( |
| 287 | 289 | `UPDATE origin SET token_enc = ?1, refresh_token_enc = ?2, auth_kind = ?3, |
| 288 | expires_at = ?4, needs_reauth = 0, last_error = '' | |
| 290 | expires_at = ?4, needs_reauth = 0, last_error = '', | |
| 291 | read_only = COALESCE(?6, read_only) | |
| 289 | 292 | WHERE id = ?5`, |
| 290 | 293 | ).run( |
| 291 | 294 | seal(dataDir, next.token, originId), |
| @@ -295,6 +298,7 @@ export function renewOrigin( | ||
| 295 | 298 | next.authKind, |
| 296 | 299 | next.expiresAt ?? null, |
| 297 | 300 | originId, |
| 301 | next.readOnly === undefined ? null : next.readOnly ? 1 : 0, | |
| 298 | 302 | ); |
| 299 | 303 | } |
| 300 | 304 |
src/web/origin_connection.ts added +57
| @@ -0,0 +1,57 @@ | ||
| 1 | /** | |
| 2 | * The settings of one connection, on its card (`/settings/origins`): the other forge's name and | |
| 3 | * address, what the connection may do and the one action that widens it, when it was last used, | |
| 4 | * and the repositories of the person's that follow it, each with its mode and where to change it. | |
| 5 | * Only what this forge holds: whether the other forge follows this one is on their side. | |
| 6 | */ | |
| 7 | ||
| 8 | import type { Database } from "bun:sqlite"; | |
| 9 | import { getReposByIds } from "../core/repos.ts"; | |
| 10 | import { listFollowing } from "../core/upstream_follow.ts"; | |
| 11 | import type { Origin } from "../origins/types.ts"; | |
| 12 | import { repoBase } from "./chrome.ts"; | |
| 13 | import { html, when, type Html } from "./html.ts"; | |
| 14 | import { btn, time } from "./ui.ts"; | |
| 15 | ||
| 16 | const hostOf = (url: string): string => URL.parse(url)?.host ?? url; | |
| 17 | ||
| 18 | /** The person's repositories whose follow reads through this origin, or from its host. */ | |
| 19 | export function followsOf(db: Database, origin: Origin) { | |
| 20 | const host = hostOf(origin.webUrl); | |
| 21 | const follows = listFollowing(db).filter( | |
| 22 | (f) => f.originId === origin.id || hostOf(f.url) === host, | |
| 23 | ); | |
| 24 | const repos = getReposByIds(db, follows.map((f) => f.repoId)); | |
| 25 | return follows.flatMap((f) => { | |
| 26 | const repo = repos.get(f.repoId); | |
| 27 | return repo !== undefined && repo.ownerId === origin.userId ? [{ f, repo }] : []; | |
| 28 | }); | |
| 29 | } | |
| 30 | ||
| 31 | export function connectionBlock(db: Database, origin: Origin): Html { | |
| 32 | const follows = followsOf(db, origin); | |
| 33 | return html`<div class="small mt" data-connection-settings> | |
| 34 | <p> | |
| 35 | <strong>${hostOf(origin.webUrl)}</strong> · ${origin.webUrl}<br /> | |
| 36 | ${origin.readOnly ? "May read only." : "May read and write."} | |
| 37 | ${origin.lastSyncAt === null ? "Not used yet." : html`Last used ${time(origin.lastSyncAt)}.`} | |
| 38 | </p> | |
| 39 | ${when(origin.readOnly, () => html`<details> | |
| 40 | <summary>Allow writing</summary> | |
| 41 | <form method="post" action="/settings/origins/${origin.id}/token"> | |
| 42 | <p>Paste a token that may write; it replaces this one in place and nothing disconnects.</p> | |
| 43 | <input type="hidden" name="write" value="1" /> | |
| 44 | <input type="password" name="token" aria-label="Token that may write" autocomplete="off" required /> | |
| 45 | ${btn("Replace token", { size: "sm", variant: "outline" })} | |
| 46 | </form> | |
| 47 | </details>`)} | |
| 48 | ${follows.length === 0 | |
| 49 | ? html`<p class="muted">None of your repositories follow this forge.</p>` | |
| 50 | : html`<ul>${follows.map(({ f, repo }) => html`<li> | |
| 51 | <a href="${repoBase(repo)}">${repo.ownerName}/${repo.name}</a> | |
| 52 | follows it: ${f.mode === "take-updates" ? "takes updates" : "tells you"}; | |
| 53 | crew merges conflicting updates ${f.crewResolves ? "on" : "off"} | |
| 54 | · <a href="${repoBase(repo)}/settings#upstream">change</a> | |
| 55 | </li>`)}</ul>`} | |
| 56 | </div>`; | |
| 57 | } |
src/web/pages/origins.ts +5 −1
| @@ -70,6 +70,7 @@ import { | ||
| 70 | 70 | type Origin, |
| 71 | 71 | } from "../../core/origins.ts"; |
| 72 | 72 | import { getReposByIds } from "../../core/repos.ts"; |
| 73 | import { connectionBlock } from "../origin_connection.ts"; | |
| 73 | 74 | import { notePhase, takeSessionTime, timePhase } from "../../core/route_timing.ts"; |
| 74 | 75 | import type { Ctx } from "../../http/context.ts"; |
| 75 | 76 | import { oauthCallbackUri } from "../../http/api.ts"; |
| @@ -308,7 +309,7 @@ function pausedNote(origin: Origin): Html { | ||
| 308 | 309 | </div>`; |
| 309 | 310 | } |
| 310 | 311 | |
| 311 | function originCard(origin: Origin, mirrors: number, paused = false): Html { | |
| 312 | function originCard(db: Database, origin: Origin, mirrors: number, paused = false): Html { | |
| 312 | 313 | const base = `/settings/origins/${origin.id}`; |
| 313 | 314 | return row({ |
| 314 | 315 | lead: icon(originIcon(origin)), |
| @@ -340,6 +341,7 @@ function originCard(origin: Origin, mirrors: number, paused = false): Html { | ||
| 340 | 341 | () => html`<p class="small mt">Another forge. ${forgeCarries(origin.runNote)}</p>`, |
| 341 | 342 | )} |
| 342 | 343 | ${when(paused, () => pausedNote(origin))} |
| 344 | ${connectionBlock(db, origin)} | |
| 343 | 345 | <div class="bar mt"> |
| 344 | 346 | <form method="post" action="${base}/sync"> |
| 345 | 347 | ${btn("Sync now", { size: "sm", icon: "sync" })} |
| @@ -877,6 +879,7 @@ function originsView( | ||
| 877 | 879 | <ul class="list setting-list" data-origins> |
| 878 | 880 | ${origins.map((o) => |
| 879 | 881 | originCard( |
| 882 | ctx.app.db, | |
| 880 | 883 | o, |
| 881 | 884 | mirrors.get(o.id)?.count ?? 0, |
| 882 | 885 | writesPaused(ctx.app, o.id) !== undefined, |
| @@ -1408,6 +1411,7 @@ export async function originReplaceToken(ctx: Ctx): Promise<Response> { | ||
| 1408 | 1411 | ctx.auth.user, |
| 1409 | 1412 | ctx.params.id ?? "", |
| 1410 | 1413 | field(form, "token"), |
| 1414 | field(form, "write") === "1", | |
| 1411 | 1415 | ); |
| 1412 | 1416 | if (!replaced.ok) |
| 1413 | 1417 | return flashTo(ctx, back, { |
test/origin-connection.test.ts added +82
| @@ -0,0 +1,82 @@ | ||
| 1 | import { afterAll, beforeAll, describe, expect, test } from "bun:test"; | |
| 2 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 3 | import { tmpdir } from "node:os"; | |
| 4 | import { join } from "node:path"; | |
| 5 | import { createApp, type App } from "../src/app.ts"; | |
| 6 | import { createRepo } from "../src/actions/repos.ts"; | |
| 7 | import { disconnectOrigin, storeOrigin } from "../src/actions/origins.ts"; | |
| 8 | import { listOrigins } from "../src/core/origins.ts"; | |
| 9 | import { createUser, type User } from "../src/core/users.ts"; | |
| 10 | import { putFollow } from "../src/core/upstream_follow.ts"; | |
| 11 | import { connectionBlock } from "../src/web/origin_connection.ts"; | |
| 12 | import { promote } from "./helpers/members.ts"; | |
| 13 | ||
| 14 | /** A connection's settings block, widening in place, and disconnect; no network. */ | |
| 15 | describe("a connection's settings", () => { | |
| 16 | let dir = ""; | |
| 17 | let app: App; | |
| 18 | let user: User; | |
| 19 | const at = { | |
| 20 | kind: "gitea" as const, | |
| 21 | apiUrl: "https://friend.example/api/v1", | |
| 22 | webUrl: "https://friend.example", | |
| 23 | login: "benji", | |
| 24 | authKind: "pat" as const, | |
| 25 | software: "forge", | |
| 26 | }; | |
| 27 | ||
| 28 | beforeAll(async () => { | |
| 29 | dir = await mkdtemp(join(tmpdir(), "conn-")); | |
| 30 | app = createApp({ dataDir: dir, port: 0 }); | |
| 31 | const made = await createUser(app.db, { | |
| 32 | origin: "seed", | |
| 33 | trusted: true, | |
| 34 | username: "tre", | |
| 35 | email: "[email protected]", | |
| 36 | password: "a-long-passphrase-1", | |
| 37 | }); | |
| 38 | if (!made.ok) throw new Error(made.error); | |
| 39 | user = made.value; | |
| 40 | promote(app.db, user); | |
| 41 | }); | |
| 42 | afterAll(async () => { | |
| 43 | await rm(dir, { recursive: true, force: true }); | |
| 44 | }); | |
| 45 | ||
| 46 | test("shows scope and follows; a wider token upgrades in place; disconnect removes it", async () => { | |
| 47 | const first = storeOrigin(app, user, { ...at, token: "read-token", readOnly: true }); | |
| 48 | if (!first.ok) throw new Error(first.error); | |
| 49 | const repo = await createRepo(app, user, { name: "yard", visibility: "private" }); | |
| 50 | if (!repo.ok) throw new Error(String(repo.error)); | |
| 51 | putFollow(app.db, { | |
| 52 | repoId: repo.value.id, | |
| 53 | url: "https://friend.example/benji/yard.git", | |
| 54 | branch: "main", | |
| 55 | enabled: true, | |
| 56 | byId: user.id, | |
| 57 | originId: first.value.id, | |
| 58 | mode: "take-updates", | |
| 59 | }); | |
| 60 | ||
| 61 | const text = connectionBlock(app.db, first.value).toString(); | |
| 62 | expect(text).toContain("friend.example"); | |
| 63 | expect(text).toContain("May read only."); | |
| 64 | expect(text).toContain("tre/yard"); | |
| 65 | expect(text).toContain("takes updates"); | |
| 66 | expect(text).toContain("crew merges conflicting updates off"); | |
| 67 | expect(text).toContain("Allow writing"); | |
| 68 | ||
| 69 | const again = storeOrigin(app, user, { ...at, token: "write-token" }); | |
| 70 | if (!again.ok) throw new Error(again.error); | |
| 71 | expect(again.value.id).toBe(first.value.id); | |
| 72 | expect(again.value.readOnly).toBe(false); | |
| 73 | expect(connectionBlock(app.db, again.value).toString()).toContain("May read and write."); | |
| 74 | // A write connection connected again is still a mistake, not a fix. | |
| 75 | const third = storeOrigin(app, user, { ...at, token: "other" }); | |
| 76 | expect(third.ok).toBe(false); | |
| 77 | ||
| 78 | const gone = await disconnectOrigin(app, user, first.value.id); | |
| 79 | expect(gone.ok).toBe(true); | |
| 80 | expect(listOrigins(app.db, user.id)).toHaveLength(0); | |
| 81 | }); | |
| 82 | }); |
new above
offline