| 1 | import { env } from "cloudflare:workers"; |
| 2 | import { GitPullRequest, MessageSquare, Plus, Sparkles, X } from "lucide-react"; |
| 3 | import { useEffect, useRef } from "react"; |
| 4 | import { Form, Link, useRouteLoaderData } from "react-router"; |
| 5 | |
| 6 | import type { Route } from "./+types/issues"; |
| 7 | import { page } from "../../lib/meta"; |
| 8 | import { ButtonLink, ComputeNote, EmptyState, ErrorText, SubmitButton, TimeAgo } from "../../components/ui"; |
| 9 | import { Checkbox } from "../../components/ui/checkbox"; |
| 10 | import { |
| 11 | Assignee, |
| 12 | AssigneeStack, |
| 13 | IssueIcon, |
| 14 | Label, |
| 15 | StateTabs, |
| 16 | } from "../../components/work"; |
| 17 | import { computeNoteFor } from "../../lib/compute.server"; |
| 18 | import { isWaitingMessage } from "../../lib/compute"; |
| 19 | import { work } from "../../lib/services.server"; |
| 20 | import { assertSameOrigin, getViewer, requireUser, unwrap } from "../../lib/session.server"; |
| 21 | import { accessTo, refusal } from "../../lib/access.server"; |
| 22 | |
| 23 | /** As many sandboxes as one request may start. */ |
| 24 | const MAX_ASSIGNED_AT_ONCE = 10; |
| 25 | |
| 26 | export function meta({ params, ...args }: Route.MetaArgs) { |
| 27 | return page(args, { title: `Issues · ${params.owner}/${params.repo} · g1t` }); |
| 28 | } |
| 29 | |
| 30 | export async function loader({ params, context, request }: Route.LoaderArgs) { |
| 31 | const viewer = getViewer(context); |
| 32 | const path = { namespace: params.owner, name: params.repo }; |
| 33 | const query = new URL(request.url).searchParams; |
| 34 | const state = query.get("state") === "closed" ? "closed" : "open"; |
| 35 | const label = query.get("label") ?? ""; |
| 36 | // Assigning agents needs Write: Read cannot spend compute. All at once: |
| 37 | // only the plan's note waits for the viewer's role. |
| 38 | const access = accessTo(context, params); |
| 39 | const [{ can }, issues, labels, agentsEnabled, computeNote] = await Promise.all([ |
| 40 | access, |
| 41 | work.listIssues(path, viewer, { state, label: label || undefined }), |
| 42 | work.listLabels(path, viewer), |
| 43 | env.RUNNER.enabled(viewer, path), |
| 44 | // Before a member assigns: whether the workspace's plan lets agents start. |
| 45 | access.then(({ can }) => (can.run ? computeNoteFor(params.owner, "agent") : null)), |
| 46 | ]); |
| 47 | return { |
| 48 | issues: unwrap(issues), |
| 49 | labels: unwrap(labels), |
| 50 | state, |
| 51 | label, |
| 52 | agentsEnabled: agentsEnabled && can.run, |
| 53 | computeNote, |
| 54 | } as const; |
| 55 | } |
| 56 | |
| 57 | /** Assigns each selected issue to g1t, one run of its own each. */ |
| 58 | export async function action({ request, params, context }: Route.ActionArgs) { |
| 59 | assertSameOrigin(request); |
| 60 | const user = requireUser(context, request); |
| 61 | const path = { namespace: params.owner, name: params.repo }; |
| 62 | const numbers = (await request.formData()) |
| 63 | .getAll("issue") |
| 64 | .map(Number) |
| 65 | .filter((number) => Number.isInteger(number) && number > 0); |
| 66 | if (numbers.length === 0) return { error: "Select the issues to assign first." }; |
| 67 | const denied = await refusal(context, params, "run"); |
| 68 | if (denied) return { error: denied }; |
| 69 | if (numbers.length > MAX_ASSIGNED_AT_ONCE) { |
| 70 | return { error: `Assign at most ${MAX_ASSIGNED_AT_ONCE} issues at a time.` }; |
| 71 | } |
| 72 | const results = await Promise.all( |
| 73 | numbers.map((number) => env.RUNNER.run(user, path, number)), |
| 74 | ); |
| 75 | // Over the workspace's agents-at-once cap, the rest wait in the queue. |
| 76 | const waiting = results.filter((result) => !result.ok && isWaitingMessage(result.error.message)).length; |
| 77 | const refused = results.find((result) => !result.ok && !isWaitingMessage(result.error.message)); |
| 78 | const started = results.filter((result) => result.ok).length; |
| 79 | return { |
| 80 | started, |
| 81 | waiting, |
| 82 | error: |
| 83 | refused && !refused.ok |
| 84 | ? `${started} of ${numbers.length} assigned. ${refused.error.message}` |
| 85 | : null, |
| 86 | }; |
| 87 | } |
| 88 | |
| 89 | export default function Issues({ loaderData, actionData, params }: Route.ComponentProps) { |
| 90 | const { issues, labels, state, label, agentsEnabled } = loaderData; |
| 91 | const repo = `/${params.owner}/${params.repo}`; |
| 92 | const base = `${repo}/issues`; |
| 93 | const stateQuery = state === "closed" ? "state=closed" : ""; |
| 94 | const assignable = agentsEnabled && state === "open" && issues.length > 0; |
| 95 | |
| 96 | // An archived repository's issues are locked: no new ones. |
| 97 | const layout = useRouteLoaderData("routes/repo/layout") as { repo?: { archivedAt?: string | null } } | undefined; |
| 98 | const archived = Boolean(layout?.repo?.archivedAt); |
| 99 | // Handed over: the ticks are cleared. Refused: they stay, to try again. |
| 100 | const form = useRef<HTMLFormElement>(null); |
| 101 | useEffect(() => { |
| 102 | if (actionData && !actionData.error) form.current?.reset(); |
| 103 | }, [actionData]); |
| 104 | return ( |
| 105 | <div> |
| 106 | <StateTabs |
| 107 | to={base} |
| 108 | state={state} |
| 109 | query={label ? `label=${encodeURIComponent(label)}` : ""} |
| 110 | action={ |
| 111 | archived ? undefined : ( |
| 112 | <ButtonLink to={`${base}/new`}> |
| 113 | <Plus size={15} /> |
| 114 | New issue |
| 115 | </ButtonLink> |
| 116 | ) |
| 117 | } |
| 118 | /> |
| 119 | <div className="mt-3 flex flex-wrap items-center gap-1.5"> |
| 120 | {labels.map((name) => { |
| 121 | const active = name === label; |
| 122 | const query = [stateQuery, active ? "" : `label=${encodeURIComponent(name)}`] |
| 123 | .filter(Boolean) |
| 124 | .join("&"); |
| 125 | return ( |
| 126 | <Link |
| 127 | key={name} |
| 128 | to={`${base}?${query}`} |
| 129 | className={`flex items-center gap-1 rounded-full transition-opacity ${ |
| 130 | label && !active ? "opacity-45 hover:opacity-100" : "" |
| 131 | }`} |
| 132 | > |
| 133 | <Label name={name} /> |
| 134 | {active && <X size={12} className="text-muted" />} |
| 135 | </Link> |
| 136 | ); |
| 137 | })} |
| 138 | </div> |
| 139 | <Form ref={form} method="post" className="mt-4"> |
| 140 | {assignable && ( |
| 141 | <div className="mb-3 flex flex-wrap items-center gap-3 rounded-xl border border-accent/30 bg-accent/5 px-4 py-2.5"> |
| 142 | <Sparkles size={15} className="shrink-0 text-accent" /> |
| 143 | <p className="min-w-0 grow text-sm text-muted"> |
| 144 | Tick the issues to hand over. g1t takes each one in a run of its own, |
| 145 | and they all work at once. |
| 146 | </p> |
| 147 | <SubmitButton variant="accent" pending="Starting sandboxes…"> |
| 148 | Assign to g1t |
| 149 | </SubmitButton> |
| 150 | {loaderData.computeNote && ( |
| 151 | <div className="basis-full"> |
| 152 | <ComputeNote note={loaderData.computeNote} /> |
| 153 | </div> |
| 154 | )} |
| 155 | </div> |
| 156 | )} |
| 157 | {actionData?.error ? ( |
| 158 | <div className="mb-3"> |
| 159 | <ErrorText>{actionData.error}</ErrorText> |
| 160 | </div> |
| 161 | ) : ( |
| 162 | actionData?.started != null && ( |
| 163 | <p className="mb-3 text-sm text-muted"> |
| 164 | {actionData.started}{" "} |
| 165 | {actionData.started === 1 ? "agent is" : "agents are"} starting. Each |
| 166 | issue shows its pull request as it appears. |
| 167 | {actionData.waiting |
| 168 | ? ` ${actionData.waiting} more ${actionData.waiting === 1 ? "waits" : "wait"} for a free slot and start by themselves.` |
| 169 | : ""} |
| 170 | </p> |
| 171 | ) |
| 172 | )} |
| 173 | {issues.length === 0 ? ( |
| 174 | <EmptyState |
| 175 | title={ |
| 176 | label |
| 177 | ? `No ${state} issues labelled ${label}` |
| 178 | : state === "open" |
| 179 | ? "No open issues" |
| 180 | : "No closed issues" |
| 181 | } |
| 182 | > |
| 183 | An issue says what should change: a bug, a feature, a question. |
| 184 | Agents and people open pull requests against it. |
| 185 | </EmptyState> |
| 186 | ) : ( |
| 187 | <ul className="divide-y divide-line overflow-hidden rounded-xl border border-line"> |
| 188 | {issues.map((issue) => ( |
| 189 | <li key={issue.id} className="flex items-start transition-colors hover:bg-surface"> |
| 190 | {assignable && ( |
| 191 | <Checkbox |
| 192 | name="issue" |
| 193 | value={String(issue.number)} |
| 194 | // Already being worked on: nothing more to hand over. |
| 195 | disabled={issue.agent != null || issue.queued} |
| 196 | aria-label={`Select issue #${issue.number}`} |
| 197 | className="mt-4 ml-4 disabled:opacity-30" |
| 198 | /> |
| 199 | )} |
| 200 | <Link |
| 201 | prefetch="intent" |
| 202 | to={`${base}/${issue.number}`} |
| 203 | className="flex min-w-0 grow items-start gap-3 px-4 py-3" |
| 204 | > |
| 205 | <span className="mt-0.5"> |
| 206 | <IssueIcon issue={issue} /> |
| 207 | </span> |
| 208 | <span className="min-w-0 grow"> |
| 209 | <span className="flex flex-wrap items-center gap-x-2 gap-y-1"> |
| 210 | <span className="font-medium">{issue.title}</span> |
| 211 | {issue.labels.map((name) => ( |
| 212 | <Label key={name} name={name} /> |
| 213 | ))} |
| 214 | {issue.state === "open" && issue.agent && ( |
| 215 | <Assignee agent={issue.agent} /> |
| 216 | )} |
| 217 | {issue.state === "open" && issue.queued && !issue.agent && ( |
| 218 | <span className="rounded-full border border-line px-2 py-px text-xs text-muted"> |
| 219 | queued for g1t |
| 220 | {issue.blockedBy.length > 0 && |
| 221 | `, after ${issue.blockedBy.map((number) => `#${number}`).join(", ")}`} |
| 222 | </span> |
| 223 | )} |
| 224 | </span> |
| 225 | <span className="mt-0.5 block text-xs text-faint"> |
| 226 | #{issue.number} opened <TimeAgo at={issue.createdAt} /> by{" "} |
| 227 | {issue.author.username} |
| 228 | {issue.requestedBy && <> for {issue.requestedBy.username}</>} |
| 229 | {issue.resolvedBy != null && ( |
| 230 | <span className="text-merged"> · resolved by #{issue.resolvedBy}</span> |
| 231 | )} |
| 232 | </span> |
| 233 | </span> |
| 234 | <span className="mt-0.5 flex shrink-0 items-center gap-3 text-xs text-muted"> |
| 235 | <AssigneeStack people={issue.assignees} /> |
| 236 | {issue.pullCount > 0 && ( |
| 237 | <span |
| 238 | className="flex items-center gap-1" |
| 239 | title={`${issue.pullCount} pull ${issue.pullCount === 1 ? "request" : "requests"}`} |
| 240 | > |
| 241 | <GitPullRequest size={13} /> |
| 242 | {issue.pullCount} |
| 243 | </span> |
| 244 | )} |
| 245 | {issue.commentCount > 0 && ( |
| 246 | <span className="flex items-center gap-1"> |
| 247 | <MessageSquare size={13} /> |
| 248 | {issue.commentCount} |
| 249 | </span> |
| 250 | )} |
| 251 | </span> |
| 252 | </Link> |
| 253 | </li> |
| 254 | ))} |
| 255 | </ul> |
| 256 | )} |
| 257 | </Form> |
| 258 | </div> |
| 259 | ); |
| 260 | } |