| 1 | /** |
| 2 | * Workspace aliases, as the Aliases page reads its forms: a name staff |
| 3 | * point at a workspace (`g1t` leads to `flagon-io`). Identity checks each |
| 4 | * again, and knows what this cannot: whether a person or workspace has the |
| 5 | * name. No Workers imports, so it can be tested under Node. |
| 6 | */ |
| 7 | import { isSlug, type Parsed } from "./forms.ts"; |
| 8 | |
| 9 | /** The longest note or reason, as identity allows. */ |
| 10 | export const MAX_ALIAS_NOTE = 500; |
| 11 | |
| 12 | export type NewAlias = { alias: string; workspace: string; note: string }; |
| 13 | |
| 14 | function note(raw: string, missing: string): Parsed<string> { |
| 15 | const value = raw.trim(); |
| 16 | if (!value) return { ok: false, error: missing }; |
| 17 | if ([...value].length > MAX_ALIAS_NOTE) return { ok: false, error: `Keep it to ${MAX_ALIAS_NOTE} characters.` }; |
| 18 | return { ok: true, value }; |
| 19 | } |
| 20 | |
| 21 | /** The add form: an alias, the workspace it leads to, and why. */ |
| 22 | export function parseNewAlias(alias: string, workspace: string, why: string): Parsed<NewAlias> { |
| 23 | const name = alias.trim().toLowerCase(); |
| 24 | if (!isSlug(name)) return { ok: false, error: "An alias uses lowercase letters, digits and single hyphens, up to 39 characters." }; |
| 25 | const slug = workspace.trim().toLowerCase(); |
| 26 | if (!isSlug(slug)) return { ok: false, error: "Give the workspace's slug, such as flagon-io." }; |
| 27 | if (name === slug) return { ok: false, error: "An alias cannot be the workspace's own slug." }; |
| 28 | const reason = note(why, "Say why the alias exists."); |
| 29 | if (!reason.ok) return reason; |
| 30 | return { ok: true, value: { alias: name, workspace: slug, note: reason.value } }; |
| 31 | } |
| 32 | |
| 33 | /** The remove form's reason, for sudo's audit log. */ |
| 34 | export function parseRemovalReason(why: string): Parsed<string> { |
| 35 | return note(why, "Say why the alias is being removed."); |
| 36 | } |