| 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 | draft: form.get("intent") === "draft", |
| 73 | }, |
| 74 | ); |
| 75 | if (!result.ok) return { error: result.error.message }; |
| 76 | throw redirect(`/${params.owner}/${params.repo}/pull/${result.value.number}`); |
| 77 | } |
| 78 | |
| 79 | export default function NewPull({ loaderData, actionData, params }: Route.ComponentProps) { |
| 80 | const { defaultBranch, base, bases, branches, selected, issue } = loaderData; |
| 81 | const remote = cloneUrl(useAddresses(), `${params.owner}/${params.repo}`); |
| 82 | |
| 83 | if (branches.length === 0) { |
| 84 | return ( |
| 85 | <div className="max-w-2xl"> |
| 86 | <EmptyState title="No branch to open a pull request from"> |
| 87 | Push a branch to this repository, then come back. |
| 88 | </EmptyState> |
| 89 | <div className="mt-4 space-y-2"> |
| 90 | <CopyLine prompt text="git switch -c my-change" /> |
| 91 | <CopyLine prompt text={`git push ${remote} my-change`} /> |
| 92 | </div> |
| 93 | <p className="mt-4 text-sm text-muted"> |
| 94 | Agents do not need a branch. Open an issue and assign agents to it, |
| 95 | and each gets a pull request with its own fork. |
| 96 | </p> |
| 97 | </div> |
| 98 | ); |
| 99 | } |
| 100 | |
| 101 | return ( |
| 102 | <Form method="post" className="max-w-2xl space-y-4"> |
| 103 | <div className="flex flex-wrap items-center gap-2 rounded-xl border border-line bg-surface px-4 py-3 text-sm"> |
| 104 | <GitBranch size={15} className="text-faint" /> |
| 105 | <span className="text-muted">Merge</span> |
| 106 | <Combobox |
| 107 | name="branch" |
| 108 | defaultValue={branches.includes(selected) ? selected : branches[0]} |
| 109 | aria-label="Branch to merge" |
| 110 | searchPlaceholder="Find a branch" |
| 111 | emptyText="No branch by that name." |
| 112 | options={branches.map((name) => ({ value: name, label: name, icon: <GitBranch /> }))} |
| 113 | className="h-8 w-auto max-w-full min-w-40 font-mono" |
| 114 | /> |
| 115 | <span className="text-muted">into</span> |
| 116 | <Combobox |
| 117 | name="base" |
| 118 | defaultValue={base} |
| 119 | aria-label="Branch to merge into" |
| 120 | searchPlaceholder="Find a branch" |
| 121 | emptyText="No branch by that name." |
| 122 | options={bases.map((name) => ({ |
| 123 | value: name, |
| 124 | label: name, |
| 125 | icon: <GitBranch />, |
| 126 | description: name === defaultBranch ? "default" : undefined, |
| 127 | }))} |
| 128 | className="h-8 w-auto max-w-full min-w-32 font-mono" |
| 129 | /> |
| 130 | </div> |
| 131 | <Field label="Title"> |
| 132 | <Input name="title" required autoFocus maxLength={200} /> |
| 133 | </Field> |
| 134 | <Field label="Description" hint="What changed and why. Markdown works."> |
| 135 | <Textarea name="body" rows={8} /> |
| 136 | </Field> |
| 137 | <Field |
| 138 | label="Issue (optional)" |
| 139 | hint={`The number of the issue this resolves. Merging the pull request into ${defaultBranch} closes it.`} |
| 140 | > |
| 141 | <Input name="issue" type="number" min={1} defaultValue={issue} placeholder="12" /> |
| 142 | </Field> |
| 143 | <ErrorText>{actionData?.error}</ErrorText> |
| 144 | <div className="flex flex-wrap items-center gap-2"> |
| 145 | <SubmitButton name="intent" value="open" match={{ intent: "open" }} pending="Opening…"> |
| 146 | Open pull request |
| 147 | </SubmitButton> |
| 148 | <SubmitButton name="intent" value="draft" match={{ intent: "draft" }} variant="quiet" pending="Opening…"> |
| 149 | Open as draft |
| 150 | </SubmitButton> |
| 151 | <span className="text-xs text-faint">A draft can't merge, and agents' review routines wait, until you mark it ready.</span> |
| 152 | </div> |
| 153 | </Form> |
| 154 | ); |
| 155 | } |