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