Skip to content

g1t/packages/contracts/src/scopes.ts

431 lines20,682 bytesCodeBlame
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 reaches whatever its owner can reach (a workspace's token, that
7 * workspace); what a request may do is the intersection of the owner's
8 * role and the token's scopes.
9 */
10
11export type ScopeResource =
12 | "repo"
13 | "code"
14 | "security"
15 | "packages"
16 | "issues"
17 | "pull_requests"
18 | "agents"
19 | "workflows"
20 | "memory"
21 | "account"
22 | "notifications"
23 | "workspace"
24 | "billing"
25 | "access"
26 | "webhooks"
27 | "secrets"
28 | "runners";
29
30export type ScopeLevel = "read" | "write" | "run" | "delete" | "admin";
31
32/** Every scope, grouped by resource, least first. */
33export const SCOPES = [
34 { scope: "repo:read", description: "See repositories, their settings, labels, timelines and security alerts, and search" },
35 { scope: "repo:write", description: "Create repositories, rename branches and change how pull requests merge" },
36 { scope: "repo:admin", description: "Rename, archive, transfer, delete or change who can see a repository, change its rulesets, and dismiss security alerts" },
37 { scope: "code:read", description: "Clone and fetch private repositories with git" },
38 { scope: "code:write", description: "Push commits with git" },
39 { scope: "security:read", description: "See secret scanning, code scanning and vulnerability alerts, custom patterns, the dependency graph and SBOM, and security settings" },
40 { scope: "security:write", description: "Dismiss and reopen alerts, bypass push protection, review bypass requests, manage custom patterns, upload SARIF and change security settings" },
41 { scope: "packages:read", description: "Pull container images and install private packages" },
42 { scope: "packages:write", description: "Push container images and publish packages" },
43 { scope: "packages:delete", description: "Delete packages and their versions" },
44 { scope: "issues:read", description: "Read issues, comments and plans" },
45 { scope: "issues:write", description: "Open, edit, close and comment on issues" },
46 { scope: "pull_requests:read", description: "Read pull requests, their changes, sessions and merge queues" },
47 { scope: "pull_requests:write", description: "Open, review, close and merge pull requests" },
48 { scope: "agents:run", description: "Put g1t agents to work and message them, which uses the workspace's money" },
49 { scope: "workflows:read", description: "Read workflows, runs and logs" },
50 { scope: "workflows:write", description: "Run, cancel, rerun and turn workflows on or off" },
51 { scope: "memory:read", description: "Recall memory and search the workspace's context" },
52 { scope: "memory:write", description: "Save memory for the next agent" },
53 { scope: "account:read", description: "Read your email addresses, invites, invitations and pinned projects" },
54 { scope: "account:write", description: "Change your email addresses, make invites, answer invitations and pin projects" },
55 { scope: "notifications:read", description: "See your inbox, its threads, and what you subscribe to and watch" },
56 { scope: "notifications:write", description: "Mark notifications read, done, saved or snoozed, subscribe to threads and watch repositories" },
57 { scope: "workspace:read", description: "Read workspace settings, invites, integrations, model routes, teams and rulesets" },
58 { scope: "workspace:admin", description: "Create and delete workspaces, invite members, connect integrations, create, change and delete teams, and change the workspace's rulesets" },
59 { scope: "billing:read", description: "See a workspace's usage, budget, AI credit and invoices" },
60 { scope: "billing:write", description: "Change a workspace's budget and buy AI credit" },
61 { scope: "access:read", description: "See who has access to repositories" },
62 { scope: "access:admin", description: "Give and take away access to repositories, a team's included" },
63 { scope: "webhooks:read", description: "See webhooks and their deliveries" },
64 { scope: "webhooks:admin", description: "Create, change and delete webhooks" },
65 { scope: "secrets:read", description: "List secrets (never their values) and read variables" },
66 { scope: "secrets:admin", description: "Set and delete secrets and variables" },
67 { scope: "runners:read", description: "See self-hosted runners, their groups and where agents run" },
68 { scope: "runners:admin", description: "Register and remove self-hosted runners, change their groups and settings" },
69] as const;
70
71export type Scope = (typeof SCOPES)[number]["scope"];
72
73/** Resources in the order settings show them, with their names for people. */
74export const SCOPE_RESOURCES: { resource: ScopeResource; label: string }[] = [
75 { resource: "repo", label: "Repositories" },
76 { resource: "code", label: "Code" },
77 { resource: "security", label: "Security" },
78 { resource: "packages", label: "Packages" },
79 { resource: "issues", label: "Issues" },
80 { resource: "pull_requests", label: "Pull requests" },
81 { resource: "agents", label: "g1t agents" },
82 { resource: "workflows", label: "Workflows" },
83 { resource: "memory", label: "Memory and context" },
84 { resource: "account", label: "Your account" },
85 { resource: "notifications", label: "Notifications" },
86 { resource: "workspace", label: "Workspaces" },
87 { resource: "billing", label: "Billing" },
88 { resource: "access", label: "Who has access" },
89 { resource: "webhooks", label: "Webhooks" },
90 { resource: "secrets", label: "Secrets and variables" },
91 { resource: "runners", label: "Self-hosted runners" },
92];
93
94const LEVEL_ORDER: Record<ScopeLevel, number> = { read: 0, write: 1, run: 2, delete: 3, admin: 4 };
95
96export function scopeResource(scope: Scope): ScopeResource {
97 return scope.split(":")[0] as ScopeResource;
98}
99
100export function scopeLevel(scope: Scope): ScopeLevel {
101 return scope.split(":")[1] as ScopeLevel;
102}
103
104export function isScope(text: string): text is Scope {
105 return SCOPES.some((row) => row.scope === text);
106}
107
108/** Changes that are hard to undo, or decide who can reach what. */
109export function isDangerous(scope: Scope): boolean {
110 const level = scopeLevel(scope);
111 return level === "admin" || level === "delete";
112}
113
114export function describeScope(scope: Scope): string {
115 return SCOPES.find((row) => row.scope === scope)?.description ?? scope;
116}
117
118/** Whether holding `held` gives `needed`: the same resource, at its level or lower. */
119export function scopeIncludes(held: Scope, needed: Scope): boolean {
120 return (
121 scopeResource(held) === scopeResource(needed) &&
122 LEVEL_ORDER[scopeLevel(held)] >= LEVEL_ORDER[scopeLevel(needed)]
123 );
124}
125
126/** The levels a resource has, least first. */
127export function levelsOf(resource: ScopeResource): ScopeLevel[] {
128 return SCOPES.filter((row) => scopeResource(row.scope) === resource).map((row) => scopeLevel(row.scope));
129}
130
131/** Scopes from text separated by spaces or commas, in table order; unknown ones are left out. */
132export function parseScopes(text: string): Scope[] {
133 const given = new Set(text.split(/[\s,]+/).map((part) => part.trim().toLowerCase()));
134 return SCOPES.map((row) => row.scope).filter((scope) => given.has(scope));
135}
136
137/** What a token stores for full access. */
138export const FULL_ACCESS = "*";
139
140export type PresetId = "read_only" | "agent" | "ci" | "full";
141
142/** Starting points for choosing scopes. `*` is full access. */
143export const PRESET_SCOPES = {
144 read_only: [
145 "repo:read", "code:read", "security:read", "packages:read", "issues:read", "pull_requests:read", "workflows:read", "memory:read", "account:read", "notifications:read", "workspace:read", "billing:read", "access:read", "webhooks:read", "secrets:read", "runners:read",
146 ] as const,
147 agent: [
148 "repo:read", "code:read", "code:write", "security:read", "packages:read", "issues:read", "issues:write", "pull_requests:read", "pull_requests:write", "agents:run", "workflows:read", "memory:read", "memory:write", "account:read", "notifications:read", "notifications:write", "workspace:read", "billing:read", "access:read", "webhooks:read", "secrets:read",
149 ] as const,
150 ci: [
151 "repo:read", "code:read", "code:write", "packages:read", "packages:write", "workflows:read", "workflows:write",
152 ] as const,
153 full: [
154 "*",
155 ] as const,
156};
157
158export const PRESETS: { id: PresetId; label: string; description: string }[] = [
159 { id: "read_only", label: "Read only", description: "Read everything you can read; change nothing." },
160 { id: "agent", label: "Agent", description: "Read everything, work on issues and pull requests, push code and run g1t agents." },
161 { id: "ci", label: "CI", description: "Clone and push code, push and pull packages, and run workflows." },
162 { id: "full", label: "Full access", description: "Everything you can do, including deleting repositories and changing who has access." },
163];
164
165/** The scopes of a preset, or null for full access. */
166export function presetScopes(id: PresetId): Scope[] | null {
167 if (id === "full") return null;
168 return [...PRESET_SCOPES[id]] as Scope[];
169}
170
171/** What an OAuth client gets when it asks for nothing in particular. */
172export const OAUTH_DEFAULT_SCOPES: Scope[] = [...PRESET_SCOPES.agent];
173
174/** The operation each scope gates, by the API's operation names. */
175export const OPERATION_SCOPES = [
176 ["list_emails", "account:read"],
177 ["add_email", "account:write"],
178 ["remove_email", "account:write"],
179 ["update_email_settings", "account:write"],
180 ["list_invites", "account:read"],
181 ["create_invite", "account:write"],
182 ["revoke_invite", "account:write"],
183 ["list_my_repo_invitations", "account:read"],
184 ["accept_repo_invitation", "account:write"],
185 ["decline_repo_invitation", "account:write"],
186 // Your pinned projects: a preference of your account.
187 ["list_pinned_projects", "account:read"],
188 ["pin_project", "account:write"],
189 ["unpin_project", "account:write"],
190 ["reorder_pinned_projects", "account:write"],
191 // Your inbox: notifications, subscriptions and watching.
192 ["list_notifications", "notifications:read"],
193 ["get_notification_thread", "notifications:read"],
194 ["get_thread_subscription", "notifications:read"],
195 ["get_repo_subscription", "notifications:read"],
196 ["list_watched_repos", "notifications:read"],
197 ["mark_notifications_read", "notifications:write"],
198 ["mark_thread_read", "notifications:write"],
199 ["mark_thread_done", "notifications:write"],
200 ["save_thread", "notifications:write"],
201 ["snooze_thread", "notifications:write"],
202 ["set_thread_subscription", "notifications:write"],
203 ["delete_thread_subscription", "notifications:write"],
204 ["set_repo_subscription", "notifications:write"],
205 ["delete_repo_subscription", "notifications:write"],
206 ["create_workspace", "workspace:admin"],
207 ["delete_workspace", "workspace:admin"],
208 ["get_workspace", "workspace:read"],
209 ["update_workspace", "workspace:admin"],
210 ["list_workspace_invites", "workspace:read"],
211 ["invite_member", "workspace:admin"],
212 ["revoke_workspace_invite", "workspace:admin"],
213 ["list_integrations", "workspace:read"],
214 ["connect_integration", "workspace:admin"],
215 ["disconnect_integration", "workspace:admin"],
216 ["test_integration", "workspace:admin"],
217 ["get_model_routes", "workspace:read"],
218 ["set_model_routes", "workspace:admin"],
219 ["list_teams", "workspace:read"],
220 ["get_team", "workspace:read"],
221 ["list_team_members", "workspace:read"],
222 ["list_child_teams", "workspace:read"],
223 ["list_team_repos", "workspace:read"],
224 ["list_user_teams", "workspace:read"],
225 ["create_team", "workspace:admin"],
226 ["list_workspace_rulesets", "workspace:read"],
227 ["get_workspace_ruleset", "workspace:read"],
228 ["list_workspace_rule_evaluations", "workspace:read"],
229 ["create_workspace_ruleset", "workspace:admin"],
230 ["update_workspace_ruleset", "workspace:admin"],
231 ["delete_workspace_ruleset", "workspace:admin"],
232 ["update_team", "workspace:admin"],
233 ["delete_team", "workspace:admin"],
234 ["set_team_member", "workspace:admin"],
235 ["remove_team_member", "workspace:admin"],
236 ["set_team_review_assignment", "workspace:admin"],
237 // A workspace's billing: usage, budget, AI credit and invoices.
238 ["get_usage", "billing:read"],
239 ["get_budget", "billing:read"],
240 ["get_ai_credit", "billing:read"],
241 ["list_invoices", "billing:read"],
242 ["get_billing_details", "billing:read"],
243 ["set_budget", "billing:write"],
244 ["buy_ai_credit", "billing:write"],
245 ["list_repos", "repo:read"],
246 ["get_repo", "repo:read"],
247 ["search", "repo:read"],
248 ["list_events", "repo:read"],
249 ["list_labels", "repo:read"],
250 ["list_milestones", "repo:read"],
251 ["get_milestone", "repo:read"],
252 ["create_label", "issues:write"],
253 ["update_label", "issues:write"],
254 ["delete_label", "issues:write"],
255 ["add_default_labels", "issues:write"],
256 ["create_milestone", "issues:write"],
257 ["update_milestone", "issues:write"],
258 ["delete_milestone", "issues:write"],
259 ["get_repo_settings", "repo:read"],
260 ["list_check_names", "repo:read"],
261 ["list_deleted_repos", "repo:read"],
262 ["list_security_alerts", "repo:read"],
263 ["get_codeowners_errors", "repo:read"],
264 ["create_repo", "repo:write"],
265 ["update_repo", "repo:write"],
266 ["update_repo_settings", "repo:write"],
267 ["list_repo_rulesets", "repo:read"],
268 ["get_repo_ruleset", "repo:read"],
269 ["get_branch_rules", "repo:read"],
270 ["list_rule_evaluations", "repo:read"],
271 ["create_repo_ruleset", "repo:admin"],
272 ["update_repo_ruleset", "repo:admin"],
273 ["delete_repo_ruleset", "repo:admin"],
274 ["rename_branch", "repo:write"],
275 ["rename_repo", "repo:admin"],
276 ["transfer_repo", "repo:admin"],
277 ["archive_repo", "repo:admin"],
278 ["unarchive_repo", "repo:admin"],
279 ["set_repo_visibility", "repo:admin"],
280 ["delete_repo", "repo:admin"],
281 ["restore_repo", "repo:admin"],
282 ["purge_repo", "repo:admin"],
283 ["dismiss_security_alert", "repo:admin"],
284 ["reopen_security_alert", "repo:admin"],
285 // The security suite.
286 ["list_secret_scanning_alerts", "security:read"],
287 ["get_secret_scanning_alert", "security:read"],
288 ["list_secret_scanning_locations", "security:read"],
289 ["list_bypass_requests", "security:read"],
290 ["list_custom_patterns", "security:read"],
291 ["list_code_scanning_alerts", "security:read"],
292 ["get_code_scanning_alert", "security:read"],
293 ["list_code_scanning_analyses", "security:read"],
294 ["get_sarif_upload", "security:read"],
295 ["list_vulnerability_alerts", "security:read"],
296 ["get_vulnerability_alert", "security:read"],
297 ["get_dependency_graph", "security:read"],
298 ["get_sbom", "security:read"],
299 ["compare_dependencies", "security:read"],
300 ["get_security_settings", "security:read"],
301 ["get_workspace_security_settings", "security:read"],
302 ["get_security_overview", "security:read"],
303 ["update_secret_scanning_alert", "security:write"],
304 ["bypass_push_protection", "security:write"],
305 ["check_secret_validity", "security:write"],
306 ["review_bypass_request", "security:write"],
307 ["create_custom_pattern", "security:write"],
308 ["update_custom_pattern", "security:write"],
309 ["delete_custom_pattern", "security:write"],
310 ["dry_run_custom_pattern", "security:write"],
311 ["update_code_scanning_alert", "security:write"],
312 ["upload_sarif", "security:write"],
313 ["update_vulnerability_alert", "security:write"],
314 ["fix_security_alert", "security:write"],
315 ["update_security_settings", "security:write"],
316 ["update_workspace_security_settings", "security:write"],
317 ["list_issues", "issues:read"],
318 ["get_issue", "issues:read"],
319 ["get_plan", "issues:read"],
320 ["create_issue", "issues:write"],
321 ["update_issue", "issues:write"],
322 ["list_issue_labels", "issues:read"],
323 ["add_issue_labels", "issues:write"],
324 ["set_issue_labels", "issues:write"],
325 ["remove_issue_labels", "issues:write"],
326 ["close_issue", "issues:write"],
327 ["reopen_issue", "issues:write"],
328 ["add_comment", "issues:write"],
329 ["import_issue", "issues:write"],
330 ["apply_plan", "issues:write"],
331 ["list_pull_requests", "pull_requests:read"],
332 ["get_pull_request", "pull_requests:read"],
333 ["get_pull_request_changes", "pull_requests:read"],
334 ["read_session", "pull_requests:read"],
335 ["get_merge_queue", "pull_requests:read"],
336 ["create_pull_request", "pull_requests:write"],
337 ["update_pull_request", "pull_requests:write"],
338 ["record_session", "pull_requests:write"],
339 ["mark_pull_request_ready", "pull_requests:write"],
340 ["close_pull_request", "pull_requests:write"],
341 ["review_pull_request", "pull_requests:write"],
342 ["merge_pull_request", "pull_requests:write"],
343 ["request_reviewers", "pull_requests:write"],
344 ["remove_requested_reviewers", "pull_requests:write"],
345 ["assign_issue", "agents:run"],
346 ["delegate", "agents:run"],
347 ["plan_work", "agents:run"],
348 ["message_agent", "agents:run"],
349 ["answer_message", "agents:run"],
350 ["take_messages", "agents:run"],
351 ["list_workflows", "workflows:read"],
352 ["list_workflow_runs", "workflows:read"],
353 ["get_workflow_run", "workflows:read"],
354 ["get_job_logs", "workflows:read"],
355 ["dispatch_workflow", "workflows:write"],
356 ["cancel_workflow_run", "workflows:write"],
357 ["rerun_workflow_run", "workflows:write"],
358 ["update_workflow", "workflows:write"],
359 ["recall", "memory:read"],
360 ["search_context", "memory:read"],
361 ["get_entity", "memory:read"],
362 ["get_context", "memory:read"],
363 ["remember", "memory:write"],
364 ["list_collaborators", "access:read"],
365 ["get_collaborator_permission", "access:read"],
366 ["list_repo_invitations", "access:read"],
367 ["list_outside_collaborators", "access:read"],
368 ["add_collaborator", "access:admin"],
369 ["update_collaborator", "access:admin"],
370 ["remove_collaborator", "access:admin"],
371 ["revoke_repo_invitation", "access:admin"],
372 ["set_base_permission", "access:admin"],
373 ["set_team_repo", "access:admin"],
374 ["remove_team_repo", "access:admin"],
375 ["list_webhooks", "webhooks:read"],
376 ["list_webhook_deliveries", "webhooks:read"],
377 ["create_webhook", "webhooks:admin"],
378 ["update_webhook", "webhooks:admin"],
379 ["delete_webhook", "webhooks:admin"],
380 ["ping_webhook", "webhooks:admin"],
381 ["redeliver_webhook", "webhooks:admin"],
382 ["list_actions_secrets", "secrets:read"],
383 ["list_actions_variables", "secrets:read"],
384 ["set_actions_secret", "secrets:admin"],
385 ["delete_actions_secret", "secrets:admin"],
386 ["set_actions_variable", "secrets:admin"],
387 ["delete_actions_variable", "secrets:admin"],
388 // Self-hosted runners.
389 ["list_runners", "runners:read"],
390 ["list_runner_groups", "runners:read"],
391 ["get_runner_settings", "runners:read"],
392 ["create_runner_registration_token", "runners:admin"],
393 ["remove_runner", "runners:admin"],
394 ["create_runner_group", "runners:admin"],
395 ["update_runner_group", "runners:admin"],
396 ["delete_runner_group", "runners:admin"],
397 ["update_runner_settings", "runners:admin"],
398] as const;
399
400/**
401 * The scopes a token or grant holds, as stored: null for full access, or
402 * the list. `legacy` marks a token made before scopes, which has full
403 * access until someone narrows it.
404 */
405export type TokenScopes = {
406 scopes: Scope[] | null;
407 legacy: boolean;
408};
409
410/**
411 * How settings group scopes into a checklist: each group's scopes, least
412 * first. Admin scopes are not here; they are under "Dangerous" on their
413 * own (see `DANGEROUS_SCOPES`). Every other scope is in exactly one group.
414 */
415export const SCOPE_GROUPS: { id: string; label: string; scopes: Scope[] }[] = [
416 { id: "code", label: "Repositories & code", scopes: ["repo:read", "repo:write", "code:read", "code:write"] },
417 { id: "security", label: "Security", scopes: ["security:read", "security:write"] },
418 { id: "packages", label: "Packages", scopes: ["packages:read", "packages:write"] },
419 { id: "work", label: "Issues & pull requests", scopes: ["issues:read", "issues:write", "pull_requests:read", "pull_requests:write"] },
420 { id: "agents", label: "Agents", scopes: ["agents:run"] },
421 { id: "workflows", label: "Workflows", scopes: ["workflows:read", "workflows:write"] },
422 { id: "memory", label: "Memory & search", scopes: ["memory:read", "memory:write"] },
423 { id: "account", label: "Account", scopes: ["account:read", "account:write"] },
424 { id: "notifications", label: "Notifications", scopes: ["notifications:read", "notifications:write"] },
425 { id: "workspace", label: "Workspace", scopes: ["workspace:read", "access:read", "webhooks:read", "secrets:read"] },
426 { id: "billing", label: "Billing", scopes: ["billing:read", "billing:write"] },
427 { id: "runners", label: "Runners", scopes: ["runners:read"] },
428];
429
430/** The admin and delete scopes, shown under "Dangerous" behind a warning. */
431export const DANGEROUS_SCOPES: Scope[] = SCOPES.map((row) => row.scope).filter(isDangerous);