Public: anyone can read this; the crew can change it.

Connect a forge as an origin: paste its URL and a token, and its repositories are listed and mirrored #8916

Closed opened this
Closed without merging

torque/forge:torque/8901-connect-a-forge-as-an-origin-paste-its-u → main 5 commits fork

  • Closed without merging. Nothing was applied to main.

Review

Sign in to review this change.
20 files changed +567 −20
docs/ORIGINS.md +23
@@ -12,6 +12,29 @@ code; everything else is a thin per-forge adapter behind one `Provider`
1212 interface (`src/origins/types.ts`). All forges share the same units, so the
1313 adapters are small and boring, which is the point.
1414
15 ## A forge as an origin
16
17 Another forge (this software) is an origin kind of its own, `forge`. On
18 **Settings → Origins**, paste its address and a token (`forge_…`, from its
19 Settings → Tokens) and nothing else: discovery asks `GET /api/v1/version`,
20 which answers `{"software":"forge", …}` with no sign-in, and the row is shown
21 as "a forge". The adapter (`src/origins/providers/forge.ts`) speaks the forge's
22 own `/api/v1`, not a Gitea shape.
23
24 - Write access is the upstream's own `permissions` on each repository, for the
25 token; an upstream that does not say reads as read-only.
26 - The picker lists the account's repositories, private ones too: the token
27 sees what its person sees.
28 - Mirroring, fetching and write-through follow the same rules as other kinds;
29 git is authenticated per command through the environment, so the token is
30 never in argv or the repository's config. Migrate from it carries the same
31 credential.
32 - Not yet: importing issues and pulls (a sync is clean, with nothing to
33 import), browsing trees and commits remotely, and writing comments or issues
34 back. Those refuse in words rather than faking a result.
35 - `test/origins-forge.test.ts` runs two real forges in one process against
36 each other.
37
1538 ## Truth
1639
1740 - Native repo: **git is truth**, the database is an index (as before).
src/contract/openapi/routes/getApiV1ReposOwnerName.ts +1 −1
@@ -4,7 +4,7 @@ export const route: Route = {
44 method: "get",
55 path: REPO,
66 summary:
7 "Read one repository; `provenance` is where a migrated one came from ({ source, sourceUrl, parents: [{ fullName, url }], migratedAt }), null when it was not migrated or its owner hid it; `upstream` is the stored follow check ({ repo, behind, checkedAt, conflict }), shown to an admin of the repository, null when it follows nothing or to anyone else",
7 "Read one repository; `permissions` is what the caller may do here ({ push, admin }); `provenance` is where a migrated one came from ({ source, sourceUrl, parents: [{ fullName, url }], migratedAt }), null when it was not migrated or its owner hid it; `upstream` is the stored follow check ({ repo, behind, checkedAt, conflict }), shown to an admin of the repository, null when it follows nothing or to anyone else",
88 auth: false,
99 refusals: { not_found: "404: no such repository, or it is not readable by the caller" },
1010 };
src/contract/openapi/routes/getApiV1Version.ts added +8
@@ -0,0 +1,8 @@
1 import type { Route } from "../shared.ts";
2
3 export const route: Route = {
4 method: "get",
5 path: "/api/v1/version",
6 summary: "What software this is and its version, the same to everyone: how another forge recognises this one when it is connected as an origin",
7 auth: false,
8 };
src/contract/openapi/routes/index.ts +1
@@ -180,6 +180,7 @@ export { route as getApiV1UserPins } from "./getApiV1UserPins.ts";
180180 export { route as getApiV1UsersUsername } from "./getApiV1UsersUsername.ts";
181181 export { route as getApiV1UsersUsernameRepos } from "./getApiV1UsersUsernameRepos.ts";
182182 export { route as getApiV1UsersUsernameSignins } from "./getApiV1UsersUsernameSignins.ts";
183 export { route as getApiV1Version } from "./getApiV1Version.ts";
183184 export { route as getContractOpenapiJson } from "./getContractOpenapiJson.ts";
184185 export { route as getMetrics } from "./getMetrics.ts";
185186 export { route as patchApiV1AdminSettings } from "./patchApiV1AdminSettings.ts";
src/core/migration-pins/20261103140000-a-forge-is-an-origin.pin added +1
@@ -0,0 +1,1 @@
1 560aece073658a58b8da8fd06de2e3972ed19f08b0b001407770bd4da7edd509
src/core/migrations/20261103140000-a-forge-is-an-origin.ts added +8
@@ -0,0 +1,8 @@
1 // A forge can be an origin (#8901). `origin.kind` is CHECKed to the three
2 // forges it knew, SQLite cannot widen a CHECK in place, and copying the table
3 // would cascade through the mirrors, pins and caches that reference it. So a
4 // forge's row keeps a kind the CHECK allows and says what it is in its own
5 // column; `core/origins.ts` reads the two as one `OriginKind`. Additive.
6 export default `
7 ALTER TABLE origin ADD COLUMN software TEXT NOT NULL DEFAULT '';
8 `;
src/core/origins.ts +9 −4
@@ -35,13 +35,15 @@ interface OriginRow {
3535 refresh_token_enc: string;
3636 expires_at: string | null;
3737 needs_reauth: number;
38 /** 'forge' for this forge's own kind, which `kind` cannot hold; else ''. */
39 software: string;
3840 }
3941
4042 function toOrigin(row: OriginRow): Origin {
4143 return {
4244 id: row.id,
4345 userId: row.user_id,
44 kind: row.kind,
46 kind: row.software === "forge" ? "forge" : row.kind,
4547 apiUrl: row.api_url,
4648 webUrl: row.web_url,
4749 login: row.login,
@@ -93,12 +95,14 @@ export function createOrigin(
9395 const originId = id();
9496 db.query(
9597 `INSERT INTO origin (id, user_id, kind, api_url, web_url, login, token_enc,
96 auth_kind, refresh_token_enc, expires_at)
97 VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10)`,
98 auth_kind, refresh_token_enc, expires_at, software)
99 VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11)`,
98100 ).run(
99101 originId,
100102 input.userId,
101 input.kind,
103 // The kind column is CHECKed to three; this forge's own is told apart by
104 // `software` (migration 20261103140000).
105 input.kind === "forge" ? "gitea" : input.kind,
102106 input.apiUrl,
103107 input.webUrl,
104108 input.login,
@@ -108,6 +112,7 @@ export function createOrigin(
108112 ? ""
109113 : seal(dataDir, input.refreshToken, originId),
110114 input.expiresAt ?? null,
115 input.kind === "forge" ? "forge" : "",
111116 );
112117 return ok(getOriginById(db, originId)!);
113118 }
src/core/repos.ts +4 −3
@@ -6,6 +6,7 @@
66 */
77
88 import type { Database } from "bun:sqlite";
9 import { ORIGIN_KIND_SQL } from "../origins/types.ts";
910 import { rmSync } from "node:fs";
1011 import { isSafeName } from "../git/guard.ts";
1112 import { installHooks } from "../git/hooks.ts";
@@ -47,7 +48,7 @@ const log = createLog("repos");
4748 */
4849 export interface RepoOrigin {
4950 readonly originId: string;
50 readonly kind: "github" | "forgejo" | "gitea";
51 readonly kind: "github" | "forgejo" | "gitea" | "forge";
5152 /** `owner/name` on the origin. */
5253 readonly fullName: string;
5354 readonly htmlUrl: string;
@@ -216,7 +217,7 @@ interface RepoRow {
216217 sealed_for: string | null;
217218 archived_at: string | null;
218219 origin_id: string | null;
219 origin_kind: "github" | "forgejo" | "gitea" | null;
220 origin_kind: "github" | "forgejo" | "gitea" | "forge" | null;
220221 origin_full_name: string | null;
221222 origin_html_url: string | null;
222223 last_fetch_at: string | null;
@@ -227,7 +228,7 @@ interface RepoRow {
227228 // LEFT JOIN, because the origin may have been disconnected out from under a
228229 // mirror — the repo survives, the link does not.
229230 const SELECT = `
230 SELECT repo.*, user.username AS owner_name, origin.kind AS origin_kind
231 SELECT repo.*, user.username AS owner_name, ${ORIGIN_KIND_SQL} AS origin_kind
231232 FROM repo JOIN user ON user.id = repo.owner_id
232233 LEFT JOIN origin ON origin.id = repo.origin_id`;
233234
src/core/search.ts +2 −1
@@ -24,6 +24,7 @@
2424 */
2525
2626 import type { Database } from "bun:sqlite";
27 import { ORIGIN_KIND_SQL } from "../origins/types.ts";
2728 import { readFile } from "node:fs/promises";
2829 import { join } from "node:path";
2930 import { runGitCapture } from "../git/env.ts";
@@ -299,7 +300,7 @@ export function searchOriginRepos(
299300 const like = `%${likeEscape(query.trim())}%`;
300301 const rows = db
301302 .query<OriginRepoHitRow, [string, string, number]>(
302 `SELECT origin_repo.origin_id, origin.kind AS origin_kind,
303 `SELECT origin_repo.origin_id, ${ORIGIN_KIND_SQL} AS origin_kind,
303304 origin_repo.full_name, origin_repo.description,
304305 origin_repo.private, origin_repo.html_url,
305306 origin_repo.open_issues, origin_repo.open_pulls,
src/http/api.ts +12
@@ -355,6 +355,7 @@ import { egressSnapshot, egressTotals } from "../apps/egress.ts";
355355 import { egressMetrics } from "../apps/egress_metrics.ts";
356356 import { forgeMetrics } from "../metrics/forge.ts";
357357 import { appSources } from "../metrics/app_sources.ts";
358 import { VERSION } from "../cli/help.ts";
358359 import { ADMIN_WORDS } from "../actions/admin_words.ts";
359360 import { ANSWER_WORDS, ORDER_WORDS, VISIBILITY_WORDS } from "../core/arg_words.ts";
360361
@@ -478,6 +479,11 @@ function repoJson(ctx: Ctx, repo: Repo) {
478479 return {
479480 ...rest,
480481 origin: originFacts(repo),
482 // What the caller may do here: the one answer a mirroring forge reads.
483 permissions: {
484 push: canWrite(ctx.app.db, repo, ctx.auth.user),
485 admin: canAdmin(ctx.app.db, repo, ctx.auth.user),
486 },
481487 counts:
482488 repo.counts ??
483489 countsForRepos(ctx.app.db, [repo.id]).get(repo.id) ??
@@ -3104,6 +3110,12 @@ export function apiRoutes(router: Router<Handler>): void {
31043110 );
31053111 });
31063112
3113 // What software this is, for another forge asked to connect to this one
3114 // (`origins/discover.ts`): one fixed answer, the same to everyone.
3115 router.add("GET", "/api/v1/version", () =>
3116 json({ software: "forge", version: `forge-${VERSION}` }, 200, { "cache-control": "public, max-age=300" }),
3117 );
3118
31073119 // Open egress connections per app and destination, as Prometheus text
31083120 // (#3719). Repository names are in the labels, so a site admin only.
31093121 router.add("GET", "/metrics", (ctx) => {
src/origins/discover.ts +15 −4
@@ -205,13 +205,15 @@ export async function discover(
205205 });
206206 }
207207
208 // One cheap look at the standard, then the probe that actually answers.
209 const node = await viaNodeInfo(web, probe);
210 if (node) return ok(node);
211
208 // This forge's own door first: its version answer names it outright, and
209 // is the only probe that tells it from a Gitea.
212210 const version = await viaVersionEndpoint(web, probe);
213211 if (version) return ok(version);
214212
213 // Then the standard, for software that publishes it.
214 const node = await viaNodeInfo(web, probe);
215 if (node) return ok(node);
216
215217 return err(probe.refused ? "private_host" : "unreachable");
216218 }
217219
@@ -230,6 +232,15 @@ async function viaVersionEndpoint(
230232 if (!doc.ok) return undefined;
231233 const version = asString(field(doc.value, "version"));
232234 if (version === "") return undefined;
235 // This forge says so in a field of its own; no Gitea or Forgejo sends one.
236 if (asString(field(doc.value, "software")) === "forge")
237 return {
238 kind: "forge",
239 apiUrl: `${web}/api/v1`,
240 webUrl: web,
241 via: "forge-version",
242 software: version,
243 };
233244 const kind: OriginKind = version.includes("+gitea-") ? "forgejo" : "gitea";
234245 return {
235246 kind,
src/origins/providers/forge.ts added +273
@@ -0,0 +1,273 @@
1 /**
2 * This forge, as somebody else's origin. It speaks the forge's own API
3 * (`/api/v1`, a `forge_…` token as Bearer, cursor pages of `{items,
4 * next_cursor}`) and nothing Gitea-shaped.
5 *
6 * What it does: say who the token is, list the account's repositories
7 * (private too — the token sees what its person sees), describe one, list its
8 * branches, and authenticate git, which is all a mirror, a write-through push
9 * and a migration need. What it does not do yet: mirror issues and pulls
10 * (a listing is an empty page, so a sync is clean rather than failing every
11 * tick), browse trees and commits, or write comments and issues back; each of
12 * those says so in words instead of guessing at a shape.
13 */
14
15 import { basicHeader, gitAuthEnv } from "../credentials.ts";
16 import type { Diff } from "../../git/diff.ts";
17 import type {
18 OriginKind,
19 Page,
20 Provider,
21 ProviderCredential,
22 ProviderResult,
23 RemoteBlob,
24 RemoteBranch,
25 RemoteComment,
26 RemoteCommit,
27 RemoteIdentity,
28 RemoteIssue,
29 RemoteLabel,
30 RemoteReadme,
31 RemoteRepo,
32 RemoteReview,
33 RemoteReviewState,
34 RemoteTreeEntry,
35 TokenReach,
36 } from "../types.ts";
37 import { NO_ACCOUNT } from "../words.ts";
38 import {
39 ApiClient,
40 asArray,
41 asBool,
42 asNumber,
43 asString,
44 asTime,
45 dig,
46 fail,
47 field,
48 seg,
49 splitFullName,
50 succeed,
51 } from "./http.ts";
52
53 const PAGE = 100;
54
55 const NOT_YET = (what: string): string =>
56 `${what} is not read from another forge yet`;
57 const NO_WRITE = (what: string): string =>
58 `${what} is not written to another forge yet`;
59
60 export class ForgeProvider implements Provider {
61 readonly kind: OriginKind = "forge";
62 readonly apiUrl: string;
63 readonly webUrl: string;
64 private readonly api: ApiClient;
65 private login: string | undefined;
66
67 constructor(private readonly credential: ProviderCredential) {
68 this.apiUrl = credential.apiUrl;
69 this.webUrl = credential.webUrl;
70 this.login = credential.login;
71 this.api = new ApiClient(credential, (token) => ({
72 authorization: `Bearer ${token}`,
73 }));
74 }
75
76 async whoami(): Promise<ProviderResult<RemoteIdentity>> {
77 const result = await this.api.call({ path: "/user" });
78 if (!result.ok) return result;
79 const login = asString(field(result.value.data, "username"));
80 if (login === "") return fail("invalid_response", NO_ACCOUNT);
81 this.login = login;
82 return succeed({
83 login,
84 displayName: asString(field(result.value.data, "displayName")) || login,
85 htmlUrl: `${this.webUrl}/${login}`,
86 });
87 }
88
89 /** A forge token is its person's: it lists private repositories. */
90 async tokenReach(): Promise<ProviderResult<TokenReach>> {
91 return succeed("private");
92 }
93
94 /** The account's own repositories, by cursor (the last repo id of a page). */
95 async listRepos(cursor?: string): Promise<ProviderResult<Page<RemoteRepo>>> {
96 if (this.login === undefined) {
97 const who = await this.whoami();
98 if (!who.ok) return who;
99 }
100 const login = this.login!;
101 const result = await this.api.call({
102 path: `/users/${seg(login)}/repos`,
103 query: { limit: PAGE, cursor },
104 });
105 if (!result.ok) return result;
106 const next = field(result.value.data, "next_cursor");
107 return succeed({
108 items: asArray(field(result.value.data, "items")).map((raw) =>
109 toRepo(raw, this.webUrl),
110 ),
111 next: typeof next === "string" || typeof next === "number" ? String(next) : null,
112 });
113 }
114
115 async getRepo(fullName: string): Promise<ProviderResult<RemoteRepo>> {
116 const named = splitFullName(fullName);
117 if (!named) return fail("not_found", `not a repository name: ${fullName}`);
118 const result = await this.api.call({
119 path: `/repos/${seg(named.owner)}/${seg(named.name)}`,
120 });
121 if (!result.ok) return result;
122 return succeed(toRepo(result.value.data, this.webUrl));
123 }
124
125 async listBranches(
126 fullName: string,
127 ): Promise<ProviderResult<readonly RemoteBranch[]>> {
128 const named = splitFullName(fullName);
129 if (!named) return fail("not_found", `not a repository name: ${fullName}`);
130 const branches: RemoteBranch[] = [];
131 let cursor: string | undefined;
132 for (let page = 0; page < 10; page++) {
133 const result = await this.api.call({
134 path: `/repos/${seg(named.owner)}/${seg(named.name)}/branches`,
135 query: { limit: 200, cursor },
136 });
137 if (!result.ok) return result;
138 for (const raw of asArray(field(result.value.data, "items"))) {
139 const name = asString(field(raw, "name"));
140 if (name === "") continue;
141 branches.push({
142 name,
143 oid: asString(field(raw, "oid")),
144 protected: asBool(field(raw, "protected")),
145 });
146 }
147 const next = field(result.value.data, "next_cursor");
148 if (typeof next !== "string" || next === "") break;
149 cursor = next;
150 }
151 return succeed(branches);
152 }
153
154 async listTags(): Promise<ProviderResult<readonly RemoteBranch[]>> {
155 return succeed([]);
156 }
157 async listTree(): Promise<ProviderResult<readonly RemoteTreeEntry[]>> {
158 return fail("not_found", NOT_YET("a file tree"));
159 }
160 async getBlob(): Promise<ProviderResult<RemoteBlob>> {
161 return fail("not_found", NOT_YET("a file"));
162 }
163 async getReadme(): Promise<ProviderResult<RemoteReadme | null>> {
164 return succeed(null);
165 }
166 async listCommits(): Promise<ProviderResult<Page<RemoteCommit>>> {
167 return fail("not_found", NOT_YET("history"));
168 }
169 async getCommitDiff(): Promise<ProviderResult<Diff>> {
170 return fail("not_found", NOT_YET("a commit"));
171 }
172 async getPullDiff(): Promise<ProviderResult<Diff>> {
173 return fail("not_found", NOT_YET("a pull"));
174 }
175
176 // Conversations: empty, so a sync of a mirror is clean (see the header).
177 async listLabels(): Promise<ProviderResult<readonly RemoteLabel[]>> {
178 return succeed([]);
179 }
180 async listIssues(): Promise<ProviderResult<Page<RemoteIssue>>> {
181 return succeed({ items: [], next: null });
182 }
183 async getIssue(): Promise<ProviderResult<RemoteIssue>> {
184 return fail("not_found", NOT_YET("an issue"));
185 }
186 async searchIssues(): Promise<ProviderResult<readonly RemoteIssue[]>> {
187 return succeed([]);
188 }
189 async listComments(): Promise<ProviderResult<readonly RemoteComment[]>> {
190 return succeed([]);
191 }
192 async listReviews(): Promise<ProviderResult<readonly RemoteReview[]>> {
193 return succeed([]);
194 }
195 async listReviewStates(): Promise<
196 ProviderResult<readonly RemoteReviewState[]>
197 > {
198 return succeed([]);
199 }
200
201 async createIssue(): Promise<ProviderResult<RemoteIssue>> {
202 return fail("forbidden", NO_WRITE("an issue"));
203 }
204 async createComment(): Promise<ProviderResult<RemoteComment>> {
205 return fail("forbidden", NO_WRITE("a comment"));
206 }
207 async setState(): Promise<ProviderResult<RemoteIssue>> {
208 return fail("forbidden", NO_WRITE("a state change"));
209 }
210 async setLabels(): Promise<ProviderResult<readonly RemoteLabel[]>> {
211 return fail("forbidden", NO_WRITE("a label"));
212 }
213 async createPull(): Promise<ProviderResult<RemoteIssue>> {
214 return fail("forbidden", NO_WRITE("a pull"));
215 }
216 async submitReview(): Promise<ProviderResult<RemoteReviewState>> {
217 return fail("forbidden", NO_WRITE("a review"));
218 }
219 async forkRepo(): Promise<ProviderResult<RemoteRepo>> {
220 return fail("forbidden", NO_WRITE("a fork"));
221 }
222 async archiveRepo(): Promise<ProviderResult<void>> {
223 return fail("forbidden", NO_WRITE("an archive"));
224 }
225
226 /**
227 * git over HTTPS takes `Basic <login>:<token>`: this forge reads a
228 * `forge_…` secret in the password slot as the token. Only the header,
229 * scoped to the clone URL's host, reaches git (`credentials.ts`).
230 */
231 async gitAuthEnv(remoteUrl: string): Promise<Record<string, string>> {
232 const token = await this.credential.token();
233 if (!token.ok) return {};
234 return gitAuthEnv(
235 remoteUrl,
236 basicHeader(this.login ?? this.credential.login ?? "forge", token.value),
237 );
238 }
239 }
240
241 /** One repo of this forge's JSON (`repoJson` in `http/api.ts`). */
242 function toRepo(raw: unknown, web: string): RemoteRepo {
243 const owner = asString(field(raw, "ownerName"));
244 const name = asString(field(raw, "name"));
245 const fullName = `${owner}/${name}`;
246 const base = `${web.replace(/\/+$/, "")}/${seg(owner)}/${seg(name)}`;
247 return {
248 remoteId: asString(field(raw, "id")),
249 fullName,
250 owner,
251 name,
252 description: asString(field(raw, "description")),
253 // Anything not plainly public is private: never widen on import.
254 private: asString(field(raw, "visibility")) !== "public",
255 fork: field(raw, "forkParentId") !== null && field(raw, "forkParentId") !== undefined,
256 parent: null,
257 defaultBranch: asString(field(raw, "defaultBranch")) || "main",
258 cloneUrl: `${base}.git`,
259 sshUrl: "",
260 htmlUrl: base,
261 pushedAt: asTime(field(raw, "pushedAt")),
262 // The upstream's own answer for this token (`permissions` in `repoJson`,
263 // `http/api.ts`). A forge too old to say reads as read-only: never widen.
264 permissions: {
265 push: dig(raw, "permissions", "push") === true,
266 admin: dig(raw, "permissions", "admin") === true,
267 },
268 openIssues: asNumber(dig(raw, "counts", "openIssues")),
269 openPulls: asNumber(dig(raw, "counts", "openPulls")),
270 openIssuesIncludesPulls: false,
271 sizeKb: 0,
272 };
273 }
src/origins/providers/index.ts
Binary file not shown.
src/origins/types.ts +17 −2
@@ -12,7 +12,22 @@
1212
1313 import type { Diff } from "../git/diff.ts";
1414
15 export type OriginKind = "github" | "forgejo" | "gitea";
15 export type OriginKind = "github" | "forgejo" | "gitea" | "forge";
16
17 /** What a person calls each kind. */
18 export const KIND_LABEL: Readonly<Record<OriginKind, string>> = {
19 github: "GitHub",
20 forgejo: "Forgejo",
21 gitea: "Gitea",
22 forge: "a forge",
23 };
24
25 /**
26 * The kind of an `origin` row in SQL: this forge answers `/api/v1/version`
27 * like Forgejo does, so its row keeps a stored kind and says `forge` in
28 * `software` (`OriginRow` in `core/origins.ts` reads it the same way).
29 */
30 export const ORIGIN_KIND_SQL = "CASE WHEN origin.software = 'forge' THEN 'forge' ELSE origin.kind END";
1631
1732 /** How an origin's credential was obtained. */
1833 export type AuthKind = "pat" | "oauth";
@@ -490,6 +505,6 @@ export interface Discovered {
490505 readonly apiUrl: string;
491506 readonly webUrl: string;
492507 /** How we knew: the evidence to show the user. */
493 readonly via: "known" | "nodeinfo" | "version-endpoint";
508 readonly via: "known" | "nodeinfo" | "version-endpoint" | "forge-version";
494509 readonly software: string;
495510 }
src/tui/screens/where.ts +1 −1
@@ -85,7 +85,7 @@ export function originUrl(src: RepoSource): string {
8585 /** Which forge software is behind this source's origin, when there is one. */
8686 export function originKind(
8787 src: RepoSource,
88 ): "github" | "forgejo" | "gitea" | undefined {
88 ): "github" | "forgejo" | "gitea" | "forge" | undefined {
8989 if (src.where.kind === "origin") return src.where.origin.kind;
9090 return src.where.repo.origin?.kind;
9191 }
src/web/icons.ts +1
@@ -92,6 +92,7 @@ const PATHS = {
9292 // three nodes, and a cup — never a claim to be their trademark.
9393 github: `<path d="M15 22v-4a4.8 4.8 0 0 0-1-3.5c3 0 6-2 6-5.5.08-1.25-.27-2.48-1-3.5.28-1.15.28-2.35 0-3.5 0 0-1 0-3 1.5-2.64-.5-5.36-.5-8 0C6 2 5 2 5 2c-.3 1.15-.3 2.35 0 3.5A5.403 5.403 0 0 0 4 9c0 3.5 3 5.5 6 5.5-.39.49-.68 1.05-.85 1.65-.17.6-.22 1.23-.15 1.85v4"/><path d="M9 18c-4.51 2-5-2-7-2"/>`,
9494 forgejo: `<circle cx="6.5" cy="5" r="2.5"/><circle cx="17.5" cy="5" r="2.5"/><circle cx="12" cy="19" r="2.5"/><path d="M17.5 7.5v1.75a3 3 0 0 1-3 3h-2a2 2 0 0 0-2 2v2.25"/><path d="M6.5 7.5v6"/>`,
95 forge: `<path d="M4 6h16l-3 4h-3v4h4v3H6v-3h4v-4H7z"/>`,
9596 gitea: `<path d="M3 8h12v7a4 4 0 0 1-4 4H7a4 4 0 0 1-4-4z"/><path d="M15 10h2.5a3 3 0 0 1 0 6H15"/><path d="M7 5V2.5"/><path d="M11 5V2.5"/>`,
9697 // Apps, runs and the crew (docs/APPS.md).
9798 rocket: `<path d="M4.5 16.5c-1.5 1.26-2 5-2 5s3.74-.5 5-2c.71-.84.7-2.13-.09-2.91a2.18 2.18 0 0 0-2.91-.09z"/><path d="m12 15-3-3a22 22 0 0 1 2-3.95A12.88 12.88 0 0 1 22 2c0 2.72-.78 7.5-6 11a22.35 22.35 0 0 1-4 2z"/><path d="M9 12H4s.55-3.03 2-4c1.62-1.08 5 0 5 0"/><path d="M12 15v5s3.03-.55 4-2c1.08-1.62 0-5 0-5"/>`,
src/web/pages/origins.ts +6 −3
@@ -44,6 +44,7 @@ import {
4444 } from "../../origins/trusted-hosts.ts";
4545 import { reattachMessage } from "./frozen.ts";
4646 import { replaceTokenMessage } from "../../origins/token-reach.ts";
47 import { KIND_LABEL } from "../../origins/types.ts";
4748 import {
4849 listDetachedMirrors,
4950 mirrorsByOrigin,
@@ -238,7 +239,7 @@ export function accountSections(
238239 }
239240
240241 /** The mark a forge wears wherever it is named. */
241 export function originIcon(kind: "github" | "forgejo" | "gitea"): IconName {
242 export function originIcon(kind: "github" | "forgejo" | "gitea" | "forge"): IconName {
242243 return kind;
243244 }
244245
@@ -328,7 +329,7 @@ function originCard(origin: Origin, mirrors: number): Html {
328329 title: html`${origin.login}<span class="muted"> @ </span>${hostLabel(
329330 origin.webUrl,
330331 )}`,
331 meta: html`${origin.authKind === "oauth"
332 meta: html`${origin.kind === "forge" ? `${KIND_LABEL.forge} · ` : ""}${origin.authKind === "oauth"
332333 ? "OAuth"
333334 : "personal access token"}
334335 ·
@@ -608,7 +609,9 @@ function connectPath(
608609 function tokenSection(values: ConnectValues): Html {
609610 return html`<p class="muted small">
610611 Nothing to register, anywhere. The token is validated against the forge
611 before it is stored, encrypted at rest, and never shown again.
612 before it is stored, encrypted at rest, and never shown again. Another
613 forge of this kind connects the same way: paste its address and one of
614 its tokens.
612615 </p>
613616 <form method="post" action="/settings/origins/token">
614617 ${uiField({
src/web/pages/search.ts +3 −1
@@ -149,7 +149,9 @@ function repoHitRow(ctx: Ctx, apps: Apps, hit: RepoHit): Html {
149149 ? "forgejo"
150150 : hit.originKind === "gitea"
151151 ? "gitea"
152 : "globe",
152 : hit.originKind === "forge"
153 ? "forge"
154 : "globe",
153155 ),
154156 title: hit.fullName,
155157 titleAttr: hit.fullName,
test/origins-forge.test.ts added +181
@@ -0,0 +1,181 @@
1 /**
2 * A forge as another forge's origin (#8901), end to end: two real forges in
3 * one process, the second connected to the first by its address and a token.
4 * No fake is involved; the upstream is this very app.
5 */
6 import { afterAll, beforeAll, describe, expect, test } from "bun:test";
7 import { mkdtemp, rm } from "node:fs/promises";
8 import { tmpdir } from "node:os";
9 import { join } from "node:path";
10 import { createApp, type App } from "../src/app.ts";
11 import { serve, type Servers } from "../src/http/server.ts";
12 import { discover } from "../src/origins/discover.ts";
13 import { settled } from "../src/origins/jobs.ts";
14 import { getRepo } from "../src/core/repos.ts";
15 import { repoDir } from "../src/git/repos.ts";
16 import { syncOrigin } from "../src/origins/sync.ts";
17 import { getOriginById } from "../src/core/origins.ts";
18 import { seedAdmin } from "./helpers/admin.ts";
19 import { tokenFor } from "./helpers/tokens.ts";
20 import { gitClientEnv } from "./helpers/git-client.ts";
21 import { mirrorAndWait } from "./fake-origin.ts";
22 import { quietLogs } from "./helpers/globals.ts";
23
24 quietLogs();
25
26 const PASSWORD = "octo-login-secret";
27 const ALICE = "alice-login-secret";
28 const basic = (user: string, secret: string): string =>
29 `Basic ${Buffer.from(`${user}:${secret}`).toString("base64")}`;
30
31 describe("a forge connected as an origin", () => {
32 let upDir: string;
33 let dir: string;
34 let up: App;
35 let upServers: Servers;
36 let app: App;
37 let servers: Servers;
38 let token = "";
39 let originId = "";
40
41 const upApi = (path: string, body?: unknown) =>
42 fetch(`${upServers.url}${path}`, {
43 method: body === undefined ? "GET" : "POST",
44 headers: { "content-type": "application/json", authorization: basic("octo", PASSWORD) },
45 ...(body === undefined ? {} : { body: JSON.stringify(body) }),
46 });
47 const api = (path: string, body?: unknown) =>
48 fetch(`${servers.url}${path}`, {
49 method: body === undefined ? "GET" : "POST",
50 headers: { "content-type": "application/json", authorization: basic("alice", ALICE) },
51 ...(body === undefined ? {} : { body: JSON.stringify(body) }),
52 });
53 const commit = async (repo: string, file: string, content: string) =>
54 upApi(`/api/v1/repos/octo/${repo}/contents`, {
55 branch: "main",
56 message: `add ${file}`,
57 files: [{ path: file, content }],
58 });
59
60 beforeAll(async () => {
61 upDir = await mkdtemp(join(tmpdir(), "forge-up-"));
62 dir = await mkdtemp(join(tmpdir(), "forge-down-"));
63 up = createApp({ dataDir: upDir, port: 0 });
64 upServers = await serve(up);
65 await seedAdmin(up, { username: "octo", email: "[email protected]", password: PASSWORD });
66 await upApi("/api/v1/repos", { name: "vault", visibility: "private" });
67 await commit("vault", "a.txt", "one\n");
68 await upApi("/api/v1/repos", { name: "open", visibility: "public" });
69 await commit("open", "b.txt", "two\n");
70 token = tokenFor(up, "octo", "rw", "mirror");
71
72 app = createApp({ dataDir: dir, port: 0 });
73 servers = await serve(app);
74 await seedAdmin(app, { username: "alice", email: "[email protected]", password: ALICE });
75 });
76
77 afterAll(async () => {
78 servers.stop();
79 upServers.stop();
80 app.close();
81 up.close();
82 await rm(dir, { recursive: true, force: true });
83 await rm(upDir, { recursive: true, force: true });
84 });
85
86 test("the version endpoint names the software, and discovery believes it", async () => {
87 const version = (await (await fetch(`${upServers.url}/api/v1/version`)).json()) as { software: string };
88 expect(version.software).toBe("forge");
89 const found = await discover(upServers.url);
90 expect(found.ok && found.value.kind).toBe("forge");
91 expect(found.ok && found.value.apiUrl).toBe(`${upServers.url}/api/v1`);
92 });
93
94 test("a URL and a token connect it as a forge, and a wrong token does not", async () => {
95 const bad = await api("/api/v1/user/origins", { host: upServers.url, token: "forge_nope" });
96 expect(bad.status).toBe(422);
97
98 const connected = await api("/api/v1/user/origins", { host: upServers.url, token });
99 expect(connected.status).toBe(201);
100 const origin = (await connected.json()) as { id: string; kind: string; login: string };
101 expect(origin.kind).toBe("forge");
102 expect(origin.login).toBe("octo");
103 originId = origin.id;
104 expect(JSON.stringify(origin)).not.toContain(token);
105
106 const page = await (
107 await fetch(`${servers.url}/settings/origins`, {
108 headers: { authorization: basic("alice", ALICE) },
109 })
110 ).text();
111 expect(page).toContain("a forge ·");
112 });
113
114 test("the picker lists the account's repositories, private too", async () => {
115 const listed = (await (await api(`/api/v1/user/origins/${originId}/repos?refresh=1`)).json()) as {
116 fullName: string;
117 private: boolean;
118 }[];
119 const byName = new Map(listed.map((r) => [r.fullName, r.private]));
120 expect(byName.get("octo/vault")).toBe(true);
121 expect(byName.get("octo/open")).toBe(false);
122 });
123
124 test("write access is the upstream's own answer, not a guess from who owns it", async () => {
125 const mine = (await (await upApi("/api/v1/repos/octo/open")).json()) as {
126 permissions: { push: boolean; admin: boolean };
127 };
128 expect(mine.permissions).toEqual({ push: true, admin: true });
129 const stranger = (await (await fetch(`${upServers.url}/api/v1/repos/octo/open`)).json()) as {
130 permissions: { push: boolean; admin: boolean };
131 };
132 expect(stranger.permissions).toEqual({ push: false, admin: false });
133 });
134
135 test("mirroring a private repository fetches it, with the token out of the repo", async () => {
136 const job = await mirrorAndWait(await api(`/api/v1/user/origins/${originId}/mirror`, { fullName: "octo/vault" }));
137 expect(job?.stage).toBe("done");
138 const mirrored = getRepo(app.db, "alice", "vault")!;
139 expect(mirrored.visibility).toBe("private");
140 expect(mirrored.origin?.kind).toBe("forge");
141 const dir = repoDir(app.reposRoot, "alice", "vault");
142 const proc = Bun.spawn(["git", "log", "--format=%s", "main"], { cwd: dir, env: gitClientEnv(), stdout: "pipe" });
143 expect((await new Response(proc.stdout).text()).trim()).toBe("add a.txt");
144 const config = await Bun.file(join(dir, "config")).text();
145 expect(config).not.toContain(token);
146 expect(config).not.toContain("extraHeader");
147 });
148
149 test("a sync follows the upstream", async () => {
150 await commit("vault", "c.txt", "three\n");
151 const origin = getOriginById(app.db, originId)!;
152 const report = await syncOrigin(app, origin);
153 expect(report.error ?? "").toBe("");
154 const dir = repoDir(app.reposRoot, "alice", "vault");
155 const proc = Bun.spawn(["git", "log", "--format=%s", "main"], { cwd: dir, env: gitClientEnv(), stdout: "pipe" });
156 expect((await new Response(proc.stdout).text()).trim().split("\n")).toEqual(["add c.txt", "add a.txt"]);
157 });
158
159 test("a write the adapter cannot make is refused in words, not faked", async () => {
160 const wrote = await api("/api/v1/repos/alice/vault/issues", { title: "from downstream" });
161 expect(wrote.status).toBeGreaterThanOrEqual(400);
162 });
163
164 test("migrating a private repository from it carries the credential, never argv or config", async () => {
165 const started = await api("/api/v1/migrate", {
166 source: { originId, fullName: "octo/vault" },
167 name: "vault-copy",
168 });
169 expect(started.status).toBe(202);
170 const body = (await started.json()) as { job: { id: string } };
171 const job = await settled(body.job.id);
172 expect(job?.error).toBeUndefined();
173 expect(job?.stage).toBe("done");
174 const copy = getRepo(app.db, "alice", "vault-copy")!;
175 expect(copy.origin).toBeNull();
176 const dir = repoDir(app.reposRoot, "alice", "vault-copy");
177 const config = await Bun.file(join(dir, "config")).text();
178 expect(config).not.toContain(token);
179 expect(config).not.toContain("extraHeader");
180 });
181 });
test/route-timing-coverage.test.ts +1
@@ -20,6 +20,7 @@ import { seedAdmin } from "./helpers/admin.ts";
2020 /** Routes that answer 200 without a `session` phase → why. */
2121 const UNTIMED: Record<string, string> = {
2222 "/metrics": "Prometheus text exposition, scraped by token; not a page read",
23 "/api/v1/version": "one fixed answer, the same to everyone; no read to time (#8901)",
2324 };
2425
2526 /**

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs