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