| 1 | import type { Viewer } from "@g1t/contracts"; |
| 2 | |
| 3 | import type { ForkSource } from "../components/fork-bar"; |
| 4 | import { repoFor } from "./access.server"; |
| 5 | import { repos } from "./services.server"; |
| 6 | |
| 7 | /** |
| 8 | * The repository the one at `params` was forked from, for its fork bar |
| 9 | * (components/fork-bar.tsx), when it is a fork whose parent the viewer can |
| 10 | * read; null otherwise, or when either cannot be read just now. |
| 11 | */ |
| 12 | export async function forkSourceFor( |
| 13 | context: Parameters<typeof repoFor>[0], |
| 14 | params: { owner?: string; repo?: string }, |
| 15 | viewer: Viewer, |
| 16 | ): Promise<ForkSource | null> { |
| 17 | try { |
| 18 | const found = await repoFor(context, params); |
| 19 | if (!found.ok || !found.value.parent) return null; |
| 20 | const parent = await repos.get(found.value.parent, viewer); |
| 21 | if (!parent.ok) return null; |
| 22 | return { |
| 23 | namespace: parent.value.namespace, |
| 24 | name: parent.value.name, |
| 25 | defaultBranch: parent.value.defaultBranch, |
| 26 | forkDefault: found.value.defaultBranch, |
| 27 | }; |
| 28 | } catch { |
| 29 | return null; |
| 30 | } |
| 31 | } |