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.

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