pr_01m47d24b0e6n91zwymwxg0vpx/packages/contracts/src/access.ts
| 1 | /** |
| 2 | * Who may do what in a repository: repository roles, the capabilities each |
| 3 | * one carries, and how a person's permission is worked out. |
| 4 | * |
| 5 | * Mirrors `crates/contracts/src/access.rs`, which holds the one permission |
| 6 | * table; a test there reads `CAPABILITIES` and `OWNER_ONLY` below and fails |
| 7 | * when the two differ. Keep each row on one line. |
| 8 | */ |
| 9 | import type { Membership, Role, User } from "./identity"; |
| 10 | import type { Result } from "./result"; |
| 11 | |
| 12 | /** What someone may do in one repository, from least to most. */ |
| 13 | export type RepoRole = "read" | "triage" | "write" | "maintain" | "admin"; |
| 14 | |
| 15 | export const REPO_ROLES: readonly RepoRole[] = ["read", "triage", "write", "maintain", "admin"]; |
| 16 | |
| 17 | export const REPO_ROLE_LABELS: Record<RepoRole, string> = { |
| 18 | read: "Read", |
| 19 | triage: "Triage", |
| 20 | write: "Write", |
| 21 | maintain: "Maintain", |
| 22 | admin: "Admin", |
| 23 | }; |
| 24 | |
| 25 | /** One line on what each role is for, as role pickers show it. */ |
| 26 | export const REPO_ROLE_SUMMARIES: Record<RepoRole, string> = { |
| 27 | read: "Read and clone; open issues and pull requests, and comment.", |
| 28 | triage: "Read, and manage issues and pull requests: label, assign, close.", |
| 29 | write: "Triage, and push, merge, and put agents to work.", |
| 30 | maintain: "Write, and manage settings and branch protection.", |
| 31 | admin: "Everything: webhooks, secrets, deployments, access, name and visibility.", |
| 32 | }; |
| 33 | |
| 34 | /** What every member of a workspace gets on each of its repositories. */ |
| 35 | export type BasePermission = "none" | "read" | "write" | "admin"; |
| 36 | |
| 37 | export const BASE_PERMISSIONS: readonly BasePermission[] = ["none", "read", "write", "admin"]; |
| 38 | |
| 39 | /** Unless an owner changes it: what members could do before roles. */ |
| 40 | export const DEFAULT_BASE_PERMISSION: BasePermission = "write"; |
| 41 | |
| 42 | export const BASE_PERMISSION_LABELS: Record<BasePermission, string> = { |
| 43 | none: "No permission", |
| 44 | read: "Read", |
| 45 | write: "Write", |
| 46 | admin: "Admin", |
| 47 | }; |
| 48 | |
| 49 | export type Capability = |
| 50 | | "read" |
| 51 | | "participate" |
| 52 | | "triage" |
| 53 | | "push" |
| 54 | | "merge" |
| 55 | | "run" |
| 56 | | "manage_settings" |
| 57 | | "manage_protection" |
| 58 | | "manage_integrations" |
| 59 | | "manage_access" |
| 60 | | "administer" |
| 61 | | "delete"; |
| 62 | |
| 63 | /** The permission table: the least role for each capability. */ |
| 64 | export const CAPABILITIES = [ |
| 65 | { capability: "read", role: "read", about: "See code, issues and pull requests; clone and fetch" }, |
| 66 | { capability: "participate", role: "read", about: "Open issues and pull requests, and comment" }, |
| 67 | { capability: "triage", role: "triage", about: "Label, assign, close and reopen issues and pull requests" }, |
| 68 | { capability: "push", role: "write", about: "Push to branches that are not protected" }, |
| 69 | { capability: "merge", role: "write", about: "Merge pull requests and use the merge queue" }, |
| 70 | { capability: "run", role: "write", about: "Assign agents and start runs, plans and workflows" }, |
| 71 | { capability: "manage_settings", role: "maintain", about: "Change the description, topics, and pull request and agent settings" }, |
| 72 | { capability: "manage_protection", role: "maintain", about: "Change branch protection and guardrails" }, |
| 73 | { capability: "manage_integrations", role: "admin", about: "Manage webhooks, secrets, variables, deployments and domains" }, |
| 74 | { capability: "manage_access", role: "admin", about: "Manage who has access, and invitations" }, |
| 75 | { capability: "administer", role: "admin", about: "Rename, archive, change visibility and the default branch" }, |
| 76 | { capability: "delete", role: "admin", about: "Transfer or delete the repository (owners of the workspace only)" }, |
| 77 | ] as const satisfies readonly { capability: Capability; role: RepoRole; about: string }[]; |
| 78 | |
| 79 | /** Capabilities that also need an owner of the repository's workspace. */ |
| 80 | export const OWNER_ONLY = ["delete"] as const satisfies readonly Capability[]; |
| 81 | |
| 82 | /** A person's role on one repository, given directly. */ |
| 83 | export type RepoGrant = { repo_id: string; workspace: string; role: RepoRole }; |
| 84 | |
| 85 | /** What `permission` needs to know about a repository. */ |
| 86 | export type RepoRef = { id: string; namespace: string; isPrivate: boolean }; |
| 87 | |
| 88 | const rank = (role: RepoRole): number => REPO_ROLES.indexOf(role); |
| 89 | |
| 90 | /** The higher of two roles; null is lower than any. */ |
| 91 | export function maxRole(a: RepoRole | null | undefined, b: RepoRole | null | undefined): RepoRole | null { |
| 92 | if (!a) return b ?? null; |
| 93 | if (!b) return a; |
| 94 | return rank(a) >= rank(b) ? a : b; |
| 95 | } |
| 96 | |
| 97 | export function leastRole(capability: Capability): RepoRole { |
| 98 | return CAPABILITIES.find((row) => row.capability === capability)?.role ?? "admin"; |
| 99 | } |
| 100 | |
| 101 | /** Whether `role` has `capability`, going by the table alone. */ |
| 102 | export function allows(role: RepoRole | null | undefined, capability: Capability): boolean { |
| 103 | return role != null && rank(role) >= rank(leastRole(capability)); |
| 104 | } |
| 105 | |
| 106 | export function baseRole(base: BasePermission | null | undefined): RepoRole | null { |
| 107 | const value = base ?? DEFAULT_BASE_PERMISSION; |
| 108 | return value === "none" ? null : value; |
| 109 | } |
| 110 | |
| 111 | function membershipRole(user: User, membership: Membership): RepoRole | null { |
| 112 | if (user.kind === "workspace") return "admin"; |
| 113 | return membership.role === "owner" ? "admin" : baseRole(membership.base_permission); |
| 114 | } |
| 115 | |
| 116 | /** The user's role on the repository, not counting that it may be public. */ |
| 117 | export function granted(user: User, repo: RepoRef): RepoRole | null { |
| 118 | const namespace = repo.namespace.toLowerCase(); |
| 119 | const membership = user.workspaces?.find((m) => m.slug.toLowerCase() === namespace); |
| 120 | let role = membership ? membershipRole(user, membership) : null; |
| 121 | for (const grant of user.grants ?? []) { |
| 122 | if (grant.repo_id === repo.id) role = maxRole(role, grant.role); |
| 123 | } |
| 124 | return role; |
| 125 | } |
| 126 | |
| 127 | /** The viewer's effective role on a repository; null means they may not see it. */ |
| 128 | export function permission(viewer: User | null | undefined, repo: RepoRef): RepoRole | null { |
| 129 | const role = viewer ? granted(viewer, repo) : null; |
| 130 | return repo.isPrivate ? role : maxRole(role, "read"); |
| 131 | } |
| 132 | |
| 133 | /** Whether the viewer may do `capability` in the repository. */ |
| 134 | export function can(viewer: User | null | undefined, repo: RepoRef, capability: Capability): boolean { |
| 135 | if (!allows(permission(viewer, repo), capability)) return false; |
| 136 | if ((OWNER_ONLY as readonly Capability[]).includes(capability)) { |
| 137 | const namespace = repo.namespace.toLowerCase(); |
| 138 | return viewer?.workspaces?.find((m) => m.slug.toLowerCase() === namespace)?.role === "owner"; |
| 139 | } |
| 140 | return true; |
| 141 | } |
| 142 | |
| 143 | /** Every capability, true or false, for one viewer and repository: what pages pass to their components. */ |
| 144 | export type Abilities = Record<Capability, boolean>; |
| 145 | |
| 146 | export function abilities(viewer: User | null | undefined, repo: RepoRef): Abilities { |
| 147 | return Object.fromEntries(CAPABILITIES.map((row) => [row.capability, can(viewer, repo, row.capability)])) as Abilities; |
| 148 | } |
| 149 | |
| 150 | /** The sentence shown beside something the viewer cannot use. */ |
| 151 | export function needs(capability: Capability): string { |
| 152 | if ((OWNER_ONLY as readonly Capability[]).includes(capability)) return "Only an owner of the workspace can do this."; |
| 153 | return `Needs the ${REPO_ROLE_LABELS[leastRole(capability)]} role or higher.`; |
| 154 | } |
| 155 | |
| 156 | /** Whether the user belongs to the workspace or has a role on one of its repositories. */ |
| 157 | export function hasAccessIn(user: User | null | undefined, namespace: string): boolean { |
| 158 | const slug = namespace.toLowerCase(); |
| 159 | return !!user && (!!user.workspaces?.some((m) => m.slug.toLowerCase() === slug) || !!user.grants?.some((g) => g.workspace.toLowerCase() === slug)); |
| 160 | } |
| 161 | |
| 162 | /** The workspaces where the user has repositories shared with them without being a member. */ |
| 163 | export function sharedWorkspaces(user: User | null | undefined): string[] { |
| 164 | if (!user) return []; |
| 165 | const member = new Set((user.workspaces ?? []).map((m) => m.slug)); |
| 166 | return [...new Set((user.grants ?? []).map((g) => g.workspace).filter((slug) => !member.has(slug)))]; |
| 167 | } |
| 168 | |
| 169 | // --- Who has access ---------------------------------------------------------- |
| 170 | |
| 171 | export type AccessSource = "owner" | "base" | "direct"; |
| 172 | |
| 173 | export type Collaborator = { |
| 174 | username: string; |
| 175 | name: string | null; |
| 176 | avatar: string | null; |
| 177 | role: RepoRole; |
| 178 | source: AccessSource; |
| 179 | direct: RepoRole | null; |
| 180 | /** Null for an outside collaborator. */ |
| 181 | workspace_role: Role | null; |
| 182 | }; |
| 183 | |
| 184 | export type RepoInvitationStatus = "pending" | "accepted" | "declined" | "revoked" | "expired"; |
| 185 | |
| 186 | export type RepoInvitation = { |
| 187 | id: string; |
| 188 | /** `workspace/name`. */ |
| 189 | repo: string; |
| 190 | repo_id: string; |
| 191 | invitee: string | null; |
| 192 | email: string | null; |
| 193 | role: RepoRole; |
| 194 | invited_by: string | null; |
| 195 | /** The inviter's avatar hash, served at `/avatars/<avatar>`; null for the generated letter avatar. */ |
| 196 | inviter_avatar?: string | null; |
| 197 | status: RepoInvitationStatus; |
| 198 | created_at: string; |
| 199 | expires_at: string; |
| 200 | }; |
| 201 | |
| 202 | export type RepoAccess = { |
| 203 | repo: string; |
| 204 | base_permission: BasePermission; |
| 205 | people: Collaborator[]; |
| 206 | invitations: RepoInvitation[]; |
| 207 | viewer_role: RepoRole | null; |
| 208 | can_manage: boolean; |
| 209 | }; |
| 210 | |
| 211 | export type Added = |
| 212 | | { result: "granted"; collaborator: Collaborator } |
| 213 | | { result: "invited"; invitation: RepoInvitation }; |
| 214 | |
| 215 | export type PermissionInfo = { |
| 216 | username: string; |
| 217 | role: RepoRole | null; |
| 218 | source: AccessSource | null; |
| 219 | capabilities: Capability[]; |
| 220 | }; |
| 221 | |
| 222 | export type OutsideCollaborator = { |
| 223 | username: string; |
| 224 | name: string | null; |
| 225 | avatar: string | null; |
| 226 | repos: { repo: string; role: RepoRole }[]; |
| 227 | }; |
| 228 | |
| 229 | /** Identity's access methods, by repository path. */ |
| 230 | export interface AccessClient { |
| 231 | repoAccess(owner: string, name: string, viewer: User | null): Promise<Result<RepoAccess>>; |
| 232 | addCollaborator(actor: User, owner: string, name: string, invitee: string, role: RepoRole): Promise<Result<Added>>; |
| 233 | setCollaboratorRole(actor: User, owner: string, name: string, username: string, role: RepoRole): Promise<Result<Collaborator>>; |
| 234 | removeCollaborator(actor: User, owner: string, name: string, username: string): Promise<Result<boolean>>; |
| 235 | collaboratorPermission(viewer: User | null, owner: string, name: string, username: string): Promise<Result<PermissionInfo>>; |
| 236 | myRepoInvitations(user: User): Promise<RepoInvitation[]>; |
| 237 | respondRepoInvitation(user: User, id: string, accept: boolean): Promise<Result<RepoInvitation>>; |
| 238 | revokeRepoInvitation(actor: User, owner: string, name: string, id: string): Promise<Result<RepoInvitation>>; |
| 239 | setBasePermission(actor: User, slug: string, base: BasePermission): Promise<Result<BasePermission>>; |
| 240 | outsideCollaborators(viewer: User | null, slug: string): Promise<Result<OutsideCollaborator[]>>; |
| 241 | } |