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.

458 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; Work is the workspace's work
218 * items and the sessions working on them, for every member;
219 * Chat, Code and Artifacts are where work happens; Notifications
220 * spans them; People is who belongs; Workspace is the workspace itself,
221 * its settings area: who has access, money, policies and how each part is
222 * set up; Apps is everything installed that you
223 * can use. `account` is your own settings, under the avatar. `site` is
224 * g1t's own pages (policies, support, a new workspace): no workspace's, so
225 * no mode is lit and no mode's sidebar sits beside them. `browse` is g1t
226 * beyond your workspaces (Explore, search, a profile, another workspace
227 * and its public projects): the rail lights Explore, and Explore's sidebar
228 * sits beside them (`browsingOf`). Each mode has a sidebar of its own, or
229 * none, and which one is lit follows the address.
230 */
231export type ModeKey =
232 | "home"
233 | "work"
234 | "chat"
235 | "notifications"
236 | "code"
237 | "artifacts"
238 | "routines"
239 | "people"
240 | "workspace"
241 | "apps"
242 | "account"
243 | "site"
244 | "browse";
245
246/** Workspace pages under `-/`, by the mode they belong to. Anything else of the workspace's is Code's. */
247const PAGE_MODES: Record<string, ModeKey> = {
248 home: "home",
249 // Work's sessions (`-/work/sessions`) are Work's too.
250 work: "work",
251 chat: "chat",
252 artifacts: "artifacts",
253 routines: "routines",
254 apps: "apps",
255 people: "people",
256 members: "workspace",
257 permissions: "workspace",
258 sso: "workspace",
259 teams: "people",
260 workspace: "workspace",
261 usage: "workspace",
262 billing: "workspace",
263 integrations: "workspace",
264 ai: "workspace",
265 guardrails: "workspace",
266 rules: "workspace",
267 audit: "workspace",
268 settings: "workspace",
269 repositories: "workspace",
270 tokens: "workspace",
271 "personal-access-tokens": "workspace",
272 secrets: "workspace",
273 actions: "workspace",
274 runners: "workspace",
275 webhooks: "workspace",
276 emoji: "workspace",
277};
278
279/** First segments that are g1t's own pages, never a workspace: none is any mode's. */
280const SITE_PAGES = new Set(["explore", "search", "support", "policies", "security", "status", "u", "invite", "workspaces"]);
281
282/**
283 * The mode a path is in, for the workspace `slug`. A repository (anyone's)
284 * and a new project are Code's; g1t's own pages (Explore, search, a
285 * profile) are `site`: public, and no workspace's mode.
286 */
287export function modeOf(pathname: string, slug: string | null): ModeKey {
288 const path = pagePath(pathname);
289 if (path === "/") return "home";
290 if (path === "/notifications" || path.startsWith("/notifications/")) return "notifications";
291 if (path === "/settings" || path.startsWith("/settings/")) return "account";
292 const parts = path.split("/").filter(Boolean);
293 if (SITE_PAGES.has(parts[0] ?? "")) return "site";
294 if (slug && parts[0]?.toLowerCase() === slug.toLowerCase()) {
295 if (parts.length === 1) return "home";
296 if (parts[1] === "-") {
297 // The workspace's security settings are a policy; its alerts are Code's.
298 if (parts[2] === "security" && parts[3] === "settings") return "workspace";
299 return PAGE_MODES[parts[2] ?? ""] ?? "code";
300 }
301 }
302 // Another workspace's chat, where the viewer is a guest: Chat all the same.
303 if (parts[1] === "-" && parts[2] === "chat") return "chat";
304 return "code";
305}
306
307/** Where each mode's button in the rail goes, in the workspace `slug`. */
308export function modeHome(mode: ModeKey, slug: string): string {
309 switch (mode) {
310 case "home":
311 return homePagePath(slug);
312 case "work":
313 return `/${slug}/-/work`;
314 case "notifications":
315 return "/notifications";
316 case "chat":
317 return `/${slug}/-/chat`;
318 case "artifacts":
319 return `/${slug}/-/artifacts`;
320 case "routines":
321 return `/${slug}/-/routines`;
322 case "code":
323 return `/${slug}/-/overview`;
324 case "people":
325 return `/${slug}/-/people`;
326 case "workspace":
327 return `/${slug}/-/workspace`;
328 case "apps":
329 return `/${slug}/-/apps`;
330 case "account":
331 return "/settings";
332 case "site":
333 case "browse":
334 return "/explore";
335 }
336}
337
338/** First segments that are g1t's own pages and never anyone's name (packages/contracts names.ts). */
339const NOT_AN_OWNER = new Set([
340 "-", "api", "mcp", "login", "logout", "register", "new", "settings", "search", "admin", "auth", "integrations",
341 "pulls", "issues", "verify", "confirm-email", "forgot", "reset", "device", "workspaces", "u", "oauth", "assets",
342 "avatars", "docs", "explore", "about", "pricing", "policies", "security", "support", "status", "terms", "privacy",
343 "help", "blog", "invite", "invites", "waitlist", "invitations", "v2", "inbox", "notifications", "download",
344 "downloads", "install",
345]);
346
347/**
348 * Whose g1t a browsed page is: a workspace (its overview, its pages, or
349 * one of its projects, `repo`) or a person (their profile).
350 */
351export type BrowsedOwner = { kind: "workspace"; name: string; repo: string | null } | { kind: "person"; name: string };
352
353/** What the shell knows of the viewer, to tell their own pages from the rest of g1t. */
354export type Viewer = {
355 /** The workspace the shell is about. */
356 slug: string | null;
357 /** Every workspace they belong to, by slug. */
358 memberships: readonly string[];
359 /** Repositories shared with them in workspaces they do not belong to: Code lists them as theirs. */
360 shared?: readonly { namespace: string; name: string }[];
361 /** Their own username: their profile is theirs, not someone's being browsed. */
362 username?: string | null;
363};
364
365/**
366 * Whether `pathname` is g1t beyond the viewer's own workspaces, and whose
367 * (`owner`, null for Explore, search and their own profile). Null for
368 * anything of theirs. A workspace they belong to is never browsed, even
369 * when it is not the one they are in: its pages and projects switch the
370 * shell to it (lib/workspace-choice.ts `workspaceFor`), so they are that
371 * workspace's modes. A project shared with them is Code's, whose sidebar
372 * lists it. Without a workspace there is no rail, and nothing is browsed.
373 */
374export function browsingOf(pathname: string, viewer: Viewer): { owner: BrowsedOwner | null } | null {
375 if (!viewer.slug) return null;
376 const parts = pagePath(pathname).split("/").filter(Boolean);
377 const first = parts[0]?.toLowerCase();
378 if (!first) return null;
379 if (first === "explore" || first === "search") return { owner: null };
380 if (first === "u") {
381 const name = parts[1];
382 if (!name) return null;
383 const own = viewer.username != null && name.toLowerCase() === viewer.username.toLowerCase();
384 return { owner: own ? null : { kind: "person", name } };
385 }
386 // g1t's own pages, and files such as robots.txt.
387 if (NOT_AN_OWNER.has(first) || first.includes(".")) return null;
388 if (first === viewer.slug.toLowerCase() || viewer.memberships.some((slug) => slug.toLowerCase() === first)) return null;
389 // Another workspace's channels, as a guest in them, are Chat's (`modeOf`), not Explore's.
390 if (parts[1] === "-" && parts[2] === "chat") return null;
391 const repo = parts[1] && parts[1] !== "-" ? parts[1] : null;
392 if (repo && (viewer.shared ?? []).some((r) => r.namespace.toLowerCase() === first && r.name.toLowerCase() === repo.toLowerCase())) return null;
393 return { owner: { kind: "workspace", name: parts[0]!, repo } };
394}
395
396/** The mode a path is in for this viewer: `browse` beyond their workspaces (`browsingOf`), else `modeOf`'s. */
397export function shellMode(pathname: string, viewer: Viewer): ModeKey {
398 return browsingOf(pathname, viewer) ? "browse" : modeOf(pathname, viewer.slug);
399}
400
401/** A workspace's front page: Home. */
402export function homePagePath(slug: string): string {
403 return `/${slug}/-/home`;
404}
405
406/**
407 * Where g1t's front door (`/`) and a workspace's own address lead someone
408 * in the workspace `slug`: Home, or Code's Overview when the query asks
409 * for its panels (a tab or a sort).
410 */
411export function homePath(slug: string, search = ""): string {
412 const params = new URLSearchParams(search);
413 params.delete("_routes");
414 params.delete("index");
415 const query = params.toString();
416 if (params.has("tab") || params.has("sort")) return `/${slug}/-/overview?${query}`;
417 return homePagePath(slug);
418}
419
420/** The page a member without Code access sees in place of anything of Code's. */
421export function codeAccessPath(slug: string, from?: string): string {
422 return `/${slug}/-/code-access${from ? `?from=${encodeURIComponent(from)}` : ""}`;
423}
424
425/** A repository's addresses that only send the browser on to Work or to what a reference names. */
426const WORK_REDIRECTS = new Set(["issues", "labels", "milestones", "ref"]);
427
428/** The workspace pages under `-/` that are Code's: closed to a member without Code access. */
429const CODE_PAGES = new Set(["overview", "projects", "repositories", "packages", "security", "rules", "runners", "actions", "insights", "soon"]);
430
431/**
432 * Where a member without Code access goes instead of `pathname`, or null
433 * when the page is open to them. `noCode` is the workspaces (by slug) where
434 * they lack it; `chosen` is the one they are in. g1t's front door (`/`) is
435 * Home in that workspace, and so is the workspace's own address; its
436 * Code pages and every repository page is the page that says to ask an
437 * owner.
438 */
439export function codeGate(pathname: string, search: string, noCode: readonly string[], chosen: string | null): string | null {
440 if (noCode.length === 0) return null;
441 const path = pagePath(pathname);
442 if (path === "/") {
443 const slug = chosen && noCode.includes(chosen.toLowerCase()) ? chosen.toLowerCase() : null;
444 return slug ? homePagePath(slug) : null;
445 }
446 const parts = path.split("/").filter(Boolean);
447 const slug = parts[0]?.toLowerCase();
448 if (!slug || !noCode.includes(slug)) return null;
449 const from = `${path}${search && search !== "?" ? search : ""}`;
450 // The workspace's own page is Home.
451 if (parts.length === 1) return homePagePath(slug);
452 if (parts[1] === "-") return CODE_PAGES.has(parts[2] ?? "") ? codeAccessPath(slug, from) : null;
453 // An old link to a repository's issues, labels or milestones leads to
454 // Work, which is every member's; a reference (`ref`) leads to what it names.
455 if (WORK_REDIRECTS.has(parts[2] ?? "")) return null;
456 // A repository, and everything in it.
457 return codeAccessPath(slug, from);
458}