Skip to content
232 linesCodeBlameRaw
1import { ArrowLeft, Bot, GitBranch, Milestone as MilestoneIcon, Plus, Tag } from "lucide-react";
2import { Link, useLocation } from "react-router";
3
4import type { Route } from "./+types/pulls";
5import { page } from "../../lib/meta";
6import { openedBy } from "../../lib/opened-by";
7import { ButtonLink, EmptyState, TimeAgo } from "../../components/ui";
8import { Badge } from "../../components/ui/badge";
9import { Card } from "../../components/ui/card";
10import { Hint } from "../../components/ui/hint";
11import { CheckBadge } from "../../components/checks";
12import { ChangeSize, PullIcon, StateTabs } from "../../components/work";
13import { AgentBadge, useActiveRuns } from "../../components/agents";
14import { FilterMenu, LabelChip, Swatch } from "../../components/labels";
15import { colorsOf, listFilters, withFilter } from "../../lib/labels";
16import { accessTo, repoFor } from "../../lib/access.server";
17import { agents, work } from "../../lib/services.server";
18import { getViewer, unwrap } from "../../lib/session.server";
19
20export function meta({ params, ...args }: Route.MetaArgs) {
21 return page(args, { title: `Pull requests · ${params.owner}/${params.repo} · g1t` });
22}
23
24export async function loader({ params, context, request }: Route.LoaderArgs) {
25 const path = { namespace: params.owner, name: params.repo };
26 const viewer = getViewer(context);
27 // `?label=bug&milestone=3&base=release`, or the same written in `q`.
28 const { state, label, milestone, base } = listFilters(new URL(request.url).searchParams);
29 // Which an agent is working on, beside the list, so its badges come with it.
30 const [pulls, active, { can }, labels, milestones, repo] = await Promise.all([
31 work.listPulls(path, viewer, state, {
32 label: label || undefined,
33 milestone: milestone ?? undefined,
34 base: base || undefined,
35 }),
36 agents.listRuns(viewer, { repo: path, active: true, limit: 100 }).catch(() => null),
37 accessTo(context, params),
38 work.listLabels(path, viewer),
39 work.listMilestones(path, viewer, "open"),
40 // The layout looks it up in this request too.
41 repoFor(context, params),
42 ]);
43 return {
44 pulls: unwrap(pulls),
45 state,
46 label,
47 milestone,
48 base,
49 labels: labels.ok ? labels.value : [],
50 milestones: milestones.ok ? milestones.value : [],
51 defaultBranch: repo.ok ? repo.value.defaultBranch : null,
52 // As the project's agents.json has them; left out, the list fetches them.
53 active: active?.ok ? { runs: active.value, member: can.run } : undefined,
54 } as const;
55}
56
57export default function Pulls({ loaderData, params }: Route.ComponentProps) {
58 const { pulls, state, active, labels, milestones, label, milestone, defaultBranch } = loaderData;
59 const base = `/${params.owner}/${params.repo}`;
60 const list = `${base}/pulls`;
61 // Which pull requests an agent is working on this minute, and at what.
62 const working = useActiveRuns(params.owner, params.repo, active);
63 const current = new URLSearchParams(useLocation().search);
64 const colors = colorsOf(labels);
65 const milestoneTitle = milestones.find((m) => m.number === milestone)?.title ?? (milestone ? `#${milestone}` : null);
66 const filtered = [label, milestone, loaderData.base].some(Boolean);
67 const kept = new URLSearchParams(current);
68 kept.delete("state");
69 kept.delete("q");
70 return (
71 <div>
72 <StateTabs
73 to={list}
74 state={state}
75 query={kept.toString()}
76 action={
77 <ButtonLink to={`${base}/pulls/new`}>
78 <Plus size={15} />
79 New pull request
80 </ButtonLink>
81 }
82 />
83 <div className="mt-3 flex flex-wrap items-center gap-2">
84 <FilterMenu
85 label="Label"
86 active={label || undefined}
87 clearTo={label ? withFilter(list, current, "label", null) : undefined}
88 searchPlaceholder="Filter labels"
89 emptyText="No label matches."
90 options={labels.map((each) => ({
91 key: each.name,
92 to: withFilter(list, current, "label", each.name),
93 keywords: `${each.name} ${each.description}`,
94 selected: each.name === label,
95 label: (
96 <>
97 <Swatch color={each.color} />
98 <span className="truncate">{each.name}</span>
99 </>
100 ),
101 }))}
102 />
103 <FilterMenu
104 label="Milestone"
105 active={milestoneTitle ?? undefined}
106 clearTo={milestone ? withFilter(list, current, "milestone", null) : undefined}
107 searchPlaceholder="Filter milestones"
108 emptyText="No open milestone matches."
109 options={milestones.map((each) => ({
110 key: String(each.number),
111 to: withFilter(list, current, "milestone", String(each.number)),
112 keywords: `${each.title} ${each.number}`,
113 selected: each.number === milestone,
114 label: <span className="truncate">{each.title}</span>,
115 }))}
116 />
117 {loaderData.base && (
118 <Hint label="Show pull requests into every branch">
119 <Link
120 to={withFilter(list, current, "base", null)}
121 className="inline-flex h-8 items-center gap-1.5 rounded-md border border-accent/40 bg-accent/5 px-2.5 text-sm"
122 >
123 Into <span className="font-mono">{loaderData.base}</span>
124 <span className="sr-only">: show pull requests into every branch</span>
125 </Link>
126 </Hint>
127 )}
128 {filtered && (
129 <Link to={state === "closed" ? `${list}?state=closed` : list} className="text-xs text-muted hover:text-fg">
130 Clear filters
131 </Link>
132 )}
133 <span className="ml-auto flex items-center gap-1">
134 <Link
135 to={`${base}/labels`}
136 className="inline-flex h-8 items-center gap-1.5 rounded-md px-2.5 text-sm text-muted transition-colors hover:bg-surface hover:text-fg"
137 >
138 <Tag size={14} />
139 Labels
140 </Link>
141 <Link
142 to={`${base}/milestones`}
143 className="inline-flex h-8 items-center gap-1.5 rounded-md px-2.5 text-sm text-muted transition-colors hover:bg-surface hover:text-fg"
144 >
145 <MilestoneIcon size={14} />
146 Milestones
147 </Link>
148 </span>
149 </div>
150 <div className="mt-4">
151 {pulls.length === 0 ? (
152 <EmptyState
153 title={
154 filtered
155 ? `No ${state} pull requests match these filters`
156 : state === "open"
157 ? "No open pull requests"
158 : "No closed pull requests"
159 }
160 >
161 A pull request proposes a change. Assign agents to an issue and
162 each opens one in its own fork, or push a branch and open one
163 yourself.
164 </EmptyState>
165 ) : (
166 <Card asChild tone="plain" divided className="overflow-hidden">
167 <ul>
168 {pulls.map((pull) => (
169 <li key={pull.id}>
170 <Link
171 prefetch="intent"
172 to={`${base}/pull/${pull.number}`}
173 className="flex items-start gap-3 px-4 py-3 transition-colors hover:bg-surface"
174 >
175 <span className="mt-0.5">
176 <PullIcon status={pull.status} />
177 </span>
178 <span className="min-w-0 grow">
179 <span className="flex flex-wrap items-center gap-x-2 gap-y-1">
180 <span className="min-w-0 truncate font-medium">{pull.title}</span>
181 {pull.status === "draft" && (
182 <Badge className="px-1.5 font-normal text-faint">
183 draft
184 </Badge>
185 )}
186 {(pull.labels ?? []).map((name) => (
187 <LabelChip key={name} name={name} color={colors[name]} />
188 ))}
189 <AgentBadge run={working.get(pull.number)} />
190 </span>
191 <span className="mt-0.5 block text-xs text-faint">
192 #{pull.number} opened <TimeAgo at={pull.createdAt} /> by{" "}
193 {openedBy(pull).name}
194 {pull.requestedBy && <> for {pull.requestedBy.username}</>}
195 {pull.issue != null && <> · for #{pull.issue}</>}
196 {pull.supersededBy != null && <> · superseded by #{pull.supersededBy}</>}
197 {pull.milestone && (
198 <>
199 {" "}
200 · <MilestoneIcon size={11} className="inline" /> {pull.milestone.title}
201 </>
202 )}
203 {/* Into a branch other than the default one: said. */}
204 {pull.base && defaultBranch && pull.base !== defaultBranch && (
205 <>
206 {" "}
207 · <ArrowLeft size={11} className="inline" /> into{" "}
208 <span className="font-mono">{pull.base}</span>
209 </>
210 )}
211 </span>
212 </span>
213 <span className="mt-0.5 hidden sm:block">
214 <ChangeSize files={pull.files} />
215 </span>
216 <span className="mt-0.5">
217 <CheckBadge status={pull.checkStatus} />
218 </span>
219 <span className="mt-0.5 flex shrink-0 items-center gap-1 font-mono text-xs text-muted">
220 {pull.branch ? <GitBranch size={13} /> : <Bot size={13} />}
221 {pull.branch ?? pull.agent}
222 </span>
223 </Link>
224 </li>
225 ))}
226 </ul>
227 </Card>
228 )}
229 </div>
230 </div>
231 );
232}