| 1 | import { ChartColumn, Plus, Search, ShieldCheck } from "lucide-react"; |
| 2 | import { Form, Link, redirect, useSearchParams } from "react-router"; |
| 3 | |
| 4 | import type { Route } from "./+types/settings-rules"; |
| 5 | import { page } from "../../lib/meta"; |
| 6 | import { RepoSettingsHeading } from "../../components/repo-settings-heading"; |
| 7 | import { EffectiveRulesView, InsightsView, RulesetList } from "../../components/rules"; |
| 8 | import { ButtonLink } from "../../components/ui"; |
| 9 | import { Button } from "../../components/ui/button"; |
| 10 | import { Input } from "../../components/ui/input"; |
| 11 | import { work } from "../../lib/services.server"; |
| 12 | import { getViewer, unwrap } from "../../lib/session.server"; |
| 13 | import { requireInsider } from "../../lib/access.server"; |
| 14 | import { movedTab, rulesAt, rulesPath } from "../../lib/tab-paths"; |
| 15 | import { Unavailable } from "../../components/boundary"; |
| 16 | import { isFailed, part } from "../../lib/partial"; |
| 17 | |
| 18 | export function meta({ params, ...args }: Route.MetaArgs) { |
| 19 | return page(args, { title: `Rules · ${params.owner}/${params.repo} · g1t` }); |
| 20 | } |
| 21 | |
| 22 | export async function loader({ params, context, request }: Route.LoaderArgs) { |
| 23 | const viewer = getViewer(context); |
| 24 | const url = new URL(request.url); |
| 25 | // Insights is a path: `?tab=insights`, the address before, is `/settings/rules/insights`. |
| 26 | const moved = movedTab(url, (asked) => rulesPath(`/${params.owner}/${params.repo}/settings/rules`, asked)); |
| 27 | if (moved) throw redirect(moved, 301); |
| 28 | // Admin, as for branch protection. |
| 29 | const { repo, access } = await requireInsider(context, params, "manage_protection"); |
| 30 | const tab = rulesAt(url.pathname); |
| 31 | const path = { namespace: params.owner, name: params.repo }; |
| 32 | const tagged = url.searchParams.get("tag"); |
| 33 | const branch = (tagged ?? url.searchParams.get("branch") ?? repo.defaultBranch).trim() || repo.defaultBranch; |
| 34 | // What holds for a branch, and Insights, each say in their place when |
| 35 | // they could not be read; the rulesets are what the page is about. |
| 36 | const [rulesets, effective, evaluations] = await Promise.all([ |
| 37 | work.listRulesets({ repo: path }, viewer, true), |
| 38 | tab === "rulesets" ? part(work.effectiveRules(path, branch, viewer, tagged ? "tag" : "branch"), `The rules for ${branch}`) : Promise.resolve(null), |
| 39 | tab === "insights" |
| 40 | ? part( |
| 41 | work.ruleEvaluations({ repo: path }, viewer, { |
| 42 | before: url.searchParams.get("before") ?? undefined, |
| 43 | problems_only: url.searchParams.get("problems") === "1", |
| 44 | }), |
| 45 | "Insights", |
| 46 | ) |
| 47 | : Promise.resolve(null), |
| 48 | ]); |
| 49 | return { |
| 50 | tab, |
| 51 | branch, |
| 52 | target: tagged ? ("tag" as const) : ("branch" as const), |
| 53 | defaultBranch: repo.defaultBranch, |
| 54 | rulesets: unwrap(rulesets), |
| 55 | effective: isFailed(effective) ? effective : effective?.ok ? effective.value : null, |
| 56 | evaluations: isFailed(evaluations) ? evaluations : evaluations?.ok ? evaluations.value : null, |
| 57 | editable: access.can.manage_protection && !repo.archivedAt, |
| 58 | }; |
| 59 | } |
| 60 | |
| 61 | export default function RepoRules({ loaderData, params }: Route.ComponentProps) { |
| 62 | const { tab, branch, target, defaultBranch, rulesets, effective, evaluations, editable } = loaderData; |
| 63 | const base = `/${params.owner}/${params.repo}`; |
| 64 | const [search] = useSearchParams(); |
| 65 | const own = rulesets.filter((ruleset) => ruleset.level === "repository"); |
| 66 | const inherited = rulesets.filter((ruleset) => ruleset.level === "workspace"); |
| 67 | const hrefFor = (id: string, level: "repository" | "workspace") => |
| 68 | level === "workspace" ? `/${params.owner}/-/rules/${id}` : `${base}/settings/rules/${id}`; |
| 69 | const tabLink = (name: "rulesets" | "insights") => { |
| 70 | const next = new URLSearchParams(search); |
| 71 | next.delete("before"); |
| 72 | const query = next.toString(); |
| 73 | return `${rulesPath(`${base}/settings/rules`, name)}${query ? `?${query}` : ""}`; |
| 74 | }; |
| 75 | return ( |
| 76 | <div className="max-w-4xl"> |
| 77 | <RepoSettingsHeading base={base} /> |
| 78 | <nav className="mb-6 flex gap-1 border-b border-line" aria-label="Rules"> |
| 79 | {(["rulesets", "insights"] as const).map((name) => ( |
| 80 | <Link |
| 81 | key={name} |
| 82 | to={tabLink(name)} |
| 83 | aria-current={tab === name ? "page" : undefined} |
| 84 | className={`-mb-px flex items-center gap-1.5 border-b-2 px-3 py-2 text-sm ${tab === name ? "border-accent text-fg" : "border-transparent text-muted hover:text-fg"}`} |
| 85 | > |
| 86 | {name === "rulesets" ? <ShieldCheck size={14} /> : <ChartColumn size={14} />} |
| 87 | {name === "rulesets" ? "Rulesets" : "Insights"} |
| 88 | </Link> |
| 89 | ))} |
| 90 | </nav> |
| 91 | |
| 92 | {tab === "rulesets" ? ( |
| 93 | <div className="space-y-10"> |
| 94 | <section> |
| 95 | <div className="mb-3 flex flex-wrap items-center justify-between gap-3"> |
| 96 | <div> |
| 97 | <h2 className="font-medium">This repository's rulesets</h2> |
| 98 | <p className="mt-1 text-sm text-muted">They stack with the workspace's: every rule of each that targets a branch holds there.</p> |
| 99 | </div> |
| 100 | {editable && ( |
| 101 | <ButtonLink to={`${base}/settings/rules/new`}> |
| 102 | <Plus size={15} /> New ruleset |
| 103 | </ButtonLink> |
| 104 | )} |
| 105 | </div> |
| 106 | <RulesetList |
| 107 | rulesets={own} |
| 108 | level="repository" |
| 109 | hrefFor={(ruleset) => hrefFor(ruleset.id, ruleset.level)} |
| 110 | empty={ |
| 111 | <> |
| 112 | No rulesets yet. Anyone who may push can push to any branch, and pull requests merge without approvals or checks. |
| 113 | {editable && ( |
| 114 | <> |
| 115 | {" "} |
| 116 | <Link to={`${base}/settings/rules/new`} className="text-fg underline underline-offset-2"> |
| 117 | Create one |
| 118 | </Link>{" "} |
| 119 | to protect {defaultBranch}. |
| 120 | </> |
| 121 | )} |
| 122 | </> |
| 123 | } |
| 124 | /> |
| 125 | </section> |
| 126 | {inherited.length > 0 && ( |
| 127 | <section> |
| 128 | <h2 className="font-medium">From the workspace</h2> |
| 129 | <p className="mt-1 mb-3 text-sm text-muted"> |
| 130 | Set for every repository of {params.owner} they select. Owners change them in the workspace's settings. |
| 131 | </p> |
| 132 | <RulesetList rulesets={inherited} level="repository" hrefFor={(ruleset) => hrefFor(ruleset.id, ruleset.level)} empty={null} /> |
| 133 | </section> |
| 134 | )} |
| 135 | <section> |
| 136 | <h2 className="font-medium">What holds for a branch</h2> |
| 137 | <p className="mt-1 mb-3 text-sm text-muted">Every rule of every ruleset that targets it, active ones first.</p> |
| 138 | <Form method="get" className="mb-4 flex flex-wrap items-center gap-2"> |
| 139 | <div className="relative w-72 max-w-full"> |
| 140 | <Search size={14} className="absolute top-1/2 left-3 -translate-y-1/2 text-faint" /> |
| 141 | <Input |
| 142 | name={target === "tag" ? "tag" : "branch"} |
| 143 | defaultValue={branch} |
| 144 | aria-label="Branch" |
| 145 | placeholder={defaultBranch} |
| 146 | className="pl-8 font-mono text-[0.8125rem]" |
| 147 | /> |
| 148 | </div> |
| 149 | <Button type="submit" variant="outline" size="sm" className="px-3 text-sm text-muted font-normal"> |
| 150 | Show rules |
| 151 | </Button> |
| 152 | </Form> |
| 153 | {isFailed(effective) ? ( |
| 154 | <Unavailable what={effective.what} /> |
| 155 | ) : effective ? ( |
| 156 | <EffectiveRulesView effective={effective} hrefFor={hrefFor} /> |
| 157 | ) : ( |
| 158 | <p className="text-sm text-muted">The rules for that name could not be read.</p> |
| 159 | )} |
| 160 | </section> |
| 161 | </div> |
| 162 | ) : isFailed(evaluations) ? ( |
| 163 | <Unavailable what={evaluations.what} /> |
| 164 | ) : evaluations ? ( |
| 165 | <InsightsView |
| 166 | page={evaluations} |
| 167 | olderHref={evaluations.next ? `${base}/settings/rules/insights?before=${encodeURIComponent(evaluations.next)}` : null} |
| 168 | /> |
| 169 | ) : ( |
| 170 | <p className="text-sm text-muted">Insights could not be read just now.</p> |
| 171 | )} |
| 172 | </div> |
| 173 | ); |
| 174 | } |