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

A repository's following routes leave api.ts for src/http/api/following.ts #6956

Closed opened this
Closed without merging

mason/forge:mason/6328-a-repository-s-following-routes-leave-ap → main 2 commits fork

  • Closed without merging. Nothing was applied to main.

Review

Sign in to review this change.
2 files changed +248 −226
src/http/api.ts +5 −226
@@ -19,14 +19,12 @@ import {
1919 } from "../actions/origin_app_words.ts";
2020 import { getProvenance, provenanceFacts } from "../core/provenance.ts";
2121 import { goldenPath } from "../apps/golden_path.ts";
22 import { upstreamPayload } from "../spawn/upstream.ts";
2322 import { setRole } from "../actions/roles.ts";
2423 import { listPeople, NOT_A_MEMBER } from "../core/roles.ts";
2524 import { setAccountCeilings, setAccountGrant } from "../actions/model_grant.ts";
2625 import { storageLimitMessage } from "../core/storage.ts";
2726 import { voteVariant } from "../actions/variants.ts";
2827 import { notePhase, takeSessionTime, timePhase, timePhaseAsync } from "../core/route_timing.ts";
29 import { parentForgeFor } from "../core/parent_releases.ts";
3028 import { readFlow } from "../flow/read.ts";
3129 import { MILESTONE_WATCH_PERSON_ONLY, VOTE_NOT_RECORDED } from "../actions/board_words.ts";
3230 import { recordDrill } from "../core/domain_drills.ts";
@@ -79,14 +77,10 @@ import { PRIVATE_ADDRESS_REFUSED } from "../kernel/address_words.ts";
7977 import { forgeState, PARENT_HEADER } from "../spawn/clients.ts";
8078 import { familyView } from "../spawn/family.ts";
8179 import { receiveBeat, SIGNATURE_HEADER } from "../spawn/heartbeat.ts";
82 import { forgeProposals, proposeToParentForge, proposeUpstream } from "../actions/upstream.ts";
83 import { FOLLOW_MODE_REFUSALS, setFollowMode, severForge, takeForgeCode, takeForgeWords, takeOrTell } from "../actions/follow.ts";
84 import { relationOf } from "../actions/forks.ts";
85 import { chainOfRepo, forgeLinkOf } from "../spawn/chain.ts";
80 import { takeOrTell } from "../actions/follow.ts";
8681 import { noteRead, telemetryReader, type Reader } from "../actions/telemetry.ts";
8782 import { flowFor, historyFor, sourcesOut, systemFor, welcome } from "../system/telemetry.ts";
88 import { convergeFork, convergeState } from "../crew/converge.ts";
89 import { markReaders, mayDirectCrew } from "../crew/direct.ts";
83 import { markReaders } from "../crew/direct.ts";
9084 import {
9185 addCommentThrough,
9286 openIssueThrough,
@@ -417,6 +411,7 @@ import { egressGrantRoutes } from "./api/egress_grants.ts";
417411 import { forgeRoutes } from "./api/forges.ts";
418412 import { agentRoutes } from "./api/agents.ts";
419413 import { boardRoutes } from "./api/boards.ts";
414 import { followingRoutes } from "./api/following.ts";
420415 import { labelsRoutes } from "./api/labels.ts";
421416 import { orgRoutes } from "./api/orgs.ts";
422417 import { secretRoutes } from "./api/secrets.ts";
@@ -442,7 +437,7 @@ import { SPEND_WORDS } from "../actions/spend_words.ts";
442437 import { ANSWER_WORDS, ORDER_WORDS, SIGNUP_WORDS, VISIBILITY_WORDS } from "../core/arg_words.ts";
443438
444439 /** Take the parent's updates for `repo` and answer as the API does, for every route that asks. */
445 async function respondTake(ctx: Ctx, repo: Repo): Promise<Response> {
440 export async function respondTake(ctx: Ctx, repo: Repo): Promise<Response> {
446441 const took = await takeOrTell(ctx.app, ctx.auth.user, repo);
447442 if (!took.ok) {
448443 const e = took.error;
@@ -2247,223 +2242,7 @@ export function apiRoutes(router: Router<Handler>): void {
22472242 return timePhase(ctx.request, "render", () => json(page));
22482243 });
22492244
2250 // ── following. A fork's standing against its parent, read from git each
2251 // time; taking updates is git's part, converging is the crew's.
2252 router.add("GET", "/api/v1/repos/:owner/:name/following", async (ctx) => {
2253 notePhase(ctx.request, "session", takeSessionTime(ctx.request));
2254 const repo = loadRepo(ctx);
2255 if (!repo) return notFound();
2256 const relation = await timePhaseAsync(ctx.request, "relation", () =>
2257 relationOf(ctx.app, ctx.auth.user, repo),
2258 );
2259 const chain = await timePhaseAsync(ctx.request, "chain", () => chainOfRepo(ctx.app, repo));
2260 if (relation === undefined) {
2261 if (chain === undefined) return apiError("not_a_fork", 404, "this repository follows nothing");
2262 const upstream = await timePhaseAsync(ctx.request, "upstream", () => upstreamPayload(ctx.app));
2263 return timePhase(ctx.request, "render", () =>
2264 json({ chain: chain.lines, severedAt: chain.severedAt, upstream }),
2265 );
2266 }
2267 const converge =
2268 relation.state === "diverged" && !relation.clean
2269 ? timePhase(ctx.request, "converge", () => convergeState(ctx.app, repo, relation.parent))
2270 : undefined;
2271 const upstream =
2272 chain === undefined
2273 ? null
2274 : await timePhaseAsync(ctx.request, "upstream", () => upstreamPayload(ctx.app));
2275 return timePhase(ctx.request, "render", () => json({
2276 chain: chain?.lines ?? null,
2277 severedAt: chain?.severedAt ?? null,
2278 upstream,
2279 parent: `${relation.parent.ownerName}/${relation.parent.name}`,
2280 parentBranch: relation.parent.defaultBranch,
2281 // A parent whose origin was disconnected: the counts below are against
2282 // where it froze, never against its upstream (#1090).
2283 parentFrozen:
2284 relation.parent.detached === null
2285 ? null
2286 : {
2287 detachedAt: relation.parent.detached.detachedAt,
2288 frozenSince: relation.parent.detached.frozenSince,
2289 },
2290 branch: repo.defaultBranch,
2291 state: relation.state,
2292 behind: relation.behind,
2293 ahead: relation.ahead,
2294 clean: relation.clean,
2295 conflicts: relation.conflicts,
2296 forkTip: relation.forkTip,
2297 parentTip: relation.parentTip,
2298 parentForge: parentForgeFor(ctx.app.config),
2299 converge:
2300 converge === undefined
2301 ? null
2302 : {
2303 kind: converge.kind,
2304 issue: "issue" in converge ? converge.issue.number : null,
2305 pull: "pull" in converge ? converge.pull.number : null,
2306 agent:
2307 "agent" in converge && converge.agent !== undefined
2308 ? converge.agent.user.username
2309 : null,
2310 },
2311 }));
2312 });
2313
2314 router.add(
2315 "POST",
2316 "/api/v1/repos/:owner/:name/following/take",
2317 async (ctx) => {
2318 const denied = requireScope(ctx, "rw");
2319 if (denied) return denied;
2320 const repo = loadRepo(ctx);
2321 if (!repo) return notFound();
2322 // The repository that carries this forge's code takes as a pull; any other fork merges.
2323 if ((await forgeLinkOf(ctx.app))?.repo !== `${repo.ownerName}/${repo.name}`) return respondTake(ctx, repo);
2324 const took = await takeForgeCode(ctx.app, ctx.auth.user, repo);
2325 if (!took.ok)
2326 return took.error === "not_following"
2327 ? apiError("not_following", 404, FAMILY_WORDS.not_following)
2328 : respondAccess(took.error, 403);
2329 return json({ ...took.value, ...takeForgeWords(took.value) });
2330 },
2331 );
2332
2333 router.add(
2334 "POST",
2335 "/api/v1/repos/:owner/:name/following/sever",
2336 async (ctx) => {
2337 const denied = requireScope(ctx, "rw");
2338 if (denied) return denied;
2339 const repo = loadRepo(ctx);
2340 if (!repo) return notFound();
2341 const cut = await severForge(ctx.app, ctx.auth.user, repo);
2342 if (!cut.ok)
2343 return cut.error === "not_following"
2344 ? apiError("not_following", 404, FAMILY_WORDS.not_following)
2345 : respondAccess(cut.error, 403);
2346 return json({ severedAt: cut.value });
2347 },
2348 );
2349
2350 // How this forge follows the forge its code came from: its own code
2351 // repository's admin only, the same words as the page and MCP.
2352 router.add("PATCH", "/api/v1/forge/following", async (ctx) => {
2353 const denied = requireScope(ctx, "rw");
2354 if (denied) return denied;
2355 const link = await forgeLinkOf(ctx.app);
2356 const [owner, name] = (link?.repo ?? "/").split("/") as [string, string];
2357 const repo = link === undefined ? undefined : getRepo(ctx.app.db, owner, name);
2358 if (repo === undefined) return apiError("not_following", 404, FOLLOW_MODE_REFUSALS.not_following.message);
2359 const set = await setFollowMode(ctx.app, ctx.auth.user, repo, (await body(ctx.request)).mode);
2360 if (set.ok) return json({ mode: set.value });
2361 const r = FOLLOW_MODE_REFUSALS[set.error];
2362 return apiError(set.error, r.status, r.message);
2363 });
2364
2365 router.add(
2366 "POST",
2367 "/api/v1/repos/:owner/:name/following/propose",
2368 async (ctx) => {
2369 const denied = requireScope(ctx, "rw");
2370 if (denied) return denied;
2371 const repo = loadRepo(ctx);
2372 if (!repo) return notFound();
2373 const proposed = await proposeUpstream(ctx.app, ctx.auth.user, repo);
2374 if (!proposed.ok) {
2375 const e = proposed.error;
2376 if (e === "not_a_fork")
2377 return apiError("not_a_fork", 404, "this repository follows nothing");
2378 if (e === "nothing_to_propose")
2379 return apiError(
2380 "nothing_to_propose",
2381 409,
2382 "nothing here is ahead of the parent",
2383 );
2384 if (typeof e === "string") return respondAccess(e, 403);
2385 return apiError("refused", 409, e.reason);
2386 }
2387 const { pull, opened, changes } = proposed.value;
2388 return json({ number: pull.number, opened, changes, title: pull.title });
2389 },
2390 );
2391
2392 // Across forges: push this forge's change to the parent forge and open the
2393 // pull there; GET says how each proposal stands, as the parent last said.
2394 router.add(
2395 "POST",
2396 "/api/v1/repos/:owner/:name/following/propose-parent",
2397 async (ctx) => {
2398 const denied = requireScope(ctx, "rw");
2399 if (denied) return denied;
2400 const repo = loadRepo(ctx);
2401 if (!repo) return notFound();
2402 const made = await proposeToParentForge(ctx.app, ctx.auth.user, repo);
2403 if (!made.ok) {
2404 const e = made.error;
2405 if (e === "no_parent_forge")
2406 return apiError("no_parent_forge", 404, "this repository has no parent forge to propose to");
2407 if (e === "nothing_to_propose")
2408 return apiError("nothing_to_propose", 409, FAMILY_WORDS.nothing_to_propose);
2409 if (typeof e === "string") return respondAccess(e, 403);
2410 return e.kind === "unreachable"
2411 ? apiError("parent_unreachable", 502, "parent unreachable")
2412 : apiError("refused", 409, e.reason);
2413 }
2414 return json(made.value, made.value.opened ? 201 : 200);
2415 },
2416 );
2417
2418 router.add("GET", "/api/v1/repos/:owner/:name/following/proposals", async (ctx) => {
2419 notePhase(ctx.request, "session", takeSessionTime(ctx.request));
2420 const repo = loadRepo(ctx);
2421 if (!repo) return notFound();
2422 const items = await timePhaseAsync(ctx.request, "read", () =>
2423 forgeProposals(ctx.app, repo, Date.now(), ctx.auth.user !== undefined),
2424 );
2425 return timePhase(ctx.request, "render", () => json({ items }));
2426 });
2427
2428 router.add(
2429 "POST",
2430 "/api/v1/repos/:owner/:name/following/converge",
2431 async (ctx) => {
2432 const denied = requireScope(ctx, "rw");
2433 if (denied) return denied;
2434 const repo = loadRepo(ctx);
2435 if (!repo) return notFound();
2436 if (!mayDirectCrew(ctx.app.db, repo, ctx.auth.user))
2437 return respondAccess("forbidden", 403);
2438 const parent = forkParentFor(ctx.app.db, repo, ctx.auth.user);
2439 if (parent === undefined)
2440 return apiError("not_a_fork", 404, "this repository follows nothing");
2441 const outcome = await convergeFork(ctx.app, repo, parent, ctx.auth.user);
2442 if (outcome.kind === "archived") return respondAccess("archived", 409);
2443 const status =
2444 outcome.kind === "queued"
2445 ? 202
2446 : outcome.kind === "working" || outcome.kind === "proposed"
2447 ? 200
2448 : outcome.kind === "ceiling"
2449 ? 429
2450 : outcome.kind === "nobody" || outcome.kind === "forbidden" || outcome.kind === "not_member"
2451 ? 403
2452 : 409;
2453 return json(
2454 {
2455 kind: outcome.kind,
2456 issue: "issue" in outcome ? outcome.issue.number : null,
2457 pull: "pull" in outcome ? outcome.pull.number : null,
2458 agent:
2459 "agent" in outcome && outcome.agent !== undefined
2460 ? outcome.agent.user.username
2461 : null,
2462 },
2463 status,
2464 );
2465 },
2466 );
2245 followingRoutes(router);
24672246
24682247 // ── git reads. The agent arena showed the write path was complete but the
24692248 // read path was missing, so automation couldn't inspect before acting.
src/http/api/following.ts added +243
@@ -0,0 +1,243 @@
1 /**
2 * A fork's standing against its parent over the API: `GET`
3 * `/api/v1/repos/:owner/:name/following` and its `take`, `sever`, `propose`,
4 * `propose-parent`, `proposals` and `converge`, plus `PATCH`
5 * `/api/v1/forge/following`. Moved out of `api.ts` (#6328); the rules live
6 * in `actions/follow.ts`, `actions/upstream.ts` and `crew/converge.ts`.
7 */
8
9 import { FAMILY_WORDS } from "../../actions/family_words.ts";
10 import { FOLLOW_MODE_REFUSALS, setFollowMode, severForge, takeForgeCode, takeForgeWords } from "../../actions/follow.ts";
11 import { relationOf } from "../../actions/forks.ts";
12 import { forgeProposals, proposeToParentForge, proposeUpstream } from "../../actions/upstream.ts";
13 import { parentForgeFor } from "../../core/parent_releases.ts";
14 import { forkParentFor, getRepo } from "../../core/repos.ts";
15 import { notePhase, takeSessionTime, timePhase, timePhaseAsync } from "../../core/route_timing.ts";
16 import { convergeFork, convergeState } from "../../crew/converge.ts";
17 import { mayDirectCrew } from "../../crew/direct.ts";
18 import { chainOfRepo, forgeLinkOf } from "../../spawn/chain.ts";
19 import { upstreamPayload } from "../../spawn/upstream.ts";
20 import { loadRepo, respondTake } from "../api.ts";
21 import type { Handler } from "../context.ts";
22 import { apiError, body, json, notFound, requireScope, respondAccess } from "../respond.ts";
23 import type { Router } from "../router.ts";
24
25 export function followingRoutes(router: Router<Handler>): void {
26 // ── following. A fork's standing against its parent, read from git each
27 // time; taking updates is git's part, converging is the crew's.
28 router.add("GET", "/api/v1/repos/:owner/:name/following", async (ctx) => {
29 notePhase(ctx.request, "session", takeSessionTime(ctx.request));
30 const repo = loadRepo(ctx);
31 if (!repo) return notFound();
32 const relation = await timePhaseAsync(ctx.request, "relation", () =>
33 relationOf(ctx.app, ctx.auth.user, repo),
34 );
35 const chain = await timePhaseAsync(ctx.request, "chain", () => chainOfRepo(ctx.app, repo));
36 if (relation === undefined) {
37 if (chain === undefined) return apiError("not_a_fork", 404, "this repository follows nothing");
38 const upstream = await timePhaseAsync(ctx.request, "upstream", () => upstreamPayload(ctx.app));
39 return timePhase(ctx.request, "render", () =>
40 json({ chain: chain.lines, severedAt: chain.severedAt, upstream }),
41 );
42 }
43 const converge =
44 relation.state === "diverged" && !relation.clean
45 ? timePhase(ctx.request, "converge", () => convergeState(ctx.app, repo, relation.parent))
46 : undefined;
47 const upstream =
48 chain === undefined
49 ? null
50 : await timePhaseAsync(ctx.request, "upstream", () => upstreamPayload(ctx.app));
51 return timePhase(ctx.request, "render", () => json({
52 chain: chain?.lines ?? null,
53 severedAt: chain?.severedAt ?? null,
54 upstream,
55 parent: `${relation.parent.ownerName}/${relation.parent.name}`,
56 parentBranch: relation.parent.defaultBranch,
57 // A parent whose origin was disconnected: the counts below are against
58 // where it froze, never against its upstream (#1090).
59 parentFrozen:
60 relation.parent.detached === null
61 ? null
62 : {
63 detachedAt: relation.parent.detached.detachedAt,
64 frozenSince: relation.parent.detached.frozenSince,
65 },
66 branch: repo.defaultBranch,
67 state: relation.state,
68 behind: relation.behind,
69 ahead: relation.ahead,
70 clean: relation.clean,
71 conflicts: relation.conflicts,
72 forkTip: relation.forkTip,
73 parentTip: relation.parentTip,
74 parentForge: parentForgeFor(ctx.app.config),
75 converge:
76 converge === undefined
77 ? null
78 : {
79 kind: converge.kind,
80 issue: "issue" in converge ? converge.issue.number : null,
81 pull: "pull" in converge ? converge.pull.number : null,
82 agent:
83 "agent" in converge && converge.agent !== undefined
84 ? converge.agent.user.username
85 : null,
86 },
87 }));
88 });
89
90 router.add(
91 "POST",
92 "/api/v1/repos/:owner/:name/following/take",
93 async (ctx) => {
94 const denied = requireScope(ctx, "rw");
95 if (denied) return denied;
96 const repo = loadRepo(ctx);
97 if (!repo) return notFound();
98 // The repository that carries this forge's code takes as a pull; any other fork merges.
99 if ((await forgeLinkOf(ctx.app))?.repo !== `${repo.ownerName}/${repo.name}`) return respondTake(ctx, repo);
100 const took = await takeForgeCode(ctx.app, ctx.auth.user, repo);
101 if (!took.ok)
102 return took.error === "not_following"
103 ? apiError("not_following", 404, FAMILY_WORDS.not_following)
104 : respondAccess(took.error, 403);
105 return json({ ...took.value, ...takeForgeWords(took.value) });
106 },
107 );
108
109 router.add(
110 "POST",
111 "/api/v1/repos/:owner/:name/following/sever",
112 async (ctx) => {
113 const denied = requireScope(ctx, "rw");
114 if (denied) return denied;
115 const repo = loadRepo(ctx);
116 if (!repo) return notFound();
117 const cut = await severForge(ctx.app, ctx.auth.user, repo);
118 if (!cut.ok)
119 return cut.error === "not_following"
120 ? apiError("not_following", 404, FAMILY_WORDS.not_following)
121 : respondAccess(cut.error, 403);
122 return json({ severedAt: cut.value });
123 },
124 );
125
126 // How this forge follows the forge its code came from: its own code
127 // repository's admin only, the same words as the page and MCP.
128 router.add("PATCH", "/api/v1/forge/following", async (ctx) => {
129 const denied = requireScope(ctx, "rw");
130 if (denied) return denied;
131 const link = await forgeLinkOf(ctx.app);
132 const [owner, name] = (link?.repo ?? "/").split("/") as [string, string];
133 const repo = link === undefined ? undefined : getRepo(ctx.app.db, owner, name);
134 if (repo === undefined) return apiError("not_following", 404, FOLLOW_MODE_REFUSALS.not_following.message);
135 const set = await setFollowMode(ctx.app, ctx.auth.user, repo, (await body(ctx.request)).mode);
136 if (set.ok) return json({ mode: set.value });
137 const r = FOLLOW_MODE_REFUSALS[set.error];
138 return apiError(set.error, r.status, r.message);
139 });
140
141 router.add(
142 "POST",
143 "/api/v1/repos/:owner/:name/following/propose",
144 async (ctx) => {
145 const denied = requireScope(ctx, "rw");
146 if (denied) return denied;
147 const repo = loadRepo(ctx);
148 if (!repo) return notFound();
149 const proposed = await proposeUpstream(ctx.app, ctx.auth.user, repo);
150 if (!proposed.ok) {
151 const e = proposed.error;
152 if (e === "not_a_fork")
153 return apiError("not_a_fork", 404, "this repository follows nothing");
154 if (e === "nothing_to_propose")
155 return apiError(
156 "nothing_to_propose",
157 409,
158 "nothing here is ahead of the parent",
159 );
160 if (typeof e === "string") return respondAccess(e, 403);
161 return apiError("refused", 409, e.reason);
162 }
163 const { pull, opened, changes } = proposed.value;
164 return json({ number: pull.number, opened, changes, title: pull.title });
165 },
166 );
167
168 // Across forges: push this forge's change to the parent forge and open the
169 // pull there; GET says how each proposal stands, as the parent last said.
170 router.add(
171 "POST",
172 "/api/v1/repos/:owner/:name/following/propose-parent",
173 async (ctx) => {
174 const denied = requireScope(ctx, "rw");
175 if (denied) return denied;
176 const repo = loadRepo(ctx);
177 if (!repo) return notFound();
178 const made = await proposeToParentForge(ctx.app, ctx.auth.user, repo);
179 if (!made.ok) {
180 const e = made.error;
181 if (e === "no_parent_forge")
182 return apiError("no_parent_forge", 404, "this repository has no parent forge to propose to");
183 if (e === "nothing_to_propose")
184 return apiError("nothing_to_propose", 409, FAMILY_WORDS.nothing_to_propose);
185 if (typeof e === "string") return respondAccess(e, 403);
186 return e.kind === "unreachable"
187 ? apiError("parent_unreachable", 502, "parent unreachable")
188 : apiError("refused", 409, e.reason);
189 }
190 return json(made.value, made.value.opened ? 201 : 200);
191 },
192 );
193
194 router.add("GET", "/api/v1/repos/:owner/:name/following/proposals", async (ctx) => {
195 notePhase(ctx.request, "session", takeSessionTime(ctx.request));
196 const repo = loadRepo(ctx);
197 if (!repo) return notFound();
198 const items = await timePhaseAsync(ctx.request, "read", () =>
199 forgeProposals(ctx.app, repo, Date.now(), ctx.auth.user !== undefined),
200 );
201 return timePhase(ctx.request, "render", () => json({ items }));
202 });
203
204 router.add(
205 "POST",
206 "/api/v1/repos/:owner/:name/following/converge",
207 async (ctx) => {
208 const denied = requireScope(ctx, "rw");
209 if (denied) return denied;
210 const repo = loadRepo(ctx);
211 if (!repo) return notFound();
212 if (!mayDirectCrew(ctx.app.db, repo, ctx.auth.user))
213 return respondAccess("forbidden", 403);
214 const parent = forkParentFor(ctx.app.db, repo, ctx.auth.user);
215 if (parent === undefined)
216 return apiError("not_a_fork", 404, "this repository follows nothing");
217 const outcome = await convergeFork(ctx.app, repo, parent, ctx.auth.user);
218 if (outcome.kind === "archived") return respondAccess("archived", 409);
219 const status =
220 outcome.kind === "queued"
221 ? 202
222 : outcome.kind === "working" || outcome.kind === "proposed"
223 ? 200
224 : outcome.kind === "ceiling"
225 ? 429
226 : outcome.kind === "nobody" || outcome.kind === "forbidden" || outcome.kind === "not_member"
227 ? 403
228 : 409;
229 return json(
230 {
231 kind: outcome.kind,
232 issue: "issue" in outcome ? outcome.issue.number : null,
233 pull: "pull" in outcome ? outcome.pull.number : null,
234 agent:
235 "agent" in outcome && outcome.agent !== undefined
236 ? outcome.agent.user.username
237 : null,
238 },
239 status,
240 );
241 },
242 );
243 }

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs