| 1 | import { |
| 2 | Check, |
| 3 | CircleDashed, |
| 4 | GitCommitHorizontal, |
| 5 | GitMerge, |
| 6 | Layers, |
| 7 | Loader2, |
| 8 | Minus, |
| 9 | X, |
| 10 | } from "lucide-react"; |
| 11 | import { Link } from "react-router"; |
| 12 | |
| 13 | import type { QueueEntry, QueueState } from "@g1t/contracts"; |
| 14 | |
| 15 | import type { Route } from "./+types/queue"; |
| 16 | import { page } from "../../lib/meta"; |
| 17 | import { Avatar, ButtonLink, EmptyState, TimeAgo } from "../../components/ui"; |
| 18 | import { work } from "../../lib/services.server"; |
| 19 | import { getViewer, unwrap } from "../../lib/session.server"; |
| 20 | import { accessFor, repoFor } from "../../lib/access.server"; |
| 21 | import { useRefreshWhile } from "../../lib/refresh"; |
| 22 | |
| 23 | |
| 24 | export function meta({ params, ...args }: Route.MetaArgs) { |
| 25 | return page(args, { title: `Merge queue · ${params.owner}/${params.repo} · g1t` }); |
| 26 | } |
| 27 | |
| 28 | export async function loader({ params, context }: Route.LoaderArgs) { |
| 29 | const viewer = getViewer(context); |
| 30 | const path = { namespace: params.owner, name: params.repo }; |
| 31 | const [queue, repo] = await Promise.all([work.queue(path, viewer), repoFor(context, params)]); |
| 32 | return { |
| 33 | queue: unwrap(queue), |
| 34 | defaultBranch: unwrap(repo).defaultBranch, |
| 35 | // Turning the queue on is a setting: Maintain and up. |
| 36 | member: repo.ok && accessFor(viewer, repo.value).can.manage_settings, |
| 37 | }; |
| 38 | } |
| 39 | |
| 40 | const STATE: Record<QueueState, { label: string; tone: string; icon: React.ReactNode }> = { |
| 41 | waiting: { label: "Waiting", tone: "text-muted border-line", icon: <CircleDashed size={13} /> }, |
| 42 | testing: { |
| 43 | label: "Testing", |
| 44 | tone: "text-accent border-accent/40 bg-accent/10", |
| 45 | icon: <Loader2 size={13} className="animate-spin" />, |
| 46 | }, |
| 47 | passed: { label: "Passed", tone: "text-accent border-accent/40 bg-accent/10", icon: <Check size={13} /> }, |
| 48 | failed: { label: "Failed", tone: "text-danger border-danger/40 bg-danger/10", icon: <X size={13} /> }, |
| 49 | landed: { label: "Landed", tone: "text-merged border-merged/40 bg-merged/10", icon: <GitMerge size={13} /> }, |
| 50 | removed: { label: "Removed", tone: "text-faint border-line", icon: <Minus size={13} /> }, |
| 51 | }; |
| 52 | |
| 53 | function StateBadge({ state }: { state: QueueState }) { |
| 54 | const { label, tone, icon } = STATE[state]; |
| 55 | return ( |
| 56 | <span className={`inline-flex shrink-0 items-center gap-1.5 rounded-full border px-2 py-0.5 text-xs ${tone}`}> |
| 57 | {icon} |
| 58 | {label} |
| 59 | </span> |
| 60 | ); |
| 61 | } |
| 62 | |
| 63 | /** `main + #12 + #14`: the state an entry is being tested in. */ |
| 64 | function TestedAs({ entry, branch }: { entry: QueueEntry; branch: string }) { |
| 65 | return ( |
| 66 | <span className="flex flex-wrap items-center gap-1 font-mono text-xs text-muted"> |
| 67 | <span className="rounded border border-line px-1.5 py-px">{branch}</span> |
| 68 | {[...entry.ahead, entry.number].map((number) => ( |
| 69 | <span key={number} className="flex items-center gap-1"> |
| 70 | <span className="text-faint">+</span> |
| 71 | <span |
| 72 | className={`rounded border px-1.5 py-px ${ |
| 73 | number === entry.number ? "border-accent/40 text-fg" : "border-line" |
| 74 | }`} |
| 75 | > |
| 76 | #{number} |
| 77 | </span> |
| 78 | </span> |
| 79 | ))} |
| 80 | </span> |
| 81 | ); |
| 82 | } |
| 83 | |
| 84 | function Entry({ entry, base, branch, position }: { entry: QueueEntry; base: string; branch: string; position?: number }) { |
| 85 | const ran = entry.results.length; |
| 86 | const passed = entry.results.filter((result) => result.passed).length; |
| 87 | return ( |
| 88 | <li className="relative pl-10"> |
| 89 | {/* Its place on the rail. */} |
| 90 | <span |
| 91 | className={`absolute top-4 left-[13px] size-3 rounded-full border-2 ${ |
| 92 | entry.state === "testing" |
| 93 | ? "animate-pulse border-accent bg-accent/30" |
| 94 | : entry.state === "passed" || entry.state === "landed" |
| 95 | ? "border-accent bg-accent" |
| 96 | : entry.state === "failed" |
| 97 | ? "border-danger bg-danger" |
| 98 | : "border-line-strong bg-bg" |
| 99 | }`} |
| 100 | /> |
| 101 | <div className="rounded-xl border border-line bg-surface p-4 transition-colors hover:border-line-strong"> |
| 102 | <div className="flex flex-wrap items-center gap-x-3 gap-y-2"> |
| 103 | {position != null && <span className="font-mono text-xs text-faint">{position}</span>} |
| 104 | <Link to={`${base}/pull/${entry.number}`} prefetch="intent" className="min-w-0 grow font-medium hover:text-accent"> |
| 105 | {entry.title} <span className="font-normal text-faint">#{entry.number}</span> |
| 106 | </Link> |
| 107 | <StateBadge state={entry.state} /> |
| 108 | </div> |
| 109 | <div className="mt-2.5 flex flex-wrap items-center gap-x-4 gap-y-2 text-xs text-muted"> |
| 110 | {(entry.state === "testing" || entry.state === "passed" || entry.state === "failed") && ( |
| 111 | <TestedAs entry={entry} branch={branch} /> |
| 112 | )} |
| 113 | {ran > 0 && ( |
| 114 | <span className={passed === ran ? "text-accent" : "text-danger"}> |
| 115 | {passed}/{ran} checks passed |
| 116 | </span> |
| 117 | )} |
| 118 | <span className="flex items-center gap-1.5"> |
| 119 | <Avatar name={entry.agent} size={14} /> |
| 120 | {entry.agent} |
| 121 | </span> |
| 122 | <span> |
| 123 | queued by {entry.enqueuedBy} <TimeAgo at={entry.createdAt} /> |
| 124 | </span> |
| 125 | {entry.combinedCommit && ( |
| 126 | <span className="flex items-center gap-1 font-mono"> |
| 127 | <GitCommitHorizontal size={12} /> |
| 128 | {entry.combinedCommit.slice(0, 7)} |
| 129 | </span> |
| 130 | )} |
| 131 | </div> |
| 132 | {entry.error && <p className="mt-2.5 text-xs text-danger">{entry.error}</p>} |
| 133 | {entry.state === "failed" && |
| 134 | entry.results |
| 135 | .filter((result) => !result.passed) |
| 136 | .slice(0, 2) |
| 137 | .map((result) => ( |
| 138 | <details key={result.command} className="mt-2"> |
| 139 | <summary className="cursor-pointer font-mono text-xs text-danger">{result.command}</summary> |
| 140 | <pre className="mt-1.5 max-h-48 overflow-auto rounded-lg bg-bg p-3 text-xs text-muted"><code>{result.output}</code></pre> |
| 141 | </details> |
| 142 | ))} |
| 143 | </div> |
| 144 | </li> |
| 145 | ); |
| 146 | } |
| 147 | |
| 148 | export default function Queue({ loaderData, params }: Route.ComponentProps) { |
| 149 | const { queue, defaultBranch, member } = loaderData; |
| 150 | const base = `/${params.owner}/${params.repo}`; |
| 151 | const moving = queue.active.length > 0; |
| 152 | useRefreshWhile(moving); |
| 153 | |
| 154 | const testing = queue.active.filter((entry) => entry.state !== "waiting").length; |
| 155 | return ( |
| 156 | <div className="max-w-4xl"> |
| 157 | <div className="flex flex-wrap items-start justify-between gap-4"> |
| 158 | <div> |
| 159 | <h2 className="flex items-center gap-2 text-xl font-semibold tracking-tight"> |
| 160 | <Layers size={19} className="text-accent" /> |
| 161 | Merge queue |
| 162 | </h2> |
| 163 | <p className="mt-1.5 max-w-2xl text-sm text-muted"> |
| 164 | Each pull request is tested together with everything ahead of it, several |
| 165 | combinations at once, and {defaultBranch} only ever moves to a combination that |
| 166 | passed. One that fails goes back to its author; the ones behind it are tested |
| 167 | again without it. |
| 168 | </p> |
| 169 | </div> |
| 170 | {member && ( |
| 171 | <ButtonLink to={`${base}/settings`}>{queue.enabled ? "Settings" : "Turn it on"}</ButtonLink> |
| 172 | )} |
| 173 | </div> |
| 174 | |
| 175 | {!queue.enabled && queue.active.length === 0 && queue.recent.length === 0 ? ( |
| 176 | <div className="mt-8"> |
| 177 | <EmptyState title="This repository merges directly"> |
| 178 | Turn on <b>Merge through a queue</b> in the repository's settings, and merging a |
| 179 | pull request adds it here instead of changing {defaultBranch} at once. |
| 180 | </EmptyState> |
| 181 | </div> |
| 182 | ) : ( |
| 183 | <> |
| 184 | <section className="mt-8"> |
| 185 | <div className="flex items-baseline justify-between"> |
| 186 | <h3 className="text-sm font-medium">In the queue</h3> |
| 187 | <span className="text-xs text-muted"> |
| 188 | {queue.active.length} waiting to land{testing > 0 && `, ${testing} being tested now`} |
| 189 | </span> |
| 190 | </div> |
| 191 | <ol className="relative mt-3 space-y-3 before:absolute before:top-0 before:bottom-4 before:left-[18px] before:w-px before:bg-line"> |
| 192 | <li className="relative flex items-center gap-2 pl-10 text-xs text-muted"> |
| 193 | <span className="absolute left-[11px] flex size-4 items-center justify-center rounded-full bg-raised ring-1 ring-line-strong"> |
| 194 | <GitCommitHorizontal size={10} /> |
| 195 | </span> |
| 196 | <span className="font-mono text-fg">{defaultBranch}</span> |
| 197 | {queue.active[0]?.baseCommit && ( |
| 198 | <span className="font-mono">at {queue.active[0].baseCommit.slice(0, 7)}</span> |
| 199 | )} |
| 200 | </li> |
| 201 | {queue.active.map((entry, index) => ( |
| 202 | <Entry key={entry.id} entry={entry} base={base} branch={defaultBranch} position={index + 1} /> |
| 203 | ))} |
| 204 | {queue.active.length === 0 && ( |
| 205 | <li className="pl-10 text-sm text-muted">Nothing is waiting. Merged pull requests appear here.</li> |
| 206 | )} |
| 207 | </ol> |
| 208 | </section> |
| 209 | |
| 210 | {queue.recent.length > 0 && ( |
| 211 | <section className="mt-10"> |
| 212 | <h3 className="text-sm font-medium">Recently</h3> |
| 213 | <ol className="mt-3 space-y-3"> |
| 214 | {queue.recent.map((entry) => ( |
| 215 | <Entry key={entry.id} entry={entry} base={base} branch={defaultBranch} /> |
| 216 | ))} |
| 217 | </ol> |
| 218 | </section> |
| 219 | )} |
| 220 | </> |
| 221 | )} |
| 222 | </div> |
| 223 | ); |
| 224 | } |