220 lines · 12.6 KBRaw
| 1 | import { MAX_DESCRIPTION_LENGTH, type CiEnvironment } from "@refs/backend/contract"; |
| 2 | import { Banner, Button, Checkbox, Dialog, Empty, Input, InputArea, LayerCard, Tabs } from "@refs/design-system"; |
| 3 | import { ShieldCheckIcon, TrashIcon } from "@refs/design-system/icons"; |
| 4 | import { useState } from "react"; |
| 5 | import { Form, data, redirect, useNavigation } from "react-router"; |
| 6 | import type { Route } from "./+types/repo-settings"; |
| 7 | |
| 8 | import { VisibilityField } from "../components/repo-form"; |
| 9 | import { SecretTable } from "../components/secret-table"; |
| 10 | import { failed, formText, found, refs, fullName } from "../lib/session"; |
| 11 | |
| 12 | async function settingsFor(context: Route.LoaderArgs["context"], name: string) { |
| 13 | const repo = await found(refs(context).repo(name)); |
| 14 | return { name: repo.summary.name, settings: await found(repo.settings()) }; |
| 15 | } |
| 16 | |
| 17 | export const meta: Route.MetaFunction = ({ params }) => [{ title: `Settings · ${fullName(params)} · refs` }]; |
| 18 | |
| 19 | export async function loader({ params, context }: Route.LoaderArgs) { |
| 20 | const { name, settings } = await settingsFor(context, fullName(params)); |
| 21 | const [secrets, environments, protectedBranches] = await Promise.all([settings.ci.secrets(), settings.ci.environments(), settings.ci.protectedBranches()]); |
| 22 | return { name, description: settings.description, visibility: settings.visibility, secrets, environments, protectedBranches }; |
| 23 | } |
| 24 | |
| 25 | function lines(text: string): string[] { |
| 26 | return text.split(/[\n,]/).map((line) => line.trim()).filter(Boolean); |
| 27 | } |
| 28 | |
| 29 | export async function action({ request, params, context }: Route.ActionArgs) { |
| 30 | const { settings } = await settingsFor(context, fullName(params)); |
| 31 | const { ci } = settings; |
| 32 | const form = await request.formData(); |
| 33 | const intent = formText(form, "intent"); |
| 34 | |
| 35 | if (intent === "delete") { |
| 36 | const outcome = await settings.delete(formText(form, "confirm")); |
| 37 | if (!outcome.ok) return failed(outcome.problem, { intent, saved: false }); |
| 38 | throw redirect("/"); |
| 39 | } |
| 40 | |
| 41 | const environment = formText(form, "environment") || null; |
| 42 | const outcome = |
| 43 | intent === "update" |
| 44 | ? await settings.update({ description: formText(form, "description"), visibility: formText(form, "visibility") === "public" ? "public" : "private" }) |
| 45 | : intent === "secret-set" |
| 46 | ? await ci.setSecret({ name: formText(form, "name"), value: String(form.get("value") ?? ""), environment }) |
| 47 | : intent === "secret-delete" |
| 48 | ? await ci.deleteSecret(formText(form, "name"), environment) |
| 49 | : intent === "environment-save" |
| 50 | ? await ci.saveEnvironment({ name: formText(form, "name"), requiresApproval: form.get("approval") === "on", waitSeconds: Math.round(Number(formText(form, "wait") || 0) * 60), branches: lines(formText(form, "branches")) }) |
| 51 | : intent === "environment-delete" |
| 52 | ? await ci.deleteEnvironment(formText(form, "name")) |
| 53 | : intent === "protected-save" |
| 54 | ? await ci.setProtectedBranches(lines(formText(form, "patterns"))) |
| 55 | : null; |
| 56 | if (!outcome) return data({ intent, error: "Unknown action.", saved: false }, { status: 400 }); |
| 57 | return outcome.ok ? { intent, error: undefined, saved: true } : failed(outcome.problem, { intent, saved: false }); |
| 58 | } |
| 59 | |
| 60 | function EnvironmentForm({ environment }: { environment: CiEnvironment | null }) { |
| 61 | const navigation = useNavigation(); |
| 62 | const saving = navigation.state === "submitting" && navigation.formData?.get("intent") === "environment-save" && (environment === null || navigation.formData.get("name") === environment.name); |
| 63 | const [name, setName] = useState(environment?.name ?? ""); |
| 64 | return ( |
| 65 | <Form key={environment ? environment.name : navigation.state} method="post" className="flex flex-col gap-4 p-4"> |
| 66 | <input type="hidden" name="intent" value="environment-save" /> |
| 67 | {environment ? <input type="hidden" name="name" value={environment.name} /> : <Input label="Name" name="name" placeholder="production" value={name} onChange={(event) => setName(event.target.value)} required />} |
| 68 | <div className="flex flex-wrap items-start gap-4"> |
| 69 | <Input label="Wait before a deploy (minutes)" name="wait" type="number" min={0} max={43200} defaultValue={environment ? Math.round(environment.waitSeconds / 60) : 0} className="w-40" /> |
| 70 | <div className="min-w-60 flex-1"> |
| 71 | <Input label="Branches that may deploy" name="branches" placeholder="main, release/**" defaultValue={environment?.branches.join(", ") ?? ""} description="Leave empty to allow every branch." /> |
| 72 | </div> |
| 73 | </div> |
| 74 | <Checkbox name="approval" defaultChecked={environment?.requiresApproval ?? true} label="A person approves each deploy" /> |
| 75 | <div className="flex gap-2"> |
| 76 | <Button type="submit" variant={environment ? "secondary" : "primary"} loading={saving} disabled={!environment && !name}> |
| 77 | {environment ? "Save environment" : "Create environment"} |
| 78 | </Button> |
| 79 | </div> |
| 80 | </Form> |
| 81 | ); |
| 82 | } |
| 83 | |
| 84 | function DeleteRepository({ name, error }: { name: string; error?: string }) { |
| 85 | const [open, setOpen] = useState(Boolean(error)); |
| 86 | const [confirm, setConfirm] = useState(""); |
| 87 | const submitting = useNavigation().state === "submitting"; |
| 88 | return ( |
| 89 | <Dialog.Root open={open} onOpenChange={setOpen}> |
| 90 | <Dialog.Trigger |
| 91 | render={(props) => ( |
| 92 | <Button {...props} variant="secondary-destructive" icon={<TrashIcon />}> |
| 93 | Delete repository… |
| 94 | </Button> |
| 95 | )} |
| 96 | /> |
| 97 | <Dialog size="lg" className="p-0"> |
| 98 | <div className="px-6 py-4 shadow-[inset_0_-1px_0_var(--hairline)]"> |
| 99 | <Dialog.Title className="m-0 text-lg font-semibold">Delete {name}</Dialog.Title> |
| 100 | </div> |
| 101 | <Form method="post" className="flex flex-col gap-4 p-6"> |
| 102 | <input type="hidden" name="intent" value="delete" /> |
| 103 | <Dialog.Description className="m-0 text-muted"> |
| 104 | This permanently deletes <span className="font-medium text-ink">{name}</span>, its entire history and its tasks. It can't be undone. |
| 105 | </Dialog.Description> |
| 106 | {error && <Banner variant="error" description={error} />} |
| 107 | <Input label={`Type "${name}" to confirm`} name="confirm" value={confirm} onChange={(event) => setConfirm(event.target.value)} autoComplete="off" /> |
| 108 | <div className="flex justify-end gap-2"> |
| 109 | <Dialog.Close render={(props) => <Button {...props} variant="secondary">Cancel</Button>} /> |
| 110 | <Button type="submit" variant="destructive" disabled={confirm !== name} loading={submitting}> |
| 111 | Delete repository |
| 112 | </Button> |
| 113 | </div> |
| 114 | </Form> |
| 115 | </Dialog> |
| 116 | </Dialog.Root> |
| 117 | ); |
| 118 | } |
| 119 | |
| 120 | export default function RepoSettings({ loaderData, actionData }: Route.ComponentProps) { |
| 121 | const { name, secrets, environments, protectedBranches } = loaderData; |
| 122 | const navigation = useNavigation(); |
| 123 | const submitting = navigation.state === "submitting"; |
| 124 | const savingUpdate = submitting && navigation.formData?.get("intent") === "update"; |
| 125 | const savingPatterns = submitting && navigation.formData?.get("intent") === "protected-save"; |
| 126 | const updateError = actionData?.intent === "update" ? actionData.error : undefined; |
| 127 | const ciError = actionData?.intent !== "update" && actionData?.intent !== "delete" ? actionData?.error : undefined; |
| 128 | const [description, setDescription] = useState(loaderData.description); |
| 129 | const [visibility, setVisibility] = useState(loaderData.visibility); |
| 130 | const [patterns, setPatterns] = useState(protectedBranches.join("\n")); |
| 131 | const [ciTab, setCiTab] = useState<"secrets" | "environments">("secrets"); |
| 132 | const changed = description !== loaderData.description || visibility !== loaderData.visibility; |
| 133 | return ( |
| 134 | <div className="flex max-w-3xl flex-col gap-8"> |
| 135 | <section className="flex flex-col gap-3"> |
| 136 | <h2 className="m-0 text-base font-semibold">General</h2> |
| 137 | {updateError && <Banner variant="error" description={updateError} />} |
| 138 | {actionData?.intent === "update" && actionData.saved && <Banner variant="success" description="Settings saved." />} |
| 139 | <LayerCard className="p-5"> |
| 140 | <Form method="post" className="flex flex-col gap-5"> |
| 141 | <input type="hidden" name="intent" value="update" /> |
| 142 | <Input label="Description" name="description" value={description} onChange={(event) => setDescription(event.target.value)} maxLength={MAX_DESCRIPTION_LENGTH} /> |
| 143 | <VisibilityField defaultValue={loaderData.visibility} onChange={setVisibility} /> |
| 144 | <div> |
| 145 | <Button type="submit" variant="primary" disabled={!changed} loading={savingUpdate}> |
| 146 | Save changes |
| 147 | </Button> |
| 148 | </div> |
| 149 | </Form> |
| 150 | </LayerCard> |
| 151 | </section> |
| 152 | |
| 153 | <section className="flex flex-col gap-3"> |
| 154 | <div className="flex flex-col gap-1"> |
| 155 | <h2 className="m-0 text-base font-semibold">Protected branches</h2> |
| 156 | <p className="m-0 text-compact text-muted">A push by a person to one of these branches gets every secret, environment secrets too. Use * for one path segment and ** for many.</p> |
| 157 | </div> |
| 158 | <LayerCard className="p-4"> |
| 159 | <Form method="post" className="flex flex-col gap-4"> |
| 160 | <input type="hidden" name="intent" value="protected-save" /> |
| 161 | <InputArea label="Branch patterns, one per line" name="patterns" rows={3} value={patterns} onValueChange={setPatterns} className="font-mono" placeholder={"main\nrelease/**"} /> |
| 162 | <div> |
| 163 | <Button type="submit" variant="secondary" icon={<ShieldCheckIcon />} disabled={patterns === protectedBranches.join("\n")} loading={savingPatterns}> |
| 164 | Save protected branches |
| 165 | </Button> |
| 166 | </div> |
| 167 | </Form> |
| 168 | </LayerCard> |
| 169 | </section> |
| 170 | |
| 171 | <section className="flex flex-col gap-3"> |
| 172 | <Tabs variant="segmented" className="self-start" value={ciTab} onValueChange={(value) => setCiTab(value as typeof ciTab)} tabs={[{ value: "secrets", label: "Secrets" }, { value: "environments", label: "Environments" }]} /> |
| 173 | {ciError && <Banner variant="error" description={ciError} />} |
| 174 | {ciTab === "secrets" ? ( |
| 175 | <> |
| 176 | <p className="m-0 text-compact text-muted"> |
| 177 | A job gets a secret only when it names it under <code className="font-mono">secrets</code> in <code className="font-mono">.refs/ci.yml</code>. An environment secret wins over a repository secret, and a repository secret wins over an organization secret. |
| 178 | </p> |
| 179 | <SecretTable secrets={secrets} environments={environments.map((environment) => environment.name)} empty="Add a token or key above. Runs that agents start never get secrets." /> |
| 180 | </> |
| 181 | ) : ( |
| 182 | <> |
| 183 | <p className="m-0 text-compact text-muted">A job that names an environment runs only when its rules allow it. Its environment secrets need a person's approval or a push by a person to a protected branch.</p> |
| 184 | {environments.map((environment) => ( |
| 185 | <LayerCard key={environment.name}> |
| 186 | <LayerCard.Secondary className="justify-between"> |
| 187 | <span className="font-mono">{environment.name}</span> |
| 188 | <Form method="post"> |
| 189 | <input type="hidden" name="intent" value="environment-delete" /> |
| 190 | <input type="hidden" name="name" value={environment.name} /> |
| 191 | <Button type="submit" variant="ghost" size="sm" icon={<TrashIcon />}> |
| 192 | Delete |
| 193 | </Button> |
| 194 | </Form> |
| 195 | </LayerCard.Secondary> |
| 196 | <EnvironmentForm environment={environment} /> |
| 197 | </LayerCard> |
| 198 | ))} |
| 199 | <LayerCard> |
| 200 | <LayerCard.Secondary>New environment</LayerCard.Secondary> |
| 201 | {environments.length === 0 && <Empty size="sm" title="No environments" description="Create one, such as production, to hold deploys for approval." />} |
| 202 | <EnvironmentForm environment={null} /> |
| 203 | </LayerCard> |
| 204 | </> |
| 205 | )} |
| 206 | </section> |
| 207 | |
| 208 | <section className="flex flex-col gap-3"> |
| 209 | <h2 className="m-0 text-base font-semibold text-danger">Danger zone</h2> |
| 210 | <LayerCard className="flex-row flex-wrap items-center justify-between gap-4 p-5 shadow-[inset_0_0_0_1px_color-mix(in_oklab,var(--danger)_40%,transparent)]"> |
| 211 | <div className="flex flex-col gap-1"> |
| 212 | <span className="font-semibold">Delete this repository</span> |
| 213 | <span className="text-compact text-muted">Removes the repository, its history and its tasks permanently.</span> |
| 214 | </div> |
| 215 | <DeleteRepository name={name} error={actionData?.intent === "delete" ? actionData.error : undefined} /> |
| 216 | </LayerCard> |
| 217 | </section> |
| 218 | </div> |
| 219 | ); |
| 220 | } |