Skip to content

Where people and agents ship software together. The open-source git platform for the whole job: issues, agents, checks and deploys to the edge.

g1t.sh
204 linesCodeBlameRaw
1import { Download, Filter } from "lucide-react";
2import { Form, Link, data } from "react-router";
3
4import type { Route } from "./+types/audit";
5import { page } from "../../lib/meta";
6import { AuditTable } from "../../components/audit";
7import { ButtonLink, EmptyState, Field, Input } from "../../components/ui";
8import { Button } from "../../components/ui/button";
9import { Card } from "../../components/ui/card";
10import { SelectField } from "../../components/ui/select";
11import { type AuditFilters, filterHref, parseFilters, toQuery } from "../../lib/audit";
12import { auditPage, auditRetention } from "../../lib/audit.server";
13import { repos } from "../../lib/services.server";
14import { requireUser, roleIn } from "../../lib/session.server";
15
16const PAGE_SIZE = 100;
17
18/** Actions worth offering in the filter; any other can be typed. */
19const COMMON_ACTIONS = [
20 "git.push",
21 "git.fetch",
22 "create_issue",
23 "add_comment",
24 "record_session",
25 "mark_pull_request_ready",
26 "review_pull_request",
27 "merge_pull_request",
28 "remember",
29];
30
31export function meta({ params, ...args }: Route.MetaArgs) {
32 return page(args, { title: `Audit log · ${params.owner} · g1t` });
33}
34
35export async function loader({ params, context, request }: Route.LoaderArgs) {
36 const viewer = requireUser(context, request);
37 const workspace = params.owner.toLowerCase();
38 const role = roleIn(viewer, workspace);
39 if (!role) throw data("Only members of this workspace can read its audit log.", { status: 404 });
40 const filters = parseFilters(new URL(request.url).searchParams);
41 const { visibility: _, ...query } = toQuery(workspace, { kind: "all" }, filters, PAGE_SIZE);
42 const [found, projects, retention] = await Promise.all([
43 auditPage(viewer, query),
44 repos.list(viewer, { namespace: workspace }).catch(() => []),
45 auditRetention(workspace),
46 ]);
47 return {
48 workspace,
49 role,
50 filters,
51 entries: found?.entries ?? [],
52 next: found?.next ?? null,
53 projects: projects.map((repo) => repo.name),
54 retention,
55 };
56}
57
58function FilterField({
59 label,
60 name,
61 value,
62 placeholder,
63 list,
64}: {
65 label: string;
66 name: keyof AuditFilters;
67 value: string;
68 placeholder?: string;
69 list?: string;
70}) {
71 return (
72 <Field label={label}>
73 <Input name={name} defaultValue={value} placeholder={placeholder} list={list} />
74 </Field>
75 );
76}
77
78/** As tall as the inputs beside it. */
79const SELECT = "h-auto py-2";
80
81export default function WorkspaceAudit({ loaderData }: Route.ComponentProps) {
82 const { workspace, role, filters, entries, next, projects, retention } = loaderData;
83 const base = `/${workspace}/-/audit`;
84 const filtered = Object.entries(filters).some(([key, value]) => key !== "before" && value);
85 const exportHref = (format: "csv" | "json") => filterHref(`${base}/export`, { ...filters, before: "" }) + `${filtered ? "&" : "?"}format=${format}`;
86 return (
87 <div>
88 <p className="max-w-3xl text-sm text-muted">
89 Every change people, apps and workspace tokens make through the API, MCP and git: who did it, on whose behalf, with which
90 credential, to what, and whether it was allowed. Refusals name the rule that refused them.
91 {role === "owner"
92 ? " As an owner you see the whole workspace."
93 : " As a member you see what was done to the workspace's projects, and what was done by you or on your behalf."}
94 {retention != null &&
95 ` The log goes back ${retention} days, and exports the same; older entries are deleted each day.`}
96 </p>
97
98 <Card asChild className="mt-6 p-4">
99 <Form method="get">
100 <div className="grid gap-4 sm:grid-cols-2 lg:grid-cols-4">
101 <FilterField label="Actor" name="actor" value={filters.actor} placeholder="A person, or whom an app worked for" />
102 <FilterField label="Action" name="action" value={filters.action} placeholder="git.push" list="audit-actions" />
103 <FilterField label="Project" name="project" value={filters.project} placeholder="Any" list="audit-projects" />
104 <Field label="Outcome">
105 <SelectField
106 key={`outcome-${filters.outcome}`}
107 name="outcome"
108 defaultValue={filters.outcome}
109 className={SELECT}
110 options={[
111 { value: "", label: "Any" },
112 { value: "allowed", label: "Allowed" },
113 { value: "denied", label: "Denied" },
114 ]}
115 />
116 </Field>
117 <Field label="Who">
118 <SelectField
119 key={`kind-${filters.kind}`}
120 name="kind"
121 defaultValue={filters.kind}
122 className={SELECT}
123 options={[
124 { value: "", label: "Anyone" },
125 { value: "person", label: "People" },
126 { value: "workspace", label: "Workspace tokens" },
127 ]}
128 />
129 </Field>
130 <Field label="From">
131 <Input type="date" name="from" defaultValue={filters.from} />
132 </Field>
133 <Field label="To">
134 <Input type="date" name="to" defaultValue={filters.to} />
135 </Field>
136 </div>
137 {filters.run && <input type="hidden" name="run" value={filters.run} />}
138 <datalist id="audit-actions">
139 {COMMON_ACTIONS.map((action) => (
140 <option key={action} value={action} />
141 ))}
142 </datalist>
143 <datalist id="audit-projects">
144 {projects.map((name) => (
145 <option key={name} value={name} />
146 ))}
147 </datalist>
148 <div className="mt-4 flex flex-wrap items-center gap-3">
149 <Button type="submit">
150 <Filter size={14} />
151 Filter
152 </Button>
153 {filtered && (
154 <Link to={base} className="text-sm text-muted hover:text-fg">
155 Clear filters
156 </Link>
157 )}
158 {filters.run && (
159 <span className="font-mono text-xs text-muted">
160 Run {filters.run}
161 </span>
162 )}
163 <span className="grow" />
164 <ButtonLink variant="outline" to={exportHref("csv")} reloadDocument>
165 <Download size={14} />
166 CSV
167 </ButtonLink>
168 <ButtonLink variant="outline" to={exportHref("json")} reloadDocument>
169 <Download size={14} />
170 JSON
171 </ButtonLink>
172 </div>
173 </Form>
174 </Card>
175
176 <div className="mt-6">
177 {entries.length === 0 ? (
178 <EmptyState title={filtered ? "Nothing matches these filters" : "Nothing recorded yet"}>
179 {filtered
180 ? "Try a wider time range, or clear the filters."
181 : "Entries appear as people, apps and workspace tokens change things through the API, MCP and git."}
182 </EmptyState>
183 ) : (
184 <AuditTable entries={entries} base={base} />
185 )}
186 </div>
187
188 {(next || filters.before) && (
189 <div className="mt-4 flex gap-4 text-sm">
190 {filters.before && (
191 <Link to={filterHref(base, { ...filters, before: "" })} className="text-muted hover:text-fg">
192 Newest
193 </Link>
194 )}
195 {next && (
196 <Link to={filterHref(base, filters, { before: next })} className="text-muted hover:text-fg">
197 Older
198 </Link>
199 )}
200 </div>
201 )}
202 </div>
203 );
204}