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

`discover` and `provider forge` work against a 0.5.x forge, proven by a fixture of its answers #9486

Parked opened this
Closed without merging

scout/forge:scout/9456-discover-and-provider-forge-work-against → main 3 commits fork

  • Closed without merging. Nothing was applied to main.

Review

Sign in to review this change.
5 files changed +103 −2
src/origins/providers/forge.ts +3 −1
@@ -23,7 +23,7 @@ import type {
2323 RepoGroup,
2424 } from "../types.ts";
2525 import { ForgejoProvider, nextPage, toRepo } from "./forgejo.ts";
26 import { NO_ACCOUNT } from "../words.ts";
26 import { FORGE_PULL_CHECK_NEEDS, NO_ACCOUNT } from "../words.ts";
2727 import { forgeCursor, toForgeComment, toForgeWork } from "./forge-work.ts";
2828 import { asArray, asId, asString, asTime, dig, fail, field, seg, splitFullName, succeed } from "./http.ts";
2929
@@ -304,6 +304,8 @@ export class ForgeProvider extends ForgejoProvider {
304304 if (base === undefined) return fail("not_found", `not a repository name: ${fullName}`);
305305 const result = await this.api.call({ path: `${base}/pulls/${number}`, query: { diff: 0 } });
306306 if (!result.ok) return result;
307 // Before 0.7.355 a pull carries no `check`: say so, not "no checks".
308 if (field(result.value.data, "check") === undefined) return fail("invalid_response", FORGE_PULL_CHECK_NEEDS);
307309 const word = asString(field(result.value.data, "check"));
308310 return succeed({
309311 word: word === "green" || word === "red" || word === "running" ? word : "none",
src/origins/words.ts +4
@@ -33,3 +33,7 @@ export const FORGE_NOT_CARRIED =
3333 */
3434 export const FORGE_SIGN_IN_ON_PAGE =
3535 "Another forge signs in with one click on the web page; here, with a token.";
36
37 /** A forge older than 0.7.355 reports no check on a pull; said so, with the release that does. */
38 export const FORGE_PULL_CHECK_NEEDS =
39 "this forge is older than 0.7.355 and does not report a pull's check; it needs 0.7.355 or later";
test/fixtures/forge-0.5.x.json added +18
@@ -0,0 +1,18 @@
1 {
2 "about": "Answers of a 0.5.x forge (health 0.5.29), by path. Absent paths answer 404, as that release does: no /api/v1/version, no /.well-known/nodeinfo, no /api/v1/user/repos, no `check` on a pull.",
3 "token": "friend-token",
4 "open": {
5 "/api/v1/health": { "ok": true, "version": "0.5.29", "commit": "2d910c08", "draining": false },
6 "/api/v1/here": { "here": { "kind": "forge", "name": "/" }, "you": null, "actions": [], "mcp": {}, "docs": [] }
7 },
8 "authed": {
9 "/api/v1/user": { "username": "ben", "displayName": "Ben" },
10 "/api/v1/repos/ben/notes/pulls/1": { "number": 1, "title": "Tidy", "state": "open", "headOid": "abc" }
11 },
12 "repos": [
13 { "id": "1", "ownerName": "zed", "name": "other", "description": "other app", "visibility": "public", "forkParentId": null, "defaultBranch": "main" },
14 { "id": "2", "ownerName": "ben", "name": "notes", "description": "notes app", "visibility": "public", "forkParentId": null, "defaultBranch": "main" },
15 { "id": "3", "ownerName": "ben", "name": "secret", "description": "secret app", "visibility": "private", "forkParentId": null, "defaultBranch": "main" },
16 { "id": "4", "ownerName": "zed", "name": "more", "description": "more app", "visibility": "public", "forkParentId": null, "defaultBranch": "main" }
17 ]
18 }
test/forge-origin-fixture-0-5.test.ts added +77
@@ -0,0 +1,77 @@
1 import { afterAll, beforeAll, expect, test } from "bun:test";
2 import { discover } from "../src/origins/discover.ts";
3 import { providerFor } from "../src/origins/providers/index.ts";
4 import { staticCredential } from "../src/origins/types.ts";
5 import { FORGE_PULL_CHECK_NEEDS } from "../src/origins/words.ts";
6 import fixture from "./fixtures/forge-0.5.x.json";
7
8 /** A 0.5.x forge replayed from a checked-in fixture of its answers (#9456); every other path is a 404. */
9 let server: ReturnType<typeof Bun.serve>;
10 let base = "";
11 const seen: string[] = [];
12 const open = fixture.open as Record<string, unknown>;
13 const authed = fixture.authed as Record<string, unknown>;
14
15 beforeAll(() => {
16 process.env.FORGE_DEV = "1";
17 server = Bun.serve({
18 port: 0,
19 hostname: "127.0.0.1",
20 fetch(req) {
21 const url = new URL(req.url);
22 seen.push(url.pathname);
23 if (url.pathname in open) return Response.json(open[url.pathname]);
24 const ok = req.headers.get("authorization") === `Bearer ${fixture.token}`;
25 if (url.pathname in authed)
26 return ok ? Response.json(authed[url.pathname]) : new Response("{}", { status: 401 });
27 if (url.pathname === "/api/v1/repos") {
28 if (!ok) return new Response("{}", { status: 401 });
29 const after = Number(url.searchParams.get("cursor") ?? 0);
30 const limit = Number(url.searchParams.get("limit") ?? 30);
31 const rows = fixture.repos.filter((r) => Number(r.id) > after).slice(0, limit);
32 const last = rows[rows.length - 1];
33 return Response.json({ items: rows, next_cursor: rows.length >= limit && last ? last.id : null });
34 }
35 return new Response("{}", { status: 404 });
36 },
37 });
38 base = `http://127.0.0.1:${server.port}`;
39 });
40 afterAll(() => server.stop(true));
41
42 const provider = () =>
43 providerFor(
44 staticCredential({
45 kind: "gitea",
46 software: "forge",
47 apiUrl: `${base}/api/v1`,
48 webUrl: base,
49 login: "ben",
50 token: fixture.token,
51 }),
52 );
53
54 test("discover names the 0.5.x forge from its fixture, with no version endpoint", async () => {
55 const found = await discover(base);
56 expect(found.ok).toBe(true);
57 if (!found.ok) return;
58 expect(found.value.software).toBe("forge 0.5.29");
59 expect(found.value.via).toBe("health");
60 });
61
62 test("provider forge lists the account's repositories from the fixture, never /user/repos", async () => {
63 const page = await provider().listRepos();
64 expect(page.ok).toBe(true);
65 if (!page.ok) return;
66 expect(page.value.items.map((r) => r.fullName)).toEqual(["ben/notes", "ben/secret"]);
67 expect(seen).toContain("/api/v1/repos");
68 });
69
70 test("a pull's check, which that release lacks, fails naming the version it needs", async () => {
71 const check = await provider().getPullCheck?.("ben/notes", 1);
72 if (check === undefined) throw new Error("no getPullCheck");
73 expect(check.ok).toBe(false);
74 if (check.ok) return;
75 expect(check.error.message).toBe(FORGE_PULL_CHECK_NEEDS);
76 expect(check.error.message).toContain("0.7.355");
77 });
test/origin-words.test.ts +1 −1
@@ -15,7 +15,7 @@ const PROVIDERS = [
1515 const sentences = Object.entries(words) as [string, string][];
1616
1717 test("each sentence is non-empty", () => {
18 expect(sentences.length).toBe(7);
18 expect(sentences.length).toBe(8);
1919 for (const [, s] of sentences) expect(s.trim().length).toBeGreaterThan(0);
2020 });
2121

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs