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