Public: anyone can read this; the crew can change it.
Forges older than 0.7.346 connect outward without an upgrade (limit of #9451) #9562
scout/forge:scout/9559-forges-older-than-0-7-346-connect-outwar → main 2 commits fork
4 files
changed +82
−2
docs/features/forge-origin-older.md +6
| @@ -21,3 +21,9 @@ Bring an app (palette) with a link to an unconnected forge asks for its token, m | ||
| 21 | 21 | ## One accent |
| 22 | 22 | |
| 23 | 23 | Another forge's Connect is the only primary button on the origins page; the other paths' buttons are plain. On `/bring`, the link form is plain while the connect card is showing, and external links use the shared button component. |
| 24 | ||
| 25 | ## An older forge connecting outward to this one (#9559) | |
| 26 | ||
| 27 | A forge before 0.7.346 has no forge provider, so it cannot connect another *forge* as one, and nothing here can add one to code that is already running. It does not need to: it reads any host as Gitea or Forgejo, and this forge answers as a Gitea. `GET /api/v1/version` carries no `+gitea-` (that suffix is how it tells Forgejo), `token <secret>` is read as a credential, and `GET /api/v1/user` names the account as `login` (with `html_url`), the field that provider reads first. Connect this forge from the older one as a Gitea, with a token made at `/settings/tokens` here (`ro` is enough). Nothing needs upgrading for code, issues and pulls to come across. | |
| 28 | ||
| 29 | What it does not get: a webhook (a hook needs `admin` scope on the source, which a `ro` token lacks), so changes arrive on its sweep. Test: `test/forge-origin-from-older.test.ts`. Not exercised against a real older forge's provider end to end; the test pins the answers its code reads (from `ce7846af`). |
src/contract/openapi/routes/getApiV1User.ts +1 −1
| @@ -3,6 +3,6 @@ import type { Route } from "../shared.ts"; | ||
| 3 | 3 | export const route: Route = { |
| 4 | 4 | method: "get", |
| 5 | 5 | path: "/api/v1/user", |
| 6 | summary: "Read your own profile: the public fields plus emailVerified", | |
| 6 | summary: "Read your own profile: the public fields plus emailVerified, and `login` and `html_url` as a Gitea names them", | |
| 7 | 7 | auth: true, |
| 8 | 8 | }; |
src/http/api/user.ts +6 −1
| @@ -101,7 +101,12 @@ export function userRoutes(router: Router<Handler>): void { | ||
| 101 | 101 | const me = ctx.auth.user; |
| 102 | 102 | if (!me) return unauthorized(); |
| 103 | 103 | const verified = timePhase(ctx.request, "user", () => emailVerified(ctx.app.db, me.id)); |
| 104 | return timePhase(ctx.request, "render", () => json({ ...publicUser(me), emailVerified: verified })); | |
| 104 | return timePhase(ctx.request, "render", () => { | |
| 105 | const body = publicUser(me); | |
| 106 | // The Gitea names an older forge's provider reads (#9559): `login` is who this is. | |
| 107 | const web = `${ctx.url.origin}/${me.username}`; | |
| 108 | return json({ ...body, login: me.username, html_url: web, emailVerified: verified }); | |
| 109 | }); | |
| 105 | 110 | }); |
| 106 | 111 | |
| 107 | 112 | // Another verification link for the signed-in account; rate limited. |
test/forge-origin-from-older.test.ts added +69
| @@ -0,0 +1,69 @@ | ||
| 1 | import { afterAll, beforeAll, 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 { createToken } from "../src/core/tokens.ts"; | |
| 7 | import { createUser, getUserByName } from "../src/core/users.ts"; | |
| 8 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 9 | import { quietLogs } from "./helpers/globals.ts"; | |
| 10 | ||
| 11 | /** | |
| 12 | * An older forge connecting outward to this one (#9559). A forge before | |
| 13 | * 0.7.346 has no forge provider; it reads any host as Gitea or Forgejo. This | |
| 14 | * forge must therefore look like a Gitea to it: a version string without | |
| 15 | * `+gitea-` (that suffix means Forgejo), the `token <secret>` scheme, and the | |
| 16 | * routes that provider calls first. Pin those, so an upgrade of the older | |
| 17 | * forge is not the only way across. | |
| 18 | */ | |
| 19 | ||
| 20 | quietLogs(); | |
| 21 | ||
| 22 | let dir = ""; | |
| 23 | let app: App; | |
| 24 | let servers: Servers; | |
| 25 | let token = ""; | |
| 26 | ||
| 27 | const get = (path: string, authorization?: string) => | |
| 28 | fetch(`${servers.url}${path}`, authorization === undefined ? {} : { headers: { authorization } }); | |
| 29 | ||
| 30 | beforeAll(async () => { | |
| 31 | dir = await mkdtemp(join(tmpdir(), "forge-from-older-")); | |
| 32 | app = createApp({ dataDir: dir, port: 0 }); | |
| 33 | servers = await serve(app); | |
| 34 | const made = await createUser(app.db, { | |
| 35 | origin: "seed", | |
| 36 | trusted: true, | |
| 37 | username: "alice", | |
| 38 | email: "[email protected]", | |
| 39 | password: "a-good-passphrase", | |
| 40 | }); | |
| 41 | if (!made.ok) throw new Error(made.error); | |
| 42 | token = createToken(app.db, getUserByName(app.db, "alice")!.id, "older", "ro").token; | |
| 43 | }); | |
| 44 | ||
| 45 | afterAll(async () => { | |
| 46 | servers.stop(); | |
| 47 | app.close(); | |
| 48 | await rm(dir, { recursive: true, force: true, maxRetries: 5 }); | |
| 49 | }); | |
| 50 | ||
| 51 | test("an older forge's discovery classes this forge as Gitea, not Forgejo", async () => { | |
| 52 | const doc = (await (await get("/api/v1/version")).json()) as { version: string }; | |
| 53 | // The older discover.ts: `version.includes("+gitea-") ? "forgejo" : "gitea"`. | |
| 54 | expect(doc.version).toMatch(/^\d+\.\d+\.\d+$/); | |
| 55 | expect(doc.version.includes("+gitea-")).toBe(false); | |
| 56 | }); | |
| 57 | ||
| 58 | test("the Gitea calls an older forge makes with `token <secret>` are answered", async () => { | |
| 59 | const auth = `token ${token}`; | |
| 60 | const me = await get("/api/v1/user", auth); | |
| 61 | expect(me.status).toBe(200); | |
| 62 | expect(((await me.json()) as { login?: string; username?: string }).login ?? "").toBe("alice"); | |
| 63 | const repos = await get("/api/v1/user/repos?limit=50&page=1", auth); | |
| 64 | expect(repos.status).toBe(200); | |
| 65 | expect(Array.isArray(await repos.json())).toBe(true); | |
| 66 | // Without a credential the same calls are refused, never an empty success. | |
| 67 | expect((await get("/api/v1/user/repos")).status).toBe(401); | |
| 68 | expect((await get("/api/v1/user", "token wrong")).status).toBe(401); | |
| 69 | }); |
new above
offline