| 1 | import { |
| 2 | Check, |
| 3 | CircleDashed, |
| 4 | GitCommitHorizontal, |
| 5 | GitMerge, |
| 6 | Layers, |
| 7 | Loader2, |
| 8 | Minus, |
| 9 | X, |
| 10 | } from "lucide-react"; |
| 11 | import { Suspense } from "react"; |
| 12 | import { Await, Link } from "react-router"; |
| 13 | |
| 14 | import type { QueueEntry, QueueState, RepoPath, Viewer } from "@g1t/contracts"; |
| 15 | |
| 16 | import type { Route } from "./+types/queue"; |
| 17 | import { page } from "../../lib/meta"; |
| 18 | import { Avatar, ButtonLink, EmptyState, TimeAgo } from "../../components/ui"; |
| 19 | import { actions, work } from "../../lib/services.server"; |
| 20 | import { getViewer, unwrap } from "../../lib/session.server"; |
| 21 | import { accessFor, repoFor } from "../../lib/access.server"; |
| 22 | import { useRefreshWhile } from "../../lib/refresh"; |
| 23 | |
| 24 | |
| 25 | /** A merge_group workflow run on a combined state. */ |
| 26 | type GroupRun = { id: string; name: string }; |
| 27 | |
| 28 | export function meta({ params, ...args }: Route.MetaArgs) { |
| 29 | return page(args, { title: `Merge queue · ${params.owner}/${params.repo} · g1t` }); |
| 30 | } |
| 31 | |
| 32 | export async function loader({ params, context }: Route.LoaderArgs) { |
| 33 | const viewer = getViewer(context); |
| 34 | const path = { namespace: params.owner, name: params.repo }; |
| 35 | const [queue, repo] = await Promise.all([work.queue(path, viewer), repoFor(context, params)]); |
| 36 | const view = unwrap(queue); |
| 37 | return { |
| 38 | queue: view, |
| 39 | // The merge_group runs on each combined state, to link its checks to: |
| 40 | // streamed, so the queue shows at once. |
| 41 | groupRuns: mergeGroupRuns(path, viewer, [...view.active, ...view.recent]), |
| 42 | defaultBranch: unwrap(repo).defaultBranch, |
| 43 | // Turning the queue on is a setting: Maintain and up. |
| 44 | member: repo.ok && accessFor(viewer, repo.value).can.manage_settings, |
| 45 | }; |
| 46 | } |
| 47 | |
| 48 | /** The merge_group runs on each entry's combined state, by commit; empty where none could be read. */ |
| 49 | function mergeGroupRuns(path: RepoPath, viewer: Viewer, entries: QueueEntry[]): Promise<Record<string, GroupRun[]>> { |
| 50 | const commits = [...new Set(entries.map((entry) => entry.combinedCommit).filter((sha) => sha != null))].slice(0, 20); |
| 51 | return Promise.all( |
| 52 | commits.map((sha) => |
| 53 | actions |
| 54 | .runs(path, viewer, { sha, event: "merge_group", limit: 10 }) |
| 55 | .then((found) => [sha, found.ok ? found.value.map((run) => ({ id: run.id, name: run.name })) : []] as const) |
| 56 | .catch(() => [sha, []] as const), |
| 57 | ), |
| 58 | ).then((found) => Object.fromEntries(found)); |
| 59 | } |
| 60 | |
| 61 | const STATE: Record<QueueState, { label: string; tone: string; icon: React.ReactNode }> = { |
| 62 | waiting: { label: "Waiting", tone: "text-muted border-line", icon: <CircleDashed size={13} /> }, |
| 63 | testing: { |
| 64 | label: "Testing", |
| 65 | tone: "text-accent border-accent/40 bg-accent/10", |
| 66 | icon: <Loader2 size={13} className="animate-spin" />, |
| 67 | }, |
| 68 | passed: { label: "Passed", tone: "text-success border-success/40 bg-success/10", icon: <Check size={13} /> }, |
| 69 | failed: { label: "Failed", tone: "text-danger border-danger/40 bg-danger/10", icon: <X size={13} /> }, |
| 70 | landed: { label: "Landed", tone: "text-merged border-merged/40 bg-merged/10", icon: <GitMerge size={13} /> }, |
| 71 | removed: { label: "Removed", tone: "text-faint border-line", icon: <Minus size={13} /> }, |
| 72 | }; |
| 73 | |
| 74 | function StateBadge({ state }: { state: QueueState }) { |
| 75 | const { label, tone, icon } = STATE[state]; |
| 76 | return ( |
| 77 | <span className={`inline-flex shrink-0 items-center gap-1.5 rounded-full border px-2 py-0.5 text-xs ${tone}`}> |
| 78 | {icon} |
| 79 | {label} |
| 80 | </span> |
| 81 | ); |
| 82 | } |
| 83 | |
| 84 | /** `main + #12 + #14`: the state an entry is being tested in. */ |
| 85 | function TestedAs({ entry, branch }: { entry: QueueEntry; branch: string }) { |
| 86 | return ( |
| 87 | <span className="flex flex-wrap items-center gap-1 font-mono text-xs text-muted"> |
| 88 | <span className="rounded border border-line px-1.5 py-px">{branch}</span> |
| 89 | {[...entry.ahead, entry.number].map((number) => ( |
| 90 | <span key={number} className="flex items-center gap-1"> |
| 91 | <span className="text-faint">+</span> |
| 92 | <span |
| 93 | className={`rounded border px-1.5 py-px ${ |
| 94 | number === entry.number ? "border-accent/40 text-fg" : "border-line" |
| 95 | }`} |
| 96 | > |
| 97 | #{number} |
| 98 | </span> |
| 99 | </span> |
| 100 | ))} |
| 101 | </span> |
| 102 | ); |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * How an entry's checks went, linked to the merge_group run on its combined |
| 107 | * state; with several runs, each is named and linked after the count. |
| 108 | */ |
| 109 | function GroupChecks({ entry, base, runs }: { entry: QueueEntry; base: string; runs: GroupRun[] }) { |
| 110 | const ran = entry.results.length; |
| 111 | const passed = entry.results.filter((result) => result.passed).length; |
| 112 | const [first, ...more] = runs; |
| 113 | const tone = passed === ran ? "text-success" : "text-danger"; |
| 114 | const count = ran > 0 ? `${passed}/${ran} checks passed` : first ? "merge_group run" : null; |
| 115 | if (!count) return null; |
| 116 | return ( |
| 117 | <span className="flex flex-wrap items-center gap-x-2"> |
| 118 | {first ? ( |
| 119 | <Link to={`${base}/actions/runs/${first.id}`} className={`underline-offset-2 hover:underline ${ran > 0 ? tone : "text-muted"}`}> |
| 120 | {count} |
| 121 | {more.length > 0 && ` · ${first.name}`} |
| 122 | </Link> |
| 123 | ) : ( |
| 124 | <span className={tone}>{count}</span> |
| 125 | )} |
| 126 | {more.map((run) => ( |
| 127 | <Link key={run.id} to={`${base}/actions/runs/${run.id}`} className="text-muted underline-offset-2 hover:text-fg hover:underline"> |
| 128 | {run.name} |
| 129 | </Link> |
| 130 | ))} |
| 131 | </span> |
| 132 | ); |
| 133 | } |
| 134 | |
| 135 | function Entry({ |
| 136 | entry, |
| 137 | base, |
| 138 | branch, |
| 139 | position, |
| 140 | groupRuns, |
| 141 | }: { |
| 142 | entry: QueueEntry; |
| 143 | base: string; |
| 144 | branch: string; |
| 145 | position?: number; |
| 146 | groupRuns: Promise<Record<string, GroupRun[]>>; |
| 147 | }) { |
| 148 | const runsOf = (found: Record<string, GroupRun[]>) => (entry.combinedCommit ? (found[entry.combinedCommit] ?? []) : []); |
| 149 | return ( |
| 150 | <li className="relative pl-10"> |
| 151 | {/* Its place on the rail. */} |
| 152 | <span |
| 153 | className={`absolute top-4 left-[13px] size-3 rounded-full border-2 ${ |
| 154 | entry.state === "testing" |
| 155 | ? "animate-pulse border-accent bg-accent/30" |
| 156 | : entry.state === "passed" || entry.state === "landed" |
| 157 | ? "border-success bg-success" |
| 158 | : entry.state === "failed" |
| 159 | ? "border-danger bg-danger" |
| 160 | : "border-line-strong bg-bg" |
| 161 | }`} |
| 162 | /> |
| 163 | <div className="rounded-xl border border-line bg-surface p-4 transition-colors hover:border-line-strong"> |
| 164 | <div className="flex flex-wrap items-center gap-x-3 gap-y-2"> |
| 165 | {position != null && <span className="font-mono text-xs text-faint">{position}</span>} |
| 166 | <Link to={`${base}/pull/${entry.number}`} prefetch="intent" className="min-w-0 grow font-medium hover:text-accent"> |
| 167 | {entry.title} <span className="font-normal text-faint">#{entry.number}</span> |
| 168 | </Link> |
| 169 | <StateBadge state={entry.state} /> |
| 170 | </div> |
| 171 | <div className="mt-2.5 flex flex-wrap items-center gap-x-4 gap-y-2 text-xs text-muted"> |
| 172 | {(entry.state === "testing" || entry.state === "passed" || entry.state === "failed") && ( |
| 173 | <TestedAs entry={entry} branch={branch} /> |
| 174 | )} |
| 175 | <Suspense fallback={<GroupChecks entry={entry} base={base} runs={[]} />}> |
| 176 | <Await resolve={groupRuns} errorElement={<GroupChecks entry={entry} base={base} runs={[]} />}> |
| 177 | {(found) => <GroupChecks entry={entry} base={base} runs={runsOf(found)} />} |
| 178 | </Await> |
| 179 | </Suspense> |
| 180 | <span className="flex items-center gap-1.5"> |
| 181 | <Avatar name={entry.agent} size={14} /> |
| 182 | {entry.agent} |
| 183 | </span> |
| 184 | <span> |
| 185 | queued by {entry.enqueuedBy} <TimeAgo at={entry.createdAt} /> |
| 186 | </span> |
| 187 | {entry.combinedCommit && ( |
| 188 | <span className="flex items-center gap-1 font-mono"> |
| 189 | <GitCommitHorizontal size={12} /> |
| 190 | {entry.combinedCommit.slice(0, 7)} |
| 191 | </span> |
| 192 | )} |
| 193 | </div> |
| 194 | {entry.error && <p className="mt-2.5 text-xs text-danger">{entry.error}</p>} |
| 195 | {entry.state === "failed" && |
| 196 | entry.results |
| 197 | .filter((result) => !result.passed) |
| 198 | .slice(0, 2) |
| 199 | .map((result) => ( |
| 200 | <details key={result.command} className="mt-2"> |
| 201 | <summary className="cursor-pointer font-mono text-xs text-danger">{result.command}</summary> |
| 202 | <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> |
| 203 | </details> |
| 204 | ))} |
| 205 | </div> |
| 206 | </li> |
| 207 | ); |
| 208 | } |
| 209 | |
| 210 | export default function Queue({ loaderData, params }: Route.ComponentProps) { |
| 211 | const { queue, defaultBranch, member, groupRuns } = loaderData; |
| 212 | const base = `/${params.owner}/${params.repo}`; |
| 213 | const moving = queue.active.length > 0; |
| 214 | useRefreshWhile(moving); |
| 215 | |
| 216 | const testing = queue.active.filter((entry) => entry.state !== "waiting").length; |
| 217 | return ( |
| 218 | <div className="max-w-4xl"> |
| 219 | <div className="flex flex-wrap items-start justify-between gap-4"> |
| 220 | <div> |
| 221 | <h2 className="flex items-center gap-2 text-xl font-semibold tracking-tight"> |
| 222 | <Layers size={19} className="text-accent" /> |
| 223 | Merge queue |
| 224 | </h2> |
| 225 | <p className="mt-1.5 max-w-2xl text-sm text-muted"> |
| 226 | Each pull request is tested together with everything ahead of it, several |
| 227 | combinations at once, and {defaultBranch} only ever moves to a combination that |
| 228 | passed. One that fails goes back to its author; the ones behind it are tested |
| 229 | again without it. |
| 230 | </p> |
| 231 | </div> |
| 232 | {member && ( |
| 233 | <ButtonLink to={`${base}/settings/rules`}>{queue.enabled ? "Settings" : "Turn it on"}</ButtonLink> |
| 234 | )} |
| 235 | </div> |
| 236 | |
| 237 | {!queue.enabled && queue.active.length === 0 && queue.recent.length === 0 ? ( |
| 238 | <div className="mt-8"> |
| 239 | <EmptyState title="This repository merges directly"> |
| 240 | Add the rule <b>Require the merge queue</b> for {defaultBranch} under{" "} |
| 241 | {member ? ( |
| 242 | <Link to={`${base}/settings/rules`} className="text-fg underline underline-offset-2"> |
| 243 | Settings → Rules |
| 244 | </Link> |
| 245 | ) : ( |
| 246 | "Settings → Rules" |
| 247 | )} |
| 248 | , and merging a pull request adds it here instead of changing {defaultBranch} at once. |
| 249 | </EmptyState> |
| 250 | </div> |
| 251 | ) : ( |
| 252 | <> |
| 253 | <section className="mt-8"> |
| 254 | <div className="flex items-baseline justify-between"> |
| 255 | <h3 className="text-sm font-medium">In the queue</h3> |
| 256 | <span className="text-xs text-muted"> |
| 257 | {queue.active.length} waiting to land{testing > 0 && `, ${testing} being tested now`} |
| 258 | </span> |
| 259 | </div> |
| 260 | <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"> |
| 261 | <li className="relative flex items-center gap-2 pl-10 text-xs text-muted"> |
| 262 | <span className="absolute left-[11px] flex size-4 items-center justify-center rounded-full bg-raised ring-1 ring-line-strong"> |
| 263 | <GitCommitHorizontal size={10} /> |
| 264 | </span> |
| 265 | <span className="font-mono text-fg">{defaultBranch}</span> |
| 266 | {queue.active[0]?.baseCommit && ( |
| 267 | <span className="font-mono">at {queue.active[0].baseCommit.slice(0, 7)}</span> |
| 268 | )} |
| 269 | </li> |
| 270 | {queue.active.map((entry, index) => ( |
| 271 | <Entry key={entry.id} entry={entry} base={base} branch={defaultBranch} position={index + 1} groupRuns={groupRuns} /> |
| 272 | ))} |
| 273 | {queue.active.length === 0 && ( |
| 274 | <li className="pl-10 text-sm text-muted">Nothing is waiting. Merged pull requests appear here.</li> |
| 275 | )} |
| 276 | </ol> |
| 277 | </section> |
| 278 | |
| 279 | {queue.recent.length > 0 && ( |
| 280 | <section className="mt-10"> |
| 281 | <h3 className="text-sm font-medium">Recently</h3> |
| 282 | <ol className="mt-3 space-y-3"> |
| 283 | {queue.recent.map((entry) => ( |
| 284 | <Entry key={entry.id} entry={entry} base={base} branch={defaultBranch} groupRuns={groupRuns} /> |
| 285 | ))} |
| 286 | </ol> |
| 287 | </section> |
| 288 | )} |
| 289 | </> |
| 290 | )} |
| 291 | </div> |
| 292 | ); |
| 293 | } |