Skip to content
80 linesCodeBlameRaw
1// Namespaces follow GitHub's rules (no leading, trailing or doubled hyphens),
2// so "--" can never appear inside one.
3const NAMESPACE = /^[a-z0-9](?:[a-z0-9]|-(?=[a-z0-9])){0,38}$/;
4const REPO_NAME = /^[a-z0-9._-]{1,100}$/;
5
6/** Routes and reserved words that may not be registered as usernames. */
7const RESERVED = new Set([
8 "api", "mcp", "login", "logout", "register", "new", "settings", "search",
9 "admin", "auth", "integrations", "pulls", "issues", "verify", "confirm-email", "forgot", "reset", "device", "workspaces", "u", "oauth", "assets", "avatars", "docs", "explore", "about", "pricing",
10 // g1t itself, and the name its agent once went by: everything g1t does is
11 // shown as `g1t`, so nobody else may be called either. `ghost` wrote what
12 // a deleted account wrote (`GHOST_USERNAME`).
13 "g1t", "g1t-agent", "ghost",
14 // Trust pages on g1t.sh, and names kept for them.
15 "policies", "security", "support", "status", "terms", "privacy", "help", "blog",
16 // Invite links, and the waitlist.
17 "invite", "invites", "waitlist",
18 // g1t.sh/invitations: the workspace invitations waiting for an answer.
19 "invitations",
20 // The container registry, at g1t.sh/v2/.
21 "v2",
22 // g1t.sh/inbox, and the name it might also go by.
23 "inbox", "notifications",
24 // g1t.sh/download, the desktop app; g1t.sh/downloads/, the releases it and the runner come from.
25 "download", "downloads",
26]);
27
28/** Whether `value`, whatever its case, is a name nobody can register: a route, or g1t's own. */
29export function isReservedName(value: string): boolean {
30 return RESERVED.has(value.trim().toLowerCase());
31}
32
33export function isValidNamespace(value: string): boolean {
34 return isNamespaceShaped(value) && !isReservedName(value);
35}
36
37/**
38 * Whether `value` has a namespace's shape, reserved or not: what a
39 * workspace's old name or an alias staff set (such as `g1t`) can be.
40 */
41export function isNamespaceShaped(value: string): boolean {
42 return NAMESPACE.test(value);
43}
44
45/**
46 * Usernames: letters of either case, digits and single hyphens, not
47 * starting or ending with a hyphen, 1 to 39 characters, never reserved in
48 * any case. The case is kept for showing; everything finds a person by the
49 * name lowercased (`canonicalUsername`), so `Ana` and `ana` are one name.
50 */
51const USERNAME = /^[A-Za-z0-9](?:[A-Za-z0-9]|-(?=[A-Za-z0-9])){0,38}$/;
52
53/** The `pattern` a username field takes in a form: the same rule, for the browser. */
54export const USERNAME_PATTERN = "[A-Za-z0-9](?:[A-Za-z0-9]|-(?=[A-Za-z0-9]))*";
55
56export function isValidUsername(value: string): boolean {
57 const name = value.trim();
58 return USERNAME.test(name) && !isReservedName(name);
59}
60
61/** The name a person is found, linked and mentioned by: lowercased. */
62export function canonicalUsername(value: string): string {
63 return value.trim().toLowerCase();
64}
65
66/** How a person's username shows: as they wrote it, or as it is kept. */
67export function shownUsername(person: { username: string; display_username?: string | null; displayUsername?: string | null }): string {
68 const display = person.display_username ?? person.displayUsername;
69 return display && display.toLowerCase() === person.username.toLowerCase() ? display : person.username;
70}
71
72export function isValidRepoName(value: string): boolean {
73 return (
74 REPO_NAME.test(value) &&
75 !value.startsWith(".") &&
76 !value.endsWith(".git") &&
77 // `/<workspace>/-/…` holds the workspace's own pages.
78 value !== "-"
79 );
80}