Skip to content

Where people and agents ship software together. The open-source git platform for the whole job: issues, agents, checks and deploys to the edge.

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