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.

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