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
440 linesCodeBlameRaw
1/**
2 * A workspace's pages and where the sidebar finds them. `g1t.sh/<workspace>`
3 * is the workspace's own page, its header and overview; Projects, Packages,
4 * Teams, People, Insights and Settings are pages of their own under `-/`,
5 * each reached from the sidebar and each with its own heading. The sidebar
6 * is always the current workspace's.
7 */
8
9/** The workspace's pages a person moves between, and which the sidebar lists. */
10export type WorkspacePageKey = "overview" | "projects" | "packages" | "teams" | "people" | "insights";
11
12/** Those pages under `-/`: the overview is the workspace's own address. */
13export const WORKSPACE_PAGES = ["projects", "packages", "teams", "people", "insights"] as const;
14
15/**
16 * Which of those pages a path is, under `/<workspace>`: null for the
17 * workspace's other pages (settings, Usage and the rest), for
18 * one package's or one team's page, and for anything else.
19 */
20export function workspacePage(pathname: string, slug: string): WorkspacePageKey | null {
21 const parts = pagePath(pathname).split("/").filter(Boolean);
22 if (parts[0]?.toLowerCase() !== slug.toLowerCase()) return null;
23 if (parts.length === 1) return "overview";
24 if (parts[1] !== "-" || parts.length !== 3) return null;
25 return (WORKSPACE_PAGES as readonly string[]).includes(parts[2]!) ? (parts[2] as WorkspacePageKey) : null;
26}
27
28/**
29 * Workspace mode's pages, in the groups its sidebar lists them under: the
30 * workspace's settings area, every page one click away. `owner` marks the
31 * pages only owners open; the sidebar shows members the rest.
32 * `path` is under `-/`.
33 */
34export type WorkspaceSettingsPage = { key: string; label: string; path: string; owner?: boolean };
35
36export type WorkspaceSettingsGroup = { key: string; title: string; pages: WorkspaceSettingsPage[] };
37
38export const WORKSPACE_SETTINGS_GROUPS: readonly WorkspaceSettingsGroup[] = [
39 {
40 key: "general",
41 title: "General",
42 pages: [{ key: "settings", label: "General", path: "settings", owner: true }],
43 },
44 {
45 key: "ai",
46 title: "AI",
47 pages: [{ key: "ai", label: "Models", path: "ai" }],
48 },
49 {
50 key: "access",
51 title: "Access",
52 pages: [
53 { key: "members", label: "Members", path: "members" },
54 { key: "invitations", label: "Invitations", path: "members/invitations", owner: true },
55 { key: "permissions", label: "Permissions", path: "permissions", owner: true },
56 { key: "tokens", label: "Access tokens", path: "tokens" },
57 { key: "personal-access-tokens", label: "Personal access tokens", path: "personal-access-tokens", owner: true },
58 ],
59 },
60 {
61 key: "money",
62 title: "Money",
63 pages: [
64 { key: "usage", label: "Usage", path: "usage" },
65 { key: "billing", label: "Billing and plans", path: "billing" },
66 ],
67 },
68 {
69 key: "compute",
70 title: "Compute",
71 pages: [{ key: "runners", label: "Runners", path: "runners", owner: true }],
72 },
73 {
74 key: "code",
75 title: "Code",
76 pages: [
77 { key: "repositories", label: "Repositories", path: "repositories" },
78 { key: "actions", label: "Actions", path: "actions" },
79 { key: "secrets", label: "Secrets and variables", path: "secrets" },
80 { key: "webhooks", label: "Webhooks", path: "webhooks" },
81 ],
82 },
83 {
84 key: "chat",
85 title: "Chat",
86 pages: [
87 { key: "chat-settings", label: "Chat settings", path: "settings/chat" },
88 { key: "emoji", label: "Emoji", path: "emoji" },
89 ],
90 },
91 {
92 key: "security",
93 title: "Security",
94 pages: [
95 { key: "security-settings", label: "Security settings", path: "security/settings" },
96 { key: "guardrails", label: "Guardrails", path: "guardrails" },
97 { key: "rules", label: "Rules", path: "rules" },
98 { key: "audit", label: "Audit log", path: "audit" },
99 ],
100 },
101 {
102 key: "integrations",
103 title: "Integrations",
104 pages: [{ key: "integrations", label: "Integrations", path: "integrations" }],
105 },
106];
107
108/**
109 * Which of Workspace mode's pages `pathname` is, under the workspace
110 * `slug`, as a group key and a page key; null for the overview and for
111 * anything else. A page's own subpages (one ruleset, a receipt) count as
112 * the page; the deepest match wins, so Invitations is its own page and
113 * not a subpage of Members.
114 */
115export function workspaceSettingsPage(pathname: string, slug: string): { group: string; page: string } | null {
116 const parts = pagePath(pathname).split("/").filter(Boolean);
117 if (parts[0]?.toLowerCase() !== slug.toLowerCase() || parts[1] !== "-") return null;
118 const rest = parts.slice(2).join("/");
119 let found: { group: string; page: string; depth: number } | null = null;
120 for (const group of WORKSPACE_SETTINGS_GROUPS) {
121 for (const page of group.pages) {
122 if (rest === page.path || rest.startsWith(`${page.path}/`)) {
123 const depth = page.path.split("/").length;
124 if (!found || depth > found.depth) found = { group: group.key, page: page.key, depth };
125 }
126 }
127 }
128 return found ? { group: found.group, page: found.page } : null;
129}
130
131/** `?tab=` as people write it, from the tabs the workspace page once had: the page it means. */
132const TAB_WORDS: Record<string, string> = {
133 overview: "",
134 projects: "-/projects",
135 repositories: "-/projects",
136 repos: "-/projects",
137 packages: "-/packages",
138 people: "-/people",
139 members: "-/members",
140 teams: "-/teams",
141 insights: "-/insights",
142 settings: "-/settings",
143};
144
145/** Workspace pages that moved, by their old name under `-/`; a new address may carry a query of its own. */
146const MOVED: Record<string, string> = {
147 // Home was called Today for a while.
148 today: "-/home",
149 "soon/teams": "-/teams",
150 "soon/insights": "-/insights",
151};
152
153/** Where the page `rest` (under `-/`) is now, or null when it has not moved. */
154function movedTo(rest: string): string | null {
155 return MOVED[rest] ?? null;
156}
157
158/** `to` with `search` merged into any query of its own, as a path under the workspace. */
159function withQuery(to: string, search: string): string {
160 const [path = "", own = ""] = to.split("?", 2);
161 const params = new URLSearchParams(search);
162 for (const [key, value] of new URLSearchParams(own)) params.set(key, value);
163 const query = params.toString();
164 return `${path ? `/${path}` : ""}${query ? `?${query}` : ""}`;
165}
166
167/** The page a request is for: a click's data request ends in `.data`. */
168export function pagePath(pathname: string): string {
169 return pathname.replace(/\.data$/, "").replace(/\/+$/, "") || "/";
170}
171
172/**
173 * The page at `pathname` under the workspace `slug` instead of its first
174 * segment, keeping its query: where an old name or an alias leads. A click
175 * asks for the page's data at `<path>.data?_routes=…`; the address given is
176 * the page's own, which the browser then asks for as usual.
177 */
178export function underWorkspace(pathname: string, search: string, slug: string): string {
179 const params = new URLSearchParams(search);
180 params.delete("_routes");
181 const query = params.toString();
182 const segments = pagePath(pathname).split("/");
183 // segments[0] is the empty string before the leading slash.
184 segments[1] = slug;
185 return `${segments.join("/")}${query ? `?${query}` : ""}`;
186}
187
188/**
189 * Where an old address of a workspace's pages is now, keeping its query;
190 * null when it has not moved. `/<workspace>?tab=projects` and the like
191 * open that page.
192 */
193export function workspaceRedirect(pathname: string, search = ""): string | null {
194 // A click asks for the page's data at `<path>.data?_routes=…`; the page
195 // is the same one a full load asks for.
196 const params = new URLSearchParams(search);
197 params.delete("_routes");
198 search = params.toString();
199 const parts = pagePath(pathname).split("/").filter(Boolean);
200 const slug = parts[0];
201 if (!slug) return null;
202 if (parts.length === 1 && params.has("tab")) {
203 const to = TAB_WORDS[(params.get("tab") ?? "").toLowerCase()];
204 if (to === undefined) return null;
205 params.delete("tab");
206 const rest = params.toString();
207 return `/${slug}${to ? `/${to}` : ""}${rest ? `?${rest}` : ""}`;
208 }
209 if (parts[1] !== "-") return null;
210 const to = movedTo(parts.slice(2).join("/"));
211 if (to === null) return null;
212 return `/${slug}${withQuery(to, search)}`;
213}
214
215/**
216 * The rail's modes. Home is the front page;
217 * Chat, Code and Artifacts are where work happens; Notifications
218 * spans them; People is who belongs; Workspace is the workspace itself,
219 * its settings area: who has access, money, policies and how each part is
220 * set up; Apps is everything installed that you
221 * can use. `account` is your own settings, under the avatar. `site` is
222 * g1t's own pages (policies, support, a new workspace): no workspace's, so
223 * no mode is lit and no mode's sidebar sits beside them. `browse` is g1t
224 * beyond your workspaces (Explore, search, a profile, another workspace
225 * and its public projects): the rail lights Explore, and Explore's sidebar
226 * sits beside them (`browsingOf`). Each mode has a sidebar of its own, or
227 * none, and which one is lit follows the address.
228 */
229export type ModeKey =
230 | "home"
231 | "chat"
232 | "notifications"
233 | "code"
234 | "artifacts"
235 | "people"
236 | "workspace"
237 | "apps"
238 | "account"
239 | "site"
240 | "browse";
241
242/** Workspace pages under `-/`, by the mode they belong to. Anything else of the workspace's is Code's. */
243const PAGE_MODES: Record<string, ModeKey> = {
244 home: "home",
245 chat: "chat",
246 artifacts: "artifacts",
247 apps: "apps",
248 people: "people",
249 members: "workspace",
250 permissions: "workspace",
251 teams: "people",
252 workspace: "workspace",
253 usage: "workspace",
254 billing: "workspace",
255 integrations: "workspace",
256 ai: "workspace",
257 guardrails: "workspace",
258 rules: "workspace",
259 audit: "workspace",
260 settings: "workspace",
261 repositories: "workspace",
262 tokens: "workspace",
263 "personal-access-tokens": "workspace",
264 secrets: "workspace",
265 actions: "workspace",
266 runners: "workspace",
267 webhooks: "workspace",
268 emoji: "workspace",
269};
270
271/** First segments that are g1t's own pages, never a workspace: none is any mode's. */
272const SITE_PAGES = new Set(["explore", "search", "support", "policies", "security", "status", "u", "invite", "workspaces"]);
273
274/**
275 * The mode a path is in, for the workspace `slug`. A repository (anyone's)
276 * and a new project are Code's; g1t's own pages (Explore, search, a
277 * profile) are `site`: public, and no workspace's mode.
278 */
279export function modeOf(pathname: string, slug: string | null): ModeKey {
280 const path = pagePath(pathname);
281 if (path === "/") return "home";
282 if (path === "/notifications" || path.startsWith("/notifications/")) return "notifications";
283 if (path === "/settings" || path.startsWith("/settings/")) return "account";
284 const parts = path.split("/").filter(Boolean);
285 if (SITE_PAGES.has(parts[0] ?? "")) return "site";
286 if (slug && parts[0]?.toLowerCase() === slug.toLowerCase()) {
287 if (parts.length === 1) return "home";
288 if (parts[1] === "-") {
289 // The workspace's security settings are a policy; its alerts are Code's.
290 if (parts[2] === "security" && parts[3] === "settings") return "workspace";
291 return PAGE_MODES[parts[2] ?? ""] ?? "code";
292 }
293 }
294 // Another workspace's chat, where the viewer is a guest: Chat all the same.
295 if (parts[1] === "-" && parts[2] === "chat") return "chat";
296 return "code";
297}
298
299/** Where each mode's button in the rail goes, in the workspace `slug`. */
300export function modeHome(mode: ModeKey, slug: string): string {
301 switch (mode) {
302 case "home":
303 return homePagePath(slug);
304 case "notifications":
305 return "/notifications";
306 case "chat":
307 return `/${slug}/-/chat`;
308 case "artifacts":
309 return `/${slug}/-/artifacts`;
310 case "code":
311 return `/${slug}/-/overview`;
312 case "people":
313 return `/${slug}/-/people`;
314 case "workspace":
315 return `/${slug}/-/workspace`;
316 case "apps":
317 return `/${slug}/-/apps`;
318 case "account":
319 return "/settings";
320 case "site":
321 case "browse":
322 return "/explore";
323 }
324}
325
326/** First segments that are g1t's own pages and never anyone's name (packages/contracts names.ts). */
327const NOT_AN_OWNER = new Set([
328 "-", "api", "mcp", "login", "logout", "register", "new", "settings", "search", "admin", "auth", "integrations",
329 "pulls", "issues", "verify", "confirm-email", "forgot", "reset", "device", "workspaces", "u", "oauth", "assets",
330 "avatars", "docs", "explore", "about", "pricing", "policies", "security", "support", "status", "terms", "privacy",
331 "help", "blog", "invite", "invites", "waitlist", "invitations", "v2", "inbox", "notifications", "download",
332 "downloads", "install",
333]);
334
335/**
336 * Whose g1t a browsed page is: a workspace (its overview, its pages, or
337 * one of its projects, `repo`) or a person (their profile).
338 */
339export type BrowsedOwner = { kind: "workspace"; name: string; repo: string | null } | { kind: "person"; name: string };
340
341/** What the shell knows of the viewer, to tell their own pages from the rest of g1t. */
342export type Viewer = {
343 /** The workspace the shell is about. */
344 slug: string | null;
345 /** Every workspace they belong to, by slug. */
346 memberships: readonly string[];
347 /** Repositories shared with them in workspaces they do not belong to: Code lists them as theirs. */
348 shared?: readonly { namespace: string; name: string }[];
349 /** Their own username: their profile is theirs, not someone's being browsed. */
350 username?: string | null;
351};
352
353/**
354 * Whether `pathname` is g1t beyond the viewer's own workspaces, and whose
355 * (`owner`, null for Explore, search and their own profile). Null for
356 * anything of theirs. A workspace they belong to is never browsed, even
357 * when it is not the one they are in: its pages and projects switch the
358 * shell to it (lib/workspace-choice.ts `workspaceFor`), so they are that
359 * workspace's modes. A project shared with them is Code's, whose sidebar
360 * lists it. Without a workspace there is no rail, and nothing is browsed.
361 */
362export function browsingOf(pathname: string, viewer: Viewer): { owner: BrowsedOwner | null } | null {
363 if (!viewer.slug) return null;
364 const parts = pagePath(pathname).split("/").filter(Boolean);
365 const first = parts[0]?.toLowerCase();
366 if (!first) return null;
367 if (first === "explore" || first === "search") return { owner: null };
368 if (first === "u") {
369 const name = parts[1];
370 if (!name) return null;
371 const own = viewer.username != null && name.toLowerCase() === viewer.username.toLowerCase();
372 return { owner: own ? null : { kind: "person", name } };
373 }
374 // g1t's own pages, and files such as robots.txt.
375 if (NOT_AN_OWNER.has(first) || first.includes(".")) return null;
376 if (first === viewer.slug.toLowerCase() || viewer.memberships.some((slug) => slug.toLowerCase() === first)) return null;
377 // Another workspace's channels, as a guest in them, are Chat's (`modeOf`), not Explore's.
378 if (parts[1] === "-" && parts[2] === "chat") return null;
379 const repo = parts[1] && parts[1] !== "-" ? parts[1] : null;
380 if (repo && (viewer.shared ?? []).some((r) => r.namespace.toLowerCase() === first && r.name.toLowerCase() === repo.toLowerCase())) return null;
381 return { owner: { kind: "workspace", name: parts[0]!, repo } };
382}
383
384/** The mode a path is in for this viewer: `browse` beyond their workspaces (`browsingOf`), else `modeOf`'s. */
385export function shellMode(pathname: string, viewer: Viewer): ModeKey {
386 return browsingOf(pathname, viewer) ? "browse" : modeOf(pathname, viewer.slug);
387}
388
389/** A workspace's front page: Home. */
390export function homePagePath(slug: string): string {
391 return `/${slug}/-/home`;
392}
393
394/**
395 * Where g1t's front door (`/`) and a workspace's own address lead someone
396 * in the workspace `slug`: Home, or Code's Overview when the query asks
397 * for its panels (a tab or a sort).
398 */
399export function homePath(slug: string, search = ""): string {
400 const params = new URLSearchParams(search);
401 params.delete("_routes");
402 params.delete("index");
403 const query = params.toString();
404 if (params.has("tab") || params.has("sort")) return `/${slug}/-/overview?${query}`;
405 return homePagePath(slug);
406}
407
408/** The page a member without Code access sees in place of anything of Code's. */
409export function codeAccessPath(slug: string, from?: string): string {
410 return `/${slug}/-/code-access${from ? `?from=${encodeURIComponent(from)}` : ""}`;
411}
412
413/** The workspace pages under `-/` that are Code's: closed to a member without Code access. */
414const CODE_PAGES = new Set(["overview", "projects", "repositories", "packages", "security", "rules", "runners", "actions", "insights", "soon"]);
415
416/**
417 * Where a member without Code access goes instead of `pathname`, or null
418 * when the page is open to them. `noCode` is the workspaces (by slug) where
419 * they lack it; `chosen` is the one they are in. g1t's front door (`/`) is
420 * Home in that workspace, and so is the workspace's own address; its
421 * Code pages and every repository page is the page that says to ask an
422 * owner.
423 */
424export function codeGate(pathname: string, search: string, noCode: readonly string[], chosen: string | null): string | null {
425 if (noCode.length === 0) return null;
426 const path = pagePath(pathname);
427 if (path === "/") {
428 const slug = chosen && noCode.includes(chosen.toLowerCase()) ? chosen.toLowerCase() : null;
429 return slug ? homePagePath(slug) : null;
430 }
431 const parts = path.split("/").filter(Boolean);
432 const slug = parts[0]?.toLowerCase();
433 if (!slug || !noCode.includes(slug)) return null;
434 const from = `${path}${search && search !== "?" ? search : ""}`;
435 // The workspace's own page is Home.
436 if (parts.length === 1) return homePagePath(slug);
437 if (parts[1] === "-") return CODE_PAGES.has(parts[2] ?? "") ? codeAccessPath(slug, from) : null;
438 // A repository, and everything in it.
439 return codeAccessPath(slug, from);
440}