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