| 1 | import { env } from "cloudflare:workers"; |
| 2 | import { Download, Sparkles } from "lucide-react"; |
| 3 | import { Form, Link, redirect, useNavigation, useSearchParams } from "react-router"; |
| 4 | |
| 5 | import { PROVIDERS } from "@g1t/contracts"; |
| 6 | |
| 7 | import type { Route } from "./+types/issue-new"; |
| 8 | import { page } from "../../lib/meta"; |
| 9 | import { Button, ErrorText, Field, Input, Textarea } from "../../components/ui"; |
| 10 | import { CheckboxOption } from "../../components/ui/checkbox"; |
| 11 | import { Label } from "../../components/work"; |
| 12 | import { integrations, work } from "../../lib/services.server"; |
| 13 | import { assertSameOrigin, requireUser, unwrap } from "../../lib/session.server"; |
| 14 | import { accessTo } from "../../lib/access.server"; |
| 15 | import { computeNoteFor } from "../../lib/compute.server"; |
| 16 | import { delegateForm, issuePath, notStarted } from "../../lib/delegate"; |
| 17 | |
| 18 | export function meta({ params, ...args }: Route.MetaArgs) { |
| 19 | return page(args, { title: `New issue · ${params.owner}/${params.repo} · g1t` }); |
| 20 | } |
| 21 | |
| 22 | export 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, connections, agents, computeNote] = await Promise.all([ |
| 28 | work.listLabels(path, user), |
| 29 | integrations.list(params.owner.toLowerCase(), user), |
| 30 | can.run ? env.RUNNER.enabled(user, path) : false, |
| 31 | can.run ? computeNoteFor(params.owner, "agent") : null, |
| 32 | ]); |
| 33 | // Systems a ticket can be imported from. |
| 34 | const sources = connections.ok |
| 35 | ? [...new Set(connections.value.filter((c) => c.kind === "tracker" || c.provider === "sentry").map((c) => c.provider))] |
| 36 | : []; |
| 37 | return { labels: unwrap(labels), sources, canAssign: can.run, agents, computeNote }; |
| 38 | } |
| 39 | |
| 40 | export async function action({ request, params, context }: Route.ActionArgs) { |
| 41 | assertSameOrigin(request); |
| 42 | const user = requireUser(context, request); |
| 43 | const form = await request.formData(); |
| 44 | if (form.get("intent") === "import") { |
| 45 | const imported = await integrations.import( |
| 46 | user, |
| 47 | { namespace: params.owner, name: params.repo }, |
| 48 | String(form.get("reference") ?? ""), |
| 49 | form.get("assign") === "on", |
| 50 | ); |
| 51 | if (!imported.ok) return { importError: imported.error.message }; |
| 52 | throw redirect(`/${params.owner}/${params.repo}/issues/${imported.value.number}`); |
| 53 | } |
| 54 | const path = { namespace: params.owner, name: params.repo }; |
| 55 | // Assigned to g1t-agent as it opens: one step, and the agent starts. |
| 56 | if (form.get("agent") === "on") { |
| 57 | const delegated = await env.RUNNER.delegate(user, path, delegateForm(form)); |
| 58 | if (!delegated.ok) return { error: delegated.error.message }; |
| 59 | const { issue, agent } = delegated.value; |
| 60 | const refused = notStarted(agent, path, issue.number); |
| 61 | if (!refused) throw redirect(issuePath(path, issue.number)); |
| 62 | return { notStarted: refused }; |
| 63 | } |
| 64 | const result = await work.openIssue( |
| 65 | user, |
| 66 | path, |
| 67 | { |
| 68 | title: String(form.get("title") ?? ""), |
| 69 | body: String(form.get("body") ?? ""), |
| 70 | labels: [ |
| 71 | ...form.getAll("label").map(String), |
| 72 | ...String(form.get("labels") ?? "").split(","), |
| 73 | ], |
| 74 | checks: String(form.get("checks") ?? "").split("\n"), |
| 75 | }, |
| 76 | ); |
| 77 | if (!result.ok) return { error: result.error.message }; |
| 78 | throw redirect(`/${params.owner}/${params.repo}/issues/${result.value.number}`); |
| 79 | } |
| 80 | |
| 81 | export default function NewIssue({ loaderData, actionData }: Route.ComponentProps) { |
| 82 | const busy = useNavigation().state === "submitting"; |
| 83 | // "Put an agent on …" in the palette arrives with ?agent=1. |
| 84 | const [params] = useSearchParams(); |
| 85 | const refused = actionData && "notStarted" in actionData ? actionData.notStarted : null; |
| 86 | const names = loaderData.sources.map((source) => PROVIDERS[source].label); |
| 87 | return ( |
| 88 | <div className="max-w-2xl"> |
| 89 | {names.length > 0 && ( |
| 90 | <Form method="post" className="mb-8 rounded-xl border border-line bg-surface p-4"> |
| 91 | <input type="hidden" name="intent" value="import" /> |
| 92 | <p className="flex items-center gap-2 text-sm font-medium"> |
| 93 | <Download size={15} className="text-muted" /> |
| 94 | Bring one in from {names.join(" or ")} |
| 95 | </p> |
| 96 | <p className="mt-1 text-xs text-muted"> |
| 97 | Give a key such as TECH-1234 or paste its address. The issue stays linked: agents read the |
| 98 | original, and it hears back when the work lands. |
| 99 | </p> |
| 100 | <div className="mt-3 flex flex-wrap items-center gap-2"> |
| 101 | <div className="min-w-56 grow"> |
| 102 | <Input name="reference" required placeholder="TECH-1234" aria-label="Ticket key or address" /> |
| 103 | </div> |
| 104 | <CheckboxOption name="assign" label="Put an agent on it" className="items-center" labelClassName="text-muted" /> |
| 105 | <Button type="submit" variant="quiet" disabled={busy}> |
| 106 | Import |
| 107 | </Button> |
| 108 | </div> |
| 109 | {actionData && "importError" in actionData && ( |
| 110 | <div className="mt-2"> |
| 111 | <ErrorText>{actionData.importError}</ErrorText> |
| 112 | </div> |
| 113 | )} |
| 114 | </Form> |
| 115 | )} |
| 116 | <Form method="post" className="space-y-4"> |
| 117 | <Field label="Title"> |
| 118 | <Input |
| 119 | name="title" |
| 120 | required |
| 121 | autoFocus |
| 122 | maxLength={200} |
| 123 | placeholder="Parser drops the last line of a file without a trailing newline" |
| 124 | /> |
| 125 | </Field> |
| 126 | <Field |
| 127 | label="Description" |
| 128 | hint="What is wrong or wanted, and anything needed to act on it. An agent given this issue works from this text." |
| 129 | > |
| 130 | <Textarea name="body" rows={8} /> |
| 131 | </Field> |
| 132 | <fieldset> |
| 133 | <legend className="mb-1.5 text-sm font-medium text-muted">Labels</legend> |
| 134 | <div className="flex flex-wrap items-center gap-x-3 gap-y-2"> |
| 135 | {loaderData.labels.map((name) => ( |
| 136 | <CheckboxOption key={name} name="label" value={name} label={<Label name={name} />} className="items-center gap-1.5" /> |
| 137 | ))} |
| 138 | </div> |
| 139 | <div className="mt-2"> |
| 140 | <Input |
| 141 | name="labels" |
| 142 | aria-label="Other labels" |
| 143 | placeholder="Others, separated by commas: performance, good first issue" |
| 144 | /> |
| 145 | </div> |
| 146 | </fieldset> |
| 147 | <Field |
| 148 | label="Acceptance checks (optional)" |
| 149 | hint="One command per line. A pull request for this issue should make them all pass." |
| 150 | > |
| 151 | <Textarea name="checks" rows={3} placeholder="cargo test" /> |
| 152 | </Field> |
| 153 | {loaderData.canAssign && ( |
| 154 | <div className="rounded-xl border border-merged/25 bg-merged/[0.04] px-3.5 py-3"> |
| 155 | <CheckboxOption |
| 156 | name="agent" |
| 157 | defaultChecked={params.get("agent") === "1"} |
| 158 | label={ |
| 159 | <span className="flex items-center gap-1.5 font-medium"> |
| 160 | <Sparkles size={14} className="text-merged" /> |
| 161 | Assign g1t-agent now |
| 162 | </span> |
| 163 | } |
| 164 | description="It opens a pull request for this issue in a sandbox of its own and sees it through checks and review. There is no model or agent count to choose." |
| 165 | /> |
| 166 | {(!loaderData.agents || loaderData.computeNote) && ( |
| 167 | <p className="mt-2 pl-6 text-xs text-warn"> |
| 168 | {loaderData.computeNote ?? "This workspace's agents have no model yet. The issue still opens, and says why the agent did not start."} |
| 169 | </p> |
| 170 | )} |
| 171 | </div> |
| 172 | )} |
| 173 | {refused && ( |
| 174 | <div className="rounded-lg border border-warn/30 bg-warn/[0.06] p-3 text-sm"> |
| 175 | <p> |
| 176 | Opened{" "} |
| 177 | <Link to={refused.to} className="font-medium text-fg hover:underline"> |
| 178 | #{refused.number} |
| 179 | </Link> |
| 180 | , but g1t-agent did not start. {refused.message} |
| 181 | </p> |
| 182 | {refused.fix && ( |
| 183 | <Link to={refused.fix.to} className="mt-2 inline-block text-sm font-medium text-fg hover:underline"> |
| 184 | {refused.fix.label} |
| 185 | </Link> |
| 186 | )} |
| 187 | </div> |
| 188 | )} |
| 189 | <ErrorText>{actionData && "error" in actionData ? actionData.error : null}</ErrorText> |
| 190 | <Button type="submit">Open issue</Button> |
| 191 | </Form> |
| 192 | </div> |
| 193 | ); |
| 194 | } |