| 1 | import { GitBranch } from "lucide-react"; |
| 2 | import { Form, redirect } from "react-router"; |
| 3 | |
| 4 | import type { Route } from "./+types/pull-new"; |
| 5 | import { page } from "../../lib/meta"; |
| 6 | import { cloneUrl, useAddresses } from "../../lib/addresses"; |
| 7 | import { Combobox } from "../../components/ui/combobox"; |
| 8 | import { |
| 9 | SubmitButton, |
| 10 | CopyLine, |
| 11 | EmptyState, |
| 12 | ErrorText, |
| 13 | Field, |
| 14 | Input, |
| 15 | Textarea, |
| 16 | } from "../../components/ui"; |
| 17 | import { repos, work } from "../../lib/services.server"; |
| 18 | import { assertSameOrigin, requireUser, unwrap } from "../../lib/session.server"; |
| 19 | |
| 20 | export function meta({ params, ...args }: Route.MetaArgs) { |
| 21 | return page(args, { title: `New pull request · ${params.owner}/${params.repo} · g1t` }); |
| 22 | } |
| 23 | |
| 24 | export async function loader({ request, params, context }: Route.LoaderArgs) { |
| 25 | const user = requireUser(context, request); |
| 26 | const path = { namespace: params.owner, name: params.repo }; |
| 27 | const [repo, branches, open] = await Promise.all([ |
| 28 | repos.get(path, user), |
| 29 | repos.branches(path, user), |
| 30 | work.listPulls(path, user, "open"), |
| 31 | ]); |
| 32 | const { defaultBranch } = unwrap(repo); |
| 33 | const all = unwrap(branches).filter((branch) => !branch.name.startsWith("g1t-queue/")); |
| 34 | const query = new URL(request.url).searchParams; |
| 35 | // Into the default branch unless another is asked for (Compare's base). |
| 36 | const asked = query.get("base") ?? ""; |
| 37 | const base = all.some((branch) => branch.name === asked) ? asked : defaultBranch; |
| 38 | const baseHead = all.find((branch) => branch.name === base)?.hash; |
| 39 | // A branch that already has an open pull request into this base cannot |
| 40 | // have another. |
| 41 | const taken = new Set(open.ok ? open.value.filter((pull) => (pull.base ?? defaultBranch) === base).map((pull) => pull.branch) : []); |
| 42 | return { |
| 43 | defaultBranch, |
| 44 | base, |
| 45 | bases: all.map((branch) => branch.name), |
| 46 | branches: all |
| 47 | // A branch at the same commit as the base has nothing to merge. |
| 48 | .filter((branch) => branch.name !== base && branch.hash !== baseHead) |
| 49 | .map((branch) => branch.name) |
| 50 | .filter((name) => !taken.has(name)), |
| 51 | selected: query.get("branch") ?? "", |
| 52 | issue: query.get("issue") ?? "", |
| 53 | }; |
| 54 | } |
| 55 | |
| 56 | export async function action({ request, params, context }: Route.ActionArgs) { |
| 57 | assertSameOrigin(request); |
| 58 | const user = requireUser(context, request); |
| 59 | const form = await request.formData(); |
| 60 | const issue = Number(form.get("issue")); |
| 61 | const result = await work.openPull( |
| 62 | user, |
| 63 | { namespace: params.owner, name: params.repo }, |
| 64 | { |
| 65 | branch: String(form.get("branch") ?? ""), |
| 66 | title: String(form.get("title") ?? ""), |
| 67 | body: String(form.get("body") ?? ""), |
| 68 | issue: issue > 0 ? issue : undefined, |
| 69 | agent: user.username, |
| 70 | runtime: "external", |
| 71 | base: String(form.get("base") ?? "") || undefined, |
| 72 | }, |
| 73 | ); |
| 74 | if (!result.ok) return { error: result.error.message }; |
| 75 | throw redirect(`/${params.owner}/${params.repo}/pull/${result.value.number}`); |
| 76 | } |
| 77 | |
| 78 | export default function NewPull({ loaderData, actionData, params }: Route.ComponentProps) { |
| 79 | const { defaultBranch, base, bases, branches, selected, issue } = loaderData; |
| 80 | const remote = cloneUrl(useAddresses(), `${params.owner}/${params.repo}`); |
| 81 | |
| 82 | if (branches.length === 0) { |
| 83 | return ( |
| 84 | <div className="max-w-2xl"> |
| 85 | <EmptyState title="No branch to open a pull request from"> |
| 86 | Push a branch to this repository, then come back. |
| 87 | </EmptyState> |
| 88 | <div className="mt-4 space-y-2"> |
| 89 | <CopyLine prompt text="git switch -c my-change" /> |
| 90 | <CopyLine prompt text={`git push ${remote} my-change`} /> |
| 91 | </div> |
| 92 | <p className="mt-4 text-sm text-muted"> |
| 93 | Agents do not need a branch. Open an issue and assign agents to it, |
| 94 | and each gets a pull request with its own fork. |
| 95 | </p> |
| 96 | </div> |
| 97 | ); |
| 98 | } |
| 99 | |
| 100 | return ( |
| 101 | <Form method="post" className="max-w-2xl space-y-4"> |
| 102 | <div className="flex flex-wrap items-center gap-2 rounded-xl border border-line bg-surface px-4 py-3 text-sm"> |
| 103 | <GitBranch size={15} className="text-faint" /> |
| 104 | <span className="text-muted">Merge</span> |
| 105 | <Combobox |
| 106 | name="branch" |
| 107 | defaultValue={branches.includes(selected) ? selected : branches[0]} |
| 108 | aria-label="Branch to merge" |
| 109 | searchPlaceholder="Find a branch" |
| 110 | emptyText="No branch by that name." |
| 111 | options={branches.map((name) => ({ value: name, label: name, icon: <GitBranch /> }))} |
| 112 | className="h-8 w-auto max-w-full min-w-40 font-mono" |
| 113 | /> |
| 114 | <span className="text-muted">into</span> |
| 115 | <Combobox |
| 116 | name="base" |
| 117 | defaultValue={base} |
| 118 | aria-label="Branch to merge into" |
| 119 | searchPlaceholder="Find a branch" |
| 120 | emptyText="No branch by that name." |
| 121 | options={bases.map((name) => ({ |
| 122 | value: name, |
| 123 | label: name, |
| 124 | icon: <GitBranch />, |
| 125 | description: name === defaultBranch ? "default" : undefined, |
| 126 | }))} |
| 127 | className="h-8 w-auto max-w-full min-w-32 font-mono" |
| 128 | /> |
| 129 | </div> |
| 130 | <Field label="Title"> |
| 131 | <Input name="title" required autoFocus maxLength={200} /> |
| 132 | </Field> |
| 133 | <Field label="Description" hint="What changed and why. Markdown works."> |
| 134 | <Textarea name="body" rows={8} /> |
| 135 | </Field> |
| 136 | <Field |
| 137 | label="Issue (optional)" |
| 138 | hint={`The number of the issue this resolves. Merging the pull request into ${defaultBranch} closes it.`} |
| 139 | > |
| 140 | <Input name="issue" type="number" min={1} defaultValue={issue} placeholder="12" /> |
| 141 | </Field> |
| 142 | <ErrorText>{actionData?.error}</ErrorText> |
| 143 | <SubmitButton pending="Opening…">Open pull request</SubmitButton> |
| 144 | </Form> |
| 145 | ); |
| 146 | } |