g1t/services/runner/src/surroundings.ts
| 1 | /** |
| 2 | * What an agent run may be told about the projects around its repository: |
| 3 | * only those the person it acts for can read. Someone who reads every |
| 4 | * repository in the workspace is told of all of them; anyone else (a member |
| 5 | * whose base permission is None, an outside collaborator) of the projects |
| 6 | * the projects service lists for them. |
| 7 | * |
| 8 | * Only type imports, so it can be tested on its own. |
| 9 | */ |
| 10 | |
| 11 | export type Neighbour = { slug: string; as: string | null }; |
| 12 | |
| 13 | export type ProjectSurroundings = { |
| 14 | slug: string; |
| 15 | name: string; |
| 16 | dependencies: { dependsOn: Neighbour[]; usedBy: Neighbour[] }; |
| 17 | }; |
| 18 | |
| 19 | /** `projects` with the neighbours not in `visible` left out; null `visible` reads everything. */ |
| 20 | export function readableSurroundings(projects: ProjectSurroundings[], visible: Set<string> | null): ProjectSurroundings[] { |
| 21 | if (!visible) return projects; |
| 22 | const keep = (list: Neighbour[]) => list.filter((neighbour) => visible.has(neighbour.slug.toLowerCase())); |
| 23 | return projects.map((project) => ({ |
| 24 | ...project, |
| 25 | dependencies: { dependsOn: keep(project.dependencies.dependsOn), usedBy: keep(project.dependencies.usedBy) }, |
| 26 | })); |
| 27 | } |