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