import { MAX_DESCRIPTION_LENGTH, type CiEnvironment } from "@refs/backend/contract"; import { Banner, Button, Checkbox, Dialog, Empty, Input, InputArea, LayerCard, Tabs } from "@refs/design-system"; import { ShieldCheckIcon, TrashIcon } from "@refs/design-system/icons"; import { useState } from "react"; import { Form, data, redirect, useNavigation } from "react-router"; import type { Route } from "./+types/repo-settings"; import { VisibilityField } from "../components/repo-form"; import { SecretTable } from "../components/secret-table"; import { failed, formText, found, refs, fullName } from "../lib/session"; async function settingsFor(context: Route.LoaderArgs["context"], name: string) { const repo = await found(refs(context).repo(name)); return { name: repo.summary.name, settings: await found(repo.settings()) }; } export const meta: Route.MetaFunction = ({ params }) => [{ title: `Settings · ${fullName(params)} · refs` }]; export async function loader({ params, context }: Route.LoaderArgs) { const { name, settings } = await settingsFor(context, fullName(params)); const [secrets, environments, protectedBranches] = await Promise.all([settings.ci.secrets(), settings.ci.environments(), settings.ci.protectedBranches()]); return { name, description: settings.description, visibility: settings.visibility, secrets, environments, protectedBranches }; } function lines(text: string): string[] { return text.split(/[\n,]/).map((line) => line.trim()).filter(Boolean); } export async function action({ request, params, context }: Route.ActionArgs) { const { settings } = await settingsFor(context, fullName(params)); const { ci } = settings; const form = await request.formData(); const intent = formText(form, "intent"); if (intent === "delete") { const outcome = await settings.delete(formText(form, "confirm")); if (!outcome.ok) return failed(outcome.problem, { intent, saved: false }); throw redirect("/"); } const environment = formText(form, "environment") || null; const outcome = intent === "update" ? await settings.update({ description: formText(form, "description"), visibility: formText(form, "visibility") === "public" ? "public" : "private" }) : intent === "secret-set" ? await ci.setSecret({ name: formText(form, "name"), value: String(form.get("value") ?? ""), environment }) : intent === "secret-delete" ? await ci.deleteSecret(formText(form, "name"), environment) : intent === "environment-save" ? 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")) }) : intent === "environment-delete" ? await ci.deleteEnvironment(formText(form, "name")) : intent === "protected-save" ? await ci.setProtectedBranches(lines(formText(form, "patterns"))) : null; if (!outcome) return data({ intent, error: "Unknown action.", saved: false }, { status: 400 }); return outcome.ok ? { intent, error: undefined, saved: true } : failed(outcome.problem, { intent, saved: false }); } function EnvironmentForm({ environment }: { environment: CiEnvironment | null }) { const navigation = useNavigation(); const saving = navigation.state === "submitting" && navigation.formData?.get("intent") === "environment-save" && (environment === null || navigation.formData.get("name") === environment.name); const [name, setName] = useState(environment?.name ?? ""); return (
{environment ? : setName(event.target.value)} required />}
); } function DeleteRepository({ name, error }: { name: string; error?: string }) { const [open, setOpen] = useState(Boolean(error)); const [confirm, setConfirm] = useState(""); const submitting = useNavigation().state === "submitting"; return ( ( )} />
Delete {name}
This permanently deletes {name}, its entire history and its tasks. It can't be undone. {error && } setConfirm(event.target.value)} autoComplete="off" />
} />
); } export default function RepoSettings({ loaderData, actionData }: Route.ComponentProps) { const { name, secrets, environments, protectedBranches } = loaderData; const navigation = useNavigation(); const submitting = navigation.state === "submitting"; const savingUpdate = submitting && navigation.formData?.get("intent") === "update"; const savingPatterns = submitting && navigation.formData?.get("intent") === "protected-save"; const updateError = actionData?.intent === "update" ? actionData.error : undefined; const ciError = actionData?.intent !== "update" && actionData?.intent !== "delete" ? actionData?.error : undefined; const [description, setDescription] = useState(loaderData.description); const [visibility, setVisibility] = useState(loaderData.visibility); const [patterns, setPatterns] = useState(protectedBranches.join("\n")); const [ciTab, setCiTab] = useState<"secrets" | "environments">("secrets"); const changed = description !== loaderData.description || visibility !== loaderData.visibility; return (

General

{updateError && } {actionData?.intent === "update" && actionData.saved && }
setDescription(event.target.value)} maxLength={MAX_DESCRIPTION_LENGTH} />

Protected branches

A push by a person to one of these branches gets every secret, environment secrets too. Use * for one path segment and ** for many.

setCiTab(value as typeof ciTab)} tabs={[{ value: "secrets", label: "Secrets" }, { value: "environments", label: "Environments" }]} /> {ciError && } {ciTab === "secrets" ? ( <>

A job gets a secret only when it names it under secrets in .refs/ci.yml. An environment secret wins over a repository secret, and a repository secret wins over an organization secret.

environment.name)} empty="Add a token or key above. Runs that agents start never get secrets." /> ) : ( <>

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.

{environments.map((environment) => ( {environment.name}
))} New environment {environments.length === 0 && } )}

Danger zone

Delete this repository Removes the repository, its history and its tasks permanently.
); }