Skip to content

g1t/apps/web/app/routes/repo/issue-new.tsx

226 lines9,839 bytesCodeBlame
1import { env } from "cloudflare:workers";
2import { Download, Sparkles } from "lucide-react";
3import { Form, Link, redirect, useSearchParams } from "react-router";
4
5import { PROVIDERS } from "@g1t/contracts";
6
7import type { Route } from "./+types/issue-new";
8import { page } from "../../lib/meta";
9import { ErrorText, Field, Input, SubmitButton, Textarea } from "../../components/ui";
10import { CheckboxOption } from "../../components/ui/checkbox";
11import { LabelChip } from "../../components/labels";
12import { integrations, work } from "../../lib/services.server";
13import { assertSameOrigin, requireUser, unwrap } from "../../lib/session.server";
14import { accessTo } from "../../lib/access.server";
15import { computeNoteFor } from "../../lib/compute.server";
16import { delegateForm, issuePath, notStarted } from "../../lib/delegate";
17
18export function meta({ params, ...args }: Route.MetaArgs) {
19 return page(args, { title: `New issue · ${params.owner}/${params.repo} · g1t` });
20}
21
22export async function loader({ request, params, context }: Route.LoaderArgs) {
23 const user = requireUser(context, request);
24 const path = { namespace: params.owner, name: params.repo };
25 // Putting an agent on it needs Write (Run): Read cannot spend compute.
26 const { can } = await accessTo(context, params);
27 const [labels, milestones, connections, agents, computeNote] = await Promise.all([
28 work.listLabels(path, user),
29 can.triage ? work.listMilestones(path, user, "open") : null,
30 integrations.list(params.owner.toLowerCase(), user),
31 can.run ? env.RUNNER.enabled(user, path) : false,
32 can.run ? computeNoteFor(params.owner, "agent") : null,
33 ]);
34 // Systems a ticket can be imported from.
35 const sources = connections.ok
36 ? [...new Set(connections.value.filter((c) => c.kind === "tracker" || c.provider === "sentry").map((c) => c.provider))]
37 : [];
38 return {
39 labels: unwrap(labels),
40 milestones: milestones?.ok ? milestones.value : [],
41 // Making labels, and choosing a milestone, need Triage.
42 canTriage: can.triage,
43 // Making labels needs Write; choosing them and a milestone, Triage.
44 canMakeLabels: can.manage_labels,
45 sources,
46 canAssign: can.run,
47 agents,
48 computeNote,
49 };
50}
51
52export async function action({ request, params, context }: Route.ActionArgs) {
53 assertSameOrigin(request);
54 const user = requireUser(context, request);
55 const form = await request.formData();
56 if (form.get("intent") === "import") {
57 const imported = await integrations.import(
58 user,
59 { namespace: params.owner, name: params.repo },
60 String(form.get("reference") ?? ""),
61 form.get("assign") === "on",
62 );
63 if (!imported.ok) return { importError: imported.error.message };
64 throw redirect(`/${params.owner}/${params.repo}/issues/${imported.value.number}`);
65 }
66 const path = { namespace: params.owner, name: params.repo };
67 // Assigned to g1t as it opens: one step, and the agent starts.
68 if (form.get("agent") === "on") {
69 const delegated = await env.RUNNER.delegate(user, path, delegateForm(form));
70 if (!delegated.ok) return { error: delegated.error.message };
71 const { issue, agent } = delegated.value;
72 const refused = notStarted(agent, path, issue.number);
73 if (!refused) throw redirect(issuePath(path, issue.number));
74 return { notStarted: refused };
75 }
76 const result = await work.openIssue(
77 user,
78 path,
79 {
80 title: String(form.get("title") ?? ""),
81 body: String(form.get("body") ?? ""),
82 labels: [
83 ...form.getAll("label").map(String),
84 ...String(form.get("labels") ?? "").split(","),
85 ],
86 milestone: Number(form.get("milestone")) || undefined,
87 },
88 );
89 if (!result.ok) return { error: result.error.message };
90 throw redirect(`/${params.owner}/${params.repo}/issues/${result.value.number}`);
91}
92
93export default function NewIssue({ loaderData, actionData }: Route.ComponentProps) {
94 // "Put an agent on …" in the palette arrives with ?agent=1.
95 const [params] = useSearchParams();
96 const refused = actionData && "notStarted" in actionData ? actionData.notStarted : null;
97 const names = loaderData.sources.map((source) => PROVIDERS[source].label);
98 return (
99 <div className="max-w-2xl">
100 {names.length > 0 && (
101 <Form method="post" className="mb-8 rounded-xl border border-line bg-surface p-4">
102 <input type="hidden" name="intent" value="import" />
103 <p className="flex items-center gap-2 text-sm font-medium">
104 <Download size={15} className="text-muted" />
105 Bring one in from {names.join(" or ")}
106 </p>
107 <p className="mt-1 text-xs text-muted">
108 Give a key such as TECH-1234 or paste its address. The issue stays linked: agents read the
109 original, and it hears back when the work lands.
110 </p>
111 <div className="mt-3 flex flex-wrap items-center gap-2">
112 <div className="min-w-56 grow">
113 <Input name="reference" required placeholder="TECH-1234" aria-label="Ticket key or address" />
114 </div>
115 <CheckboxOption name="assign" label="Put an agent on it" className="items-center" labelClassName="text-muted" />
116 <SubmitButton variant="quiet" match={{ intent: "import" }} pending="Importing…">
117 Import
118 </SubmitButton>
119 </div>
120 {actionData && "importError" in actionData && (
121 <div className="mt-2">
122 <ErrorText>{actionData.importError}</ErrorText>
123 </div>
124 )}
125 </Form>
126 )}
127 <Form method="post" className="space-y-4">
128 <Field label="Title">
129 <Input
130 name="title"
131 required
132 autoFocus
133 maxLength={200}
134 placeholder="Parser drops the last line of a file without a trailing newline"
135 />
136 </Field>
137 <Field
138 label="Description"
139 hint="What is wrong or wanted, and anything needed to act on it. An agent given this issue works from this text. To say what done looks like, add a list under a “## Definition of done” heading. Pull requests for it merge once the checks the default branch requires pass."
140 >
141 <Textarea name="body" rows={8} />
142 </Field>
143 <fieldset>
144 <legend className="mb-1.5 text-sm font-medium text-muted">Labels</legend>
145 <div className="flex flex-wrap items-center gap-x-3 gap-y-2">
146 {loaderData.labels.map((label) => (
147 <CheckboxOption
148 key={label.name}
149 name="label"
150 value={label.name}
151 label={<LabelChip name={label.name} color={label.color} />}
152 className="items-center gap-1.5"
153 />
154 ))}
155 </div>
156 {loaderData.canMakeLabels && (
157 <div className="mt-2">
158 <Input
159 name="labels"
160 aria-label="New labels"
161 placeholder="New labels, separated by commas: performance, area: cli"
162 />
163 </div>
164 )}
165 </fieldset>
166 {loaderData.canTriage && loaderData.milestones.length > 0 && (
167 <Field label="Milestone">
168 <select
169 name="milestone"
170 defaultValue=""
171 className="h-9 w-full rounded-md border border-line bg-bg px-2 text-sm outline-none hover:border-line-strong focus:border-accent-dim"
172 >
173 <option value="">None</option>
174 {loaderData.milestones.map((milestone) => (
175 <option key={milestone.number} value={milestone.number}>
176 {milestone.title}
177 {milestone.dueOn ? ` (due ${milestone.dueOn})` : ""}
178 </option>
179 ))}
180 </select>
181 </Field>
182 )}
183 {loaderData.canAssign && (
184 <div className="rounded-xl border border-accent/25 bg-accent/[0.04] px-3.5 py-3">
185 <CheckboxOption
186 name="agent"
187 defaultChecked={params.get("agent") === "1"}
188 label={
189 <span className="flex items-center gap-1.5 font-medium">
190 <Sparkles size={14} className="text-accent" />
191 Assign g1t now
192 </span>
193 }
194 description="It opens a pull request for this issue in a sandbox of its own and sees it through: this repository's workflows run on it as its checks, and it revises until the required ones pass and the review approves. There is no model or agent count to choose."
195 />
196 {(!loaderData.agents || loaderData.computeNote) && (
197 <p className="mt-2 pl-6 text-xs text-warn">
198 {loaderData.computeNote ?? "This workspace's agents have no model yet. The issue still opens, and says why the agent did not start."}
199 </p>
200 )}
201 </div>
202 )}
203 {refused && (
204 <div className="rounded-lg border border-warn/30 bg-warn/[0.06] p-3 text-sm">
205 <p>
206 Opened{" "}
207 <Link to={refused.to} className="font-medium text-fg hover:underline">
208 #{refused.number}
209 </Link>
210 , but g1t did not start. {refused.message}
211 </p>
212 {refused.fix && (
213 <Link to={refused.fix.to} className="mt-2 inline-block text-sm font-medium text-fg hover:underline">
214 {refused.fix.label}
215 </Link>
216 )}
217 </div>
218 )}
219 <ErrorText>{actionData && "error" in actionData ? actionData.error : null}</ErrorText>
220 <SubmitButton name="intent" value="open" pending="Opening…">
221 Open issue
222 </SubmitButton>
223 </Form>
224 </div>
225 );
226}