ref0-git 0.12.0 → 0.20.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (63) hide show
  1. package/app/apps/github/generated/git-rest.ts +320 -4
  2. package/app/apps/github/generated/routeTree.gen.ts +42 -21
  3. package/app/apps/github/migrations/postgres/0001_auth.sql +8 -1
  4. package/app/apps/github/migrations/sql/0005_repo_grants.sql +10 -0
  5. package/app/apps/github/src/clients/allowed.ts +9 -4
  6. package/app/apps/github/src/clients/host-calls.ts +59 -34
  7. package/app/apps/github/src/components/repos/your-repos.tsx +57 -11
  8. package/app/apps/github/src/components/states/states.tsx +1 -19
  9. package/app/apps/github/src/components/view-tabs.tsx +6 -2
  10. package/app/apps/github/src/context/context.ts +51 -5
  11. package/app/apps/github/src/context/host.ts +72 -36
  12. package/app/apps/github/src/context/repo-grants.ts +35 -0
  13. package/app/apps/github/src/hooks/repos.ts +14 -3
  14. package/app/apps/github/src/issues/issue-list.tsx +11 -3
  15. package/app/apps/github/src/issues/issues.ts +11 -8
  16. package/app/apps/github/src/lists/across-list.tsx +358 -0
  17. package/app/apps/github/src/lists/across.ts +239 -0
  18. package/app/apps/github/src/names/owners.ts +3 -1
  19. package/app/apps/github/src/palette/targets.ts +3 -2
  20. package/app/apps/github/src/policy.ts +493 -102
  21. package/app/apps/github/src/private-repos.ts +24 -24
  22. package/app/apps/github/src/pulls/pulls.ts +2 -1
  23. package/app/apps/github/src/routes/$owner/$repo/settings/access.tsx +1 -1
  24. package/app/apps/github/src/routes/-nav.tsx +40 -12
  25. package/app/apps/github/src/routes/index.tsx +2 -1
  26. package/app/apps/github/src/routes/issues.tsx +15 -0
  27. package/app/apps/github/src/routes/orgs/$owner/-org-list.ts +31 -0
  28. package/app/apps/github/src/routes/orgs/$owner/issues.tsx +10 -3
  29. package/app/apps/github/src/routes/orgs/$owner/pulls.tsx +10 -3
  30. package/app/apps/github/src/routes/pulls.tsx +15 -0
  31. package/app/apps/github/src/seed/seed.ts +104 -52
  32. package/app/apps/github/src/server.ts +17 -7
  33. package/app/apps/github/src/settings/access-page.tsx +308 -0
  34. package/app/apps/github/src/settings/access.ts +156 -0
  35. package/app/apps/github/src/settings/settings-layout.tsx +0 -16
  36. package/app/apps/github/src/signin/approval.ts +24 -16
  37. package/app/apps/github/src/signin/auth.ts +29 -19
  38. package/app/apps/github/src/signin/orgs.ts +57 -14
  39. package/app/apps/github/src/signin/repo-grants.ts +141 -0
  40. package/app/apps/github/src/signin/revoke.ts +9 -17
  41. package/app/apps/github/src/signin/unborn.ts +33 -20
  42. package/app/pnpm-workspace.yaml +2 -2
  43. package/dist/cli/child.mjs +73 -3
  44. package/dist/cli/commands.mjs +2 -0
  45. package/dist/cli/documents.mjs +42 -2
  46. package/dist/cli/keys.mjs +431 -0
  47. package/dist/cli/main.mjs +148 -0
  48. package/dist/cli/prompt.mjs +40 -1
  49. package/dist/cli/ref0-git.mjs +4 -141
  50. package/dist/cli/setup.mjs +118 -16
  51. package/dist/cli/sign-in.mjs +1 -1
  52. package/dist/cli/tools-node.mjs +20 -0
  53. package/dist/cli/tools.mjs +12 -0
  54. package/dist/init/app.mjs +5 -2
  55. package/dist/init/cloudflare.mjs +8 -3
  56. package/dist/init/git-module.mjs +13 -8
  57. package/dist/init/metadata.mjs +1 -1
  58. package/dist/init/node.mjs +1 -1
  59. package/dist/init/template.mjs +1 -0
  60. package/dist/init/templates.mjs +24 -19
  61. package/dist/init/versions.mjs +1 -1
  62. package/package.json +5 -5
  63. package/app/apps/github/src/routes/orgs/$owner/releases.tsx +0 -8
@@ -1,4 +1,4 @@
1
- import { accept, gitError, Policy, Rules, reject } from '@ref0/git';
1
+ import { gitError, Policy, Rules } from '@ref0/git';
2
2
  import type {
3
3
  ActorId,
4
4
  AuthorizeInput,
@@ -6,25 +6,37 @@ import type {
6
6
  GitError,
7
7
  InternalRepoId,
8
8
  NamedActor,
9
+ Operation,
9
10
  PushRule,
10
11
  RefName,
12
+ RepoAccess,
11
13
  RepoName,
12
14
  TokenInfo,
13
15
  TokenOperation,
14
16
  } from '@ref0/git';
15
- import { defaultOrg, defaultPeople, People } from '@ref0/github';
17
+ import {
18
+ defaultOrg,
19
+ defaultPeople,
20
+ defaultUser,
21
+ People,
22
+ RepoGrants,
23
+ repoGrantsMost,
24
+ } from '@ref0/github';
16
25
  import type {
17
26
  GitHubAuthorization,
18
27
  GitHubAuthorize,
19
28
  GitHubOrg,
20
29
  GitHubReadableRepos,
21
30
  GitHubReadableReposInput,
31
+ GitHubRepoOperation,
22
32
  GitHubTeam,
23
33
  GitHubUser,
24
34
  PeopleAnswer,
25
35
  PeopleFor,
26
36
  PeopleInput,
27
37
  PeopleMembers,
38
+ PeopleAccounts,
39
+ PeopleAccountsAnswer,
28
40
  PeopleEmails,
29
41
  PeopleMembership,
30
42
  PeopleOrg,
@@ -35,17 +47,30 @@ import type {
35
47
  PeopleTeams,
36
48
  PeopleUser,
37
49
  PeopleViewer,
50
+ GrantRepo,
51
+ PersonGrant,
52
+ RepoGrantsInput,
53
+ RepoGrantsList,
54
+ RepoGrantsMovedAnswer,
55
+ RepoGrantsPut,
56
+ RepoGrantsRemove,
57
+ RepoGrantsRole,
58
+ RepoGrantsShared,
59
+ RepoGrantsMoved,
60
+ RepoRole,
61
+ TeamGrant,
38
62
  } from '@ref0/github';
39
63
  import * as Clock from 'effect/Clock';
40
64
  import * as Effect from 'effect/Effect';
41
65
 
42
66
  import { type StarterApprovalOptions, starterApprovalPage } from './signin/approval.ts';
43
- import type { Account, Avatars, Membership } from './context/context.ts';
67
+ import type { Account, Avatars, Grant, Grants, Membership } from './context/context.ts';
44
68
  import { admin } from './names/owners.ts';
45
69
 
46
70
  export { admin };
47
71
 
48
72
  import { orgTokenView, type Orgs } from './signin/orgs.ts';
73
+ import { grantsPage } from './signin/repo-grants.ts';
49
74
 
50
75
  /**
51
76
  * The host's say, as a starter writes it: anyone reads a public repo, a person signed in owns the
@@ -63,18 +88,27 @@ interface Listed<Item> {
63
88
  readonly next?: string;
64
89
  }
65
90
 
66
- /** A membership the policy read, and when, by the `Clock`. */
67
- interface KeptMembership {
91
+ /** A membership or a grant the policy read, and when, by the `Clock`. */
92
+ interface Kept<A> {
68
93
  readonly at: number;
69
- readonly answer: Promise<Membership | undefined>;
94
+ readonly answer: Promise<A>;
70
95
  }
71
96
 
72
97
  /**
73
- * How long, in milliseconds, a membership read is kept on its actor: past one request's reads, and
74
- * well under a stream's 9-second heartbeat, whose check then reads it again.
98
+ * How long, in milliseconds, a membership or a grant read is kept on its actor: past one request's
99
+ * reads, and well under a stream's 9-second heartbeat, whose check then reads it again.
75
100
  */
76
101
  const membershipLife = 1_000;
77
102
 
103
+ /**
104
+ * The Git store reads one shared repo's name takes: `git.ts`' `repoName` is `RepoRecords.record`,
105
+ * which reads the record, its catalogue entry and the record again.
106
+ */
107
+ export const nameReads = 3;
108
+
109
+ /** The store calls `readableRepos` may make for each owner past `most`, as its contract says. */
110
+ const ownerCallsPast = 6;
111
+
78
112
  /** What a people lookup answers a token that no longer holds: no one, and nothing listed. */
79
113
  const refusedLookup = (input: PeopleInput): PeopleAnswer => {
80
114
  switch (input.kind) {
@@ -86,6 +120,8 @@ const refusedLookup = (input: PeopleInput): PeopleAnswer => {
86
120
  case 'emails':
87
121
  case 'membership':
88
122
  return [];
123
+ case 'accounts':
124
+ return {};
89
125
  case 'search':
90
126
  return { found: [], incomplete: false };
91
127
  default:
@@ -97,7 +133,7 @@ const refusedLookup = (input: PeopleInput): PeopleAnswer => {
97
133
  * A read of the starter's own tables, as the directory's Effect: one that rejects fails closed as
98
134
  * Git's `UNAVAILABLE`, in fixed words, so neither D1's error nor its words reach the drop-in.
99
135
  */
100
- const read = <A>(run: () => Promise<A>): Effect.Effect<A, GitError> =>
136
+ const read = <A>(run: (signal: AbortSignal) => Promise<A>): Effect.Effect<A, GitError> =>
101
137
  Effect.tryPromise({
102
138
  try: run,
103
139
  catch: () => gitError('UNAVAILABLE', "The starter's directory can't be read now"),
@@ -123,6 +159,33 @@ const reaches = (input: PeopleOrgs, org: string) => {
123
159
  return under.startsWith(token.prefix) || token.prefix.startsWith(under);
124
160
  };
125
161
 
162
+ /** Each role's rank: a role holds what every role below it holds. */
163
+ const roleRank: Readonly<Record<RepoRole, number>> = { read: 0, write: 1, admin: 2 };
164
+
165
+ /** Whether a grant of `role` holds what `needs` asks. */
166
+ const roleHolds = (role: RepoRole | undefined, needs: RepoRole) =>
167
+ role !== undefined && roleRank[role] >= roleRank[needs];
168
+
169
+ /**
170
+ * The role a grant needs for one of Git's operations (#1998): `read` to read and fetch, `admin` to
171
+ * change or delete the repo, set its private refs or lift its file rules on a push, and `write`
172
+ * for the rest, a push, its objects, a fork's sync and the merge queue among them.
173
+ */
174
+ const gitNeeds = (kind: Operation['kind']): RepoRole => {
175
+ switch (kind) {
176
+ case 'read':
177
+ case 'fetch':
178
+ return 'read';
179
+ case 'repo.update':
180
+ case 'repo.delete':
181
+ case 'refs.private':
182
+ case 'files.allow':
183
+ return 'admin';
184
+ default:
185
+ return 'write';
186
+ }
187
+ };
188
+
126
189
  /** What anyone signed in may do on a public repo they don't own, as on GitHub. */
127
190
  const contributions: ReadonlySet<string> = new Set([
128
191
  'issue.create',
@@ -147,10 +210,20 @@ const authorsOwn: ReadonlySet<string> = new Set([
147
210
  'review.resolve',
148
211
  ]);
149
212
 
150
- const signInFirstRefusal = { message: 'Sign in first' };
213
+ /**
214
+ * The role a grant needs for one of the drop-in's operations (#1998): `read` for a read and for
215
+ * what anyone signed in may do on a public repo, `admin` for a hook's and for a change to the
216
+ * repo's collaborators, and `write` for the rest.
217
+ */
218
+ const gitHubNeeds = (kind: GitHubRepoOperation['kind']): RepoRole => {
219
+ if (kind.startsWith('hook.')) return 'admin';
151
220
 
152
- /** `signInFirstRefusal`, as the drop-in's half answers it. */
153
- const signInFirst = reject(signInFirstRefusal);
221
+ if (kind.startsWith('collaborator.') && kind !== 'collaborator.read') return 'admin';
222
+
223
+ return kind.endsWith('.read') || contributions.has(kind) ? 'read' : 'write';
224
+ };
225
+
226
+ const signInFirstRefusal = { message: 'Sign in first' };
154
227
 
155
228
  /**
156
229
  * The owner a token reaches past its subject's own login, if any: a repo token's repo's owner, by
@@ -179,21 +252,24 @@ const orgCoverage = (operation: TokenOperation) => {
179
252
  return org !== '' && !org.includes('/') ? org : undefined;
180
253
  };
181
254
 
182
- /** Who a call's token holds for, a token's caller or the host's own sign-in. */
255
+ /**
256
+ * Who a call's token holds for, a token's caller or the host's own sign-in, and the repo the call
257
+ * names, when it names one.
258
+ */
183
259
  interface Holder {
184
260
  readonly actor: AuthorizeInput['actor'] | Caller['actor'];
185
261
  readonly subject?: ActorId;
186
262
  readonly token?: TokenInfo;
263
+ readonly repo?: RepoAccess | undefined;
187
264
  }
188
265
 
189
266
  /**
190
267
  * The refusal of a token whose person left the org it's over, or joined it again after it was
191
268
  * made: it works for nothing, public reads included, as GitHub's inactive token (Q740).
192
269
  */
193
- const tokenEndedRefusal = { message: "This token's person no longer holds the org it's over" };
194
-
195
- /** `tokenEndedRefusal`, as the drop-in's half answers it. */
196
- const tokenEnded = reject(tokenEndedRefusal);
270
+ const tokenEndedRefusal = {
271
+ message: "This token's person no longer holds the org or the shared repo it's over",
272
+ };
197
273
 
198
274
  /** At most `most` of an owner's private repos the caller may read, as the owner index asks. */
199
275
  export type ReadableRepos = (
@@ -211,15 +287,20 @@ export interface HostPolicy {
211
287
  readonly readableRepos: ReadableRepos;
212
288
  }
213
289
 
290
+ /** The host's policy over its repos' grants, with the drop-in's `RepoGrants` over the same rows. */
291
+ export interface GrantsPolicy extends HostPolicy {
292
+ readonly repoGrants: RepoGrants.RepoGrants['Service'];
293
+ }
294
+
214
295
  /**
215
296
  * At most `most` of the private repos under `prefix`, and whether it holds more past them, listed
216
- * through `host`, the drop-in's unchecked `repos.list` (Q788).
297
+ * through Git's unchecked `repos.list`, as `Git.host` calls it (Q788). It runs in the search's
298
+ * fiber, so its store calls count on the search's request, and interrupting the search stops it.
217
299
  */
218
300
  export type PrivateRepos = (
219
301
  prefix: string,
220
302
  most: number,
221
- host: GitHubReadableReposInput['host'],
222
- ) => Promise<{ readonly names: readonly RepoName[]; readonly more: boolean }>;
303
+ ) => Effect.Effect<{ readonly names: readonly RepoName[]; readonly more: boolean }, GitError>;
223
304
 
224
305
  /**
225
306
  * Where the drop-in's people find an uploaded picture (Q757): the app's origin, whose
@@ -230,22 +311,37 @@ export interface Pictures {
230
311
  readonly avatars: Pick<Avatars, 'hash'>;
231
312
  }
232
313
 
233
- /** The name `internalId`'s repo has now, `undefined` once none holds it. */
314
+ /**
315
+ * The internal ID of the repo named `name` now, `undefined` when none is, read in the caller's
316
+ * fiber, so its store calls count on the caller's request.
317
+ */
318
+ export type RepoIdOf = (name: RepoName) => Effect.Effect<InternalRepoId | undefined, GitError>;
319
+
320
+ /**
321
+ * The name `internalId`'s repo has now, `undefined` once none holds it, read in the caller's
322
+ * fiber, so its store calls count on the caller's request.
323
+ */
234
324
  export type RepoNameOf = (
235
325
  internalId: InternalRepoId,
236
- signal: AbortSignal,
237
- ) => Promise<RepoName | undefined>;
326
+ ) => Effect.Effect<RepoName | undefined, GitError>;
238
327
 
239
328
  /**
240
329
  * What the starter's policy reads past its orgs: its `Pictures`, both or neither,
241
- * `privateRepos`, which lists an owner's private repos for the owner index's hook, and
242
- * `repoName`, which the drop-in's owner check reads. With no `privateRepos`, it names no private
243
- * repo, and with no `repoName`, the owner check reads the name the request named.
330
+ * `privateRepos`, which lists an owner's private repos for the owner index's hook, `repoName`,
331
+ * which the drop-in's owner check reads, and `grants`, its repos' grants to people (#1998), with
332
+ * `repoId`, which finds the repo a token over one names. With no `privateRepos`, it names no
333
+ * private repo; with no `repoName`, the owner check reads the name the request named; and with no
334
+ * `grants`, only an owner reaches a private repo.
244
335
  */
245
336
  export type HostPolicyOptions = (
246
337
  | Pictures
247
338
  | { readonly origin?: undefined; readonly avatars?: undefined }
248
- ) & { readonly privateRepos?: PrivateRepos; readonly repoName?: RepoNameOf };
339
+ ) & {
340
+ readonly privateRepos?: PrivateRepos;
341
+ readonly repoName?: RepoNameOf;
342
+ readonly grants?: Grants;
343
+ readonly repoId?: RepoIdOf;
344
+ };
249
345
 
250
346
  /**
251
347
  * What of `owner`'s repos `token` covers: every one, those under the token's longer prefix, its
@@ -263,16 +359,31 @@ const coverOf = (owner: string, token: GitHubReadableReposInput['token']) => {
263
359
  return token.prefix.startsWith(under) ? { prefix: token.prefix } : undefined;
264
360
  };
265
361
 
362
+ /** What of an owner's repos a token covers, as `coverOf` answers it. */
363
+ type Cover = NonNullable<ReturnType<typeof coverOf>>;
364
+
365
+ /** Whether `cover` covers the repo `name`. */
366
+ const covers = (cover: Cover, name: RepoName) =>
367
+ cover.repo === undefined ? name.startsWith(cover.prefix) : name === cover.repo;
368
+
266
369
  /**
267
- * The starter's policy over `orgs`: who owns what, by an org's `member` rows, and the drop-in's
268
- * directory of people and orgs, by Better Auth's tables, with what `options` adds.
370
+ * The starter's policy over `orgs`: who owns what, by an org's `member` rows, and who else may do
371
+ * what on a repo, by its grants, and the drop-in's directory of people and orgs, by Better Auth's
372
+ * tables, with what `options` adds. With `grants`, it answers the drop-in's `RepoGrants` too.
269
373
  */
270
- export const hostPolicy = (orgs: Orgs, options: HostPolicyOptions = {}): HostPolicy => {
374
+ export function hostPolicy(
375
+ orgs: Orgs,
376
+ options: HostPolicyOptions & { readonly grants: Grants },
377
+ ): GrantsPolicy;
378
+ export function hostPolicy(orgs: Orgs, options?: HostPolicyOptions): HostPolicy;
379
+ export function hostPolicy(orgs: Orgs, options: HostPolicyOptions = {}): HostPolicy | GrantsPolicy {
271
380
  const {
272
381
  origin,
273
382
  avatars,
274
- privateRepos = async () => ({ names: [], more: false }),
383
+ privateRepos = () => Effect.succeed({ names: [], more: false }),
275
384
  repoName,
385
+ grants,
386
+ repoId,
276
387
  } = options;
277
388
 
278
389
  /**
@@ -288,6 +399,21 @@ export const hostPolicy = (orgs: Orgs, options: HostPolicyOptions = {}): HostPol
288
399
  return { avatar_url: `${origin}/icons/${encodeURIComponent(login)}?v=${hash}` };
289
400
  };
290
401
 
402
+ /**
403
+ * A person who signed up, under the login they hold however `login` spells it, by the name they
404
+ * go by, as GitHub names someone who set one, so a merge the drop-in makes for them is authored
405
+ * under it, and with their upload's `avatar_url`. None for a login no one signed up as.
406
+ */
407
+ const personShown = Effect.fnUntraced(function* (login: string) {
408
+ const person = yield* read(() => orgs.person(login));
409
+ if (person === undefined) return undefined;
410
+
411
+ const found = yield* defaultUser(person.login);
412
+ const picture = yield* read(() => pictureOf(person.login, { kind: 'user', id: person.id }));
413
+
414
+ return { ...found, name: person.name, ...picture } satisfies GitHubUser;
415
+ });
416
+
291
417
  /** An org as `defaultOrg` makes it, with its upload's `avatar_url` when it has one. */
292
418
  const orgOf = Effect.fnUntraced(function* (login: string) {
293
419
  const org = yield* defaultOrg(login);
@@ -297,28 +423,66 @@ export const hostPolicy = (orgs: Orgs, options: HostPolicyOptions = {}): HostPol
297
423
  });
298
424
 
299
425
  /**
300
- * Each org's membership of an actor, kept on the actor Git's `identify` made for the request, so
301
- * a page that checks 32 of an org's repos asks D1 once (Q739), and for `membershipLife` at most.
302
- * `identify` and `appAuth` make a new actor for each request, but a stream holds one caller for
303
- * as long as it's open and asks `authorize` again before each batch and each heartbeat, so an
304
- * answer kept for the request alone would let a member who left go on reading it (Q740).
426
+ * `ask`'s answer about `actor` under `key`, kept on the actor Git's `identify` made for the
427
+ * request, so a page that checks 32 of an org's repos asks D1 once (Q739), and for
428
+ * `membershipLife` at most. `identify` and `appAuth` make a new actor for each request, but a
429
+ * stream holds one caller for as long as it's open and asks `authorize` again before each batch
430
+ * and each heartbeat, so an answer kept for the request alone would let a member who left, or a
431
+ * collaborator removed, go on reading it (Q740).
305
432
  */
306
- const memberships = new WeakMap<ActorId, Map<string, KeptMembership>>();
433
+ const keptOn = <A>(
434
+ kept: WeakMap<ActorId, Map<string, Kept<A>>>,
435
+ actor: ActorId,
436
+ key: string,
437
+ ask: () => Promise<A>,
438
+ ) =>
439
+ Effect.flatMap(Clock.currentTimeMillis, (now) => {
440
+ const held = kept.get(actor) ?? new Map<string, Kept<A>>();
441
+ kept.set(actor, held);
442
+
443
+ const found = held.get(key);
444
+ if (found !== undefined && now - found.at < membershipLife) return read(() => found.answer);
445
+
446
+ const answer = ask();
447
+ held.set(key, { at: now, answer });
448
+
449
+ return read(() => answer);
450
+ });
451
+
452
+ const memberships = new WeakMap<ActorId, Map<string, Kept<Membership | undefined>>>();
307
453
 
308
454
  /** `actor`'s membership of the org `org`, asked once a request, and again past its life. */
309
455
  const membershipOf = (actor: ActorId, org: string) =>
310
- Effect.flatMap(Clock.currentTimeMillis, (now) => {
311
- const held = memberships.get(actor) ?? new Map<string, KeptMembership>();
312
- memberships.set(actor, held);
456
+ keptOn(memberships, actor, org, () => orgs.membership(org, actor.id));
457
+
458
+ const keptGrants = new WeakMap<ActorId, Map<string, Kept<Grant | undefined>>>();
313
459
 
314
- const kept = held.get(org);
315
- if (kept !== undefined && now - kept.at < membershipLife) return read(() => kept.answer);
460
+ /**
461
+ * `actor`'s grant on the repo `internalId`, kept as a membership is, so a stream's next check
462
+ * stops a collaborator whose grant was removed. None on a host with no grants.
463
+ */
464
+ const grantOf = (actor: ActorId, internalId: InternalRepoId) => {
465
+ if (grants === undefined) return Effect.succeed(undefined);
316
466
 
317
- const answer = orgs.membership(org, actor.id);
318
- held.set(org, { at: now, answer });
467
+ return keptOn(keptGrants, actor, internalId, async () => {
468
+ const person = await orgs.person(actor.id);
319
469
 
320
- return read(() => answer);
470
+ return person === undefined ? undefined : grants.get(internalId, person.id);
321
471
  });
472
+ };
473
+
474
+ /**
475
+ * The internal ID of the repo `name`: `access`'s, when that's the repo the call names, or else
476
+ * `repoId`'s read of it. None with no `repoId`.
477
+ */
478
+ const repoIdOf = (
479
+ name: RepoName,
480
+ access: RepoAccess | undefined,
481
+ ): Effect.Effect<InternalRepoId | undefined, GitError> => {
482
+ if (access?.name === name) return Effect.succeed(access.internalId);
483
+
484
+ return repoId === undefined ? Effect.succeed(undefined) : repoId(name);
485
+ };
322
486
 
323
487
  /** Whether `actor` belongs to the org `org`, in any role. */
324
488
  const belongs = (actor: ActorId, org: string) =>
@@ -326,12 +490,14 @@ export const hostPolicy = (orgs: Orgs, options: HostPolicyOptions = {}): HostPol
326
490
 
327
491
  /**
328
492
  * Whether a call's token holds (Q740): one over an owner other than its subject holds only while
329
- * the subject belongs to that owner, by a membership no newer than the token, so a leave refuses
330
- * it at once and a rejoin doesn't bring it back. Within the second of a join, as the same
331
- * Worker's clock reads both, a token made then holds. A session's token, the host's own sign-in
332
- * and a token over the subject's own login hold as they are.
493
+ * the subject belongs to that owner, by a membership no newer than the token, or, over one repo,
494
+ * while the subject holds a grant on it no newer than the token (#1998). So a leave or a grant's
495
+ * removal refuses it at once, and a rejoin or a grant made again doesn't bring it back. Within
496
+ * the second of a join or a grant, as the same Worker's clock reads both, a token made then
497
+ * holds. A session's token, the host's own sign-in and a token over the subject's own login hold
498
+ * as they are.
333
499
  */
334
- const tokenHolds = Effect.fnUntraced(function* ({ actor, subject, token }: Holder) {
500
+ const tokenHolds = Effect.fnUntraced(function* ({ actor, subject, token, repo }: Holder) {
335
501
  if (token === undefined || actor.anonymous === true) return true;
336
502
 
337
503
  const person = subject !== undefined && subject.id !== actor.id ? subject : actor;
@@ -339,8 +505,14 @@ export const hostPolicy = (orgs: Orgs, options: HostPolicyOptions = {}): HostPol
339
505
  if (owner === undefined || owner === person.id) return true;
340
506
 
341
507
  const membership = yield* membershipOf(person, owner);
508
+ if (membership !== undefined && membership.since <= token.createdAt) return true;
509
+
510
+ if (token.repo === undefined) return false;
342
511
 
343
- return membership !== undefined && membership.since <= token.createdAt;
512
+ const internalId = yield* repoIdOf(token.repo, repo);
513
+ const grant = internalId === undefined ? undefined : yield* grantOf(person, internalId);
514
+
515
+ return grant !== undefined && grant.createdAt <= token.createdAt;
344
516
  });
345
517
 
346
518
  /** Whether `actor` owns what `owner` holds: their own login's, and each org's they belong to. */
@@ -363,7 +535,7 @@ export const hostPolicy = (orgs: Orgs, options: HostPolicyOptions = {}): HostPol
363
535
  const { repo } = input;
364
536
  if (repo === undefined) return undefined;
365
537
 
366
- const now = yield* read(() => repoName(repo.internalId, input.signal));
538
+ const now = yield* repoName(repo.internalId);
367
539
 
368
540
  return now === undefined ? undefined : ownerOf(now);
369
541
  });
@@ -378,7 +550,7 @@ export const hostPolicy = (orgs: Orgs, options: HostPolicyOptions = {}): HostPol
378
550
  ) {
379
551
  const { caller, operation, repo } = input;
380
552
  const { actor } = caller;
381
- if (!(yield* tokenHolds(caller))) return yield* Policy.reject(tokenEndedRefusal);
553
+ if (!(yield* tokenHolds({ ...caller, repo }))) return yield* Policy.reject(tokenEndedRefusal);
382
554
 
383
555
  // Who the caller is, at Git's `/me`: theirs, once their token holds.
384
556
  if (operation.kind === 'me') {
@@ -436,7 +608,7 @@ export const hostPolicy = (orgs: Orgs, options: HostPolicyOptions = {}): HostPol
436
608
  operation.kind === 'token.list' ||
437
609
  operation.kind === 'token.revoke'
438
610
  ) {
439
- return yield* tokenRule(actor, operation);
611
+ return yield* tokenRule(actor, operation, repo);
440
612
  }
441
613
 
442
614
  // A fork lands under its holder, from any repo the person may read.
@@ -446,9 +618,16 @@ export const hostPolicy = (orgs: Orgs, options: HostPolicyOptions = {}): HostPol
446
618
  : Policy.reject({ message: 'Fork under your own login' });
447
619
  }
448
620
 
449
- const mine = 'repo' in operation && (yield* owns(actor, ownerOf(operation.repo)));
621
+ if (!('repo' in operation)) return yield* Policy.reject({ message: 'Not yours' });
622
+
623
+ if (yield* owns(actor, ownerOf(operation.repo))) return yield* Policy.accept();
450
624
 
451
- return yield* mine ? Policy.accept() : Policy.reject({ message: 'Not yours' });
625
+ // A grant decides by the repo the call acts on, by its internal ID, so a rename keeps it.
626
+ const grant = repo === undefined ? undefined : yield* grantOf(actor, repo.internalId);
627
+
628
+ return yield* roleHolds(grant?.role, gitNeeds(operation.kind))
629
+ ? Policy.accept()
630
+ : Policy.reject({ message: 'Not yours' });
452
631
  });
453
632
 
454
633
  /**
@@ -459,12 +638,14 @@ export const hostPolicy = (orgs: Orgs, options: HostPolicyOptions = {}): HostPol
459
638
  * a revoke of another's token there, which is theirs (Q521);
460
639
  * - a create over an org they belong to at that moment: all its repos, `<org>/`, or one of
461
640
  * them, never a deeper prefix;
641
+ * - a create over one repo they hold a grant on, in any role (#1998);
462
642
  * - a list, or a revoke of another's token, over an org they're an owner or an admin of,
463
643
  * read at this call.
464
644
  */
465
645
  const tokenRule = Effect.fnUntraced(function* (
466
646
  actor: NamedActor | ActorId,
467
647
  operation: TokenOperation,
648
+ access: RepoAccess | undefined,
468
649
  ) {
469
650
  if (operation.kind === 'token.revoke' && operation.subject.id === actor.id) {
470
651
  return yield* Policy.accept();
@@ -480,9 +661,16 @@ export const hostPolicy = (orgs: Orgs, options: HostPolicyOptions = {}): HostPol
480
661
  const membership = org === undefined ? undefined : yield* membershipOf(actor, org);
481
662
 
482
663
  if (operation.kind === 'token.create') {
483
- return yield* membership === undefined
664
+ if (membership !== undefined) return yield* Policy.accept();
665
+
666
+ const internalId =
667
+ operation.repo === undefined ? undefined : yield* repoIdOf(operation.repo, access);
668
+ const grant = internalId === undefined ? undefined : yield* grantOf(actor, internalId);
669
+
670
+ return yield* grant === undefined
484
671
  ? Policy.reject({
485
- message: 'Only tokens over your own repos, or an org you belong to',
672
+ message:
673
+ 'Only tokens over your own repos, an org you belong to, or a repo shared with you',
486
674
  hint: "Over an org, a token covers all of its repos or one: '<org>/' or '<org>/<repo>'",
487
675
  })
488
676
  : Policy.accept();
@@ -502,24 +690,25 @@ export const hostPolicy = (orgs: Orgs, options: HostPolicyOptions = {}): HostPol
502
690
  * author's alone, as on GitHub: not the owner's, nor the admin's unless they wrote it. An org's members, teams and hooks are its
503
691
  * own members' alone, as GitHub shows outsiders only public members and this host keeps none
504
692
  * public (#644). A person's own account, such as their notifications, is theirs once signed in.
505
- * Everything else is the owner's and the admin's. A token that no longer holds gets nothing, as
506
- * in Git's, its own account included.
693
+ * Everything else is the owner's and the admin's, and a collaborator's whose role holds it
694
+ * (#1998). A token that no longer holds gets nothing, as in Git's, its own account included.
507
695
  */
508
696
  const authorizeGitHub: GitHubAuthorize = Effect.fnUntraced(function* (
509
697
  input: GitHubAuthorization,
510
698
  ) {
511
699
  const { actor, operation } = input;
512
- if (!(yield* tokenHolds(input))) return tokenEnded;
700
+ if (!(yield* tokenHolds(input))) return yield* Policy.reject(tokenEndedRefusal);
513
701
 
514
702
  if ('org' in operation) {
515
- if (actor.anonymous === true) return signInFirst;
703
+ if (actor.anonymous === true) return yield* Policy.reject(signInFirstRefusal);
516
704
 
517
- return actor.id === admin.id || (yield* owns(actor, operation.org))
518
- ? accept()
519
- : reject({ message: "Only the org's members may do this" });
705
+ return yield* actor.id === admin.id || (yield* owns(actor, operation.org))
706
+ ? Policy.accept()
707
+ : Policy.reject({ message: "Only the org's members may do this" });
520
708
  }
521
709
 
522
- if (!('repo' in operation)) return actor.anonymous === true ? signInFirst : accept();
710
+ if (!('repo' in operation))
711
+ return yield* actor.anonymous === true ? Policy.reject(signInFirstRefusal) : Policy.accept();
523
712
 
524
713
  // Visibility as Git's read of the repo answered this caller. A repo operation that reaches
525
714
  // here without it is refused as a private repo's, for a caller outside TypeScript.
@@ -527,53 +716,88 @@ export const hostPolicy = (orgs: Orgs, options: HostPolicyOptions = {}): HostPol
527
716
  // A hook's settings and deliveries are its owner's, even on a public repo: they hold payloads
528
717
  // signed for its receiver, and the receiver's answers.
529
718
  const ownersOnly = operation.kind.startsWith('hook.');
530
- if (visible && operation.kind.endsWith('.read') && !ownersOnly) return accept();
719
+ if (visible && operation.kind.endsWith('.read') && !ownersOnly) return yield* Policy.accept();
531
720
 
532
- if (actor.anonymous === true) return signInFirst;
721
+ if (actor.anonymous === true) return yield* Policy.reject(signInFirstRefusal);
533
722
 
534
723
  // GitHub lets only a review's author edit its body, so no one else does here, the admin
535
724
  // included: the drop-in asks it as the author only once its own read shows the caller wrote it.
536
725
  if (operation.kind === 'review.edit') {
537
- return operation.asAuthor === true
538
- ? accept()
539
- : reject({ message: "Only a review's author can edit its body" });
726
+ return yield* operation.asAuthor === true
727
+ ? Policy.accept()
728
+ : Policy.reject({ message: "Only a review's author can edit its body" });
540
729
  }
541
730
 
542
- if (actor.id === admin.id || (visible && contributions.has(operation.kind))) return accept();
731
+ if (actor.id === admin.id || (visible && contributions.has(operation.kind)))
732
+ return yield* Policy.accept();
543
733
 
544
734
  // The drop-in asks an issue's edit, or a comment's or a review comment's edit or delete, as its
545
735
  // author only once its own read shows the caller wrote it (Q857), so the caller's token
546
736
  // holding the repo is all that's left to ask.
547
- if (operation.asAuthor === true && authorsOwn.has(operation.kind)) return accept();
737
+ if (operation.asAuthor === true && authorsOwn.has(operation.kind))
738
+ return yield* Policy.accept();
739
+
740
+ if (yield* owns(actor, yield* ownerNow(operation.repo, input))) return yield* Policy.accept();
741
+
742
+ const grant =
743
+ input.repo === undefined ? undefined : yield* grantOf(actor, input.repo.internalId);
744
+
745
+ return yield* roleHolds(grant?.role, gitHubNeeds(operation.kind))
746
+ ? Policy.accept()
747
+ : Policy.reject({ message: 'Only the owner may do this' });
748
+ });
749
+
750
+ /**
751
+ * The repos shared with `actor` that `cover` covers, at most `most`, by their names now, from the
752
+ * first of their grants: only as many as the hook's `most` and 6 store calls name, at
753
+ * `nameReads` a name, and `more` once others are left unread.
754
+ */
755
+ const sharedNames = Effect.fnUntraced(function* (actor: ActorId, cover: Cover, most: number) {
756
+ const none = { names: [], more: false };
757
+ if (grants === undefined || repoName === undefined) return none;
758
+
759
+ const person = yield* read(() => orgs.person(actor.id));
760
+ if (person === undefined) return none;
548
761
 
549
- return (yield* owns(actor, yield* ownerNow(operation.repo, input)))
550
- ? accept()
551
- : reject({ message: 'Only the owner may do this' });
762
+ const named = Math.min(grantsPage, Math.floor((most + ownerCallsPast) / nameReads));
763
+ const page = yield* read(() => grants.shared(person.id, { limit: named }));
764
+ const names = yield* Effect.forEach(page.items, repoName, { concurrency: 8 });
765
+ const covered = names.filter(
766
+ (name): name is RepoName => name !== undefined && covers(cover, name),
767
+ );
768
+
769
+ return {
770
+ names: covered.slice(0, most),
771
+ more: covered.length > most || page.next !== undefined,
772
+ };
552
773
  });
553
774
 
554
775
  /**
555
776
  * The owner index's hook: every private repo under `owner` for its owner, a person's own login
556
- * or an org they belong to, and the admin, as `authorizeGitHub` lets them read it; none for
557
- * anyone else. Only those the caller's token covers, listed under what it covers alone, so a
558
- * token's own repos get the checks, and neither the names nor `more` show how many it can't read.
559
- * A token over one repo names it alone, which `authorize` holds as any name. Git lists them newest
560
- * made first, since it keeps no time a repo was updated.
777
+ * or an org they belong to, and the admin, as `authorizeGitHub` lets them read it; for anyone
778
+ * else, those of the owner's repos shared with them (#1998). Only those the caller's token
779
+ * covers, listed under what it covers alone, so a token's own repos get the checks, and neither
780
+ * the names nor `more` show how many it can't read. A token over one repo names it alone, which
781
+ * `authorize` holds as any name. Git lists them newest made first, since it keeps no time a repo
782
+ * was updated.
561
783
  */
562
784
  const readableRepos: ReadableRepos = Effect.fnUntraced(function* (input) {
563
- const { actor, owner, most, token, host } = input;
785
+ const { actor, owner, most, token } = input;
564
786
  const none = { names: [], more: false };
565
787
  if (actor.anonymous === true || !(yield* tokenHolds(input))) return none;
566
788
 
567
- if (actor.id !== admin.id && !(yield* owns(actor, owner))) return none;
568
-
569
789
  const cover = coverOf(owner, token);
570
790
  if (cover === undefined || most <= 0) return none;
571
791
 
792
+ if (actor.id !== admin.id && !(yield* owns(actor, owner))) {
793
+ return yield* sharedNames(actor, cover, most);
794
+ }
795
+
572
796
  if (cover.repo !== undefined) return { names: [cover.repo], more: false };
573
797
 
574
798
  const { prefix } = cover;
575
799
 
576
- return yield* read(() => privateRepos(prefix, most, host));
800
+ return yield* privateRepos(prefix, most);
577
801
  });
578
802
 
579
803
  /**
@@ -581,25 +805,14 @@ export const hostPolicy = (orgs: Orgs, options: HostPolicyOptions = {}): HostPol
581
805
  * once it's made, and a person's orgs, an org's members and which of some logins are its members
582
806
  * from `member` rows, each shaped as the drop-in's default shapes it: a person through its
583
807
  * calls, and an org as `defaultOrg` makes it, repos or none, as anyone may see a person who
584
- * signed up. The starter keeps no two-factor record, so a two-factor filter lists no one, and
808
+ * signed up. A login or an org asked in any case answers under the one it's held as, as GitHub
809
+ * does (Q853). The starter keeps no two-factor record, so a two-factor filter lists no one, and
585
810
  * teams are the default's: none.
586
811
  */
587
812
  const people: PeopleFor = (context) => {
588
813
  const base = defaultPeople(context);
589
814
 
590
- // A person by the name they go by, as GitHub names someone who set one: a merge the drop-in
591
- // makes for them is authored under it.
592
- const user = Effect.fnUntraced(function* (input: PeopleUser) {
593
- const person = yield* read(() => orgs.person(input.login));
594
- if (person === undefined) return undefined;
595
-
596
- const found = yield* base(input);
597
- if (found === undefined) return undefined;
598
-
599
- const picture = yield* read(() => pictureOf(input.login, { kind: 'user', id: person.id }));
600
-
601
- return { ...found, name: person.name, ...picture } satisfies GitHubUser;
602
- });
815
+ const user = (input: PeopleUser) => personShown(input.login);
603
816
 
604
817
  // A login's commit addresses, the default's, for a person the app holds: none for a login
605
818
  // with no account, as its user lookup answers none.
@@ -607,11 +820,37 @@ export const hostPolicy = (orgs: Orgs, options: HostPolicyOptions = {}): HostPol
607
820
  const person = yield* read(() => orgs.person(input.login));
608
821
  if (person === undefined) return [];
609
822
 
610
- return yield* base(input);
823
+ return yield* base({ ...input, login: person.login });
824
+ });
825
+
826
+ // The default's accounts for a page's addresses, for a person the app holds, as its user
827
+ // lookup answers them: by the name they go by and their picture, and none for a login with no
828
+ // account. Each login is looked up once, however many addresses name it.
829
+ const accounts = Effect.fnUntraced(function* (input: PeopleAccounts) {
830
+ const found = Object.entries(yield* base(input));
831
+ const logins = [...new Set(found.map(([, account]) => account.login))];
832
+ const users = new Map(
833
+ yield* Effect.forEach(
834
+ logins,
835
+ (login) =>
836
+ Effect.map(user({ ...input, kind: 'user', login }), (each) => [login, each] as const),
837
+ { concurrency: 'unbounded' },
838
+ ),
839
+ );
840
+
841
+ return Object.fromEntries(
842
+ found.flatMap(([email, account]) => {
843
+ const each = users.get(account.login);
844
+
845
+ return each === undefined ? [] : [[email, each] as const];
846
+ }),
847
+ ) satisfies PeopleAccountsAnswer;
611
848
  });
612
849
 
613
850
  const org = Effect.fnUntraced(function* (input: PeopleOrg) {
614
- return (yield* read(() => orgs.org(input.org))) ? yield* orgOf(input.org) : undefined;
851
+ const slug = yield* read(() => orgs.org(input.org));
852
+
853
+ return slug === undefined ? undefined : yield* orgOf(slug);
615
854
  });
616
855
 
617
856
  const members = Effect.fnUntraced(function* (input: PeopleMembers) {
@@ -673,6 +912,8 @@ export const hostPolicy = (orgs: Orgs, options: HostPolicyOptions = {}): HostPol
673
912
  return members(input);
674
913
  case 'emails':
675
914
  return emails(input);
915
+ case 'accounts':
916
+ return accounts(input);
676
917
  case 'membership':
677
918
  return read(() => orgs.among(input.org, input.logins));
678
919
  case 'team-members':
@@ -694,6 +935,7 @@ export const hostPolicy = (orgs: Orgs, options: HostPolicyOptions = {}): HostPol
694
935
  ): Effect.Effect<Listed<GitHubUser>, GitError>;
695
936
  function lookup(input: PeopleTeams): Effect.Effect<Listed<GitHubTeam>, GitError>;
696
937
  function lookup(input: PeopleEmails): Effect.Effect<readonly string[], GitError>;
938
+ function lookup(input: PeopleAccounts): Effect.Effect<PeopleAccountsAnswer, GitError>;
697
939
  function lookup(input: PeopleMembership): Effect.Effect<readonly string[], GitError>;
698
940
  function lookup(input: PeopleTeam): Effect.Effect<GitHubTeam | undefined, GitError>;
699
941
  function lookup(input: PeopleSearch): Effect.Effect<PeopleSearchAnswer, GitError>;
@@ -708,7 +950,156 @@ export const hostPolicy = (orgs: Orgs, options: HostPolicyOptions = {}): HostPol
708
950
  return People.make(lookup);
709
951
  };
710
952
 
711
- return { authorize, authorizeGitHub, people, readableRepos };
953
+ const policy = { authorize, authorizeGitHub, people, readableRepos };
954
+
955
+ return grants === undefined
956
+ ? policy
957
+ : { ...policy, repoGrants: repoGrantsOver(orgs, grants, personShown, repoName) };
958
+ }
959
+
960
+ /** A refusal of a team's grant: the starter has no teams. */
961
+ const noTeam = () => gitError('NOT_FOUND', 'This host has no teams');
962
+
963
+ /** A refusal of a login no one signed up as. */
964
+ const noPerson = (login: string) => gitError('NOT_FOUND', `No one signed up as ${login}`);
965
+
966
+ /**
967
+ * The starter's `RepoGrants` (#1998), over `grants`, the rows its policy reads, so the drop-in's
968
+ * collaborators' routes show and change what decides: people's grants alone, as the starter has
969
+ * no teams, so a repo's teams list none, a team's grant is refused as no such team, and a move
970
+ * drops none, however often it's asked. The repo's owner, a person or any member of its org, is
971
+ * its admin, and can't be its collaborator too. A person is shown as the policy's directory shows
972
+ * them, and a shared repo by the name `repoName` reads now, leaving out a repo none holds.
973
+ */
974
+ const repoGrantsOver = (
975
+ orgs: Orgs,
976
+ grants: Grants,
977
+ personShown: (login: string) => Effect.Effect<GitHubUser | undefined, GitError>,
978
+ repoName: RepoNameOf | undefined,
979
+ ): RepoGrants.RepoGrants['Service'] => {
980
+ const personOf = (login: string) => read(() => orgs.person(login));
981
+
982
+ const listPeople = Effect.fnUntraced(function* (input: RepoGrantsList) {
983
+ const page = yield* read(() =>
984
+ grants.list(input.repo.internalId, { after: input.page?.after, limit: input.page?.limit }),
985
+ );
986
+ const logins = yield* read(() => orgs.loginsById(page.items.map((row) => row.userId)));
987
+ const found = yield* Effect.forEach(
988
+ page.items,
989
+ ({ userId, role }) => {
990
+ const login = logins.get(userId);
991
+ if (login === undefined) return Effect.succeed(undefined);
992
+
993
+ return Effect.map(personShown(login), (user) =>
994
+ user === undefined ? undefined : ({ user, role } satisfies PersonGrant),
995
+ );
996
+ },
997
+ { concurrency: 8 },
998
+ );
999
+ const items = found.filter((each) => each !== undefined);
1000
+
1001
+ return (
1002
+ page.next === undefined ? { items } : { items, next: page.next }
1003
+ ) satisfies Listed<PersonGrant>;
1004
+ });
1005
+
1006
+ const roleOf = Effect.fnUntraced(function* (input: RepoGrantsRole) {
1007
+ const owner = ownerOf(input.repo.name);
1008
+ if (owner === input.login) return 'admin' satisfies RepoRole;
1009
+
1010
+ const person = yield* personOf(input.login);
1011
+ if (person === undefined || owner === undefined) return undefined;
1012
+
1013
+ if (person.login === owner) return 'admin' satisfies RepoRole;
1014
+
1015
+ const membership = yield* read(() => orgs.membership(owner, person.login));
1016
+ if (membership !== undefined) return 'admin' satisfies RepoRole;
1017
+
1018
+ const grant = yield* read(() => grants.get(input.repo.internalId, person.id));
1019
+
1020
+ return grant?.role;
1021
+ });
1022
+
1023
+ const change = Effect.fnUntraced(function* (input: RepoGrantsPut | RepoGrantsRemove) {
1024
+ const { to } = input;
1025
+ if (to.kind === 'team') return yield* Effect.fail(noTeam());
1026
+
1027
+ const person = yield* personOf(to.login);
1028
+ if (person === undefined) return yield* Effect.fail(noPerson(to.login));
1029
+
1030
+ // The login the person holds, as `to.login` may name them in another case.
1031
+ if (input.kind === 'put' && person.login === ownerOf(input.repo.name)) {
1032
+ return yield* Effect.fail(
1033
+ gitError('INVALID_ARGUMENT', "A repo's owner is its admin, never its collaborator"),
1034
+ );
1035
+ }
1036
+
1037
+ const { internalId } = input.repo;
1038
+ if (input.kind === 'remove') return yield* read(() => grants.remove(internalId, person.id));
1039
+
1040
+ const put = yield* read(() => grants.put(internalId, person.id, input.role));
1041
+
1042
+ if (put === 'full') {
1043
+ return yield* Effect.fail(
1044
+ gitError('RESOURCE_EXHAUSTED', `A repo holds at most ${repoGrantsMost} grants`),
1045
+ );
1046
+ }
1047
+ });
1048
+
1049
+ const shared = Effect.fnUntraced(function* (input: RepoGrantsShared) {
1050
+ const person = yield* personOf(input.login);
1051
+ if (person === undefined || repoName === undefined)
1052
+ return { items: [] } satisfies Listed<GrantRepo>;
1053
+
1054
+ const page = yield* read(() =>
1055
+ grants.shared(person.id, { after: input.page?.after, limit: input.page?.limit }),
1056
+ );
1057
+ const named = yield* Effect.forEach(
1058
+ page.items,
1059
+ (internalId) =>
1060
+ Effect.map(repoName(internalId), (name) =>
1061
+ name === undefined ? undefined : ({ name, internalId } satisfies GrantRepo),
1062
+ ),
1063
+ { concurrency: 8 },
1064
+ );
1065
+ const under = input.owner === undefined ? undefined : `${input.owner}/`;
1066
+ const items = named.filter(
1067
+ (repo): repo is GrantRepo =>
1068
+ repo !== undefined && (under === undefined || repo.name.startsWith(under)),
1069
+ );
1070
+
1071
+ return (
1072
+ page.next === undefined ? { items } : { items, next: page.next }
1073
+ ) satisfies Listed<GrantRepo>;
1074
+ });
1075
+
1076
+ function answer(
1077
+ input: RepoGrantsList & { readonly of: 'person' },
1078
+ ): Effect.Effect<Listed<PersonGrant>, GitError>;
1079
+ function answer(
1080
+ input: RepoGrantsList & { readonly of: 'team' },
1081
+ ): Effect.Effect<Listed<TeamGrant>, GitError>;
1082
+ function answer(input: RepoGrantsRole): Effect.Effect<RepoRole | undefined, GitError>;
1083
+ function answer(input: RepoGrantsPut | RepoGrantsRemove): Effect.Effect<void, GitError>;
1084
+ function answer(input: RepoGrantsShared): Effect.Effect<Listed<GrantRepo>, GitError>;
1085
+ function answer(input: RepoGrantsMoved): Effect.Effect<RepoGrantsMovedAnswer, GitError>;
1086
+ function answer(input: RepoGrantsInput): Effect.Effect<unknown, GitError> {
1087
+ switch (input.kind) {
1088
+ case 'list':
1089
+ return input.of === 'person' ? listPeople(input) : Effect.succeed({ items: [] });
1090
+ case 'role':
1091
+ return roleOf(input);
1092
+ case 'put':
1093
+ case 'remove':
1094
+ return change(input);
1095
+ case 'shared':
1096
+ return shared(input);
1097
+ case 'moved':
1098
+ return Effect.succeed({ dropped: [] } satisfies RepoGrantsMovedAnswer);
1099
+ }
1100
+ }
1101
+
1102
+ return RepoGrants.make(answer);
712
1103
  };
713
1104
 
714
1105
  /** One of the host's push rules, beside the refs it covers on every repo. */