Pick any line to see why it is the way it is: the commit, the pull request and issue it came from, and what the agent was thinking.
| Invite-only launch: sign in with GitHub, repository access and lifecycle, many emails, a new look | 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"; | |
| Membership as GitHub has it: owners, roles, member privileges, 2FA | 10 | import { DEFAULT_MEMBER_PRIVILEGES, type MemberPrivileges } from "./members"; |
| Invite-only launch: sign in with GitHub, repository access and lifecycle, many emails, a new look | 11 | import type { Result } from "./result"; |
| Teams and CODEOWNERS, labels and milestones, dependency updates, the security suite, and a clearer top bar | 12 | import type { RepoTeam } from "./teams"; |
| Invite-only launch: sign in with GitHub, repository access and lifecycle, many emails, a new look | 13 | |
| 14 | /** What someone may do in one repository, from least to most. */ | |
| 15 | export type RepoRole = "read" | "triage" | "write" | "maintain" | "admin"; | |
| 16 | ||
| 17 | export const REPO_ROLES: readonly RepoRole[] = ["read", "triage", "write", "maintain", "admin"]; | |
| 18 | ||
| 19 | export const REPO_ROLE_LABELS: Record<RepoRole, string> = { | |
| 20 | read: "Read", | |
| 21 | triage: "Triage", | |
| 22 | write: "Write", | |
| 23 | maintain: "Maintain", | |
| 24 | admin: "Admin", | |
| 25 | }; | |
| 26 | ||
| 27 | /** One line on what each role is for, as role pickers show it. */ | |
| 28 | export const REPO_ROLE_SUMMARIES: Record<RepoRole, string> = { | |
| 29 | read: "Read and clone; open issues and pull requests, and comment.", | |
| Membership as GitHub has it: owners, roles, member privileges, 2FA | 30 | triage: "Read, and manage issues and pull requests: apply labels, assign, close.", |
| 31 | write: "Triage, and push, merge, manage labels, see security alerts, and put agents to work.", | |
| 32 | maintain: "Write, and manage the repository's settings and topics.", | |
| 33 | admin: "Everything: branch protection, webhooks, secrets, security, access, name and visibility.", | |
| Invite-only launch: sign in with GitHub, repository access and lifecycle, many emails, a new look | 34 | }; |
| 35 | ||
| 36 | /** What every member of a workspace gets on each of its repositories. */ | |
| 37 | export type BasePermission = "none" | "read" | "write" | "admin"; | |
| 38 | ||
| 39 | export const BASE_PERMISSIONS: readonly BasePermission[] = ["none", "read", "write", "admin"]; | |
| 40 | ||
| Membership as GitHub has it: owners, roles, member privileges, 2FA | 41 | /** What a membership that does not say gets: what members could do before roles. */ |
| Invite-only launch: sign in with GitHub, repository access and lifecycle, many emails, a new look | 42 | export const DEFAULT_BASE_PERMISSION: BasePermission = "write"; |
| 43 | ||
| Membership as GitHub has it: owners, roles, member privileges, 2FA | 44 | /** What a new workspace's members get, as on GitHub. Workspaces made before 2026-10-08 kept Write. */ |
| 45 | export const NEW_WORKSPACE_BASE_PERMISSION: BasePermission = "read"; | |
| 46 | ||
| Invite-only launch: sign in with GitHub, repository access and lifecycle, many emails, a new look | 47 | export const BASE_PERMISSION_LABELS: Record<BasePermission, string> = { |
| 48 | none: "No permission", | |
| 49 | read: "Read", | |
| 50 | write: "Write", | |
| 51 | admin: "Admin", | |
| 52 | }; | |
| 53 | ||
| 54 | export type Capability = | |
| 55 | | "read" | |
| 56 | | "participate" | |
| 57 | | "triage" | |
| 58 | | "push" | |
| 59 | | "merge" | |
| Membership as GitHub has it: owners, roles, member privileges, 2FA | 60 | | "manage_labels" |
| 61 | | "security_alerts" | |
| Invite-only launch: sign in with GitHub, repository access and lifecycle, many emails, a new look | 62 | | "run" |
| 63 | | "manage_settings" | |
| 64 | | "manage_protection" | |
| Membership as GitHub has it: owners, roles, member privileges, 2FA | 65 | | "manage_security" |
| Invite-only launch: sign in with GitHub, repository access and lifecycle, many emails, a new look | 66 | | "manage_integrations" |
| 67 | | "manage_access" | |
| 68 | | "administer" | |
| Membership as GitHub has it: owners, roles, member privileges, 2FA | 69 | | "change_visibility" |
| Invite-only launch: sign in with GitHub, repository access and lifecycle, many emails, a new look | 70 | | "delete"; |
| 71 | ||
| 72 | /** The permission table: the least role for each capability. */ | |
| 73 | export const CAPABILITIES = [ | |
| 74 | { capability: "read", role: "read", about: "See code, issues and pull requests; clone and fetch" }, | |
| 75 | { capability: "participate", role: "read", about: "Open issues and pull requests, and comment" }, | |
| Membership as GitHub has it: owners, roles, member privileges, 2FA | 76 | { capability: "triage", role: "triage", about: "Apply labels and milestones; assign, close and reopen issues and pull requests" }, |
| Invite-only launch: sign in with GitHub, repository access and lifecycle, many emails, a new look | 77 | { capability: "push", role: "write", about: "Push to branches that are not protected" }, |
| 78 | { capability: "merge", role: "write", about: "Merge pull requests and use the merge queue" }, | |
| Membership as GitHub has it: owners, roles, member privileges, 2FA | 79 | { capability: "manage_labels", role: "write", about: "Create, edit and delete labels and milestones" }, |
| 80 | { capability: "security_alerts", role: "write", about: "See and dismiss security alerts" }, | |
| Invite-only launch: sign in with GitHub, repository access and lifecycle, many emails, a new look | 81 | { capability: "run", role: "write", about: "Assign agents and start runs, plans and workflows" }, |
| 82 | { capability: "manage_settings", role: "maintain", about: "Change the description, topics, and pull request and agent settings" }, | |
| Membership as GitHub has it: owners, roles, member privileges, 2FA | 83 | { capability: "manage_protection", role: "admin", about: "Change branch protection, rulesets and guardrails" }, |
| 84 | { capability: "manage_security", role: "admin", about: "Change security settings, custom patterns and bypass reviews" }, | |
| Invite-only launch: sign in with GitHub, repository access and lifecycle, many emails, a new look | 85 | { capability: "manage_integrations", role: "admin", about: "Manage webhooks, secrets, variables, deployments and domains" }, |
| 86 | { capability: "manage_access", role: "admin", about: "Manage who has access, and invitations" }, | |
| Membership as GitHub has it: owners, roles, member privileges, 2FA | 87 | { capability: "administer", role: "admin", about: "Rename, archive and change the default branch" }, |
| 88 | { capability: "change_visibility", role: "admin", about: "Change visibility (owners only, unless member privileges allow admins)" }, | |
| 89 | { capability: "delete", role: "admin", about: "Transfer or delete the repository (owners only, unless member privileges allow admins)" }, | |
| Invite-only launch: sign in with GitHub, repository access and lifecycle, many emails, a new look | 90 | ] as const satisfies readonly { capability: Capability; role: RepoRole; about: string }[]; |
| 91 | ||
| Membership as GitHub has it: owners, roles, member privileges, 2FA | 92 | /** |
| 93 | * Capabilities that also need an owner of the repository's workspace, | |
| 94 | * unless its member privileges let members with the Admin role do them. | |
| 95 | */ | |
| 96 | export const OWNER_ONLY = ["change_visibility", "delete"] as const satisfies readonly Capability[]; | |
| 97 | ||
| 98 | /** What a security manager may do on every repository of their workspace. */ | |
| 99 | export const SECURITY_MANAGER = ["read", "participate", "security_alerts", "manage_security"] as const satisfies readonly Capability[]; | |
| 100 | ||
| 101 | /** Whether an owner-only capability is left to owners by these member privileges. */ | |
| 102 | export function ownerOnly(capability: Capability, privileges: MemberPrivileges | null | undefined): boolean { | |
| 103 | const p = { ...DEFAULT_MEMBER_PRIVILEGES, ...(privileges ?? {}) }; | |
| 104 | if (capability === "change_visibility") return !p.members_can_change_repo_visibility; | |
| 105 | if (capability === "delete") return !p.members_can_delete_repositories; | |
| 106 | return false; | |
| 107 | } | |
| Invite-only launch: sign in with GitHub, repository access and lifecycle, many emails, a new look | 108 | |
| 109 | /** A person's role on one repository, given directly. */ | |
| Teams and CODEOWNERS, labels and milestones, dependency updates, the security suite, and a clearer top bar | 110 | export type RepoGrant = { |
| 111 | repo_id: string; | |
| 112 | workspace: string; | |
| 113 | role: RepoRole; | |
| 114 | /** The team it comes through, when it is a team's grant. */ | |
| 115 | team?: string; | |
| 116 | }; | |
| Invite-only launch: sign in with GitHub, repository access and lifecycle, many emails, a new look | 117 | |
| 118 | /** What `permission` needs to know about a repository. */ | |
| 119 | export type RepoRef = { id: string; namespace: string; isPrivate: boolean }; | |
| 120 | ||
| 121 | const rank = (role: RepoRole): number => REPO_ROLES.indexOf(role); | |
| 122 | ||
| 123 | /** The higher of two roles; null is lower than any. */ | |
| 124 | export function maxRole(a: RepoRole | null | undefined, b: RepoRole | null | undefined): RepoRole | null { | |
| 125 | if (!a) return b ?? null; | |
| 126 | if (!b) return a; | |
| 127 | return rank(a) >= rank(b) ? a : b; | |
| 128 | } | |
| 129 | ||
| 130 | export function leastRole(capability: Capability): RepoRole { | |
| 131 | return CAPABILITIES.find((row) => row.capability === capability)?.role ?? "admin"; | |
| 132 | } | |
| 133 | ||
| 134 | /** Whether `role` has `capability`, going by the table alone. */ | |
| 135 | export function allows(role: RepoRole | null | undefined, capability: Capability): boolean { | |
| 136 | return role != null && rank(role) >= rank(leastRole(capability)); | |
| 137 | } | |
| 138 | ||
| 139 | export function baseRole(base: BasePermission | null | undefined): RepoRole | null { | |
| 140 | const value = base ?? DEFAULT_BASE_PERMISSION; | |
| 141 | return value === "none" ? null : value; | |
| 142 | } | |
| 143 | ||
| 144 | function membershipRole(user: User, membership: Membership): RepoRole | null { | |
| Git storage hardened, pages in tens of milliseconds, honest security alerts, and costs reconciled daily | 145 | // A workspace's own token, and g1t acting in the workspace, do what an |
| 146 | // owner can on its repositories. | |
| 147 | if (user.kind === "workspace" || user.kind === "system") return "admin"; | |
| Membership as GitHub has it: owners, roles, member privileges, 2FA | 148 | if (membership.role === "owner") return "admin"; |
| 149 | const base = baseRole(membership.base_permission); | |
| 150 | // A security manager reads every repository. | |
| 151 | return membership.org_roles?.includes("security_manager") ? maxRole(base, "read") : base; | |
| Invite-only launch: sign in with GitHub, repository access and lifecycle, many emails, a new look | 152 | } |
| 153 | ||
| 154 | /** The user's role on the repository, not counting that it may be public. */ | |
| 155 | export function granted(user: User, repo: RepoRef): RepoRole | null { | |
| 156 | const namespace = repo.namespace.toLowerCase(); | |
| 157 | const membership = user.workspaces?.find((m) => m.slug.toLowerCase() === namespace); | |
| 158 | let role = membership ? membershipRole(user, membership) : null; | |
| 159 | for (const grant of user.grants ?? []) { | |
| 160 | if (grant.repo_id === repo.id) role = maxRole(role, grant.role); | |
| 161 | } | |
| 162 | return role; | |
| 163 | } | |
| 164 | ||
| 165 | /** The viewer's effective role on a repository; null means they may not see it. */ | |
| 166 | export function permission(viewer: User | null | undefined, repo: RepoRef): RepoRole | null { | |
| 167 | const role = viewer ? granted(viewer, repo) : null; | |
| 168 | return repo.isPrivate ? role : maxRole(role, "read"); | |
| 169 | } | |
| 170 | ||
| 171 | /** Whether the viewer may do `capability` in the repository. */ | |
| 172 | export function can(viewer: User | null | undefined, repo: RepoRef, capability: Capability): boolean { | |
| Membership as GitHub has it: owners, roles, member privileges, 2FA | 173 | const namespace = repo.namespace.toLowerCase(); |
| 174 | const membership = viewer?.workspaces?.find((m) => m.slug.toLowerCase() === namespace); | |
| 175 | if (!allows(permission(viewer, repo), capability)) { | |
| 176 | return ( | |
| 177 | (SECURITY_MANAGER as readonly Capability[]).includes(capability) && | |
| 178 | (viewer?.kind ?? "user") === "user" && | |
| 179 | !!membership?.org_roles?.includes("security_manager") | |
| 180 | ); | |
| 181 | } | |
| Invite-only launch: sign in with GitHub, repository access and lifecycle, many emails, a new look | 182 | if ((OWNER_ONLY as readonly Capability[]).includes(capability)) { |
| Membership as GitHub has it: owners, roles, member privileges, 2FA | 183 | if (!membership) return false; |
| 184 | return membership.role === "owner" || !ownerOnly(capability, membership.privileges); | |
| Invite-only launch: sign in with GitHub, repository access and lifecycle, many emails, a new look | 185 | } |
| 186 | return true; | |
| 187 | } | |
| 188 | ||
| 189 | /** Every capability, true or false, for one viewer and repository: what pages pass to their components. */ | |
| 190 | export type Abilities = Record<Capability, boolean>; | |
| 191 | ||
| 192 | export function abilities(viewer: User | null | undefined, repo: RepoRef): Abilities { | |
| 193 | return Object.fromEntries(CAPABILITIES.map((row) => [row.capability, can(viewer, repo, row.capability)])) as Abilities; | |
| 194 | } | |
| 195 | ||
| 196 | /** The sentence shown beside something the viewer cannot use. */ | |
| 197 | export function needs(capability: Capability): string { | |
| Membership as GitHub has it: owners, roles, member privileges, 2FA | 198 | if ((OWNER_ONLY as readonly Capability[]).includes(capability)) |
| 199 | return "Only an owner of the workspace can do this, unless its member privileges let repository admins."; | |
| Invite-only launch: sign in with GitHub, repository access and lifecycle, many emails, a new look | 200 | return `Needs the ${REPO_ROLE_LABELS[leastRole(capability)]} role or higher.`; |
| 201 | } | |
| 202 | ||
| 203 | /** Whether the user belongs to the workspace or has a role on one of its repositories. */ | |
| 204 | export function hasAccessIn(user: User | null | undefined, namespace: string): boolean { | |
| 205 | const slug = namespace.toLowerCase(); | |
| 206 | return !!user && (!!user.workspaces?.some((m) => m.slug.toLowerCase() === slug) || !!user.grants?.some((g) => g.workspace.toLowerCase() === slug)); | |
| 207 | } | |
| 208 | ||
| 209 | /** The workspaces where the user has repositories shared with them without being a member. */ | |
| 210 | export function sharedWorkspaces(user: User | null | undefined): string[] { | |
| 211 | if (!user) return []; | |
| 212 | const member = new Set((user.workspaces ?? []).map((m) => m.slug)); | |
| 213 | return [...new Set((user.grants ?? []).map((g) => g.workspace).filter((slug) => !member.has(slug)))]; | |
| 214 | } | |
| 215 | ||
| 216 | // --- Who has access ---------------------------------------------------------- | |
| 217 | ||
| Teams and CODEOWNERS, labels and milestones, dependency updates, the security suite, and a clearer top bar | 218 | export type AccessSource = "owner" | "base" | "direct" | "team"; |
| Invite-only launch: sign in with GitHub, repository access and lifecycle, many emails, a new look | 219 | |
| 220 | export type Collaborator = { | |
| 221 | username: string; | |
| 222 | name: string | null; | |
| 223 | avatar: string | null; | |
| 224 | role: RepoRole; | |
| 225 | source: AccessSource; | |
| 226 | direct: RepoRole | null; | |
| 227 | /** Null for an outside collaborator. */ | |
| 228 | workspace_role: Role | null; | |
| Teams and CODEOWNERS, labels and milestones, dependency updates, the security suite, and a clearer top bar | 229 | /** The highest role a team gives them here, and that team's slug. */ |
| 230 | team_role?: RepoRole | null; | |
| 231 | team?: string | null; | |
| Invite-only launch: sign in with GitHub, repository access and lifecycle, many emails, a new look | 232 | }; |
| 233 | ||
| 234 | export type RepoInvitationStatus = "pending" | "accepted" | "declined" | "revoked" | "expired"; | |
| 235 | ||
| 236 | export type RepoInvitation = { | |
| 237 | id: string; | |
| 238 | /** `workspace/name`. */ | |
| 239 | repo: string; | |
| 240 | repo_id: string; | |
| 241 | invitee: string | null; | |
| 242 | email: string | null; | |
| 243 | role: RepoRole; | |
| 244 | invited_by: string | null; | |
| 245 | /** The inviter's avatar hash, served at `/avatars/<avatar>`; null for the generated letter avatar. */ | |
| 246 | inviter_avatar?: string | null; | |
| 247 | status: RepoInvitationStatus; | |
| 248 | created_at: string; | |
| 249 | expires_at: string; | |
| 250 | }; | |
| 251 | ||
| 252 | export type RepoAccess = { | |
| 253 | repo: string; | |
| 254 | base_permission: BasePermission; | |
| 255 | people: Collaborator[]; | |
| Teams and CODEOWNERS, labels and milestones, dependency updates, the security suite, and a clearer top bar | 256 | /** The workspace's teams given a role on it. */ |
| 257 | teams?: RepoTeam[]; | |
| Invite-only launch: sign in with GitHub, repository access and lifecycle, many emails, a new look | 258 | invitations: RepoInvitation[]; |
| 259 | viewer_role: RepoRole | null; | |
| 260 | can_manage: boolean; | |
| 261 | }; | |
| 262 | ||
| 263 | export type Added = | |
| 264 | | { result: "granted"; collaborator: Collaborator } | |
| 265 | | { result: "invited"; invitation: RepoInvitation }; | |
| 266 | ||
| 267 | export type PermissionInfo = { | |
| 268 | username: string; | |
| 269 | role: RepoRole | null; | |
| 270 | source: AccessSource | null; | |
| 271 | capabilities: Capability[]; | |
| 272 | }; | |
| 273 | ||
| 274 | export type OutsideCollaborator = { | |
| 275 | username: string; | |
| 276 | name: string | null; | |
| 277 | avatar: string | null; | |
| 278 | repos: { repo: string; role: RepoRole }[]; | |
| 279 | }; | |
| 280 | ||
| 281 | /** Identity's access methods, by repository path. */ | |
| 282 | export interface AccessClient { | |
| 283 | repoAccess(owner: string, name: string, viewer: User | null): Promise<Result<RepoAccess>>; | |
| 284 | addCollaborator(actor: User, owner: string, name: string, invitee: string, role: RepoRole): Promise<Result<Added>>; | |
| 285 | setCollaboratorRole(actor: User, owner: string, name: string, username: string, role: RepoRole): Promise<Result<Collaborator>>; | |
| 286 | removeCollaborator(actor: User, owner: string, name: string, username: string): Promise<Result<boolean>>; | |
| 287 | collaboratorPermission(viewer: User | null, owner: string, name: string, username: string): Promise<Result<PermissionInfo>>; | |
| 288 | myRepoInvitations(user: User): Promise<RepoInvitation[]>; | |
| 289 | respondRepoInvitation(user: User, id: string, accept: boolean): Promise<Result<RepoInvitation>>; | |
| 290 | revokeRepoInvitation(actor: User, owner: string, name: string, id: string): Promise<Result<RepoInvitation>>; | |
| 291 | setBasePermission(actor: User, slug: string, base: BasePermission): Promise<Result<BasePermission>>; | |
| 292 | outsideCollaborators(viewer: User | null, slug: string): Promise<Result<OutsideCollaborator[]>>; | |
| 293 | } |