Skip to content
1,079 linesCodeBlameRaw
1//! REST: each route maps an HTTP request onto one operation.
2
3use serde_json::{Map, Value};
4
5use crate::operations::Op;
6use crate::security::SecurityOp;
7
8pub struct Route {
9 pub method: &'static str,
10 /// Segments starting with `:` are parameters.
11 pub path: &'static str,
12 pub op: Op,
13 /// Query parameters the route reads, as `(name in the URL, input name)`.
14 pub query: &'static [(&'static str, &'static str)],
15}
16
17const fn route(
18 method: &'static str,
19 path: &'static str,
20 op: Op,
21 query: &'static [(&'static str, &'static str)],
22) -> Route {
23 Route {
24 method,
25 path,
26 op,
27 query,
28 }
29}
30
31pub const ROUTES: &[Route] = &[
32 route("GET", "/user", Op::Whoami, &[]),
33 route("POST", "/workspaces", Op::CreateWorkspace, &[]),
34 route("GET", "/workspaces/:workspace", Op::GetWorkspace, &[]),
35 route("DELETE", "/workspaces/:workspace", Op::DeleteWorkspace, &[]),
36 route("GET", "/user/emails", Op::ListEmails, &[]),
37 route("POST", "/user/emails", Op::AddEmail, &[]),
38 route("DELETE", "/user/emails/:email", Op::RemoveEmail, &[]),
39 route("PATCH", "/user/email-settings", Op::UpdateEmailSettings, &[]),
40 route("GET", "/user/invites", Op::ListInvites, &[]),
41 route("POST", "/user/invites", Op::CreateInvite, &[]),
42 route("DELETE", "/user/invites/:id", Op::RevokeInvite, &[]),
43 route("GET", "/workspaces/:workspace/invitations", Op::ListWorkspaceInvites, &[]),
44 route("POST", "/workspaces/:workspace/invitations", Op::InviteMember, &[]),
45 route("DELETE", "/workspaces/:workspace/invitations/:id", Op::RevokeWorkspaceInvite, &[]),
46 // Who has access. GitHub's addresses, but for adding someone, which
47 // takes an email address as well as a username.
48 route("GET", "/repos/:owner/:name/collaborators", Op::ListCollaborators, &[]),
49 route("POST", "/repos/:owner/:name/collaborators", Op::AddCollaborator, &[]),
50 route("PATCH", "/repos/:owner/:name/collaborators/:username", Op::UpdateCollaborator, &[]),
51 route("DELETE", "/repos/:owner/:name/collaborators/:username", Op::RemoveCollaborator, &[]),
52 route(
53 "GET",
54 "/repos/:owner/:name/collaborators/:username/permission",
55 Op::GetCollaboratorPermission,
56 &[],
57 ),
58 route("GET", "/repos/:owner/:name/invitations", Op::ListRepoInvitations, &[]),
59 route("DELETE", "/repos/:owner/:name/invitations/:id", Op::RevokeRepoInvitation, &[]),
60 route("GET", "/user/repository_invitations", Op::ListMyRepoInvitations, &[]),
61 // Your notifications: threads, marking them, and what you subscribe
62 // to and watch. GitHub's addresses, with g1t's saved and snoozed.
63 route("GET", "/notifications", Op::ListNotifications, &[("all", "all"), ("participating", "participating"), ("view", "view"), ("reason", "reason"), ("severity", "severity"), ("since", "since"), ("before", "before"), ("cursor", "cursor"), ("per_page", "per_page")]),
64 route("PUT", "/notifications", Op::MarkNotificationsRead, &[]),
65 route("GET", "/notifications/threads/:id", Op::GetNotificationThread, &[]),
66 route("PATCH", "/notifications/threads/:id", Op::MarkThreadRead, &[]),
67 route("DELETE", "/notifications/threads/:id", Op::MarkThreadDone, &[]),
68 route("PUT", "/notifications/threads/:id/saved", Op::SaveThread, &[]),
69 route("DELETE", "/notifications/threads/:id/saved", Op::SaveThread, &[]),
70 route("PUT", "/notifications/threads/:id/snooze", Op::SnoozeThread, &[]),
71 route("DELETE", "/notifications/threads/:id/snooze", Op::SnoozeThread, &[]),
72 route("GET", "/notifications/threads/:id/subscription", Op::GetThreadSubscription, &[]),
73 route("PUT", "/notifications/threads/:id/subscription", Op::SetThreadSubscription, &[]),
74 route("DELETE", "/notifications/threads/:id/subscription", Op::DeleteThreadSubscription, &[]),
75 route("GET", "/repos/:owner/:name/notifications", Op::ListNotifications, &[("all", "all"), ("participating", "participating"), ("view", "view"), ("reason", "reason"), ("severity", "severity"), ("since", "since"), ("before", "before"), ("cursor", "cursor"), ("per_page", "per_page")]),
76 route("PUT", "/repos/:owner/:name/notifications", Op::MarkNotificationsRead, &[]),
77 route("GET", "/repos/:owner/:name/subscription", Op::GetRepoSubscription, &[]),
78 route("PUT", "/repos/:owner/:name/subscription", Op::SetRepoSubscription, &[]),
79 route("DELETE", "/repos/:owner/:name/subscription", Op::DeleteRepoSubscription, &[]),
80 route("GET", "/repos/:owner/:name/issues/:number/subscription", Op::GetThreadSubscription, &[]),
81 route("PUT", "/repos/:owner/:name/issues/:number/subscription", Op::SetThreadSubscription, &[]),
82 route("DELETE", "/repos/:owner/:name/issues/:number/subscription", Op::DeleteThreadSubscription, &[]),
83 route("GET", "/user/subscriptions", Op::ListWatchedRepos, &[]),
84 // Your pinned projects in a workspace, in your order.
85 route("GET", "/user/pinned_projects/:workspace", Op::ListPinnedProjects, &[]),
86 route("PUT", "/user/pinned_projects/:workspace", Op::ReorderPinnedProjects, &[]),
87 route("PUT", "/user/pinned_projects/:workspace/:project", Op::PinProject, &[]),
88 route("DELETE", "/user/pinned_projects/:workspace/:project", Op::UnpinProject, &[]),
89 route("PATCH", "/user/repository_invitations/:id", Op::AcceptRepoInvitation, &[]),
90 route("DELETE", "/user/repository_invitations/:id", Op::DeclineRepoInvitation, &[]),
91 route("PATCH", "/workspaces/:workspace", Op::UpdateWorkspace, &[]),
92 route("PUT", "/workspaces/:workspace/base_permission", Op::SetBasePermission, &[]),
93 route(
94 "GET",
95 "/workspaces/:workspace/outside_collaborators",
96 Op::ListOutsideCollaborators,
97 &[],
98 ),
99 // Teams: a workspace's groups of members, with roles on repositories.
100 route("GET", "/workspaces/:workspace/teams", Op::ListTeams, &[("q", "query")]),
101 route("POST", "/workspaces/:workspace/teams", Op::CreateTeam, &[]),
102 route("GET", "/workspaces/:workspace/teams/:team", Op::GetTeam, &[]),
103 route("PATCH", "/workspaces/:workspace/teams/:team", Op::UpdateTeam, &[]),
104 route("DELETE", "/workspaces/:workspace/teams/:team", Op::DeleteTeam, &[]),
105 route(
106 "GET",
107 "/workspaces/:workspace/teams/:team/members",
108 Op::ListTeamMembers,
109 &[("include_child_teams", "include_child_teams")],
110 ),
111 route("PUT", "/workspaces/:workspace/teams/:team/members/:username", Op::SetTeamMember, &[]),
112 route("DELETE", "/workspaces/:workspace/teams/:team/members/:username", Op::RemoveTeamMember, &[]),
113 route("GET", "/workspaces/:workspace/teams/:team/teams", Op::ListChildTeams, &[]),
114 route("GET", "/workspaces/:workspace/teams/:team/repos", Op::ListTeamRepos, &[]),
115 route("PUT", "/workspaces/:workspace/teams/:team/repos/:repo", Op::SetTeamRepo, &[]),
116 route("DELETE", "/workspaces/:workspace/teams/:team/repos/:repo", Op::RemoveTeamRepo, &[]),
117 route(
118 "PUT",
119 "/workspaces/:workspace/teams/:team/review_assignment",
120 Op::SetTeamReviewAssignment,
121 &[],
122 ),
123 route("GET", "/workspaces/:workspace/members/:username/teams", Op::ListUserTeams, &[]),
124 // A workspace's billing: usage, budget, AI credit and invoices.
125 route(
126 "GET",
127 "/workspaces/:workspace/usage",
128 Op::GetUsage,
129 &[("from", "from"), ("until", "until"), ("products", "products"), ("projects", "projects"), ("group_by", "group_by")],
130 ),
131 route("GET", "/workspaces/:workspace/budget", Op::GetBudget, &[]),
132 route("PUT", "/workspaces/:workspace/budget", Op::SetBudget, &[]),
133 route("GET", "/workspaces/:workspace/ai_credit", Op::GetAiCredit, &[]),
134 route("POST", "/workspaces/:workspace/ai_credit/checkout", Op::BuyAiCredit, &[]),
135 route("GET", "/workspaces/:workspace/invoices", Op::ListInvoices, &[]),
136 route("GET", "/workspaces/:workspace/billing_details", Op::GetBillingDetails, &[]),
137 // The AI Gateway's log of a workspace's requests.
138 route("GET", "/workspaces/:workspace/gateway/requests", Op::ListGatewayRequests, &[("limit", "limit"), ("before", "before")]),
139 // Code owners: the CODEOWNERS file, checked.
140 route("GET", "/repos/:owner/:name/codeowners/errors", Op::GetCodeownersErrors, &[("ref", "ref")]),
141 // Security alerts: secrets and vulnerable dependencies.
142 route(
143 "GET",
144 "/repos/:owner/:name/security/alerts",
145 Op::ListSecurityAlerts,
146 &[("state", "state"), ("kind", "kind")],
147 ),
148 route("POST", "/repos/:owner/:name/security/alerts/:id/dismiss", Op::DismissSecurityAlert, &[]),
149 route("POST", "/repos/:owner/:name/security/alerts/:id/reopen", Op::ReopenSecurityAlert, &[]),
150 // The security suite: secret scanning, code scanning, vulnerability
151 // alerts and the supply chain, at the common addresses.
152 route("GET", "/repos/:owner/:name/secret-scanning/alerts", Op::Security(SecurityOp::ListSecretAlerts), &[("state", "state"), ("secret_type", "secret_type"), ("validity", "validity"), ("bypassed", "bypassed")]),
153 route("GET", "/workspaces/:workspace/secret-scanning/alerts", Op::Security(SecurityOp::ListSecretAlerts), &[("state", "state"), ("secret_type", "secret_type"), ("validity", "validity"), ("bypassed", "bypassed")]),
154 route("GET", "/repos/:owner/:name/secret-scanning/alerts/:id", Op::Security(SecurityOp::GetSecretAlert), &[]),
155 route("PATCH", "/repos/:owner/:name/secret-scanning/alerts/:id", Op::Security(SecurityOp::UpdateSecretAlert), &[]),
156 route("GET", "/repos/:owner/:name/secret-scanning/alerts/:id/locations", Op::Security(SecurityOp::ListSecretLocations), &[]),
157 route("POST", "/repos/:owner/:name/secret-scanning/alerts/:id/bypass", Op::Security(SecurityOp::BypassPushProtection), &[]),
158 route("POST", "/repos/:owner/:name/secret-scanning/alerts/:id/validity", Op::Security(SecurityOp::CheckSecretValidity), &[]),
159 route("GET", "/workspaces/:workspace/secret-scanning/bypass-requests", Op::Security(SecurityOp::ListBypassRequests), &[("state", "state"), ("repo", "repo")]),
160 route("PATCH", "/workspaces/:workspace/secret-scanning/bypass-requests/:id", Op::Security(SecurityOp::ReviewBypassRequest), &[]),
161 route("POST", "/repos/:owner/:name/secret-scanning/custom-patterns/dry-run", Op::Security(SecurityOp::DryRunCustomPattern), &[]),
162 route("POST", "/workspaces/:workspace/secret-scanning/custom-patterns/dry-run", Op::Security(SecurityOp::DryRunCustomPattern), &[]),
163 route("GET", "/repos/:owner/:name/secret-scanning/custom-patterns", Op::Security(SecurityOp::ListCustomPatterns), &[]),
164 route("GET", "/workspaces/:workspace/secret-scanning/custom-patterns", Op::Security(SecurityOp::ListCustomPatterns), &[]),
165 route("POST", "/repos/:owner/:name/secret-scanning/custom-patterns", Op::Security(SecurityOp::CreateCustomPattern), &[]),
166 route("POST", "/workspaces/:workspace/secret-scanning/custom-patterns", Op::Security(SecurityOp::CreateCustomPattern), &[]),
167 route("PATCH", "/repos/:owner/:name/secret-scanning/custom-patterns/:id", Op::Security(SecurityOp::UpdateCustomPattern), &[]),
168 route("PATCH", "/workspaces/:workspace/secret-scanning/custom-patterns/:id", Op::Security(SecurityOp::UpdateCustomPattern), &[]),
169 route("DELETE", "/repos/:owner/:name/secret-scanning/custom-patterns/:id", Op::Security(SecurityOp::DeleteCustomPattern), &[]),
170 route("DELETE", "/workspaces/:workspace/secret-scanning/custom-patterns/:id", Op::Security(SecurityOp::DeleteCustomPattern), &[]),
171 route("GET", "/repos/:owner/:name/code-scanning/alerts", Op::Security(SecurityOp::ListCodeAlerts), &[("state", "state"), ("severity", "severity"), ("tool", "tool"), ("rule_id", "rule_id")]),
172 route("GET", "/workspaces/:workspace/code-scanning/alerts", Op::Security(SecurityOp::ListCodeAlerts), &[("state", "state"), ("severity", "severity"), ("tool", "tool"), ("rule_id", "rule_id")]),
173 route("GET", "/repos/:owner/:name/code-scanning/alerts/:number", Op::Security(SecurityOp::GetCodeAlert), &[]),
174 route("PATCH", "/repos/:owner/:name/code-scanning/alerts/:number", Op::Security(SecurityOp::UpdateCodeAlert), &[]),
175 route("GET", "/repos/:owner/:name/code-scanning/analyses", Op::Security(SecurityOp::ListAnalyses), &[]),
176 route("POST", "/repos/:owner/:name/code-scanning/sarifs", Op::Security(SecurityOp::UploadSarif), &[]),
177 route("GET", "/repos/:owner/:name/code-scanning/sarifs/:id", Op::Security(SecurityOp::GetSarifUpload), &[]),
178 route("GET", "/repos/:owner/:name/vulnerability-alerts", Op::Security(SecurityOp::ListVulnerabilityAlerts), &[("state", "state"), ("severity", "severity"), ("ecosystem", "ecosystem"), ("package", "package")]),
179 route("GET", "/workspaces/:workspace/vulnerability-alerts", Op::Security(SecurityOp::ListVulnerabilityAlerts), &[("state", "state"), ("severity", "severity"), ("ecosystem", "ecosystem"), ("package", "package")]),
180 route("GET", "/repos/:owner/:name/vulnerability-alerts/:id", Op::Security(SecurityOp::GetVulnerabilityAlert), &[]),
181 route("PATCH", "/repos/:owner/:name/vulnerability-alerts/:id", Op::Security(SecurityOp::UpdateVulnerabilityAlert), &[]),
182 route("POST", "/repos/:owner/:name/security/alerts/:id/fix", Op::Security(SecurityOp::FixAlert), &[]),
183 route("GET", "/repos/:owner/:name/dependency-graph", Op::Security(SecurityOp::GetDependencyGraph), &[]),
184 route("GET", "/repos/:owner/:name/dependency-graph/sbom", Op::Security(SecurityOp::GetSbom), &[]),
185 route("GET", "/repos/:owner/:name/dependency-graph/compare/:basehead", Op::Security(SecurityOp::CompareDependencies), &[]),
186 route("GET", "/repos/:owner/:name/security/settings", Op::Security(SecurityOp::GetSettings), &[]),
187 route("PATCH", "/repos/:owner/:name/security/settings", Op::Security(SecurityOp::UpdateSettings), &[]),
188 route("GET", "/workspaces/:workspace/security/settings", Op::Security(SecurityOp::GetWorkspaceSettings), &[]),
189 route("PATCH", "/workspaces/:workspace/security/settings", Op::Security(SecurityOp::UpdateWorkspaceSettings), &[]),
190 route("GET", "/workspaces/:workspace/security/overview", Op::Security(SecurityOp::GetOverview), &[("days", "days")]),
191 route("GET", "/repos", Op::ListRepos, &[("q", "query")]),
192 route(
193 "GET",
194 "/search",
195 Op::Search,
196 &[("q", "query"), ("type", "type"), ("page", "page"), ("per_page", "per_page")],
197 ),
198 route("POST", "/repos", Op::CreateRepo, &[]),
199 route("GET", "/repos/:owner/:name", Op::GetRepo, &[]),
200 route("PATCH", "/repos/:owner/:name", Op::UpdateRepo, &[]),
201 route("POST", "/repos/:owner/:name/transfer", Op::TransferRepo, &[]),
202 route("DELETE", "/repos/:owner/:name", Op::DeleteRepo, &[]),
203 route(
204 "GET",
205 "/workspaces/:workspace/repos/deleted",
206 Op::ListDeletedRepos,
207 &[],
208 ),
209 route("POST", "/repos/:owner/:name/restore", Op::RestoreRepo, &[]),
210 route("POST", "/repos/:owner/:name/purge", Op::PurgeRepo, &[]),
211 route("POST", "/repos/:owner/:name/rename", Op::RenameRepo, &[]),
212 route("POST", "/repos/:owner/:name/archive", Op::ArchiveRepo, &[]),
213 route("POST", "/repos/:owner/:name/unarchive", Op::UnarchiveRepo, &[]),
214 route(
215 "POST",
216 "/repos/:owner/:name/visibility",
217 Op::SetRepoVisibility,
218 &[],
219 ),
220 route(
221 "POST",
222 "/repos/:owner/:name/branches/:branch/rename",
223 Op::RenameBranch,
224 &[],
225 ),
226 route(
227 "GET",
228 "/repos/:owner/:name/settings",
229 Op::GetRepoSettings,
230 &[],
231 ),
232 route(
233 "PATCH",
234 "/repos/:owner/:name/settings",
235 Op::UpdateRepoSettings,
236 &[],
237 ),
238 route("GET", "/repos/:owner/:name/check-names", Op::ListCheckNames, &[]),
239 route("GET", "/repos/:owner/:name/queue", Op::GetMergeQueue, &[]),
240 route(
241 "POST",
242 "/repos/:owner/:name/pulls/:number/messages",
243 Op::MessageAgent,
244 &[],
245 ),
246 route(
247 "POST",
248 "/repos/:owner/:name/pulls/:number/messages/take",
249 Op::TakeMessages,
250 &[],
251 ),
252 route(
253 "POST",
254 "/repos/:owner/:name/messages/:id/answer",
255 Op::AnswerMessage,
256 &[],
257 ),
258 route("POST", "/repos/:owner/:name/memory", Op::Remember, &[]),
259 route(
260 "GET",
261 "/repos/:owner/:name/memory",
262 Op::Recall,
263 &[("q", "query"), ("limit", "limit")],
264 ),
265 route(
266 "GET",
267 "/repos/:owner/:name/events",
268 Op::ListEvents,
269 &[("before", "before")],
270 ),
271 route("GET", "/repos/:owner/:name/labels", Op::ListLabels, &[]),
272 route("POST", "/repos/:owner/:name/labels", Op::CreateLabel, &[]),
273 route("POST", "/repos/:owner/:name/labels/defaults", Op::AddDefaultLabels, &[]),
274 route("PATCH", "/repos/:owner/:name/labels/:label", Op::UpdateLabel, &[]),
275 route("DELETE", "/repos/:owner/:name/labels/:label", Op::DeleteLabel, &[]),
276 route("GET", "/repos/:owner/:name/issues/:number/labels", Op::ListIssueLabels, &[]),
277 route("POST", "/repos/:owner/:name/issues/:number/labels", Op::AddIssueLabels, &[]),
278 route("PUT", "/repos/:owner/:name/issues/:number/labels", Op::SetIssueLabels, &[]),
279 route("DELETE", "/repos/:owner/:name/issues/:number/labels", Op::RemoveIssueLabels, &[]),
280 route("DELETE", "/repos/:owner/:name/issues/:number/labels/:label", Op::RemoveIssueLabels, &[]),
281 route("GET", "/repos/:owner/:name/milestones", Op::ListMilestones, &[("state", "state")]),
282 route("POST", "/repos/:owner/:name/milestones", Op::CreateMilestone, &[]),
283 route("GET", "/repos/:owner/:name/milestones/:milestone", Op::GetMilestone, &[]),
284 route("PATCH", "/repos/:owner/:name/milestones/:milestone", Op::UpdateMilestone, &[]),
285 route("DELETE", "/repos/:owner/:name/milestones/:milestone", Op::DeleteMilestone, &[]),
286 route(
287 "GET",
288 "/repos/:owner/:name/issues",
289 Op::ListIssues,
290 &[("state", "state"), ("label", "label"), ("milestone", "milestone")],
291 ),
292 route("POST", "/repos/:owner/:name/issues", Op::CreateIssue, &[]),
293 route(
294 "GET",
295 "/repos/:owner/:name/issues/:number",
296 Op::GetIssue,
297 &[],
298 ),
299 route(
300 "PATCH",
301 "/repos/:owner/:name/issues/:number",
302 Op::UpdateIssue,
303 &[],
304 ),
305 route(
306 "POST",
307 "/repos/:owner/:name/issues/:number/close",
308 Op::CloseIssue,
309 &[],
310 ),
311 route(
312 "POST",
313 "/repos/:owner/:name/issues/:number/reopen",
314 Op::ReopenIssue,
315 &[],
316 ),
317 route(
318 "POST",
319 "/repos/:owner/:name/issues/:number/assign",
320 Op::AssignIssue,
321 &[],
322 ),
323 route(
324 "POST",
325 "/repos/:owner/:name/issues/import",
326 Op::ImportIssue,
327 &[],
328 ),
329 route(
330 "POST",
331 "/repos/:owner/:name/issues/delegate",
332 Op::Delegate,
333 &[],
334 ),
335 route(
336 "GET",
337 "/repos/:owner/:name/context",
338 Op::GetContext,
339 &[("reference", "reference")],
340 ),
341 route(
342 "GET",
343 "/workspaces/:workspace/context/search",
344 Op::SearchContext,
345 &[("q", "query"), ("project", "project"), ("kinds", "kinds"), ("limit", "limit")],
346 ),
347 route(
348 "GET",
349 "/workspaces/:workspace/context/:kind/:id",
350 Op::GetEntity,
351 &[],
352 ),
353 route(
354 "GET",
355 "/workspaces/:workspace/integrations",
356 Op::ListIntegrations,
357 &[],
358 ),
359 route(
360 "POST",
361 "/workspaces/:workspace/integrations",
362 Op::ConnectIntegration,
363 &[],
364 ),
365 route(
366 "GET",
367 "/repos/:owner/:name/hooks",
368 Op::ListWebhooks,
369 &[],
370 ),
371 route(
372 "POST",
373 "/repos/:owner/:name/hooks",
374 Op::CreateWebhook,
375 &[],
376 ),
377 route(
378 "PATCH",
379 "/repos/:owner/:name/hooks/:id",
380 Op::UpdateWebhook,
381 &[],
382 ),
383 route(
384 "DELETE",
385 "/repos/:owner/:name/hooks/:id",
386 Op::DeleteWebhook,
387 &[],
388 ),
389 route(
390 "POST",
391 "/repos/:owner/:name/hooks/:id/pings",
392 Op::PingWebhook,
393 &[],
394 ),
395 route(
396 "GET",
397 "/repos/:owner/:name/hooks/:id/deliveries",
398 Op::ListWebhookDeliveries,
399 &[],
400 ),
401 route(
402 "POST",
403 "/repos/:owner/:name/hooks/:id/deliveries/:delivery/redeliver",
404 Op::RedeliverWebhook,
405 &[],
406 ),
407 route(
408 "GET",
409 "/workspaces/:workspace/hooks",
410 Op::ListWebhooks,
411 &[],
412 ),
413 route(
414 "POST",
415 "/workspaces/:workspace/hooks",
416 Op::CreateWebhook,
417 &[],
418 ),
419 route(
420 "PATCH",
421 "/workspaces/:workspace/hooks/:id",
422 Op::UpdateWebhook,
423 &[],
424 ),
425 route(
426 "DELETE",
427 "/workspaces/:workspace/hooks/:id",
428 Op::DeleteWebhook,
429 &[],
430 ),
431 route(
432 "POST",
433 "/workspaces/:workspace/hooks/:id/pings",
434 Op::PingWebhook,
435 &[],
436 ),
437 route(
438 "GET",
439 "/workspaces/:workspace/hooks/:id/deliveries",
440 Op::ListWebhookDeliveries,
441 &[],
442 ),
443 route(
444 "POST",
445 "/workspaces/:workspace/hooks/:id/deliveries/:delivery/redeliver",
446 Op::RedeliverWebhook,
447 &[],
448 ),
449 route(
450 "GET",
451 "/workspaces/:workspace/model-routes",
452 Op::GetModelRoutes,
453 &[],
454 ),
455 route(
456 "PUT",
457 "/workspaces/:workspace/model-routes",
458 Op::SetModelRoutes,
459 &[],
460 ),
461 route(
462 "DELETE",
463 "/workspaces/:workspace/integrations/:id",
464 Op::DisconnectIntegration,
465 &[],
466 ),
467 route(
468 "POST",
469 "/workspaces/:workspace/integrations/:id/test",
470 Op::TestIntegration,
471 &[],
472 ),
473 route(
474 "GET",
475 "/repos/:owner/:name/actions/workflows",
476 Op::ListWorkflows,
477 &[],
478 ),
479 route(
480 "GET",
481 "/repos/:owner/:name/actions/workflows/:workflow/runs",
482 Op::ListWorkflowRuns,
483 &[("branch", "branch"), ("event", "event"), ("per_page", "limit")],
484 ),
485 route(
486 "POST",
487 "/repos/:owner/:name/actions/workflows/:workflow/dispatches",
488 Op::DispatchWorkflow,
489 &[],
490 ),
491 route(
492 "PATCH",
493 "/repos/:owner/:name/actions/workflows/:workflow",
494 Op::UpdateWorkflow,
495 &[],
496 ),
497 route(
498 "PUT",
499 "/repos/:owner/:name/actions/workflows/:workflow/enable",
500 Op::UpdateWorkflow,
501 &[],
502 ),
503 route(
504 "PUT",
505 "/repos/:owner/:name/actions/workflows/:workflow/disable",
506 Op::UpdateWorkflow,
507 &[],
508 ),
509 route(
510 "GET",
511 "/repos/:owner/:name/actions/runs",
512 Op::ListWorkflowRuns,
513 &[("workflow", "workflow"), ("branch", "branch"), ("event", "event"), ("pull", "pull"), ("head_sha", "sha"), ("per_page", "limit")],
514 ),
515 route(
516 "GET",
517 "/repos/:owner/:name/actions/runs/:id",
518 Op::GetWorkflowRun,
519 &[],
520 ),
521 route(
522 "POST",
523 "/repos/:owner/:name/actions/runs/:id/cancel",
524 Op::CancelWorkflowRun,
525 &[],
526 ),
527 route(
528 "POST",
529 "/repos/:owner/:name/actions/runs/:id/rerun",
530 Op::RerunWorkflowRun,
531 &[],
532 ),
533 route(
534 "POST",
535 "/repos/:owner/:name/actions/runs/:id/rerun-failed-jobs",
536 Op::RerunWorkflowRun,
537 &[],
538 ),
539 route(
540 "GET",
541 "/repos/:owner/:name/actions/jobs/:job/logs",
542 Op::GetJobLogs,
543 &[("after", "after")],
544 ),
545 route(
546 "GET",
547 "/repos/:owner/:name/actions/secrets",
548 Op::ListActionsSecrets,
549 &[],
550 ),
551 route(
552 "PUT",
553 "/repos/:owner/:name/actions/secrets/:setting",
554 Op::SetActionsSecret,
555 &[],
556 ),
557 route(
558 "DELETE",
559 "/repos/:owner/:name/actions/secrets/:setting",
560 Op::DeleteActionsSecret,
561 &[],
562 ),
563 route(
564 "GET",
565 "/repos/:owner/:name/actions/variables",
566 Op::ListActionsVariables,
567 &[],
568 ),
569 route(
570 "POST",
571 "/repos/:owner/:name/actions/variables",
572 Op::SetActionsVariable,
573 &[],
574 ),
575 route(
576 "PATCH",
577 "/repos/:owner/:name/actions/variables/:setting",
578 Op::SetActionsVariable,
579 &[],
580 ),
581 route(
582 "DELETE",
583 "/repos/:owner/:name/actions/variables/:setting",
584 Op::DeleteActionsVariable,
585 &[],
586 ),
587 route(
588 "GET",
589 "/workspaces/:workspace/actions/secrets",
590 Op::ListActionsSecrets,
591 &[],
592 ),
593 route(
594 "PUT",
595 "/workspaces/:workspace/actions/secrets/:setting",
596 Op::SetActionsSecret,
597 &[],
598 ),
599 route(
600 "DELETE",
601 "/workspaces/:workspace/actions/secrets/:setting",
602 Op::DeleteActionsSecret,
603 &[],
604 ),
605 route(
606 "GET",
607 "/workspaces/:workspace/actions/variables",
608 Op::ListActionsVariables,
609 &[],
610 ),
611 route(
612 "POST",
613 "/workspaces/:workspace/actions/variables",
614 Op::SetActionsVariable,
615 &[],
616 ),
617 route(
618 "PATCH",
619 "/workspaces/:workspace/actions/variables/:setting",
620 Op::SetActionsVariable,
621 &[],
622 ),
623 route(
624 "DELETE",
625 "/workspaces/:workspace/actions/variables/:setting",
626 Op::DeleteActionsVariable,
627 &[],
628 ),
629 // Self-hosted runners: a repository's own, or a workspace's.
630 route("GET", "/repos/:owner/:name/actions/runners", Op::ListRunners, &[]),
631 route("POST", "/repos/:owner/:name/actions/runners/registration-token", Op::CreateRunnerRegistrationToken, &[]),
632 route("DELETE", "/repos/:owner/:name/actions/runners/:id", Op::RemoveRunner, &[]),
633 route("GET", "/repos/:owner/:name/actions/runner-settings", Op::GetRunnerSettings, &[]),
634 route("PATCH", "/repos/:owner/:name/actions/runner-settings", Op::UpdateRunnerSettings, &[]),
635 route("GET", "/workspaces/:workspace/actions/runners", Op::ListRunners, &[]),
636 route("POST", "/workspaces/:workspace/actions/runners/registration-token", Op::CreateRunnerRegistrationToken, &[]),
637 route("DELETE", "/workspaces/:workspace/actions/runners/:id", Op::RemoveRunner, &[]),
638 route("GET", "/workspaces/:workspace/actions/runner-settings", Op::GetRunnerSettings, &[]),
639 route("PATCH", "/workspaces/:workspace/actions/runner-settings", Op::UpdateRunnerSettings, &[]),
640 route("GET", "/workspaces/:workspace/actions/runner-groups", Op::ListRunnerGroups, &[]),
641 route("POST", "/workspaces/:workspace/actions/runner-groups", Op::CreateRunnerGroup, &[]),
642 route("PATCH", "/workspaces/:workspace/actions/runner-groups/:id", Op::UpdateRunnerGroup, &[]),
643 route("DELETE", "/workspaces/:workspace/actions/runner-groups/:id", Op::DeleteRunnerGroup, &[]),
644 route("POST", "/repos/:owner/:name/plans", Op::PlanWork, &[]),
645 route("GET", "/repos/:owner/:name/plans/:plan", Op::GetPlan, &[]),
646 route(
647 "POST",
648 "/repos/:owner/:name/plans/:plan/apply",
649 Op::ApplyPlan,
650 &[],
651 ),
652 route(
653 "POST",
654 "/repos/:owner/:name/issues/:number/comments",
655 Op::AddComment,
656 &[],
657 ),
658 route(
659 "GET",
660 "/repos/:owner/:name/pulls",
661 Op::ListPullRequests,
662 &[("state", "state"), ("label", "label"), ("milestone", "milestone"), ("base", "base")],
663 ),
664 route(
665 "POST",
666 "/repos/:owner/:name/pulls",
667 Op::CreatePullRequest,
668 &[],
669 ),
670 route(
671 "GET",
672 "/repos/:owner/:name/pulls/:number",
673 Op::GetPullRequest,
674 &[],
675 ),
676 route(
677 "PATCH",
678 "/repos/:owner/:name/pulls/:number",
679 Op::UpdatePullRequest,
680 &[],
681 ),
682 route(
683 "GET",
684 "/repos/:owner/:name/pulls/:number/changes",
685 Op::GetPullRequestChanges,
686 &[],
687 ),
688 route(
689 "POST",
690 "/repos/:owner/:name/pulls/:number/reviews",
691 Op::ReviewPullRequest,
692 &[],
693 ),
694 route(
695 "POST",
696 "/repos/:owner/:name/pulls/:number/requested_reviewers",
697 Op::RequestReviewers,
698 &[],
699 ),
700 route(
701 "DELETE",
702 "/repos/:owner/:name/pulls/:number/requested_reviewers",
703 Op::RemoveRequestedReviewers,
704 &[],
705 ),
706 route(
707 "GET",
708 "/repos/:owner/:name/pulls/:number/session",
709 Op::ReadSession,
710 &[("after", "after")],
711 ),
712 route(
713 "POST",
714 "/repos/:owner/:name/pulls/:number/session",
715 Op::RecordSession,
716 &[],
717 ),
718 route(
719 "POST",
720 "/repos/:owner/:name/pulls/:number/ready",
721 Op::MarkPullRequestReady,
722 &[],
723 ),
724 route(
725 "POST",
726 "/repos/:owner/:name/pulls/:number/close",
727 Op::ClosePullRequest,
728 &[],
729 ),
730 route(
731 "POST",
732 "/repos/:owner/:name/pulls/:number/merge",
733 Op::MergePullRequest,
734 &[],
735 ),
736];
737
738impl Route {
739 /// The names of the route's path parameters, in order.
740 pub fn params(&self) -> impl Iterator<Item = &'static str> {
741 self.path
742 .split('/')
743 .filter_map(|segment| segment.strip_prefix(':'))
744 }
745
746 /// The values of the path parameters, if `path` is this route's.
747 fn matches<'a>(&self, path: &'a str) -> Option<Vec<(&'static str, &'a str)>> {
748 let mut values = Vec::new();
749 let mut actual = path.trim_end_matches('/').split('/');
750 for expected in self.path.split('/') {
751 let segment = actual.next()?;
752 match expected.strip_prefix(':') {
753 Some(name) if !segment.is_empty() => values.push((name, segment)),
754 Some(_) => return None,
755 None if expected == segment => {}
756 None => return None,
757 }
758 }
759 actual.next().is_none().then_some(values)
760 }
761}
762
763/// A path segment with its `%XX` escapes decoded; as given when that is not
764/// UTF-8.
765fn percent_decoded(segment: &str) -> String {
766 let bytes = segment.as_bytes();
767 let mut out = Vec::with_capacity(bytes.len());
768 let mut i = 0;
769 while i < bytes.len() {
770 let escaped = (bytes[i] == b'%')
771 .then(|| segment.get(i + 1..i + 3))
772 .flatten()
773 .filter(|hex| hex.bytes().all(|byte| byte.is_ascii_hexdigit()))
774 .and_then(|hex| u8::from_str_radix(hex, 16).ok());
775 match escaped {
776 Some(byte) => {
777 out.push(byte);
778 i += 3;
779 }
780 None => {
781 out.push(bytes[i]);
782 i += 1;
783 }
784 }
785 }
786 String::from_utf8(out).unwrap_or_else(|_| segment.to_owned())
787}
788
789/// The route for a request, and the operation input it describes.
790///
791/// The input is the JSON body, overlaid with the query parameters the route
792/// reads and then with what the path names: `owner` and `name` become
793/// `repo`, as does a team's `repo` with its `workspace`, and `number`
794/// becomes an integer.
795pub fn resolve(
796 method: &str,
797 path: &str,
798 query: &[(String, String)],
799 body: Value,
800) -> Option<(&'static Route, Value)> {
801 let (route, params) = ROUTES
802 .iter()
803 .filter(|route| route.method == method)
804 .find_map(|route| Some((route, route.matches(path)?)))?;
805
806 let mut input = match body {
807 Value::Object(fields) => fields,
808 _ => Map::new(),
809 };
810 for (name, key) in route.query {
811 if let Some((_, value)) = query.iter().find(|(query_name, _)| query_name == name) {
812 input.insert((*key).to_owned(), Value::String(value.clone()));
813 }
814 }
815 let param = |wanted: &str| {
816 params
817 .iter()
818 .find(|(name, _)| *name == wanted)
819 .map(|(_, value)| *value)
820 };
821 if let (Some(owner), Some(name)) = (param("owner"), param("name")) {
822 input.insert("repo".to_owned(), Value::String(format!("{owner}/{name}")));
823 }
824 for key in ["plan", "id", "workspace", "delivery", "workflow", "job", "setting", "username", "team", "basehead"] {
825 if let Some(value) = param(key) {
826 input.insert(key.to_owned(), Value::String(value.to_owned()));
827 }
828 }
829 // A repository of a team's workspace, named by itself.
830 if let (Some(workspace), Some(name)) = (param("workspace"), param("repo")) {
831 input.insert("repo".to_owned(), Value::String(format!("{workspace}/{name}")));
832 }
833 // A branch name may hold slashes, sent URL-encoded as one segment, and
834 // a label's name spaces.
835 if let Some(branch) = param("branch") {
836 input.insert("branch".to_owned(), Value::String(percent_decoded(branch)));
837 }
838 if let Some(label) = param("label") {
839 input.insert("label".to_owned(), Value::String(percent_decoded(label)));
840 }
841 if let Some(milestone) = param("milestone") {
842 // Not a number: zero, which no milestone has.
843 input.insert("milestone".to_owned(), milestone.parse::<u32>().unwrap_or(0).into());
844 }
845 // GitHub says some things with the path alone.
846 if route.path.ends_with("/enable") || route.path.ends_with("/disable") {
847 input.insert("enabled".to_owned(), Value::Bool(route.path.ends_with("/enable")));
848 }
849 if route.path.ends_with("/rerun-failed-jobs") {
850 input.insert("failed_only".to_owned(), Value::Bool(true));
851 }
852 // Unsaving and waking a thread are a DELETE of what PUT made.
853 if route.method == "DELETE" && route.path.ends_with("/saved") {
854 input.insert("saved".to_owned(), Value::Bool(false));
855 }
856 if route.method == "DELETE" && route.path.ends_with("/snooze") {
857 input.remove("until");
858 }
859 if let Some(number) = param("number") {
860 // Not a number: zero, which no issue or pull request has.
861 input.insert(
862 "number".to_owned(),
863 number.parse::<u32>().unwrap_or(0).into(),
864 );
865 }
866 Some((route, Value::Object(input)))
867}
868
869#[cfg(test)]
870mod tests {
871 use serde_json::json;
872
873 use super::*;
874
875 #[test]
876 fn a_path_resolves_to_its_operation_and_input() {
877 let (route, input) = resolve(
878 "POST",
879 "/repos/flagon-io/hello/pulls/14/merge",
880 &[],
881 json!({ "keep_issue_open": true, "number": 99, "repo": "someone/else" }),
882 )
883 .unwrap();
884 assert_eq!(route.op, Op::MergePullRequest);
885 // What the path names wins over the body.
886 assert_eq!(
887 input,
888 json!({ "keep_issue_open": true, "number": 14, "repo": "flagon-io/hello" })
889 );
890 }
891
892 #[test]
893 fn a_branch_with_slashes_is_one_encoded_segment() {
894 let (route, input) = resolve(
895 "POST",
896 "/repos/flagon-io/hello/branches/feature%2Flogin/rename",
897 &[],
898 json!({ "new_name": "feature/sign-in" }),
899 )
900 .unwrap();
901 assert_eq!(route.op, Op::RenameBranch);
902 assert_eq!(
903 input,
904 json!({ "new_name": "feature/sign-in", "branch": "feature/login", "repo": "flagon-io/hello" })
905 );
906 assert_eq!(percent_decoded("100%"), "100%");
907 assert_eq!(percent_decoded("a%2bb%zz"), "a+b%zz");
908 }
909
910 #[test]
911 fn a_collaborator_is_named_by_username() {
912 let (route, input) = resolve(
913 "PATCH",
914 "/repos/flagon-io/hello/collaborators/ada",
915 &[],
916 json!({ "role": "maintain" }),
917 )
918 .unwrap();
919 assert_eq!(route.op, Op::UpdateCollaborator);
920 assert_eq!(input, json!({ "role": "maintain", "username": "ada", "repo": "flagon-io/hello" }));
921 let (route, input) = resolve("DELETE", "/user/repository_invitations/rin_1", &[], Value::Null).unwrap();
922 assert_eq!(route.op, Op::DeclineRepoInvitation);
923 assert_eq!(input, json!({ "id": "rin_1" }));
924 }
925
926 #[test]
927 fn teams_are_addressed_by_workspace_and_slug() {
928 let query = [("q".to_owned(), "back".to_owned())];
929 let (route, input) = resolve("GET", "/workspaces/acme/teams", &query, Value::Null).unwrap();
930 assert_eq!(route.op, Op::ListTeams);
931 assert_eq!(input, json!({ "query": "back", "workspace": "acme" }));
932 let (route, input) = resolve("PATCH", "/workspaces/acme/teams/backend", &[], json!({ "name": "Back end" })).unwrap();
933 assert_eq!(route.op, Op::UpdateTeam);
934 assert_eq!(input, json!({ "name": "Back end", "workspace": "acme", "team": "backend" }));
935 let (route, input) =
936 resolve("PUT", "/workspaces/acme/teams/backend/members/ana", &[], json!({ "role": "maintainer" })).unwrap();
937 assert_eq!(route.op, Op::SetTeamMember);
938 assert_eq!(input, json!({ "role": "maintainer", "workspace": "acme", "team": "backend", "username": "ana" }));
939 let query = [("include_child_teams".to_owned(), "true".to_owned())];
940 let (route, input) = resolve("GET", "/workspaces/acme/teams/backend/members", &query, Value::Null).unwrap();
941 assert_eq!(route.op, Op::ListTeamMembers);
942 assert_eq!(input, json!({ "include_child_teams": "true", "workspace": "acme", "team": "backend" }));
943 // A repository is named by itself, in the team's workspace.
944 let (route, input) =
945 resolve("PUT", "/workspaces/acme/teams/backend/repos/rocket", &[], json!({ "role": "write" })).unwrap();
946 assert_eq!(route.op, Op::SetTeamRepo);
947 assert_eq!(input, json!({ "role": "write", "workspace": "acme", "team": "backend", "repo": "acme/rocket" }));
948 let op = |method: &str, path: &str| resolve(method, path, &[], Value::Null).unwrap().0.op;
949 assert_eq!(op("DELETE", "/workspaces/acme/teams/backend/repos/rocket"), Op::RemoveTeamRepo);
950 assert_eq!(op("GET", "/workspaces/acme/teams/backend/teams"), Op::ListChildTeams);
951 assert_eq!(op("GET", "/workspaces/acme/teams/backend/repos"), Op::ListTeamRepos);
952 assert_eq!(op("PUT", "/workspaces/acme/teams/backend/review_assignment"), Op::SetTeamReviewAssignment);
953 assert_eq!(op("DELETE", "/workspaces/acme/teams/backend"), Op::DeleteTeam);
954 assert_eq!(op("POST", "/workspaces/acme/teams"), Op::CreateTeam);
955 assert_eq!(op("GET", "/workspaces/acme/teams/backend"), Op::GetTeam);
956 assert_eq!(op("DELETE", "/workspaces/acme/teams/backend/members/ana"), Op::RemoveTeamMember);
957 let (route, input) = resolve("GET", "/workspaces/acme/members/ana/teams", &[], Value::Null).unwrap();
958 assert_eq!(route.op, Op::ListUserTeams);
959 assert_eq!(input, json!({ "workspace": "acme", "username": "ana" }));
960 }
961
962 #[test]
963 fn reviewers_are_requested_and_code_owners_checked_on_a_repository() {
964 let body = json!({ "reviewers": ["ana"], "team_reviewers": ["backend"] });
965 let (route, input) = resolve("POST", "/repos/acme/rocket/pulls/7/requested_reviewers", &[], body.clone()).unwrap();
966 assert_eq!(route.op, Op::RequestReviewers);
967 assert_eq!(
968 input,
969 json!({ "reviewers": ["ana"], "team_reviewers": ["backend"], "repo": "acme/rocket", "number": 7 })
970 );
971 let (route, _) = resolve("DELETE", "/repos/acme/rocket/pulls/7/requested_reviewers", &[], body).unwrap();
972 assert_eq!(route.op, Op::RemoveRequestedReviewers);
973 let query = [("ref".to_owned(), "main".to_owned())];
974 let (route, input) = resolve("GET", "/repos/acme/rocket/codeowners/errors", &query, Value::Null).unwrap();
975 assert_eq!(route.op, Op::GetCodeownersErrors);
976 assert_eq!(input, json!({ "ref": "main", "repo": "acme/rocket" }));
977 }
978
979 #[test]
980 fn notifications_are_addressed_as_threads_and_by_issue() {
981 let (route, input) = resolve("DELETE", "/notifications/threads/ntf_1", &[], Value::Null).unwrap();
982 assert_eq!(route.op, Op::MarkThreadDone);
983 assert_eq!(input, json!({ "id": "ntf_1" }));
984 let (route, input) = resolve("DELETE", "/notifications/threads/ntf_1/saved", &[], Value::Null).unwrap();
985 assert_eq!(route.op, Op::SaveThread);
986 assert_eq!(input, json!({ "id": "ntf_1", "saved": false }));
987 let (route, input) = resolve("DELETE", "/notifications/threads/ntf_1/snooze", &[], json!({ "until": "x" })).unwrap();
988 assert_eq!(route.op, Op::SnoozeThread);
989 assert_eq!(input, json!({ "id": "ntf_1" }));
990 let query = [("all".to_owned(), "true".to_owned()), ("per_page".to_owned(), "50".to_owned())];
991 let (route, input) = resolve("GET", "/repos/acme/rocket/notifications", &query, Value::Null).unwrap();
992 assert_eq!(route.op, Op::ListNotifications);
993 assert_eq!(input, json!({ "all": "true", "per_page": "50", "repo": "acme/rocket" }));
994 let (route, input) = resolve("PUT", "/repos/acme/rocket/issues/7/subscription", &[], json!({ "ignored": true })).unwrap();
995 assert_eq!(route.op, Op::SetThreadSubscription);
996 assert_eq!(input, json!({ "ignored": true, "number": 7, "repo": "acme/rocket" }));
997 assert_eq!(resolve("GET", "/user/subscriptions", &[], Value::Null).unwrap().0.op, Op::ListWatchedRepos);
998 }
999
1000 #[test]
1001 fn labels_and_milestones_are_named_in_the_path() {
1002 let (route, input) = resolve("PATCH", "/repos/acme/web/labels/good%20first%20issue", &[], json!({ "color": "7057ff" })).unwrap();
1003 assert_eq!(route.op, Op::UpdateLabel);
1004 assert_eq!(input, json!({ "color": "7057ff", "label": "good first issue", "repo": "acme/web" }));
1005 let (route, input) = resolve("DELETE", "/repos/acme/web/issues/7/labels/bug", &[], Value::Null).unwrap();
1006 assert_eq!(route.op, Op::RemoveIssueLabels);
1007 assert_eq!(input, json!({ "label": "bug", "number": 7, "repo": "acme/web" }));
1008 let (route, input) = resolve("DELETE", "/repos/acme/web/issues/7/labels", &[], Value::Null).unwrap();
1009 assert_eq!(route.op, Op::RemoveIssueLabels);
1010 assert_eq!(input, json!({ "number": 7, "repo": "acme/web" }));
1011 let (route, _) = resolve("POST", "/repos/acme/web/labels/defaults", &[], Value::Null).unwrap();
1012 assert_eq!(route.op, Op::AddDefaultLabels);
1013 let (route, input) = resolve("PATCH", "/repos/acme/web/milestones/3", &[], json!({ "state": "closed" })).unwrap();
1014 assert_eq!(route.op, Op::UpdateMilestone);
1015 assert_eq!(input, json!({ "state": "closed", "milestone": 3, "repo": "acme/web" }));
1016 let (route, input) = resolve("PATCH", "/repos/acme/web/pulls/9", &[], json!({ "base": "release" })).unwrap();
1017 assert_eq!(route.op, Op::UpdatePullRequest);
1018 assert_eq!(input, json!({ "base": "release", "number": 9, "repo": "acme/web" }));
1019 }
1020
1021 #[test]
1022 fn billing_is_addressed_by_workspace() {
1023 let query = [("from".to_owned(), "2026-10-01".to_owned()), ("products".to_owned(), "agent,sandboxes".to_owned())];
1024 let (route, input) = resolve("GET", "/workspaces/acme/usage", &query, Value::Null).unwrap();
1025 assert_eq!(route.op, Op::GetUsage);
1026 assert_eq!(input, json!({ "from": "2026-10-01", "products": "agent,sandboxes", "workspace": "acme" }));
1027 let (route, input) = resolve("PUT", "/workspaces/acme/budget", &[], json!({ "alerts": [50] })).unwrap();
1028 assert_eq!(route.op, Op::SetBudget);
1029 assert_eq!(input, json!({ "alerts": [50], "workspace": "acme" }));
1030 let op = |method: &str, path: &str| resolve(method, path, &[], Value::Null).unwrap().0.op;
1031 assert_eq!(op("GET", "/workspaces/acme/budget"), Op::GetBudget);
1032 assert_eq!(op("GET", "/workspaces/acme/ai_credit"), Op::GetAiCredit);
1033 assert_eq!(op("POST", "/workspaces/acme/ai_credit/checkout"), Op::BuyAiCredit);
1034 assert_eq!(op("GET", "/workspaces/acme/invoices"), Op::ListInvoices);
1035 assert_eq!(op("GET", "/workspaces/acme/billing_details"), Op::GetBillingDetails);
1036 assert_eq!(op("GET", "/workspaces/acme/gateway/requests"), Op::ListGatewayRequests);
1037 }
1038
1039 #[test]
1040 fn query_parameters_are_renamed() {
1041 let query = [
1042 ("q".to_owned(), "parser".to_owned()),
1043 ("x".to_owned(), "y".to_owned()),
1044 ];
1045 let (route, input) = resolve("GET", "/repos", &query, Value::Null).unwrap();
1046 assert_eq!(route.op, Op::ListRepos);
1047 assert_eq!(input, json!({ "query": "parser" }));
1048 }
1049
1050 #[test]
1051 fn method_and_shape_must_match() {
1052 assert!(resolve("GET", "/repos/a/b/issues/1/close", &[], Value::Null).is_none());
1053 assert!(resolve("GET", "/repos/a", &[], Value::Null).is_none());
1054 assert!(resolve("GET", "/repos/a/b/issues/1/extra", &[], Value::Null).is_none());
1055 assert!(resolve("GET", "/repos/a/b/", &[], Value::Null).is_some());
1056 }
1057
1058 #[test]
1059 fn every_parameter_and_query_name_is_an_input() {
1060 for route in ROUTES {
1061 let properties = route.op.properties();
1062 for (_, key) in route.query {
1063 assert!(properties.contains_key(*key), "{}: {key}", route.path);
1064 }
1065 for name in route.params() {
1066 let covered = matches!(name, "owner" | "name") && properties.contains_key("repo")
1067 || properties.contains_key(name);
1068 assert!(covered, "{}: {name}", route.path);
1069 }
1070 }
1071 }
1072
1073 #[test]
1074 fn every_operation_has_a_route() {
1075 for op in Op::ALL {
1076 assert!(ROUTES.iter().any(|route| route.op == op), "{}", op.name());
1077 }
1078 }
1079}