| 1 | /** |
| 2 | * Adds a starter CI workflow to a repository that has no checks, as a pull |
| 3 | * request opened by whoever asked, and goes to it. Posted by the "Add CI" |
| 4 | * prompt (components/add-ci.tsx). Anyone who can push may. |
| 5 | */ |
| 6 | import { redirect } from "react-router"; |
| 7 | |
| 8 | import type { Route } from "./+types/add-ci"; |
| 9 | import { addCi } from "../../lib/add-ci.server"; |
| 10 | import { refusal } from "../../lib/access.server"; |
| 11 | import { assertSameOrigin, requireUser } from "../../lib/session.server"; |
| 12 | |
| 13 | export async function action({ request, params, context }: Route.ActionArgs) { |
| 14 | assertSameOrigin(request); |
| 15 | const user = requireUser(context, request); |
| 16 | const refused = await refusal(context, params, "push"); |
| 17 | if (refused) return { error: refused }; |
| 18 | const added = await addCi(user, { namespace: params.owner, name: params.repo }); |
| 19 | if (!added.ok) return { error: added.message }; |
| 20 | throw redirect(`/${params.owner}/${params.repo}/pull/${added.value.number}`); |
| 21 | } |
| 22 | |
| 23 | export async function loader({ params }: Route.LoaderArgs) { |
| 24 | throw redirect(`/${params.owner}/${params.repo}/actions`); |
| 25 | } |