Skip to content
1,105 linesCodeBlameRaw
1//! Scopes: what an access token may do on its owner's behalf.
2//!
3//! A personal access token, a workspace's token and an application signed
4//! in with OAuth each carry a set of scopes. A token reaches whatever the
5//! one it acts as can reach: a person's token, that person's workspaces and
6//! repositories; a workspace's token, that workspace. What a request may do
7//! is the intersection of two things: the role of whoever the token acts as
8//! (see [`crate::access`]) and the token's scopes.
9//!
10//! Each scope is a resource and a level, written `resource:level`, such as
11//! `issues:write`. A higher level of a resource includes the lower ones:
12//! `repo:admin` includes `repo:write`, which includes `repo:read`.
13//!
14//! This module is the one source of truth: the API (REST and MCP) and git
15//! enforce it, and identity stores it. `packages/contracts/src/scopes.ts`
16//! mirrors the table for the site; a test keeps the two the same.
17
18use serde::{Deserialize, Serialize};
19
20use crate::credentials::Decision;
21
22/// Something a token can be given access to.
23#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
24pub enum Resource {
25 Account,
26 Notifications,
27 Workspace,
28 Billing,
29 Repo,
30 Code,
31 Security,
32 Packages,
33 Issues,
34 PullRequests,
35 Agents,
36 Workflows,
37 Memory,
38 Access,
39 Webhooks,
40 Secrets,
41 Runners,
42 Models,
43}
44
45impl Resource {
46 pub const ALL: [Resource; 18] = [
47 Resource::Repo,
48 Resource::Code,
49 Resource::Security,
50 Resource::Packages,
51 Resource::Issues,
52 Resource::PullRequests,
53 Resource::Agents,
54 Resource::Workflows,
55 Resource::Memory,
56 Resource::Account,
57 Resource::Notifications,
58 Resource::Workspace,
59 Resource::Billing,
60 Resource::Access,
61 Resource::Webhooks,
62 Resource::Secrets,
63 Resource::Runners,
64 Resource::Models,
65 ];
66
67 pub fn as_str(self) -> &'static str {
68 match self {
69 Resource::Account => "account",
70 Resource::Notifications => "notifications",
71 Resource::Workspace => "workspace",
72 Resource::Billing => "billing",
73 Resource::Repo => "repo",
74 Resource::Code => "code",
75 Resource::Security => "security",
76 Resource::Packages => "packages",
77 Resource::Issues => "issues",
78 Resource::PullRequests => "pull_requests",
79 Resource::Agents => "agents",
80 Resource::Workflows => "workflows",
81 Resource::Memory => "memory",
82 Resource::Access => "access",
83 Resource::Webhooks => "webhooks",
84 Resource::Secrets => "secrets",
85 Resource::Runners => "runners",
86 Resource::Models => "models",
87 }
88 }
89
90 /// Its name, for people.
91 pub fn label(self) -> &'static str {
92 match self {
93 Resource::Account => "Your account",
94 Resource::Notifications => "Notifications",
95 Resource::Workspace => "Workspaces",
96 Resource::Billing => "Billing",
97 Resource::Repo => "Repositories",
98 Resource::Code => "Code",
99 Resource::Security => "Security",
100 Resource::Packages => "Packages",
101 Resource::Issues => "Issues",
102 Resource::PullRequests => "Pull requests",
103 Resource::Agents => "g1t agents",
104 Resource::Workflows => "Workflows",
105 Resource::Memory => "Memory and context",
106 Resource::Access => "Who has access",
107 Resource::Webhooks => "Webhooks",
108 Resource::Secrets => "Secrets and variables",
109 Resource::Runners => "Self-hosted runners",
110 Resource::Models => "AI Gateway",
111 }
112 }
113}
114
115/// How much of a resource.
116#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
117pub enum Level {
118 Read,
119 Write,
120 /// Starting g1t's agents, which spends the workspace's money.
121 Run,
122 /// Deleting what cannot be brought back, such as a package's versions.
123 Delete,
124 Admin,
125}
126
127impl Level {
128 pub fn as_str(self) -> &'static str {
129 match self {
130 Level::Read => "read",
131 Level::Write => "write",
132 Level::Run => "run",
133 Level::Delete => "delete",
134 Level::Admin => "admin",
135 }
136 }
137}
138
139/// One scope. Its text form, `resource:level`, is what tokens store, OAuth
140/// clients ask for, and errors name.
141#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
142pub enum Scope {
143 RepoRead,
144 RepoWrite,
145 RepoAdmin,
146 CodeRead,
147 CodeWrite,
148 SecurityRead,
149 SecurityWrite,
150 PackagesRead,
151 PackagesWrite,
152 PackagesDelete,
153 IssuesRead,
154 IssuesWrite,
155 PullRequestsRead,
156 PullRequestsWrite,
157 AgentsRun,
158 WorkflowsRead,
159 WorkflowsWrite,
160 MemoryRead,
161 MemoryWrite,
162 AccountRead,
163 AccountWrite,
164 NotificationsRead,
165 NotificationsWrite,
166 WorkspaceRead,
167 WorkspaceAdmin,
168 BillingRead,
169 BillingWrite,
170 AccessRead,
171 AccessAdmin,
172 WebhooksRead,
173 WebhooksAdmin,
174 SecretsRead,
175 SecretsAdmin,
176 RunnersRead,
177 RunnersAdmin,
178 ModelsRead,
179 ModelsWrite,
180}
181
182impl Scope {
183 /// Every scope, grouped by resource, least first.
184 pub const ALL: [Scope; 37] = [
185 Scope::RepoRead,
186 Scope::RepoWrite,
187 Scope::RepoAdmin,
188 Scope::CodeRead,
189 Scope::CodeWrite,
190 Scope::SecurityRead,
191 Scope::SecurityWrite,
192 Scope::PackagesRead,
193 Scope::PackagesWrite,
194 Scope::PackagesDelete,
195 Scope::IssuesRead,
196 Scope::IssuesWrite,
197 Scope::PullRequestsRead,
198 Scope::PullRequestsWrite,
199 Scope::AgentsRun,
200 Scope::WorkflowsRead,
201 Scope::WorkflowsWrite,
202 Scope::MemoryRead,
203 Scope::MemoryWrite,
204 Scope::AccountRead,
205 Scope::AccountWrite,
206 Scope::NotificationsRead,
207 Scope::NotificationsWrite,
208 Scope::WorkspaceRead,
209 Scope::WorkspaceAdmin,
210 Scope::BillingRead,
211 Scope::BillingWrite,
212 Scope::AccessRead,
213 Scope::AccessAdmin,
214 Scope::WebhooksRead,
215 Scope::WebhooksAdmin,
216 Scope::SecretsRead,
217 Scope::SecretsAdmin,
218 Scope::RunnersRead,
219 Scope::RunnersAdmin,
220 Scope::ModelsRead,
221 Scope::ModelsWrite,
222 ];
223
224 pub fn as_str(self) -> &'static str {
225 match self {
226 Scope::RepoRead => "repo:read",
227 Scope::RepoWrite => "repo:write",
228 Scope::RepoAdmin => "repo:admin",
229 Scope::CodeRead => "code:read",
230 Scope::CodeWrite => "code:write",
231 Scope::SecurityRead => "security:read",
232 Scope::SecurityWrite => "security:write",
233 Scope::PackagesRead => "packages:read",
234 Scope::PackagesWrite => "packages:write",
235 Scope::PackagesDelete => "packages:delete",
236 Scope::IssuesRead => "issues:read",
237 Scope::IssuesWrite => "issues:write",
238 Scope::PullRequestsRead => "pull_requests:read",
239 Scope::PullRequestsWrite => "pull_requests:write",
240 Scope::AgentsRun => "agents:run",
241 Scope::WorkflowsRead => "workflows:read",
242 Scope::WorkflowsWrite => "workflows:write",
243 Scope::MemoryRead => "memory:read",
244 Scope::MemoryWrite => "memory:write",
245 Scope::AccountRead => "account:read",
246 Scope::AccountWrite => "account:write",
247 Scope::NotificationsRead => "notifications:read",
248 Scope::NotificationsWrite => "notifications:write",
249 Scope::WorkspaceRead => "workspace:read",
250 Scope::WorkspaceAdmin => "workspace:admin",
251 Scope::BillingRead => "billing:read",
252 Scope::BillingWrite => "billing:write",
253 Scope::AccessRead => "access:read",
254 Scope::AccessAdmin => "access:admin",
255 Scope::WebhooksRead => "webhooks:read",
256 Scope::WebhooksAdmin => "webhooks:admin",
257 Scope::SecretsRead => "secrets:read",
258 Scope::SecretsAdmin => "secrets:admin",
259 Scope::RunnersRead => "runners:read",
260 Scope::RunnersAdmin => "runners:admin",
261 Scope::ModelsRead => "models:read",
262 Scope::ModelsWrite => "models:write",
263 }
264 }
265
266 pub fn parse(text: &str) -> Option<Scope> {
267 let text = text.trim().to_ascii_lowercase();
268 Scope::ALL.into_iter().find(|scope| scope.as_str() == text)
269 }
270
271 pub fn resource(self) -> Resource {
272 let name = self.as_str().split_once(':').map_or("", |(resource, _)| resource);
273 Resource::ALL
274 .into_iter()
275 .find(|resource| resource.as_str() == name)
276 .unwrap_or(Resource::Account)
277 }
278
279 pub fn level(self) -> Level {
280 match self.as_str().rsplit_once(':').map_or("", |(_, level)| level) {
281 "write" => Level::Write,
282 "run" => Level::Run,
283 "delete" => Level::Delete,
284 "admin" => Level::Admin,
285 _ => Level::Read,
286 }
287 }
288
289 /// Whether holding `self` gives `other`: the same resource, at the same
290 /// level or a lower one.
291 pub fn includes(self, other: Scope) -> bool {
292 self.resource() == other.resource() && self.level() >= other.level()
293 }
294
295 /// Changes that are hard or impossible to undo, or that decide who can
296 /// reach what. Shown behind a warning wherever scopes are chosen.
297 pub fn dangerous(self) -> bool {
298 matches!(self.level(), Level::Admin | Level::Delete)
299 }
300
301 /// What it lets a token do, in plain words.
302 pub fn describe(self) -> &'static str {
303 match self {
304 Scope::RepoRead => "See repositories, their settings, labels, timelines and security alerts, and search",
305 Scope::RepoWrite => "Create repositories, rename branches and change how pull requests merge",
306 Scope::RepoAdmin => "Rename, archive, transfer, delete or change who can see a repository, change its rulesets, and dismiss security alerts",
307 Scope::CodeRead => "Clone and fetch private repositories with git",
308 Scope::CodeWrite => "Push commits with git",
309 Scope::SecurityRead => "See secret scanning, code scanning and vulnerability alerts, custom patterns, the dependency graph and SBOM, and security settings",
310 Scope::SecurityWrite => "Dismiss and reopen alerts, bypass push protection, review bypass requests, manage custom patterns, upload SARIF and change security settings",
311 Scope::PackagesRead => "Pull container images and install private packages",
312 Scope::PackagesWrite => "Push container images and publish packages",
313 Scope::PackagesDelete => "Delete packages and their versions",
314 Scope::IssuesRead => "Read issues, comments and plans",
315 Scope::IssuesWrite => "Open, edit, close and comment on issues",
316 Scope::PullRequestsRead => "Read pull requests, their changes, sessions and merge queues",
317 Scope::PullRequestsWrite => "Open, review, close and merge pull requests",
318 Scope::AgentsRun => "Put g1t agents to work and message them, which uses the workspace's money",
319 Scope::WorkflowsRead => "Read workflows, runs and logs",
320 Scope::WorkflowsWrite => "Run, cancel, rerun and turn workflows on or off",
321 Scope::MemoryRead => "Recall memory and search the workspace's context",
322 Scope::MemoryWrite => "Save memory for the next agent",
323 Scope::AccountRead => "Read your email addresses, invites, invitations and pinned projects",
324 Scope::AccountWrite => "Change your email addresses, make invites, answer invitations and pin projects",
325 Scope::NotificationsRead => "See your inbox, its threads, and what you subscribe to and watch",
326 Scope::NotificationsWrite => "Mark notifications read, done, saved or snoozed, subscribe to threads and watch repositories",
327 Scope::WorkspaceRead => "Read workspace settings, invites, integrations, model routes, teams and rulesets",
328 Scope::WorkspaceAdmin => "Create and delete workspaces, invite members, connect integrations, create, change and delete teams, and change the workspace's rulesets",
329 Scope::BillingRead => "See a workspace's usage, budget, AI credit and invoices",
330 Scope::BillingWrite => "Change a workspace's budget and buy AI credit",
331 Scope::AccessRead => "See who has access to repositories",
332 Scope::AccessAdmin => "Give and take away access to repositories, a team's included",
333 Scope::WebhooksRead => "See webhooks and their deliveries",
334 Scope::WebhooksAdmin => "Create, change and delete webhooks",
335 Scope::SecretsRead => "List secrets (never their values) and read variables",
336 Scope::SecretsAdmin => "Set and delete secrets and variables",
337 Scope::RunnersRead => "See self-hosted runners, their groups and where agents run",
338 Scope::RunnersAdmin => "Register and remove self-hosted runners, change their groups and settings",
339 Scope::ModelsRead => "See the workspace's AI Gateway requests: their models, tokens, cost and status",
340 Scope::ModelsWrite => "Send model requests through the AI Gateway, which uses the workspace's AI credit",
341 }
342 }
343}
344
345impl Serialize for Scope {
346 fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
347 serializer.serialize_str(self.as_str())
348 }
349}
350
351impl<'de> Deserialize<'de> for Scope {
352 fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
353 let text = String::deserialize(deserializer)?;
354 Scope::parse(&text).ok_or_else(|| serde::de::Error::custom(format!("unknown scope {text}")))
355 }
356}
357
358/// Scopes as written in a token's row or an OAuth request: separated by
359/// spaces or commas. Unknown names are left out, so a client asking for a
360/// scope from a newer version gets the rest.
361pub fn parse_scopes(text: &str) -> Vec<Scope> {
362 let mut scopes: Vec<Scope> = text
363 .split(|c: char| c.is_whitespace() || c == ',')
364 .filter_map(Scope::parse)
365 .collect();
366 normalize(&mut scopes);
367 scopes
368}
369
370/// In table order, without repeats.
371pub fn normalize(scopes: &mut Vec<Scope>) {
372 let given = std::mem::take(scopes);
373 scopes.extend(Scope::ALL.into_iter().filter(|scope| given.contains(scope)));
374}
375
376/// Space-separated, as stored and as OAuth writes them.
377pub fn scopes_text(scopes: &[Scope]) -> String {
378 scopes.iter().map(|scope| scope.as_str()).collect::<Vec<_>>().join(" ")
379}
380
381/// What a token stores for full access, which is not a scope a client can
382/// ask for by name.
383pub const FULL_ACCESS: &str = "*";
384
385/// Starting points for choosing scopes.
386#[derive(Clone, Copy, Debug, PartialEq, Eq)]
387pub enum Preset {
388 ReadOnly,
389 Agent,
390 Ci,
391 Full,
392}
393
394impl Preset {
395 pub const ALL: [Preset; 4] = [Preset::ReadOnly, Preset::Agent, Preset::Ci, Preset::Full];
396
397 pub fn as_str(self) -> &'static str {
398 match self {
399 Preset::ReadOnly => "read_only",
400 Preset::Agent => "agent",
401 Preset::Ci => "ci",
402 Preset::Full => "full",
403 }
404 }
405
406 pub fn label(self) -> &'static str {
407 match self {
408 Preset::ReadOnly => "Read only",
409 Preset::Agent => "Agent",
410 Preset::Ci => "CI",
411 Preset::Full => "Full access",
412 }
413 }
414
415 /// Its scopes; `None` for full access.
416 pub fn scopes(self) -> Option<Vec<Scope>> {
417 let reads = || Scope::ALL.into_iter().filter(|scope| scope.level() == Level::Read);
418 match self {
419 Preset::ReadOnly => Some(reads().collect()),
420 Preset::Agent => {
421 // Not the machines work runs on: an agent has no business
422 // knowing a workspace's own runners.
423 let mut scopes: Vec<Scope> = reads().filter(|scope| scope.resource() != Resource::Runners).collect();
424 // And answering what needs the person it works for: marking
425 // it done, subscribing, watching.
426 scopes.extend([
427 Scope::CodeWrite,
428 Scope::IssuesWrite,
429 Scope::PullRequestsWrite,
430 Scope::AgentsRun,
431 Scope::MemoryWrite,
432 Scope::NotificationsWrite,
433 ]);
434 normalize(&mut scopes);
435 Some(scopes)
436 }
437 Preset::Ci => Some(vec![
438 Scope::RepoRead,
439 Scope::CodeRead,
440 Scope::CodeWrite,
441 Scope::PackagesRead,
442 Scope::PackagesWrite,
443 Scope::WorkflowsRead,
444 Scope::WorkflowsWrite,
445 ]),
446 Preset::Full => None,
447 }
448 }
449}
450
451/// What an OAuth client gets when it asks for nothing in particular: the
452/// agent preset. Never an admin scope.
453pub fn oauth_default() -> Vec<Scope> {
454 Preset::Agent.scopes().unwrap_or_default()
455}
456
457/// Set on a [`crate::User`] resolved from an access token: what the token
458/// may do. Absent on a signed-in session, which may do whatever its person
459/// can.
460#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
461pub struct TokenAccess {
462 /// The token's id, as audit entries and errors name it.
463 #[serde(default)]
464 pub token_id: String,
465 /// Its scopes, as `resource:level`. Absent: full access, everything the
466 /// person (or workspace) can do.
467 #[serde(default, skip_serializing_if = "Option::is_none")]
468 pub scopes: Option<Vec<String>>,
469 /// Made before tokens had scopes: full access until someone narrows it.
470 #[serde(default, skip_serializing_if = "std::ops::Not::not")]
471 pub legacy: bool,
472 /// The token's name, as its owner gave it, so a log can say which
473 /// token made a request. Absent where whoever resolved it did not say.
474 #[serde(default, skip_serializing_if = "Option::is_none")]
475 pub name: Option<String>,
476}
477
478impl TokenAccess {
479 /// Full access to everything: the access tokens made before scopes had.
480 pub fn full() -> Self {
481 TokenAccess::default()
482 }
483
484 pub fn is_full(&self) -> bool {
485 self.scopes.is_none()
486 }
487
488 /// The scopes it holds, or `None` for full access.
489 pub fn granted(&self) -> Option<Vec<Scope>> {
490 self.scopes
491 .as_ref()
492 .map(|scopes| scopes.iter().filter_map(|scope| Scope::parse(scope)).collect())
493 }
494
495 pub fn allows(&self, needed: Scope) -> bool {
496 match self.granted() {
497 None => true,
498 Some(granted) => granted.iter().any(|held| held.includes(needed)),
499 }
500 }
501}
502
503/// Every operation of the API and MCP server, with the scope it needs. An
504/// operation in [`NO_SCOPE`] needs none. The API checks that every one of
505/// its operations is in exactly one of the two.
506pub const OPERATIONS: &[(&str, Scope)] = &[
507 // Your account.
508 ("list_emails", Scope::AccountRead),
509 ("add_email", Scope::AccountWrite),
510 ("remove_email", Scope::AccountWrite),
511 ("update_email_settings", Scope::AccountWrite),
512 ("list_invites", Scope::AccountRead),
513 ("create_invite", Scope::AccountWrite),
514 ("revoke_invite", Scope::AccountWrite),
515 ("list_my_repo_invitations", Scope::AccountRead),
516 ("accept_repo_invitation", Scope::AccountWrite),
517 ("decline_repo_invitation", Scope::AccountWrite),
518 // Your pinned projects: a preference of your account.
519 ("list_pinned_projects", Scope::AccountRead),
520 ("pin_project", Scope::AccountWrite),
521 ("unpin_project", Scope::AccountWrite),
522 ("reorder_pinned_projects", Scope::AccountWrite),
523 // Your inbox: notifications, subscriptions and watching.
524 ("list_notifications", Scope::NotificationsRead),
525 ("get_notification_thread", Scope::NotificationsRead),
526 ("get_thread_subscription", Scope::NotificationsRead),
527 ("get_repo_subscription", Scope::NotificationsRead),
528 ("list_watched_repos", Scope::NotificationsRead),
529 ("mark_notifications_read", Scope::NotificationsWrite),
530 ("mark_thread_read", Scope::NotificationsWrite),
531 ("mark_thread_done", Scope::NotificationsWrite),
532 ("save_thread", Scope::NotificationsWrite),
533 ("snooze_thread", Scope::NotificationsWrite),
534 ("set_thread_subscription", Scope::NotificationsWrite),
535 ("delete_thread_subscription", Scope::NotificationsWrite),
536 ("set_repo_subscription", Scope::NotificationsWrite),
537 ("delete_repo_subscription", Scope::NotificationsWrite),
538 // Workspaces, their invites and integrations.
539 ("create_workspace", Scope::WorkspaceAdmin),
540 ("delete_workspace", Scope::WorkspaceAdmin),
541 ("get_workspace", Scope::WorkspaceRead),
542 ("update_workspace", Scope::WorkspaceAdmin),
543 ("list_workspace_invites", Scope::WorkspaceRead),
544 ("invite_member", Scope::WorkspaceAdmin),
545 ("revoke_workspace_invite", Scope::WorkspaceAdmin),
546 ("list_integrations", Scope::WorkspaceRead),
547 ("connect_integration", Scope::WorkspaceAdmin),
548 ("update_integration", Scope::WorkspaceAdmin),
549 ("disconnect_integration", Scope::WorkspaceAdmin),
550 ("test_integration", Scope::WorkspaceAdmin),
551 ("get_model_routes", Scope::WorkspaceRead),
552 ("set_model_routes", Scope::WorkspaceAdmin),
553 // Teams: reading them, and managing them. A team's role on a
554 // repository is who has access.
555 ("list_teams", Scope::WorkspaceRead),
556 ("get_team", Scope::WorkspaceRead),
557 ("list_team_members", Scope::WorkspaceRead),
558 ("list_child_teams", Scope::WorkspaceRead),
559 ("list_team_repos", Scope::WorkspaceRead),
560 ("list_user_teams", Scope::WorkspaceRead),
561 ("create_team", Scope::WorkspaceAdmin),
562 ("list_workspace_rulesets", Scope::WorkspaceRead),
563 ("get_workspace_ruleset", Scope::WorkspaceRead),
564 ("list_workspace_rule_evaluations", Scope::WorkspaceRead),
565 ("create_workspace_ruleset", Scope::WorkspaceAdmin),
566 ("update_workspace_ruleset", Scope::WorkspaceAdmin),
567 ("delete_workspace_ruleset", Scope::WorkspaceAdmin),
568 ("update_team", Scope::WorkspaceAdmin),
569 ("delete_team", Scope::WorkspaceAdmin),
570 ("set_team_member", Scope::WorkspaceAdmin),
571 ("remove_team_member", Scope::WorkspaceAdmin),
572 ("set_team_review_assignment", Scope::WorkspaceAdmin),
573 // A workspace's billing: usage, budget, AI credit and invoices.
574 ("get_usage", Scope::BillingRead),
575 ("get_budget", Scope::BillingRead),
576 ("get_ai_credit", Scope::BillingRead),
577 ("list_invoices", Scope::BillingRead),
578 ("get_billing_details", Scope::BillingRead),
579 ("set_budget", Scope::BillingWrite),
580 ("buy_ai_credit", Scope::BillingWrite),
581 // Repositories.
582 ("list_repos", Scope::RepoRead),
583 ("get_repo", Scope::RepoRead),
584 ("search", Scope::RepoRead),
585 ("list_events", Scope::RepoRead),
586 ("list_labels", Scope::RepoRead),
587 ("list_milestones", Scope::RepoRead),
588 ("get_milestone", Scope::RepoRead),
589 ("create_label", Scope::IssuesWrite),
590 ("update_label", Scope::IssuesWrite),
591 ("delete_label", Scope::IssuesWrite),
592 ("add_default_labels", Scope::IssuesWrite),
593 ("create_milestone", Scope::IssuesWrite),
594 ("update_milestone", Scope::IssuesWrite),
595 ("delete_milestone", Scope::IssuesWrite),
596 ("get_repo_settings", Scope::RepoRead),
597 ("list_check_names", Scope::RepoRead),
598 ("list_deleted_repos", Scope::RepoRead),
599 ("list_security_alerts", Scope::RepoRead),
600 ("get_codeowners_errors", Scope::RepoRead),
601 ("create_repo", Scope::RepoWrite),
602 ("update_repo", Scope::RepoWrite),
603 ("update_repo_settings", Scope::RepoWrite),
604 // Rulesets: reading them is reading the repository; changing them
605 // changes what everyone, agents included, may do, so it is admin.
606 ("list_repo_rulesets", Scope::RepoRead),
607 ("get_repo_ruleset", Scope::RepoRead),
608 ("get_branch_rules", Scope::RepoRead),
609 ("list_rule_evaluations", Scope::RepoRead),
610 ("create_repo_ruleset", Scope::RepoAdmin),
611 ("update_repo_ruleset", Scope::RepoAdmin),
612 ("delete_repo_ruleset", Scope::RepoAdmin),
613 ("rename_branch", Scope::RepoWrite),
614 ("rename_repo", Scope::RepoAdmin),
615 ("transfer_repo", Scope::RepoAdmin),
616 ("archive_repo", Scope::RepoAdmin),
617 ("unarchive_repo", Scope::RepoAdmin),
618 ("set_repo_visibility", Scope::RepoAdmin),
619 ("delete_repo", Scope::RepoAdmin),
620 ("restore_repo", Scope::RepoAdmin),
621 ("purge_repo", Scope::RepoAdmin),
622 // A dismissed secret is let through push protection.
623 ("dismiss_security_alert", Scope::RepoAdmin),
624 ("reopen_security_alert", Scope::RepoAdmin),
625 // The security suite: alerts, push protection, patterns, code
626 // scanning, the supply chain and settings.
627 ("list_secret_scanning_alerts", Scope::SecurityRead),
628 ("get_secret_scanning_alert", Scope::SecurityRead),
629 ("list_secret_scanning_locations", Scope::SecurityRead),
630 ("list_bypass_requests", Scope::SecurityRead),
631 ("list_custom_patterns", Scope::SecurityRead),
632 ("list_code_scanning_alerts", Scope::SecurityRead),
633 ("get_code_scanning_alert", Scope::SecurityRead),
634 ("list_code_scanning_analyses", Scope::SecurityRead),
635 ("get_sarif_upload", Scope::SecurityRead),
636 ("list_vulnerability_alerts", Scope::SecurityRead),
637 ("get_vulnerability_alert", Scope::SecurityRead),
638 ("get_dependency_graph", Scope::SecurityRead),
639 ("get_sbom", Scope::SecurityRead),
640 ("compare_dependencies", Scope::SecurityRead),
641 ("get_security_settings", Scope::SecurityRead),
642 ("get_workspace_security_settings", Scope::SecurityRead),
643 ("get_security_overview", Scope::SecurityRead),
644 ("update_secret_scanning_alert", Scope::SecurityWrite),
645 ("bypass_push_protection", Scope::SecurityWrite),
646 ("check_secret_validity", Scope::SecurityWrite),
647 ("review_bypass_request", Scope::SecurityWrite),
648 ("create_custom_pattern", Scope::SecurityWrite),
649 ("update_custom_pattern", Scope::SecurityWrite),
650 ("delete_custom_pattern", Scope::SecurityWrite),
651 ("dry_run_custom_pattern", Scope::SecurityWrite),
652 ("update_code_scanning_alert", Scope::SecurityWrite),
653 ("upload_sarif", Scope::SecurityWrite),
654 ("update_vulnerability_alert", Scope::SecurityWrite),
655 ("fix_security_alert", Scope::SecurityWrite),
656 ("update_security_settings", Scope::SecurityWrite),
657 ("update_workspace_security_settings", Scope::SecurityWrite),
658 // Issues and plans.
659 ("list_issues", Scope::IssuesRead),
660 ("get_issue", Scope::IssuesRead),
661 ("get_plan", Scope::IssuesRead),
662 ("create_issue", Scope::IssuesWrite),
663 ("update_issue", Scope::IssuesWrite),
664 ("list_issue_labels", Scope::IssuesRead),
665 ("add_issue_labels", Scope::IssuesWrite),
666 ("set_issue_labels", Scope::IssuesWrite),
667 ("remove_issue_labels", Scope::IssuesWrite),
668 ("close_issue", Scope::IssuesWrite),
669 ("reopen_issue", Scope::IssuesWrite),
670 ("add_comment", Scope::IssuesWrite),
671 ("import_issue", Scope::IssuesWrite),
672 ("apply_plan", Scope::IssuesWrite),
673 // Pull requests.
674 ("list_pull_requests", Scope::PullRequestsRead),
675 ("get_pull_request", Scope::PullRequestsRead),
676 ("get_pull_request_changes", Scope::PullRequestsRead),
677 ("read_session", Scope::PullRequestsRead),
678 ("get_merge_queue", Scope::PullRequestsRead),
679 ("create_pull_request", Scope::PullRequestsWrite),
680 ("update_pull_request", Scope::PullRequestsWrite),
681 ("record_session", Scope::PullRequestsWrite),
682 ("mark_pull_request_ready", Scope::PullRequestsWrite),
683 ("close_pull_request", Scope::PullRequestsWrite),
684 ("review_pull_request", Scope::PullRequestsWrite),
685 ("merge_pull_request", Scope::PullRequestsWrite),
686 ("request_reviewers", Scope::PullRequestsWrite),
687 ("remove_requested_reviewers", Scope::PullRequestsWrite),
688 // g1t's agents.
689 ("assign_issue", Scope::AgentsRun),
690 ("delegate", Scope::AgentsRun),
691 ("plan_work", Scope::AgentsRun),
692 ("message_agent", Scope::AgentsRun),
693 ("answer_message", Scope::AgentsRun),
694 ("take_messages", Scope::AgentsRun),
695 // Workflows.
696 ("list_workflows", Scope::WorkflowsRead),
697 ("list_workflow_runs", Scope::WorkflowsRead),
698 ("get_workflow_run", Scope::WorkflowsRead),
699 ("get_job_logs", Scope::WorkflowsRead),
700 ("dispatch_workflow", Scope::WorkflowsWrite),
701 ("cancel_workflow_run", Scope::WorkflowsWrite),
702 ("rerun_workflow_run", Scope::WorkflowsWrite),
703 ("update_workflow", Scope::WorkflowsWrite),
704 // Memory and the context hub.
705 ("recall", Scope::MemoryRead),
706 ("search_context", Scope::MemoryRead),
707 ("get_entity", Scope::MemoryRead),
708 ("get_context", Scope::MemoryRead),
709 ("remember", Scope::MemoryWrite),
710 // Who has access.
711 ("list_collaborators", Scope::AccessRead),
712 ("get_collaborator_permission", Scope::AccessRead),
713 ("list_repo_invitations", Scope::AccessRead),
714 ("list_outside_collaborators", Scope::AccessRead),
715 ("add_collaborator", Scope::AccessAdmin),
716 ("update_collaborator", Scope::AccessAdmin),
717 ("remove_collaborator", Scope::AccessAdmin),
718 ("revoke_repo_invitation", Scope::AccessAdmin),
719 ("set_base_permission", Scope::AccessAdmin),
720 ("set_team_repo", Scope::AccessAdmin),
721 ("remove_team_repo", Scope::AccessAdmin),
722 // Webhooks.
723 ("list_webhooks", Scope::WebhooksRead),
724 ("list_webhook_deliveries", Scope::WebhooksRead),
725 ("create_webhook", Scope::WebhooksAdmin),
726 ("update_webhook", Scope::WebhooksAdmin),
727 ("delete_webhook", Scope::WebhooksAdmin),
728 ("ping_webhook", Scope::WebhooksAdmin),
729 ("redeliver_webhook", Scope::WebhooksAdmin),
730 // Secrets and variables.
731 ("list_actions_secrets", Scope::SecretsRead),
732 ("list_actions_variables", Scope::SecretsRead),
733 ("set_actions_secret", Scope::SecretsAdmin),
734 ("delete_actions_secret", Scope::SecretsAdmin),
735 ("set_actions_variable", Scope::SecretsAdmin),
736 ("delete_actions_variable", Scope::SecretsAdmin),
737 // Self-hosted runners.
738 ("list_runners", Scope::RunnersRead),
739 ("list_runner_groups", Scope::RunnersRead),
740 ("get_runner_settings", Scope::RunnersRead),
741 ("create_runner_registration_token", Scope::RunnersAdmin),
742 ("remove_runner", Scope::RunnersAdmin),
743 ("create_runner_group", Scope::RunnersAdmin),
744 ("update_runner_group", Scope::RunnersAdmin),
745 ("delete_runner_group", Scope::RunnersAdmin),
746 ("update_runner_settings", Scope::RunnersAdmin),
747 // The AI Gateway. Sending a request to a model needs `models:write`,
748 // checked by the model proxy at models.g1t.sh, not here.
749 ("list_gateway_requests", Scope::ModelsRead),
750];
751
752/// Operations any token may use: saying who it is.
753pub const NO_SCOPE: &[&str] = &["whoami"];
754
755/// The scope `operation` needs. `None` for one in [`NO_SCOPE`]; an
756/// operation in neither list needs full access.
757pub fn scope_for(operation: &str) -> Option<Scope> {
758 OPERATIONS
759 .iter()
760 .find(|(name, _)| *name == operation)
761 .map(|(_, scope)| *scope)
762}
763
764/// What a token needs for `operation` with this input beyond its own
765/// scope: starting agents from an operation that can, and making a
766/// repository public or private.
767pub fn extra_scopes(operation: &str, input: &serde_json::Value) -> Vec<Scope> {
768 let mut extra = Vec::new();
769 let assigns = input["assign"].as_bool() == Some(true)
770 || input["agent"].as_bool() == Some(true)
771 || input["assign_agent"].as_bool() == Some(true);
772 if assigns && matches!(operation, "apply_plan" | "import_issue" | "create_issue") {
773 extra.push(Scope::AgentsRun);
774 }
775 // Fixing an alert opens an issue and puts g1t on it.
776 if operation == "fix_security_alert" {
777 extra.extend([Scope::IssuesWrite, Scope::AgentsRun]);
778 }
779 // Opening the issue an agent is put on.
780 if operation == "delegate" {
781 extra.push(Scope::IssuesWrite);
782 }
783 // A workspace's base permission is who has access.
784 if operation == "update_workspace" && input.get("base_permission").is_some_and(|v| !v.is_null()) {
785 extra.push(Scope::AccessAdmin);
786 }
787 if operation == "update_repo" && (input.get("private").is_some_and(|v| !v.is_null()) || input.get("default_branch").is_some_and(|v| !v.is_null())) {
788 extra.push(Scope::RepoAdmin);
789 }
790 extra
791}
792
793/// The scopes a call needs, its own first.
794pub fn needed(operation: &str, input: &serde_json::Value) -> Vec<Scope> {
795 scope_for(operation)
796 .into_iter()
797 .chain(extra_scopes(operation, input))
798 .collect()
799}
800
801/// Whether `access` may use `operation` with `input`. The person's (or
802/// workspace's) role is checked after this, by the service that owns what
803/// was asked about.
804pub fn decide(access: &TokenAccess, operation: &str, input: &serde_json::Value) -> Decision {
805 let rule = if access.legacy { "token:legacy" } else { "token:scope" };
806 if access.scopes.is_some() {
807 let known = NO_SCOPE.contains(&operation) || scope_for(operation).is_some();
808 if !known {
809 return Decision::deny("token:scope", format!("This access token cannot use {operation}: it needs full access."));
810 }
811 if let Some(missing) = needed(operation, input).into_iter().find(|scope| !access.allows(*scope)) {
812 return Decision::deny(
813 "token:scope",
814 format!("This access token needs the {} scope to use {operation}.", missing.as_str()),
815 );
816 }
817 }
818 Decision::allow(rule)
819}
820
821/// Whether a token may clone or fetch (`write` false), or push to (`write`
822/// true), a repository with git. `public` is whether anyone may read it,
823/// which needs no scope.
824pub fn decide_git(access: &TokenAccess, write: bool, public: bool) -> Decision {
825 let needed = if write { Scope::CodeWrite } else { Scope::CodeRead };
826 if !access.allows(needed) && (write || !public) {
827 return Decision::deny(
828 "token:scope",
829 format!("This access token needs the {} scope to {} with git.", needed.as_str(), if write { "push" } else { "clone or fetch a private repository" }),
830 );
831 }
832 Decision::allow(if access.legacy { "token:legacy" } else { "token:scope" })
833}
834
835/// Whether a token may pull (`Level::Read`), push or publish
836/// (`Level::Write`), or delete (`Level::Delete`) packages. `public` is
837/// whether anyone may pull the package, which needs no scope.
838pub fn decide_packages(access: &TokenAccess, level: Level, public: bool) -> Decision {
839 let (needed, doing) = match level {
840 Level::Read => (Scope::PackagesRead, "pull a private package"),
841 Level::Delete | Level::Admin => (Scope::PackagesDelete, "delete packages"),
842 Level::Write | Level::Run => (Scope::PackagesWrite, "push or publish packages"),
843 };
844 if !access.allows(needed) && !(level == Level::Read && public) {
845 return Decision::deny(
846 "token:scope",
847 format!("This access token needs the {} scope to {doing}.", needed.as_str()),
848 );
849 }
850 Decision::allow(if access.legacy { "token:legacy" } else { "token:scope" })
851}
852
853#[cfg(test)]
854mod tests {
855 use super::*;
856 use serde_json::json;
857
858 fn token(scopes: &[Scope]) -> TokenAccess {
859 TokenAccess {
860 token_id: "tok_1".to_owned(),
861 scopes: Some(scopes.iter().map(|scope| scope.as_str().to_owned()).collect()),
862 legacy: false,
863 name: None,
864 }
865 }
866
867 #[test]
868 fn every_scope_reads_back_and_belongs_to_a_resource() {
869 for scope in Scope::ALL {
870 assert_eq!(Scope::parse(scope.as_str()), Some(scope));
871 assert!(scope.as_str().starts_with(scope.resource().as_str()));
872 assert!(scope.includes(scope));
873 }
874 assert_eq!(Scope::parse(" Issues:Write "), Some(Scope::IssuesWrite));
875 assert_eq!(Scope::parse("issues"), None);
876 }
877
878 #[test]
879 fn a_higher_level_includes_the_lower_ones_of_its_resource_only() {
880 assert!(Scope::RepoAdmin.includes(Scope::RepoRead));
881 assert!(Scope::RepoAdmin.includes(Scope::RepoWrite));
882 assert!(Scope::IssuesWrite.includes(Scope::IssuesRead));
883 assert!(!Scope::IssuesRead.includes(Scope::IssuesWrite));
884 assert!(!Scope::RepoAdmin.includes(Scope::CodeWrite));
885 assert!(!Scope::PullRequestsWrite.includes(Scope::IssuesWrite));
886 }
887
888 #[test]
889 fn operations_are_listed_once_and_never_also_free() {
890 let mut seen = std::collections::HashSet::new();
891 for (name, _) in OPERATIONS {
892 assert!(seen.insert(*name), "{name} twice");
893 assert!(!NO_SCOPE.contains(name), "{name}");
894 }
895 }
896
897 #[test]
898 fn scopes_are_parsed_from_oauth_text_leaving_out_unknown_ones() {
899 assert_eq!(
900 parse_scopes("issues:write repo:read,bogus:thing issues:write"),
901 vec![Scope::RepoRead, Scope::IssuesWrite]
902 );
903 assert_eq!(scopes_text(&[Scope::RepoRead, Scope::IssuesWrite]), "repo:read issues:write");
904 }
905
906 #[test]
907 fn the_oauth_default_is_the_agent_preset_and_never_admin() {
908 let scopes = oauth_default();
909 assert!(scopes.contains(&Scope::IssuesWrite));
910 assert!(scopes.contains(&Scope::PullRequestsWrite));
911 assert!(scopes.contains(&Scope::AgentsRun));
912 assert!(scopes.iter().all(|scope| !scope.dangerous()), "{scopes:?}");
913 for read in Scope::ALL.into_iter().filter(|scope| scope.level() == Level::Read) {
914 // Every read but the machines work runs on.
915 assert_eq!(scopes.contains(&read), read != Scope::RunnersRead, "{read:?}");
916 }
917 assert!(Preset::ReadOnly.scopes().unwrap().iter().all(|scope| scope.level() == Level::Read));
918 assert_eq!(Preset::Full.scopes(), None);
919 }
920
921 #[test]
922 fn billing_is_read_by_presets_and_changed_by_none_but_full_access() {
923 assert!(Preset::ReadOnly.scopes().unwrap().contains(&Scope::BillingRead));
924 for preset in [Preset::ReadOnly, Preset::Agent, Preset::Ci] {
925 assert!(!preset.scopes().unwrap().contains(&Scope::BillingWrite), "{}", preset.as_str());
926 }
927 assert_eq!(scope_for("set_budget"), Some(Scope::BillingWrite));
928 assert_eq!(scope_for("buy_ai_credit"), Some(Scope::BillingWrite));
929 assert_eq!(scope_for("get_usage"), Some(Scope::BillingRead));
930 let reader = token(&[Scope::BillingRead]);
931 assert!(decide(&reader, "list_invoices", &json!({})).allowed);
932 assert!(decide(&reader, "set_budget", &json!({})).reason.unwrap().contains("billing:write"));
933 }
934
935 #[test]
936 fn the_ai_gateway_spends_only_with_models_write_which_no_preset_gives() {
937 // Reading the log is a read like any other.
938 assert_eq!(scope_for("list_gateway_requests"), Some(Scope::ModelsRead));
939 assert!(Preset::ReadOnly.scopes().unwrap().contains(&Scope::ModelsRead));
940 // Sending requests spends the workspace's AI credit: chosen on purpose.
941 for preset in [Preset::ReadOnly, Preset::Agent, Preset::Ci] {
942 assert!(!preset.scopes().unwrap().contains(&Scope::ModelsWrite), "{}", preset.as_str());
943 }
944 assert!(Scope::ModelsWrite.includes(Scope::ModelsRead));
945 assert!(!Scope::ModelsWrite.dangerous());
946 assert!(token(&[Scope::ModelsWrite]).allows(Scope::ModelsWrite));
947 assert!(!token(&[Scope::BillingWrite]).allows(Scope::ModelsWrite));
948 assert!(TokenAccess::full().allows(Scope::ModelsWrite));
949 }
950
951 #[test]
952 fn a_legacy_token_can_do_everything() {
953 let legacy = TokenAccess { legacy: true, ..TokenAccess::full() };
954 for (operation, _) in OPERATIONS {
955 assert!(decide(&legacy, operation, &json!({})).allowed, "{operation}");
956 }
957 assert_eq!(decide(&legacy, "delete_repo", &json!({})).rule, "token:legacy");
958 }
959
960 #[test]
961 fn a_missing_scope_is_named() {
962 let read = token(&[Scope::IssuesRead]);
963 assert!(decide(&read, "get_issue", &json!({})).allowed);
964 assert!(decide(&read, "whoami", &json!({})).allowed);
965 let refused = decide(&read, "create_issue", &json!({}));
966 assert!(!refused.allowed);
967 assert_eq!(refused.reason.as_deref(), Some("This access token needs the issues:write scope to use create_issue."));
968 // An operation the table does not know needs full access.
969 assert!(!decide(&read, "something_new", &json!({})).allowed);
970 }
971
972 #[test]
973 fn starting_agents_from_another_operation_needs_agents_run() {
974 let writer = token(&[Scope::IssuesWrite]);
975 assert!(decide(&writer, "apply_plan", &json!({})).allowed);
976 let refused = decide(&writer, "apply_plan", &json!({ "assign": true }));
977 assert!(refused.reason.unwrap().contains("agents:run"));
978 let maintainer = token(&[Scope::RepoWrite]);
979 assert!(decide(&maintainer, "update_repo", &json!({ "description": "x" })).allowed);
980 assert!(!decide(&maintainer, "update_repo", &json!({ "private": true })).allowed);
981 }
982
983 #[test]
984 fn a_workspaces_base_permission_needs_access_admin_too() {
985 let admin = token(&[Scope::WorkspaceAdmin]);
986 assert!(decide(&admin, "update_workspace", &json!({ "name": "Acme" })).allowed);
987 let refused = decide(&admin, "update_workspace", &json!({ "name": "Acme", "base_permission": "read" }));
988 assert!(refused.reason.unwrap().contains("access:admin"));
989 let both = token(&[Scope::WorkspaceAdmin, Scope::AccessAdmin]);
990 assert!(decide(&both, "update_workspace", &json!({ "base_permission": "read" })).allowed);
991 assert!(!decide(&token(&[Scope::WorkspaceRead]), "update_workspace", &json!({ "name": "Acme" })).allowed);
992 }
993
994 #[test]
995 fn delegating_needs_both_agents_and_issues() {
996 let agents = token(&[Scope::AgentsRun]);
997 assert!(decide(&agents, "delegate", &json!({})).reason.unwrap().contains("issues:write"));
998 let both = token(&[Scope::AgentsRun, Scope::IssuesWrite]);
999 assert!(decide(&both, "delegate", &json!({})).allowed);
1000 }
1001
1002 #[test]
1003 fn git_push_needs_code_write_and_private_reads_need_code_read() {
1004 let reader = token(&[Scope::CodeRead]);
1005 assert!(decide_git(&reader, false, false).allowed);
1006 let refused = decide_git(&reader, true, false);
1007 assert!(!refused.allowed);
1008 assert!(refused.reason.unwrap().contains("code:write"));
1009 let issues = token(&[Scope::IssuesWrite]);
1010 assert!(!decide_git(&issues, false, false).allowed);
1011 assert!(decide_git(&issues, false, true).allowed, "public code needs no scope");
1012 assert!(!decide_git(&issues, true, true).allowed, "pushing to public code still needs code:write");
1013 let writer = token(&[Scope::CodeWrite]);
1014 assert!(decide_git(&writer, true, false).allowed);
1015 assert!(decide_git(&writer, false, false).allowed, "code:write includes code:read");
1016 assert!(decide_git(&TokenAccess::full(), true, false).allowed);
1017 }
1018
1019 #[test]
1020 fn packages_need_their_own_scopes_and_public_pulls_none() {
1021 let reader = token(&[Scope::PackagesRead]);
1022 assert!(decide_packages(&reader, Level::Read, false).allowed);
1023 assert!(!decide_packages(&reader, Level::Write, false).allowed);
1024 let code = token(&[Scope::CodeWrite]);
1025 assert!(!decide_packages(&code, Level::Read, false).allowed, "code scopes are not package scopes");
1026 assert!(decide_packages(&code, Level::Read, true).allowed, "public packages pull with any token");
1027 let writer = token(&[Scope::PackagesWrite]);
1028 assert!(decide_packages(&writer, Level::Write, false).allowed);
1029 assert!(decide_packages(&writer, Level::Read, false).allowed, "packages:write includes packages:read");
1030 let refused = decide_packages(&writer, Level::Delete, false);
1031 assert!(refused.reason.unwrap().contains("packages:delete"));
1032 assert!(decide_packages(&token(&[Scope::PackagesDelete]), Level::Write, false).allowed);
1033 assert!(Scope::PackagesDelete.dangerous());
1034 // Tokens made before these scopes, and full-access ones, keep working.
1035 let legacy = TokenAccess { legacy: true, ..TokenAccess::full() };
1036 assert!(decide_packages(&legacy, Level::Delete, false).allowed);
1037 assert!(decide_packages(&TokenAccess::full(), Level::Write, false).allowed);
1038 }
1039
1040 #[test]
1041 fn token_access_travels_as_json() {
1042 let access = token(&[Scope::IssuesRead]);
1043 let wire = serde_json::to_value(&access).unwrap();
1044 assert_eq!(wire["scopes"], json!(["issues:read"]));
1045 assert!(wire.get("resources").is_none());
1046 let back: TokenAccess = serde_json::from_value(wire).unwrap();
1047 assert_eq!(back, access);
1048 let full: TokenAccess = serde_json::from_value(json!({})).unwrap();
1049 assert!(full.is_full());
1050 // A reach written by an older version is ignored: a token reaches
1051 // whatever its owner can.
1052 let older: TokenAccess = serde_json::from_value(json!({
1053 "token_id": "tok_1",
1054 "scopes": ["issues:read"],
1055 "resources": { "kind": "repositories", "repositories": ["acme/rocket"] },
1056 }))
1057 .unwrap();
1058 assert_eq!(older, access);
1059 }
1060
1061 /// The site's copy of the table, `packages/contracts/src/scopes.ts`,
1062 /// lists the same scopes in the same order, the same operations with
1063 /// the same scopes, and the same presets.
1064 #[test]
1065 fn the_typescript_mirror_has_the_same_table() {
1066 let ts = include_str!("../../../packages/contracts/src/scopes.ts");
1067 let section = |start: &str| {
1068 ts.split_once(start)
1069 .and_then(|(_, rest)| rest.split_once("] as const"))
1070 .map(|(table, _)| table)
1071 .unwrap_or_else(|| panic!("{start} in scopes.ts"))
1072 };
1073 let scopes: Vec<&str> = section("export const SCOPES = [")
1074 .lines()
1075 .filter_map(|line| line.split_once("scope: \"").and_then(|(_, rest)| rest.split_once('"')).map(|(scope, _)| scope))
1076 .collect();
1077 let expected: Vec<&str> = Scope::ALL.iter().map(|scope| scope.as_str()).collect();
1078 assert_eq!(scopes, expected);
1079 let operations: Vec<(String, String)> = section("export const OPERATION_SCOPES = [")
1080 .lines()
1081 .filter_map(|line| {
1082 let mut quoted = line.split('"').skip(1).step_by(2);
1083 Some((quoted.next()?.to_owned(), quoted.next()?.to_owned()))
1084 })
1085 .collect();
1086 let expected: Vec<(String, String)> = OPERATIONS
1087 .iter()
1088 .map(|(name, scope)| ((*name).to_owned(), scope.as_str().to_owned()))
1089 .collect();
1090 assert_eq!(operations, expected);
1091 for preset in Preset::ALL {
1092 let list = section(&format!("{}: [", preset.as_str()));
1093 let mirrored: Vec<&str> = list
1094 .split(',')
1095 .map(|item| item.trim().trim_matches('"'))
1096 .filter(|item| !item.is_empty())
1097 .collect();
1098 let expected: Vec<&str> = preset
1099 .scopes()
1100 .map(|scopes| scopes.iter().map(|scope| scope.as_str()).collect())
1101 .unwrap_or_else(|| vec!["*"]);
1102 assert_eq!(mirrored, expected, "{}", preset.as_str());
1103 }
1104 }
1105}