Skip to content
697 linesCodeBlameRaw
1/**
2 * Scopes: what an access token may do on its owner's behalf. Mirrors
3 * `crates/contracts/src/scopes.rs`, which is the source of truth; a Rust
4 * test keeps the tables here the same.
5 *
6 * A token's permissions are its scopes read per resource: each resource
7 * at none or one level, such as issues: write. What a request may do is
8 * the intersection of the owner's role, the token's reach (the workspace
9 * it is made for, and its repositories) and its permissions.
10 */
11
12export type ScopeResource =
13 | "repo"
14 | "code"
15 | "security"
16 | "packages"
17 | "issues"
18 | "pull_requests"
19 | "agents"
20 | "workflows"
21 | "workflow_files"
22 | "checks"
23 | "deployments"
24 | "observability"
25 | "memory"
26 | "account"
27 | "notifications"
28 | "chat"
29 | "workspace"
30 | "billing"
31 | "access"
32 | "webhooks"
33 | "secrets"
34 | "runners"
35 | "models"
36 | "artifacts";
37
38export type ScopeLevel = "read" | "write" | "run" | "delete" | "admin";
39
40/** Every scope, grouped by resource, least first. */
41export const SCOPES = [
42 { scope: "repo:read", description: "See repositories, their settings, labels, timelines, releases, languages, contributors and security alerts, and search" },
43 { scope: "repo:write", description: "Create repositories, create, rename and delete branches, change how pull requests merge and publish releases" },
44 { scope: "repo:admin", description: "Rename, archive, transfer, delete or change who can see a repository, change its rulesets, and dismiss security alerts" },
45 { scope: "code:read", description: "Clone and fetch private repositories with git" },
46 { scope: "code:write", description: "Push commits with git" },
47 { scope: "security:read", description: "See secret scanning, code scanning and vulnerability alerts, custom patterns, the dependency graph and SBOM, and security settings" },
48 { scope: "security:write", description: "Dismiss and reopen alerts, bypass push protection, review bypass requests, manage custom patterns, upload SARIF and change security settings" },
49 { scope: "packages:read", description: "Pull container images and install private packages" },
50 { scope: "packages:write", description: "Push container images and publish packages" },
51 { scope: "packages:delete", description: "Delete and restore packages and their versions" },
52 { scope: "issues:read", description: "Read issues and comments" },
53 { scope: "issues:write", description: "Open, edit, close and comment on issues" },
54 { scope: "pull_requests:read", description: "Read pull requests, their changes, sessions and merge queues" },
55 { scope: "pull_requests:write", description: "Open, review, close and merge pull requests" },
56 { scope: "workflows:read", description: "Read workflows, runs and logs" },
57 { scope: "workflows:write", description: "Run, cancel, rerun and turn workflows on or off" },
58 { scope: "workflow_files:write", description: "Add, change and delete workflow files under .g1t/workflows and .github/workflows, with git or the API" },
59 { scope: "checks:read", description: "Read commits' statuses, check runs, check suites and annotations" },
60 { scope: "checks:write", description: "Report statuses and check runs on commits, and ask for checks to run again" },
61 { scope: "deployments:read", description: "See deployments, their statuses and environments" },
62 { scope: "deployments:write", description: "Report deployments and their statuses, from any CI" },
63 { scope: "observability:read", description: "See projects' requests, logs, traces, errors and metrics, and their observability settings, ingest tokens and exports" },
64 { scope: "observability:write", description: "Resolve and ignore errors, and change projects' observability settings, ingest tokens and exports" },
65 { scope: "memory:read", description: "Recall memory" },
66 { scope: "memory:write", description: "Save memory for whoever works on the project next" },
67 { scope: "account:read", description: "Read your email addresses, invites, invitations, pinned projects and stars" },
68 { scope: "account:write", description: "Change your email addresses, make invites, answer invitations, pin projects and star repositories" },
69 { scope: "notifications:read", description: "See your inbox, its threads, and what you subscribe to and watch" },
70 { scope: "notifications:write", description: "Mark notifications read, done, saved or snoozed, subscribe to threads and watch repositories" },
71 { scope: "chat:read", description: "Read the conversations you are in: channels, direct messages, threads and reactions, live" },
72 { scope: "chat:write", description: "Send, edit and delete your messages, react, mark conversations read, and answer cards in chat" },
73 { scope: "workspace:read", description: "Read workspace settings, invites, integrations, teams and rulesets" },
74 { scope: "workspace:admin", description: "Create and delete workspaces, invite members, connect integrations, create, change and delete teams, and change the workspace's rulesets" },
75 { scope: "billing:read", description: "See a workspace's usage, budget and invoices" },
76 { scope: "billing:write", description: "Change a workspace's budget" },
77 { scope: "access:read", description: "See who has access to repositories" },
78 { scope: "access:admin", description: "Give and take away access to repositories, a team's included" },
79 { scope: "webhooks:read", description: "See webhooks and their deliveries" },
80 { scope: "webhooks:admin", description: "Create, change and delete webhooks" },
81 { scope: "secrets:read", description: "List secrets (never their values) and read variables" },
82 { scope: "secrets:admin", description: "Set and delete secrets and variables" },
83 { scope: "runners:read", description: "See self-hosted runners and their groups" },
84 { scope: "runners:admin", description: "Register and remove self-hosted runners, change their groups and settings" },
85 { scope: "artifacts:read", description: "List, read and search artifacts you can see, their versions, and the numbers their dashboards show" },
86 { scope: "artifacts:write", description: "Create, rename, move, edit, trash and restore artifacts, and propose changes to them" },
87 { scope: "artifacts:admin", description: "Share artifacts, change who can open them, and delete them for good" },
88] as const;
89
90/**
91 * Scopes of resources being built that tokens are not offered yet (Rust:
92 * `Resource::offered`). They parse in Rust and are typed here, but no
93 * preset, full access, OAuth request or token form hands them out, and no
94 * operation needs them. When one ships, its rows move to the end of
95 * `SCOPES` and `SCOPE_RESOURCES`. Empty now: Artifacts shipped last.
96 */
97export const UPCOMING_SCOPES: readonly { scope: Scope; description: string }[] = [
98] as const;
99
100/**
101 * Scopes of what g1t no longer has (Rust: `Resource::retired`): `agents`
102 * (g1t's own agents) and `models` (its model proxy and AI Gateway). Tokens
103 * and OAuth grants that stored them still read, but they are never offered
104 * or granted and no operation needs them: they give nothing. `isScope` and
105 * `parseScopes` leave them out.
106 */
107export const RETIRED_SCOPES = [
108 { scope: "agents:run", description: "Retired: put g1t's own agents to work. Grants nothing now" },
109 { scope: "models:read", description: "Retired: see the AI Gateway's requests. Grants nothing now" },
110 { scope: "models:write", description: "Retired: send requests through the AI Gateway. Grants nothing now" },
111] as const;
112
113export type Scope = (typeof SCOPES)[number]["scope"];
114
115/** Where a resource sits on the token form; only a person's token may hold account ones. */
116export type ResourceGroup = "repository" | "workspace" | "account";
117
118/** Resources in the order settings show them, with their names for people and their group. */
119export const SCOPE_RESOURCES: { resource: ScopeResource; label: string; group: ResourceGroup }[] = [
120 { resource: "repo", label: "Repositories", group: "repository" },
121 { resource: "code", label: "Code", group: "repository" },
122 { resource: "security", label: "Security", group: "repository" },
123 { resource: "packages", label: "Packages", group: "repository" },
124 { resource: "issues", label: "Issues", group: "repository" },
125 { resource: "pull_requests", label: "Pull requests", group: "repository" },
126 { resource: "workflows", label: "Workflows", group: "repository" },
127 { resource: "workflow_files", label: "Workflow files", group: "repository" },
128 { resource: "checks", label: "Checks and statuses", group: "repository" },
129 { resource: "deployments", label: "Deployments", group: "repository" },
130 { resource: "observability", label: "Observability", group: "repository" },
131 { resource: "memory", label: "Memory", group: "repository" },
132 { resource: "account", label: "Your account", group: "account" },
133 { resource: "notifications", label: "Notifications", group: "account" },
134 { resource: "chat", label: "Chat", group: "account" },
135 { resource: "workspace", label: "Workspaces", group: "workspace" },
136 { resource: "billing", label: "Billing", group: "workspace" },
137 { resource: "access", label: "Who has access", group: "repository" },
138 { resource: "webhooks", label: "Webhooks", group: "repository" },
139 { resource: "secrets", label: "Secrets and variables", group: "repository" },
140 { resource: "runners", label: "Self-hosted runners", group: "workspace" },
141 { resource: "artifacts", label: "Artifacts", group: "workspace" },
142];
143
144/** Resources not offered yet, as `UPCOMING_SCOPES`: settings never show them. */
145export const UPCOMING_RESOURCES: { resource: ScopeResource; label: string; group: ResourceGroup }[] = [
146];
147
148/** Resources of what g1t no longer has, as `RETIRED_SCOPES`: settings never show them. */
149export const RETIRED_RESOURCES: { resource: ScopeResource; label: string; group: ResourceGroup }[] = [
150 { resource: "agents", label: "g1t agents", group: "repository" },
151 { resource: "models", label: "AI Gateway", group: "workspace" },
152];
153
154const LEVEL_ORDER: Record<ScopeLevel, number> = { read: 0, write: 1, run: 2, delete: 3, admin: 4 };
155
156export function scopeResource(scope: Scope): ScopeResource {
157 return scope.split(":")[0] as ScopeResource;
158}
159
160export function scopeLevel(scope: Scope): ScopeLevel {
161 return scope.split(":")[1] as ScopeLevel;
162}
163
164export function isScope(text: string): text is Scope {
165 return SCOPES.some((row) => row.scope === text);
166}
167
168/** Changes that are hard to undo, or decide who can reach what. */
169export function isDangerous(scope: Scope): boolean {
170 const level = scopeLevel(scope);
171 return level === "admin" || level === "delete";
172}
173
174export function describeScope(scope: Scope): string {
175 return [...SCOPES, ...UPCOMING_SCOPES].find((row) => row.scope === scope)?.description ?? scope;
176}
177
178/** Whether holding `held` gives `needed`: the same resource, at its level or lower. */
179export function scopeIncludes(held: Scope, needed: Scope): boolean {
180 return (
181 scopeResource(held) === scopeResource(needed) &&
182 LEVEL_ORDER[scopeLevel(held)] >= LEVEL_ORDER[scopeLevel(needed)]
183 );
184}
185
186/**
187 * Whether a token holding `scopes` may do what `needed` gates (Rust:
188 * `TokenAccess::allows`). Null or absent scopes are full access, which
189 * holds every scope; unknown names give nothing.
190 */
191export function tokenAllows(scopes: readonly string[] | null | undefined, needed: Scope): boolean {
192 if (scopes === null || scopes === undefined) return true;
193 return scopes.some((held) => isScope(held) && scopeIncludes(held, needed));
194}
195
196/** The levels a resource has, least first. */
197export function levelsOf(resource: ScopeResource): ScopeLevel[] {
198 return SCOPES.filter((row) => scopeResource(row.scope) === resource).map((row) => scopeLevel(row.scope));
199}
200
201/** The token form's groups, in order. */
202export const RESOURCE_GROUPS: { group: ResourceGroup; label: string; about: string }[] = [
203 { group: "repository", label: "Repository permissions", about: "What it may do in the repositories it reaches." },
204 { group: "workspace", label: "Workspace permissions", about: "What it may do with the workspaces it reaches themselves." },
205 { group: "account", label: "Account permissions", about: "What it may do with your own account. Personal tokens only." },
206];
207
208/** A token's permissions: each resource held, at its highest level; left out is none. */
209export type Permissions = Partial<Record<ScopeResource, ScopeLevel>>;
210
211/** Scopes as permissions. Null scopes (full access) are every resource at its highest. */
212export function permissionsOf(scopes: readonly string[] | null): Permissions {
213 const permissions: Permissions = {};
214 const held = scopes === null ? SCOPES.map((row) => row.scope) : parseScopes(scopes.join(" "));
215 for (const scope of held) {
216 const resource = scopeResource(scope);
217 const now = permissions[resource];
218 if (!now || LEVEL_ORDER[scopeLevel(scope)] > LEVEL_ORDER[now]) permissions[resource] = scopeLevel(scope);
219 }
220 return permissions;
221}
222
223/** Permissions as the scopes a token stores: the highest of each resource, in table order. */
224export function scopesOfPermissions(permissions: Permissions): Scope[] {
225 const wanted = new Set(
226 Object.entries(permissions)
227 .filter(([, level]) => level)
228 .map(([resource, level]) => `${resource}:${level}`),
229 );
230 return SCOPES.map((row) => row.scope).filter((scope) => wanted.has(scope));
231}
232
233/** The longest a token with an expiry may last, in days. */
234export const MAX_TOKEN_LIFETIME_DAYS = 366;
235
236/** The most repositories a token may select. */
237export const MAX_SELECTED_REPOSITORIES = 50;
238
239/** Scopes from text separated by spaces or commas, in table order; unknown ones are left out. */
240export function parseScopes(text: string): Scope[] {
241 const given = new Set(text.split(/[\s,]+/).map((part) => part.trim().toLowerCase()));
242 return SCOPES.map((row) => row.scope).filter((scope) => given.has(scope));
243}
244
245/** What a token stores for full access. */
246export const FULL_ACCESS = "*";
247
248export type PresetId = "read_only" | "agent" | "ci" | "full";
249
250/** Starting points for choosing scopes. `*` is full access. No preset gives chat: an application gets it only by asking for it. */
251export const PRESET_SCOPES = {
252 read_only: [
253 "repo:read", "code:read", "security:read", "packages:read", "issues:read", "pull_requests:read", "workflows:read", "checks:read", "deployments:read", "observability:read", "memory:read", "account:read", "notifications:read", "workspace:read", "billing:read", "access:read", "webhooks:read", "secrets:read", "runners:read", "artifacts:read",
254 ] as const,
255 agent: [
256 "repo:read", "code:read", "code:write", "security:read", "packages:read", "issues:read", "issues:write", "pull_requests:read", "pull_requests:write", "workflows:read", "checks:read", "deployments:read", "observability:read", "memory:read", "memory:write", "account:read", "notifications:read", "notifications:write", "workspace:read", "billing:read", "access:read", "webhooks:read", "secrets:read", "artifacts:read",
257 ] as const,
258 ci: [
259 "repo:read", "code:read", "code:write", "packages:read", "packages:write", "workflows:read", "workflows:write", "checks:read", "checks:write", "deployments:read", "deployments:write",
260 ] as const,
261 full: [
262 "*",
263 ] as const,
264};
265
266export const PRESETS: { id: PresetId; label: string; description: string }[] = [
267 { id: "read_only", label: "Read only", description: "Read everything you can read; change nothing." },
268 { id: "agent", label: "Agent", description: "Read everything, work on issues and pull requests, and push code." },
269 { id: "ci", label: "CI", description: "Clone and push code, push and pull packages, run workflows, and report checks and deployments." },
270 { id: "full", label: "Full access", description: "Everything you can do, including deleting repositories and changing who has access." },
271];
272
273/** The scopes of a preset, or null for full access. */
274export function presetScopes(id: PresetId): Scope[] | null {
275 if (id === "full") return null;
276 return [...PRESET_SCOPES[id]] as Scope[];
277}
278
279/** What an OAuth client gets when it asks for nothing in particular. */
280export const OAUTH_DEFAULT_SCOPES: Scope[] = [...PRESET_SCOPES.agent];
281
282/** The operation each scope gates, by the API's operation names. */
283export const OPERATION_SCOPES = [
284 ["list_emails", "account:read"],
285 ["add_email", "account:write"],
286 ["confirm_email", "account:write"],
287 ["remove_email", "account:write"],
288 ["update_email_settings", "account:write"],
289 ["list_invitations", "account:read"],
290 ["accept_invitation", "account:write"],
291 ["decline_invitation", "account:write"],
292 ["list_my_repo_invitations", "account:read"],
293 ["accept_repo_invitation", "account:write"],
294 ["decline_repo_invitation", "account:write"],
295 // Your pinned projects: a preference of your account.
296 ["list_pinned_projects", "account:read"],
297 ["pin_project", "account:write"],
298 // Your stars: a preference of your account.
299 ["list_starred", "account:read"],
300 ["check_starred", "account:read"],
301 ["star_repo", "account:write"],
302 ["unstar_repo", "account:write"],
303 ["unpin_project", "account:write"],
304 ["reorder_pinned_projects", "account:write"],
305 // Your inbox: notifications, subscriptions and watching.
306 ["list_notifications", "notifications:read"],
307 ["get_notification_thread", "notifications:read"],
308 ["get_thread_subscription", "notifications:read"],
309 ["get_repo_subscription", "notifications:read"],
310 ["list_watched_repos", "notifications:read"],
311 ["mark_notifications_read", "notifications:write"],
312 ["mark_thread_read", "notifications:write"],
313 ["mark_thread_done", "notifications:write"],
314 ["save_thread", "notifications:write"],
315 ["snooze_thread", "notifications:write"],
316 ["set_thread_subscription", "notifications:write"],
317 ["delete_thread_subscription", "notifications:write"],
318 ["set_repo_subscription", "notifications:write"],
319 ["delete_repo_subscription", "notifications:write"],
320 ["create_workspace", "workspace:admin"],
321 ["delete_workspace", "workspace:admin"],
322 ["get_workspace", "workspace:read"],
323 ["update_workspace", "workspace:admin"],
324 ["list_members", "workspace:read"],
325 ["update_member", "workspace:admin"],
326 ["remove_member", "workspace:admin"],
327 ["transfer_ownership", "workspace:admin"],
328 ["leave_workspace", "account:write"],
329 ["list_workspace_invites", "workspace:read"],
330 ["invite_member", "workspace:admin"],
331 ["revoke_workspace_invite", "workspace:admin"],
332 ["list_integrations", "workspace:read"],
333 ["connect_integration", "workspace:admin"],
334 ["update_integration", "workspace:admin"],
335 ["disconnect_integration", "workspace:admin"],
336 ["test_integration", "workspace:admin"],
337 ["list_teams", "workspace:read"],
338 ["get_team", "workspace:read"],
339 ["list_team_members", "workspace:read"],
340 ["list_child_teams", "workspace:read"],
341 ["list_team_repos", "workspace:read"],
342 ["list_user_teams", "workspace:read"],
343 ["create_team", "workspace:admin"],
344 ["list_workspace_rulesets", "workspace:read"],
345 ["get_workspace_ruleset", "workspace:read"],
346 ["list_workspace_rule_evaluations", "workspace:read"],
347 ["create_workspace_ruleset", "workspace:admin"],
348 ["update_workspace_ruleset", "workspace:admin"],
349 ["delete_workspace_ruleset", "workspace:admin"],
350 ["update_team", "workspace:admin"],
351 ["delete_team", "workspace:admin"],
352 ["set_team_member", "workspace:admin"],
353 ["remove_team_member", "workspace:admin"],
354 ["set_team_review_assignment", "workspace:admin"],
355 // A workspace's billing: usage, budget and invoices.
356 ["get_usage", "billing:read"],
357 ["get_budget", "billing:read"],
358 ["list_invoices", "billing:read"],
359 ["get_billing_details", "billing:read"],
360 ["set_budget", "billing:write"],
361 ["list_repos", "repo:read"],
362 ["get_repo", "repo:read"],
363 ["download_tarball_archive", "code:read"],
364 ["download_zipball_archive", "code:read"],
365 // Projects follow their repositories.
366 ["list_projects", "repo:read"],
367 ["get_project", "repo:read"],
368 ["search", "repo:read"],
369 ["list_events", "repo:read"],
370 // What the default branch says about a repository, who starred it, and
371 // its releases.
372 ["get_languages", "repo:read"],
373 ["list_contributors", "repo:read"],
374 ["get_license", "repo:read"],
375 ["list_stargazers", "repo:read"],
376 ["list_releases", "repo:read"],
377 ["get_latest_release", "repo:read"],
378 ["get_release_by_tag", "repo:read"],
379 ["get_release", "repo:read"],
380 ["create_release", "repo:write"],
381 ["update_release", "repo:write"],
382 ["delete_release", "repo:write"],
383 ["list_labels", "repo:read"],
384 ["list_milestones", "repo:read"],
385 ["get_milestone", "repo:read"],
386 ["create_label", "issues:write"],
387 ["update_label", "issues:write"],
388 ["delete_label", "issues:write"],
389 ["add_default_labels", "issues:write"],
390 ["create_milestone", "issues:write"],
391 ["update_milestone", "issues:write"],
392 ["delete_milestone", "issues:write"],
393 ["get_repo_settings", "repo:read"],
394 ["list_check_names", "repo:read"],
395 ["list_deleted_repos", "repo:read"],
396 ["list_security_alerts", "repo:read"],
397 ["get_codeowners_errors", "repo:read"],
398 ["create_repo", "repo:write"],
399 ["update_repo", "repo:write"],
400 ["update_project", "repo:write"],
401 ["update_repo_settings", "repo:write"],
402 ["list_repo_rulesets", "repo:read"],
403 ["get_repo_ruleset", "repo:read"],
404 ["get_branch_rules", "repo:read"],
405 ["list_rule_evaluations", "repo:read"],
406 ["create_repo_ruleset", "repo:admin"],
407 ["update_repo_ruleset", "repo:admin"],
408 ["delete_repo_ruleset", "repo:admin"],
409 ["rename_branch", "repo:write"],
410 ["create_branch", "repo:write"],
411 ["delete_branch", "repo:write"],
412 ["rename_repo", "repo:admin"],
413 ["transfer_repo", "repo:admin"],
414 ["archive_repo", "repo:admin"],
415 ["unarchive_repo", "repo:admin"],
416 ["set_repo_visibility", "repo:admin"],
417 ["delete_repo", "repo:admin"],
418 ["restore_repo", "repo:admin"],
419 ["purge_repo", "repo:admin"],
420 ["dismiss_security_alert", "repo:admin"],
421 ["reopen_security_alert", "repo:admin"],
422 // The security suite.
423 ["list_secret_scanning_alerts", "security:read"],
424 ["get_secret_scanning_alert", "security:read"],
425 ["list_secret_scanning_locations", "security:read"],
426 ["list_bypass_requests", "security:read"],
427 ["list_custom_patterns", "security:read"],
428 ["list_code_scanning_alerts", "security:read"],
429 ["get_code_scanning_alert", "security:read"],
430 ["list_code_scanning_analyses", "security:read"],
431 ["get_sarif_upload", "security:read"],
432 ["list_vulnerability_alerts", "security:read"],
433 ["get_vulnerability_alert", "security:read"],
434 ["get_dependency_graph", "security:read"],
435 ["get_sbom", "security:read"],
436 ["compare_dependencies", "security:read"],
437 ["get_security_settings", "security:read"],
438 ["get_workspace_security_settings", "security:read"],
439 ["get_security_overview", "security:read"],
440 ["list_security_advisories", "security:read"],
441 ["get_security_advisory", "security:read"],
442 ["list_vulnerability_reports", "security:read"],
443 ["update_secret_scanning_alert", "security:write"],
444 ["bypass_push_protection", "security:write"],
445 ["check_secret_validity", "security:write"],
446 ["review_bypass_request", "security:write"],
447 ["create_custom_pattern", "security:write"],
448 ["update_custom_pattern", "security:write"],
449 ["delete_custom_pattern", "security:write"],
450 ["dry_run_custom_pattern", "security:write"],
451 ["update_code_scanning_alert", "security:write"],
452 ["upload_sarif", "security:write"],
453 ["update_vulnerability_alert", "security:write"],
454 ["update_security_settings", "security:write"],
455 ["update_workspace_security_settings", "security:write"],
456 ["create_security_advisory", "security:write"],
457 ["update_security_advisory", "security:write"],
458 ["set_security_advisory_state", "security:write"],
459 ["report_vulnerability", "security:write"],
460 ["review_vulnerability_report", "security:write"],
461 ["list_issues", "issues:read"],
462 ["get_issue", "issues:read"],
463 ["create_issue", "issues:write"],
464 ["update_issue", "issues:write"],
465 ["list_issue_labels", "issues:read"],
466 ["add_issue_labels", "issues:write"],
467 ["set_issue_labels", "issues:write"],
468 ["remove_issue_labels", "issues:write"],
469 ["close_issue", "issues:write"],
470 ["reopen_issue", "issues:write"],
471 ["add_comment", "issues:write"],
472 ["edit_comment", "issues:write"],
473 ["delete_comment", "issues:write"],
474 ["import_issue", "issues:write"],
475 ["list_pull_requests", "pull_requests:read"],
476 ["get_pull_request", "pull_requests:read"],
477 ["get_pull_request_changes", "pull_requests:read"],
478 ["read_session", "pull_requests:read"],
479 ["get_merge_queue", "pull_requests:read"],
480 ["create_pull_request", "pull_requests:write"],
481 ["update_pull_request", "pull_requests:write"],
482 ["record_session", "pull_requests:write"],
483 ["mark_pull_request_ready", "pull_requests:write"],
484 ["close_pull_request", "pull_requests:write"],
485 ["reopen_pull_request", "pull_requests:write"],
486 ["convert_pull_request_to_draft", "pull_requests:write"],
487 ["review_pull_request", "pull_requests:write"],
488 ["merge_pull_request", "pull_requests:write"],
489 ["request_reviewers", "pull_requests:write"],
490 ["remove_requested_reviewers", "pull_requests:write"],
491 ["list_workflows", "workflows:read"],
492 ["list_workflow_runs", "workflows:read"],
493 ["get_workflow_run", "workflows:read"],
494 ["get_job_logs", "workflows:read"],
495 ["dispatch_workflow", "workflows:write"],
496 ["cancel_workflow_run", "workflows:write"],
497 ["rerun_workflow_run", "workflows:write"],
498 ["update_workflow", "workflows:write"],
499 ["list_artifacts", "workflows:read"],
500 ["list_workflow_run_artifacts", "workflows:read"],
501 ["get_artifact", "workflows:read"],
502 ["download_artifact", "workflows:read"],
503 ["get_artifact_retention", "workflows:read"],
504 ["delete_artifact", "workflows:write"],
505 ["set_artifact_retention", "workflows:write"],
506 ["list_commit_statuses", "checks:read"],
507 ["get_combined_status", "checks:read"],
508 ["list_check_runs_for_ref", "checks:read"],
509 ["get_check_run", "checks:read"],
510 ["list_check_run_annotations", "checks:read"],
511 ["list_check_suites_for_ref", "checks:read"],
512 ["get_check_suite", "checks:read"],
513 ["create_commit_status", "checks:write"],
514 ["create_check_run", "checks:write"],
515 ["update_check_run", "checks:write"],
516 ["rerequest_check_run", "checks:write"],
517 ["rerequest_check_suite", "checks:write"],
518 // Deployments, wherever they run: reading them, and reporting them.
519 ["list_deployments", "deployments:read"],
520 ["get_deployment", "deployments:read"],
521 ["list_deployment_statuses", "deployments:read"],
522 ["list_environments", "deployments:read"],
523 ["get_environment", "deployments:read"],
524 ["create_deployment", "deployments:write"],
525 ["create_deployment_status", "deployments:write"],
526 ["start_deployment_build", "deployments:write"],
527 // Observability: what a project's software did, and how it is collected.
528 ["get_observability_overview", "observability:read"],
529 ["list_requests", "observability:read"],
530 ["get_request", "observability:read"],
531 ["list_logs", "observability:read"],
532 ["list_traces", "observability:read"],
533 ["get_trace", "observability:read"],
534 ["list_errors", "observability:read"],
535 ["list_metrics", "observability:read"],
536 ["get_metric", "observability:read"],
537 ["get_observability_settings", "observability:read"],
538 ["list_ingest_tokens", "observability:read"],
539 ["list_telemetry_exports", "observability:read"],
540 ["get_observability_usage", "observability:read"],
541 ["update_error", "observability:write"],
542 ["update_observability_settings", "observability:write"],
543 ["create_ingest_token", "observability:write"],
544 ["delete_ingest_token", "observability:write"],
545 ["create_telemetry_export", "observability:write"],
546 ["update_telemetry_export", "observability:write"],
547 ["delete_telemetry_export", "observability:write"],
548 // What keeps runs safe: the runs environments hold and reviewing them,
549 // approving a pull request's run, and a repository's own rules for its
550 // environments and tokens, which are an admin's.
551 ["get_pending_deployments", "workflows:read"],
552 ["review_pending_deployments", "workflows:write"],
553 ["approve_workflow_run", "workflows:write"],
554 ["get_workflow_permissions", "repo:read"],
555 ["get_fork_pr_approval", "repo:read"],
556 ["get_actions_access", "repo:read"],
557 ["update_environment", "repo:admin"],
558 ["delete_environment", "repo:admin"],
559 ["set_workflow_permissions", "repo:admin"],
560 ["set_fork_pr_approval", "repo:admin"],
561 ["set_actions_access", "repo:admin"],
562 // Starting workflows from outside, as a push would.
563 ["create_repository_dispatch", "code:write"],
564 // A workspace's policy for its repositories' tokens.
565 ["get_workspace_workflow_permissions", "workspace:read"],
566 ["set_workspace_workflow_permissions", "workspace:admin"],
567 ["list_workspace_managed_workflows", "workspace:read"],
568 ["create_workspace_managed_workflow", "workspace:admin"],
569 ["update_workspace_managed_workflow", "workspace:admin"],
570 ["delete_workspace_managed_workflow", "workspace:admin"],
571 // A workspace's rules for personal access tokens, and its members' tokens.
572 ["get_token_policy", "workspace:read"],
573 ["set_token_policy", "workspace:admin"],
574 ["list_member_tokens", "access:read"],
575 ["list_token_requests", "access:read"],
576 ["review_token_request", "access:admin"],
577 ["revoke_member_token", "access:admin"],
578 ["recall", "memory:read"],
579 ["get_context", "memory:read"],
580 ["remember", "memory:write"],
581 ["list_collaborators", "access:read"],
582 ["get_collaborator_permission", "access:read"],
583 ["list_repo_invitations", "access:read"],
584 ["list_outside_collaborators", "access:read"],
585 ["add_collaborator", "access:admin"],
586 ["update_collaborator", "access:admin"],
587 ["remove_collaborator", "access:admin"],
588 ["revoke_repo_invitation", "access:admin"],
589 ["set_base_permission", "access:admin"],
590 ["set_team_repo", "access:admin"],
591 ["remove_team_repo", "access:admin"],
592 ["list_deploy_keys", "access:read"],
593 ["get_deploy_key", "access:read"],
594 ["create_deploy_key", "access:admin"],
595 ["delete_deploy_key", "access:admin"],
596 ["list_webhooks", "webhooks:read"],
597 ["list_webhook_deliveries", "webhooks:read"],
598 ["create_webhook", "webhooks:admin"],
599 ["update_webhook", "webhooks:admin"],
600 ["delete_webhook", "webhooks:admin"],
601 ["ping_webhook", "webhooks:admin"],
602 ["redeliver_webhook", "webhooks:admin"],
603 ["list_actions_secrets", "secrets:read"],
604 ["list_actions_variables", "secrets:read"],
605 ["set_actions_secret", "secrets:admin"],
606 ["delete_actions_secret", "secrets:admin"],
607 ["set_actions_variable", "secrets:admin"],
608 ["delete_actions_variable", "secrets:admin"],
609 // Self-hosted runners.
610 ["list_runners", "runners:read"],
611 ["list_runner_groups", "runners:read"],
612 ["get_runner_settings", "runners:read"],
613 ["create_runner_registration_token", "runners:admin"],
614 ["remove_runner", "runners:admin"],
615 ["create_runner_group", "runners:admin"],
616 ["update_runner_group", "runners:admin"],
617 ["delete_runner_group", "runners:admin"],
618 ["update_runner_settings", "runners:admin"],
619 // Packages: reading them, their versions and who may use them needs
620 // `packages:read`; changing their settings, access and Manage Actions
621 // access `packages:write` (and the Admin role on the package, which the
622 // packages service checks); deleting and restoring packages and
623 // versions `packages:delete`, as the registries' own deletes do.
624 ["list_packages", "packages:read"],
625 ["get_package", "packages:read"],
626 ["list_package_versions", "packages:read"],
627 ["get_package_version", "packages:read"],
628 ["list_package_access", "packages:read"],
629 ["list_package_actions_access", "packages:read"],
630 ["update_package", "packages:write"],
631 ["link_package", "packages:write"],
632 ["unlink_package", "packages:write"],
633 ["set_package_access", "packages:write"],
634 ["remove_package_access", "packages:write"],
635 ["set_package_actions_access", "packages:write"],
636 ["remove_package_actions_access", "packages:write"],
637 ["delete_package", "packages:delete"],
638 ["restore_package", "packages:delete"],
639 ["delete_package_version", "packages:delete"],
640 ["restore_package_version", "packages:delete"],
641 // Artifacts mode's docs, slides, designs and dashboards (the `artifact`
642 // MCP tool). The artifacts service then checks the person's role on each.
643 ["list_workspace_artifacts", "artifacts:read"],
644 ["search_workspace_artifacts", "artifacts:read"],
645 ["get_workspace_artifact", "artifacts:read"],
646 ["get_workspace_artifact_content", "artifacts:read"],
647 ["list_workspace_artifact_versions", "artifacts:read"],
648 ["get_workspace_artifact_access", "artifacts:read"],
649 ["list_workspace_artifact_templates", "artifacts:read"],
650 ["list_workspace_artifact_spaces", "artifacts:read"],
651 ["query_workspace_dataset", "artifacts:read"],
652 ["create_workspace_artifact", "artifacts:write"],
653 ["update_workspace_artifact", "artifacts:write"],
654 ["edit_workspace_artifact", "artifacts:write"],
655 ["trash_workspace_artifact", "artifacts:write"],
656 ["restore_workspace_artifact", "artifacts:write"],
657 ["restore_workspace_artifact_version", "artifacts:write"],
658 ["set_workspace_artifact_access", "artifacts:admin"],
659 ["purge_workspace_artifact", "artifacts:admin"],
660] as const;
661
662/**
663 * The scopes a token or grant holds, as stored: null for full access, or
664 * the list. `legacy` marks a token made before scopes, which has full
665 * access until someone narrows it.
666 */
667export type TokenScopes = {
668 scopes: Scope[] | null;
669 legacy: boolean;
670};
671
672/**
673 * How settings group scopes into a checklist: each group's scopes, least
674 * first. Admin scopes are not here; they are under "Dangerous" on their
675 * own (see `DANGEROUS_SCOPES`). Every other scope is in exactly one group.
676 */
677export const SCOPE_GROUPS: { id: string; label: string; scopes: Scope[] }[] = [
678 { id: "code", label: "Repositories & code", scopes: ["repo:read", "repo:write", "code:read", "code:write"] },
679 { id: "security", label: "Security", scopes: ["security:read", "security:write"] },
680 { id: "packages", label: "Packages", scopes: ["packages:read", "packages:write"] },
681 { id: "work", label: "Issues & pull requests", scopes: ["issues:read", "issues:write", "pull_requests:read", "pull_requests:write"] },
682 { id: "workflows", label: "Workflows", scopes: ["workflows:read", "workflows:write", "workflow_files:write"] },
683 { id: "checks", label: "Checks", scopes: ["checks:read", "checks:write"] },
684 { id: "deployments", label: "Deployments", scopes: ["deployments:read", "deployments:write"] },
685 { id: "observability", label: "Observability", scopes: ["observability:read", "observability:write"] },
686 { id: "memory", label: "Memory", scopes: ["memory:read", "memory:write"] },
687 { id: "account", label: "Account", scopes: ["account:read", "account:write"] },
688 { id: "notifications", label: "Notifications", scopes: ["notifications:read", "notifications:write"] },
689 { id: "chat", label: "Chat", scopes: ["chat:read", "chat:write"] },
690 { id: "workspace", label: "Workspace", scopes: ["workspace:read", "access:read", "webhooks:read", "secrets:read"] },
691 { id: "billing", label: "Billing", scopes: ["billing:read", "billing:write"] },
692 { id: "runners", label: "Runners", scopes: ["runners:read"] },
693 { id: "artifacts", label: "Artifacts", scopes: ["artifacts:read", "artifacts:write"] },
694];
695
696/** The admin and delete scopes, shown under "Dangerous" behind a warning. */
697export const DANGEROUS_SCOPES: Scope[] = SCOPES.map((row) => row.scope).filter(isDangerous);