| 1 | /** |
| 2 | * Bringing a fork's branch up to date with its parent's, from the fork bar |
| 3 | * on its Code page (components/fork-bar.tsx). Write access on the fork. |
| 4 | */ |
| 5 | import { data } from "react-router"; |
| 6 | |
| 7 | import type { SyncedFork } from "@g1t/contracts"; |
| 8 | |
| 9 | import type { Route } from "./+types/sync-fork"; |
| 10 | import { repos } from "../../lib/services.server"; |
| 11 | import { assertSameOrigin, requireUser } from "../../lib/session.server"; |
| 12 | |
| 13 | export type SyncOutcome = { error: string | null; synced: SyncedFork | null }; |
| 14 | |
| 15 | export async function action({ request, params, context }: Route.ActionArgs) { |
| 16 | assertSameOrigin(request); |
| 17 | const user = requireUser(context, request); |
| 18 | const form = await request.formData(); |
| 19 | const branch = String(form.get("branch") ?? "").trim() || undefined; |
| 20 | try { |
| 21 | const synced = await repos.syncFork(user, { namespace: params.owner, name: params.repo }, branch); |
| 22 | if (!synced.ok) { |
| 23 | const status = synced.error.code === "not_found" ? 404 : synced.error.code === "forbidden" ? 403 : 409; |
| 24 | return data<SyncOutcome>({ error: synced.error.message, synced: null }, { status }); |
| 25 | } |
| 26 | return { error: null, synced: synced.value } satisfies SyncOutcome; |
| 27 | } catch { |
| 28 | return data<SyncOutcome>({ error: "That did not go through. Try again in a moment.", synced: null }, { status: 503 }); |
| 29 | } |
| 30 | } |