g1t/apps/web/app/routes/repo/settings-branches.tsx

169 lines8,912 bytesCodeBlame
1import { ChevronRight, ShieldCheck } from "lucide-react";
2import { Form, Link, useNavigation } from "react-router";
3
4import type { Route } from "./+types/settings-branches";
5import { page } from "../../lib/meta";
6import { RepoSettingsHeading } from "../../components/repo-settings-heading";
7import { SettingChoice as Choice, SettingsSection as Section, SettingToggle as Toggle } from "../../components/settings-section";
8import { Button, ErrorText, TimeAgo } from "../../components/ui";
9import { repos, work } from "../../lib/services.server";
10import { assertSameOrigin, getViewer, requireUser, unwrap } from "../../lib/session.server";
11import { requireCapability, requireInsider } from "../../lib/access.server";
12
13export function meta({ params, ...args }: Route.MetaArgs) {
14 return page(args, { title: `Branches and merging · ${params.owner}/${params.repo} · g1t` });
15}
16
17export async function loader({ params, context }: Route.LoaderArgs) {
18 const viewer = getViewer(context);
19 // Maintain and up; to anyone without a role here the page does not exist.
20 await requireInsider(context, params, "manage_protection");
21 const path = { namespace: params.owner, name: params.repo };
22 const [repo, settings] = await Promise.all([repos.get(path, viewer), work.getSettings(path, viewer)]);
23 return { repo: unwrap(repo), settings: unwrap(settings) };
24}
25
26/** A whole number from a form field, kept within `min` and `max`. */
27function count(value: FormDataEntryValue | null, min: number, max: number): number {
28 const number = Math.trunc(Number(value));
29 return Number.isFinite(number) ? Math.min(Math.max(number, min), max) : min;
30}
31
32export async function action({ request, params, context }: Route.ActionArgs) {
33 assertSameOrigin(request);
34 const user = requireUser(context, request);
35 await requireCapability(context, params, "manage_protection");
36 const form = await request.formData();
37 const path = { namespace: params.owner, name: params.repo };
38 const on = (name: string) => form.get(name) === "on";
39 // Protection belongs to the repository and how its pull requests are
40 // handled to the work service; the page is one form over both.
41 const repo = await repos.update(user, path, { protected: on("protected") });
42 if (!repo.ok) return { saved: false, error: repo.error.message };
43 const settings = await work.updateSettings(user, path, {
44 autoMerge: on("autoMerge"),
45 requireUpToDate: on("requireUpToDate"),
46 requiredApprovals: count(form.get("requiredApprovals"), 0, 6),
47 countAgentApprovals: on("countAgentApprovals"),
48 allowIgnoringChecks: !on("requireChecks"),
49 agentReview: on("agentReview"),
50 maxRevisions: count(form.get("maxRevisions"), 0, 5),
51 mergeQueue: on("mergeQueue"),
52 });
53 return settings.ok ? { saved: true, error: null } : { saved: false, error: settings.error.message };
54}
55
56export default function BranchSettings({ loaderData, actionData }: Route.ComponentProps) {
57 const { repo, settings } = loaderData;
58 const saving = useNavigation().state === "submitting";
59 const branch = repo.defaultBranch;
60 const base = `/${repo.namespace}/${repo.name}`;
61 const archived = Boolean(repo.archivedAt);
62 return (
63 <>
64 <RepoSettingsHeading base={base} />
65 <Form method="post" className="max-w-4xl">
66 <fieldset disabled={archived} className="min-w-0 space-y-8">
67 <Section title="Branch protection" about={`Rules for ${branch}, the branch everything lands on.`}>
68 <Toggle name="protected" on={repo.protected} title={`Require a pull request to change ${branch}`}>
69 Pushing to {branch} is refused, for members and agents alike, and git says why. Changes reach it only by
70 merging a pull request. The first push to an empty repository is still allowed.
71 </Toggle>
72 <Choice
73 name="requiredApprovals"
74 value={settings.requiredApprovals}
75 title="Required approvals"
76 options={[
77 [0, "None"],
78 [1, "1"],
79 [2, "2"],
80 [3, "3"],
81 ]}
82 >
83 How many reviewers must approve before a pull request can merge. A reviewer who has since asked for
84 changes blocks it, and nobody approves their own.
85 </Choice>
86 <Toggle name="countAgentApprovals" on={settings.countAgentApprovals} title="A g1t agent's approval counts">
87 With this off, required approvals have to come from people, and an agent's review is advice.
88 </Toggle>
89 <Toggle name="requireChecks" on={!settings.allowIgnoringChecks} title="Require acceptance checks to pass">
90 With this off, a member can choose to merge although the issue's checks failed or have not finished. With
91 it on, nobody can.
92 </Toggle>
93 <Toggle
94 name="requireUpToDate"
95 on={settings.requireUpToDate}
96 title="Require pull requests to be up to date before merging"
97 >
98 With this off, a pull request can be merged after {branch} has moved: g1t brings it up to date as part of
99 merging, and asks you only if there is a conflict it cannot resolve. With it on, it has to catch up first
100 and its checks run again on the result, so what lands is exactly what was checked.
101 </Toggle>
102 <Toggle name="mergeQueue" on={settings.mergeQueue} title="Merge through a queue">
103 Merging adds a pull request to the queue instead of changing {branch} at once. g1t tests it together with
104 every pull request ahead of it, several combinations at a time, and {branch} only ever moves to a
105 combination whose checks passed. One that fails leaves the queue and goes back to its author, and the ones
106 behind it are tested again without it.
107 </Toggle>
108 </Section>
109
110 <Section
111 title="g1t agents"
112 about="What happens to a pull request a g1t agent makes, from the moment it is ready."
113 >
114 <Toggle name="agentReview" on={settings.agentReview} title="Review by a second agent">
115 A different agent reads each change and posts comments on lines, a summary and a verdict. If it asks for
116 changes, the author is sent back to make them. With this off, review is left to people.
117 </Toggle>
118 <Choice
119 name="maxRevisions"
120 value={settings.maxRevisions}
121 title="Revisions before asking you"
122 options={[
123 [0, "None"],
124 [1, "1"],
125 [2, "2"],
126 [3, "3"],
127 [5, "5"],
128 ]}
129 >
130 How many times an agent is sent back to fix failed checks or address a review before g1t stops and the
131 pull request says it needs you.
132 </Choice>
133 <Toggle name="autoMerge" on={settings.autoMerge} title="Merge automatically when ready">
134 A g1t agent's pull request lands without anyone pressing merge once every rule above is met. With this
135 off, it waits for a member. Pull requests from people and from other agents always wait.
136 </Toggle>
137 <Link
138 to={`${base}/settings/guardrails`}
139 className="group flex items-center gap-3 rounded-xl border border-line p-4 transition-colors hover:border-line-strong hover:bg-surface"
140 >
141 <ShieldCheck size={16} className="shrink-0 text-accent" />
142 <span className="min-w-0 grow">
143 <span className="block text-sm font-medium">Guardrails</span>
144 <span className="mt-0.5 block text-sm text-muted">
145 What agents may reach, run and spend while they work on this repository.
146 </span>
147 </span>
148 <ChevronRight size={16} className="shrink-0 text-faint transition-transform group-hover:translate-x-0.5" />
149 </Link>
150 </Section>
151
152 <div className="sticky bottom-0 -mx-4 flex flex-wrap items-center gap-4 border-t border-line bg-bg/90 px-4 py-4 backdrop-blur">
153 <Button type="submit" disabled={saving || archived}>
154 {saving ? "Saving…" : "Save settings"}
155 </Button>
156 {actionData?.saved && <span className="text-sm text-muted">Saved.</span>}
157 <ErrorText>{actionData?.error}</ErrorText>
158 {settings.updatedBy && settings.updatedAt && !actionData && (
159 <span className="text-xs text-faint">
160 Merge rules last changed by <span className="font-mono">{settings.updatedBy}</span>{" "}
161 <TimeAgo at={settings.updatedAt} />
162 </span>
163 )}
164 </div>
165 </fieldset>
166 </Form>
167 </>
168 );
169}