| 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 | |
| 7 | pub struct Route { |
| 8 | pub method: &'static str, |
| 9 | /// Segments starting with `:` are parameters. |
| 10 | pub path: &'static str, |
| 11 | pub op: Op, |
| 12 | /// Query parameters the route reads, as `(name in the URL, input name)`. |
| 13 | pub query: &'static [(&'static str, &'static str)], |
| 14 | } |
| 15 | |
| 16 | const fn route( |
| 17 | method: &'static str, |
| 18 | path: &'static str, |
| 19 | op: Op, |
| 20 | query: &'static [(&'static str, &'static str)], |
| 21 | ) -> Route { |
| 22 | Route { |
| 23 | method, |
| 24 | path, |
| 25 | op, |
| 26 | query, |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | pub const ROUTES: &[Route] = &[ |
| 31 | route("GET", "/user", Op::Whoami, &[]), |
| 32 | route("POST", "/workspaces", Op::CreateWorkspace, &[]), |
| 33 | route("GET", "/repos", Op::ListRepos, &[("q", "query")]), |
| 34 | route("POST", "/repos", Op::CreateRepo, &[]), |
| 35 | route("GET", "/repos/:owner/:name", Op::GetRepo, &[]), |
| 36 | route("PATCH", "/repos/:owner/:name", Op::UpdateRepo, &[]), |
| 37 | route( |
| 38 | "GET", |
| 39 | "/repos/:owner/:name/settings", |
| 40 | Op::GetRepoSettings, |
| 41 | &[], |
| 42 | ), |
| 43 | route( |
| 44 | "PATCH", |
| 45 | "/repos/:owner/:name/settings", |
| 46 | Op::UpdateRepoSettings, |
| 47 | &[], |
| 48 | ), |
| 49 | route("GET", "/repos/:owner/:name/queue", Op::GetMergeQueue, &[]), |
| 50 | route( |
| 51 | "POST", |
| 52 | "/repos/:owner/:name/pulls/:number/messages", |
| 53 | Op::MessageAgent, |
| 54 | &[], |
| 55 | ), |
| 56 | route( |
| 57 | "POST", |
| 58 | "/repos/:owner/:name/pulls/:number/messages/take", |
| 59 | Op::TakeMessages, |
| 60 | &[], |
| 61 | ), |
| 62 | route( |
| 63 | "POST", |
| 64 | "/repos/:owner/:name/messages/:id/answer", |
| 65 | Op::AnswerMessage, |
| 66 | &[], |
| 67 | ), |
| 68 | route( |
| 69 | "GET", |
| 70 | "/repos/:owner/:name/events", |
| 71 | Op::ListEvents, |
| 72 | &[("before", "before")], |
| 73 | ), |
| 74 | route("GET", "/repos/:owner/:name/labels", Op::ListLabels, &[]), |
| 75 | route( |
| 76 | "GET", |
| 77 | "/repos/:owner/:name/issues", |
| 78 | Op::ListIssues, |
| 79 | &[("state", "state"), ("label", "label")], |
| 80 | ), |
| 81 | route("POST", "/repos/:owner/:name/issues", Op::CreateIssue, &[]), |
| 82 | route( |
| 83 | "GET", |
| 84 | "/repos/:owner/:name/issues/:number", |
| 85 | Op::GetIssue, |
| 86 | &[], |
| 87 | ), |
| 88 | route( |
| 89 | "PATCH", |
| 90 | "/repos/:owner/:name/issues/:number", |
| 91 | Op::UpdateIssue, |
| 92 | &[], |
| 93 | ), |
| 94 | route( |
| 95 | "POST", |
| 96 | "/repos/:owner/:name/issues/:number/close", |
| 97 | Op::CloseIssue, |
| 98 | &[], |
| 99 | ), |
| 100 | route( |
| 101 | "POST", |
| 102 | "/repos/:owner/:name/issues/:number/reopen", |
| 103 | Op::ReopenIssue, |
| 104 | &[], |
| 105 | ), |
| 106 | route( |
| 107 | "POST", |
| 108 | "/repos/:owner/:name/issues/:number/assign", |
| 109 | Op::AssignIssue, |
| 110 | &[], |
| 111 | ), |
| 112 | route( |
| 113 | "POST", |
| 114 | "/repos/:owner/:name/issues/import", |
| 115 | Op::ImportIssue, |
| 116 | &[], |
| 117 | ), |
| 118 | route( |
| 119 | "GET", |
| 120 | "/repos/:owner/:name/context", |
| 121 | Op::GetContext, |
| 122 | &[("reference", "reference")], |
| 123 | ), |
| 124 | route( |
| 125 | "GET", |
| 126 | "/workspaces/:workspace/integrations", |
| 127 | Op::ListIntegrations, |
| 128 | &[], |
| 129 | ), |
| 130 | route( |
| 131 | "POST", |
| 132 | "/workspaces/:workspace/integrations", |
| 133 | Op::ConnectIntegration, |
| 134 | &[], |
| 135 | ), |
| 136 | route( |
| 137 | "GET", |
| 138 | "/repos/:owner/:name/hooks", |
| 139 | Op::ListWebhooks, |
| 140 | &[], |
| 141 | ), |
| 142 | route( |
| 143 | "POST", |
| 144 | "/repos/:owner/:name/hooks", |
| 145 | Op::CreateWebhook, |
| 146 | &[], |
| 147 | ), |
| 148 | route( |
| 149 | "PATCH", |
| 150 | "/repos/:owner/:name/hooks/:id", |
| 151 | Op::UpdateWebhook, |
| 152 | &[], |
| 153 | ), |
| 154 | route( |
| 155 | "DELETE", |
| 156 | "/repos/:owner/:name/hooks/:id", |
| 157 | Op::DeleteWebhook, |
| 158 | &[], |
| 159 | ), |
| 160 | route( |
| 161 | "POST", |
| 162 | "/repos/:owner/:name/hooks/:id/pings", |
| 163 | Op::PingWebhook, |
| 164 | &[], |
| 165 | ), |
| 166 | route( |
| 167 | "GET", |
| 168 | "/repos/:owner/:name/hooks/:id/deliveries", |
| 169 | Op::ListWebhookDeliveries, |
| 170 | &[], |
| 171 | ), |
| 172 | route( |
| 173 | "POST", |
| 174 | "/repos/:owner/:name/hooks/:id/deliveries/:delivery/redeliver", |
| 175 | Op::RedeliverWebhook, |
| 176 | &[], |
| 177 | ), |
| 178 | route( |
| 179 | "GET", |
| 180 | "/workspaces/:workspace/hooks", |
| 181 | Op::ListWebhooks, |
| 182 | &[], |
| 183 | ), |
| 184 | route( |
| 185 | "POST", |
| 186 | "/workspaces/:workspace/hooks", |
| 187 | Op::CreateWebhook, |
| 188 | &[], |
| 189 | ), |
| 190 | route( |
| 191 | "PATCH", |
| 192 | "/workspaces/:workspace/hooks/:id", |
| 193 | Op::UpdateWebhook, |
| 194 | &[], |
| 195 | ), |
| 196 | route( |
| 197 | "DELETE", |
| 198 | "/workspaces/:workspace/hooks/:id", |
| 199 | Op::DeleteWebhook, |
| 200 | &[], |
| 201 | ), |
| 202 | route( |
| 203 | "POST", |
| 204 | "/workspaces/:workspace/hooks/:id/pings", |
| 205 | Op::PingWebhook, |
| 206 | &[], |
| 207 | ), |
| 208 | route( |
| 209 | "GET", |
| 210 | "/workspaces/:workspace/hooks/:id/deliveries", |
| 211 | Op::ListWebhookDeliveries, |
| 212 | &[], |
| 213 | ), |
| 214 | route( |
| 215 | "POST", |
| 216 | "/workspaces/:workspace/hooks/:id/deliveries/:delivery/redeliver", |
| 217 | Op::RedeliverWebhook, |
| 218 | &[], |
| 219 | ), |
| 220 | route( |
| 221 | "GET", |
| 222 | "/workspaces/:workspace/model-routes", |
| 223 | Op::GetModelRoutes, |
| 224 | &[], |
| 225 | ), |
| 226 | route( |
| 227 | "PUT", |
| 228 | "/workspaces/:workspace/model-routes", |
| 229 | Op::SetModelRoutes, |
| 230 | &[], |
| 231 | ), |
| 232 | route( |
| 233 | "DELETE", |
| 234 | "/workspaces/:workspace/integrations/:id", |
| 235 | Op::DisconnectIntegration, |
| 236 | &[], |
| 237 | ), |
| 238 | route( |
| 239 | "POST", |
| 240 | "/workspaces/:workspace/integrations/:id/test", |
| 241 | Op::TestIntegration, |
| 242 | &[], |
| 243 | ), |
| 244 | route( |
| 245 | "GET", |
| 246 | "/repos/:owner/:name/actions/workflows", |
| 247 | Op::ListWorkflows, |
| 248 | &[], |
| 249 | ), |
| 250 | route( |
| 251 | "GET", |
| 252 | "/repos/:owner/:name/actions/workflows/:workflow/runs", |
| 253 | Op::ListWorkflowRuns, |
| 254 | &[("branch", "branch"), ("event", "event"), ("per_page", "limit")], |
| 255 | ), |
| 256 | route( |
| 257 | "POST", |
| 258 | "/repos/:owner/:name/actions/workflows/:workflow/dispatches", |
| 259 | Op::DispatchWorkflow, |
| 260 | &[], |
| 261 | ), |
| 262 | route( |
| 263 | "PATCH", |
| 264 | "/repos/:owner/:name/actions/workflows/:workflow", |
| 265 | Op::UpdateWorkflow, |
| 266 | &[], |
| 267 | ), |
| 268 | route( |
| 269 | "PUT", |
| 270 | "/repos/:owner/:name/actions/workflows/:workflow/enable", |
| 271 | Op::UpdateWorkflow, |
| 272 | &[], |
| 273 | ), |
| 274 | route( |
| 275 | "PUT", |
| 276 | "/repos/:owner/:name/actions/workflows/:workflow/disable", |
| 277 | Op::UpdateWorkflow, |
| 278 | &[], |
| 279 | ), |
| 280 | route( |
| 281 | "GET", |
| 282 | "/repos/:owner/:name/actions/runs", |
| 283 | Op::ListWorkflowRuns, |
| 284 | &[("workflow", "workflow"), ("branch", "branch"), ("event", "event"), ("pull", "pull"), ("head_sha", "sha"), ("per_page", "limit")], |
| 285 | ), |
| 286 | route( |
| 287 | "GET", |
| 288 | "/repos/:owner/:name/actions/runs/:id", |
| 289 | Op::GetWorkflowRun, |
| 290 | &[], |
| 291 | ), |
| 292 | route( |
| 293 | "POST", |
| 294 | "/repos/:owner/:name/actions/runs/:id/cancel", |
| 295 | Op::CancelWorkflowRun, |
| 296 | &[], |
| 297 | ), |
| 298 | route( |
| 299 | "POST", |
| 300 | "/repos/:owner/:name/actions/runs/:id/rerun", |
| 301 | Op::RerunWorkflowRun, |
| 302 | &[], |
| 303 | ), |
| 304 | route( |
| 305 | "POST", |
| 306 | "/repos/:owner/:name/actions/runs/:id/rerun-failed-jobs", |
| 307 | Op::RerunWorkflowRun, |
| 308 | &[], |
| 309 | ), |
| 310 | route( |
| 311 | "GET", |
| 312 | "/repos/:owner/:name/actions/jobs/:job/logs", |
| 313 | Op::GetJobLogs, |
| 314 | &[("after", "after")], |
| 315 | ), |
| 316 | route( |
| 317 | "GET", |
| 318 | "/repos/:owner/:name/actions/secrets", |
| 319 | Op::ListActionsSecrets, |
| 320 | &[], |
| 321 | ), |
| 322 | route( |
| 323 | "PUT", |
| 324 | "/repos/:owner/:name/actions/secrets/:setting", |
| 325 | Op::SetActionsSecret, |
| 326 | &[], |
| 327 | ), |
| 328 | route( |
| 329 | "DELETE", |
| 330 | "/repos/:owner/:name/actions/secrets/:setting", |
| 331 | Op::DeleteActionsSecret, |
| 332 | &[], |
| 333 | ), |
| 334 | route( |
| 335 | "GET", |
| 336 | "/repos/:owner/:name/actions/variables", |
| 337 | Op::ListActionsVariables, |
| 338 | &[], |
| 339 | ), |
| 340 | route( |
| 341 | "POST", |
| 342 | "/repos/:owner/:name/actions/variables", |
| 343 | Op::SetActionsVariable, |
| 344 | &[], |
| 345 | ), |
| 346 | route( |
| 347 | "PATCH", |
| 348 | "/repos/:owner/:name/actions/variables/:setting", |
| 349 | Op::SetActionsVariable, |
| 350 | &[], |
| 351 | ), |
| 352 | route( |
| 353 | "DELETE", |
| 354 | "/repos/:owner/:name/actions/variables/:setting", |
| 355 | Op::DeleteActionsVariable, |
| 356 | &[], |
| 357 | ), |
| 358 | route( |
| 359 | "GET", |
| 360 | "/workspaces/:workspace/actions/secrets", |
| 361 | Op::ListActionsSecrets, |
| 362 | &[], |
| 363 | ), |
| 364 | route( |
| 365 | "PUT", |
| 366 | "/workspaces/:workspace/actions/secrets/:setting", |
| 367 | Op::SetActionsSecret, |
| 368 | &[], |
| 369 | ), |
| 370 | route( |
| 371 | "DELETE", |
| 372 | "/workspaces/:workspace/actions/secrets/:setting", |
| 373 | Op::DeleteActionsSecret, |
| 374 | &[], |
| 375 | ), |
| 376 | route( |
| 377 | "GET", |
| 378 | "/workspaces/:workspace/actions/variables", |
| 379 | Op::ListActionsVariables, |
| 380 | &[], |
| 381 | ), |
| 382 | route( |
| 383 | "POST", |
| 384 | "/workspaces/:workspace/actions/variables", |
| 385 | Op::SetActionsVariable, |
| 386 | &[], |
| 387 | ), |
| 388 | route( |
| 389 | "PATCH", |
| 390 | "/workspaces/:workspace/actions/variables/:setting", |
| 391 | Op::SetActionsVariable, |
| 392 | &[], |
| 393 | ), |
| 394 | route( |
| 395 | "DELETE", |
| 396 | "/workspaces/:workspace/actions/variables/:setting", |
| 397 | Op::DeleteActionsVariable, |
| 398 | &[], |
| 399 | ), |
| 400 | route("POST", "/repos/:owner/:name/plans", Op::PlanWork, &[]), |
| 401 | route("GET", "/repos/:owner/:name/plans/:plan", Op::GetPlan, &[]), |
| 402 | route( |
| 403 | "POST", |
| 404 | "/repos/:owner/:name/plans/:plan/apply", |
| 405 | Op::ApplyPlan, |
| 406 | &[], |
| 407 | ), |
| 408 | route( |
| 409 | "POST", |
| 410 | "/repos/:owner/:name/issues/:number/comments", |
| 411 | Op::AddComment, |
| 412 | &[], |
| 413 | ), |
| 414 | route( |
| 415 | "GET", |
| 416 | "/repos/:owner/:name/pulls", |
| 417 | Op::ListPullRequests, |
| 418 | &[("state", "state")], |
| 419 | ), |
| 420 | route( |
| 421 | "POST", |
| 422 | "/repos/:owner/:name/pulls", |
| 423 | Op::CreatePullRequest, |
| 424 | &[], |
| 425 | ), |
| 426 | route( |
| 427 | "GET", |
| 428 | "/repos/:owner/:name/pulls/:number", |
| 429 | Op::GetPullRequest, |
| 430 | &[], |
| 431 | ), |
| 432 | route( |
| 433 | "GET", |
| 434 | "/repos/:owner/:name/pulls/:number/changes", |
| 435 | Op::GetPullRequestChanges, |
| 436 | &[], |
| 437 | ), |
| 438 | route( |
| 439 | "POST", |
| 440 | "/repos/:owner/:name/pulls/:number/reviews", |
| 441 | Op::ReviewPullRequest, |
| 442 | &[], |
| 443 | ), |
| 444 | route( |
| 445 | "GET", |
| 446 | "/repos/:owner/:name/pulls/:number/session", |
| 447 | Op::ReadSession, |
| 448 | &[("after", "after")], |
| 449 | ), |
| 450 | route( |
| 451 | "POST", |
| 452 | "/repos/:owner/:name/pulls/:number/session", |
| 453 | Op::RecordSession, |
| 454 | &[], |
| 455 | ), |
| 456 | route( |
| 457 | "POST", |
| 458 | "/repos/:owner/:name/pulls/:number/ready", |
| 459 | Op::MarkPullRequestReady, |
| 460 | &[], |
| 461 | ), |
| 462 | route( |
| 463 | "POST", |
| 464 | "/repos/:owner/:name/pulls/:number/close", |
| 465 | Op::ClosePullRequest, |
| 466 | &[], |
| 467 | ), |
| 468 | route( |
| 469 | "POST", |
| 470 | "/repos/:owner/:name/pulls/:number/merge", |
| 471 | Op::MergePullRequest, |
| 472 | &[], |
| 473 | ), |
| 474 | ]; |
| 475 | |
| 476 | impl Route { |
| 477 | /// The names of the route's path parameters, in order. |
| 478 | pub fn params(&self) -> impl Iterator<Item = &'static str> { |
| 479 | self.path |
| 480 | .split('/') |
| 481 | .filter_map(|segment| segment.strip_prefix(':')) |
| 482 | } |
| 483 | |
| 484 | /// The values of the path parameters, if `path` is this route's. |
| 485 | fn matches<'a>(&self, path: &'a str) -> Option<Vec<(&'static str, &'a str)>> { |
| 486 | let mut values = Vec::new(); |
| 487 | let mut actual = path.trim_end_matches('/').split('/'); |
| 488 | for expected in self.path.split('/') { |
| 489 | let segment = actual.next()?; |
| 490 | match expected.strip_prefix(':') { |
| 491 | Some(name) if !segment.is_empty() => values.push((name, segment)), |
| 492 | Some(_) => return None, |
| 493 | None if expected == segment => {} |
| 494 | None => return None, |
| 495 | } |
| 496 | } |
| 497 | actual.next().is_none().then_some(values) |
| 498 | } |
| 499 | } |
| 500 | |
| 501 | /// The route for a request, and the operation input it describes. |
| 502 | /// |
| 503 | /// The input is the JSON body, overlaid with the query parameters the route |
| 504 | /// reads and then with what the path names: `owner` and `name` become |
| 505 | /// `repo`, and `number` becomes an integer. |
| 506 | pub fn resolve( |
| 507 | method: &str, |
| 508 | path: &str, |
| 509 | query: &[(String, String)], |
| 510 | body: Value, |
| 511 | ) -> Option<(&'static Route, Value)> { |
| 512 | let (route, params) = ROUTES |
| 513 | .iter() |
| 514 | .filter(|route| route.method == method) |
| 515 | .find_map(|route| Some((route, route.matches(path)?)))?; |
| 516 | |
| 517 | let mut input = match body { |
| 518 | Value::Object(fields) => fields, |
| 519 | _ => Map::new(), |
| 520 | }; |
| 521 | for (name, key) in route.query { |
| 522 | if let Some((_, value)) = query.iter().find(|(query_name, _)| query_name == name) { |
| 523 | input.insert((*key).to_owned(), Value::String(value.clone())); |
| 524 | } |
| 525 | } |
| 526 | let param = |wanted: &str| { |
| 527 | params |
| 528 | .iter() |
| 529 | .find(|(name, _)| *name == wanted) |
| 530 | .map(|(_, value)| *value) |
| 531 | }; |
| 532 | if let (Some(owner), Some(name)) = (param("owner"), param("name")) { |
| 533 | input.insert("repo".to_owned(), Value::String(format!("{owner}/{name}"))); |
| 534 | } |
| 535 | for key in ["plan", "id", "workspace", "delivery", "workflow", "job", "setting"] { |
| 536 | if let Some(value) = param(key) { |
| 537 | input.insert(key.to_owned(), Value::String(value.to_owned())); |
| 538 | } |
| 539 | } |
| 540 | // GitHub says some things with the path alone. |
| 541 | if route.path.ends_with("/enable") || route.path.ends_with("/disable") { |
| 542 | input.insert("enabled".to_owned(), Value::Bool(route.path.ends_with("/enable"))); |
| 543 | } |
| 544 | if route.path.ends_with("/rerun-failed-jobs") { |
| 545 | input.insert("failed_only".to_owned(), Value::Bool(true)); |
| 546 | } |
| 547 | if let Some(number) = param("number") { |
| 548 | // Not a number: zero, which no issue or pull request has. |
| 549 | input.insert( |
| 550 | "number".to_owned(), |
| 551 | number.parse::<u32>().unwrap_or(0).into(), |
| 552 | ); |
| 553 | } |
| 554 | Some((route, Value::Object(input))) |
| 555 | } |
| 556 | |
| 557 | #[cfg(test)] |
| 558 | mod tests { |
| 559 | use serde_json::json; |
| 560 | |
| 561 | use super::*; |
| 562 | |
| 563 | #[test] |
| 564 | fn a_path_resolves_to_its_operation_and_input() { |
| 565 | let (route, input) = resolve( |
| 566 | "POST", |
| 567 | "/repos/syntaqx/hello/pulls/14/merge", |
| 568 | &[], |
| 569 | json!({ "keep_issue_open": true, "number": 99, "repo": "someone/else" }), |
| 570 | ) |
| 571 | .unwrap(); |
| 572 | assert_eq!(route.op, Op::MergePullRequest); |
| 573 | // What the path names wins over the body. |
| 574 | assert_eq!( |
| 575 | input, |
| 576 | json!({ "keep_issue_open": true, "number": 14, "repo": "syntaqx/hello" }) |
| 577 | ); |
| 578 | } |
| 579 | |
| 580 | #[test] |
| 581 | fn query_parameters_are_renamed() { |
| 582 | let query = [ |
| 583 | ("q".to_owned(), "parser".to_owned()), |
| 584 | ("x".to_owned(), "y".to_owned()), |
| 585 | ]; |
| 586 | let (route, input) = resolve("GET", "/repos", &query, Value::Null).unwrap(); |
| 587 | assert_eq!(route.op, Op::ListRepos); |
| 588 | assert_eq!(input, json!({ "query": "parser" })); |
| 589 | } |
| 590 | |
| 591 | #[test] |
| 592 | fn method_and_shape_must_match() { |
| 593 | assert!(resolve("GET", "/repos/a/b/issues/1/close", &[], Value::Null).is_none()); |
| 594 | assert!(resolve("GET", "/repos/a", &[], Value::Null).is_none()); |
| 595 | assert!(resolve("GET", "/repos/a/b/issues/1/extra", &[], Value::Null).is_none()); |
| 596 | assert!(resolve("GET", "/repos/a/b/", &[], Value::Null).is_some()); |
| 597 | } |
| 598 | |
| 599 | #[test] |
| 600 | fn every_parameter_and_query_name_is_an_input() { |
| 601 | for route in ROUTES { |
| 602 | let properties = route.op.properties(); |
| 603 | for (_, key) in route.query { |
| 604 | assert!(properties.contains_key(*key), "{}: {key}", route.path); |
| 605 | } |
| 606 | for name in route.params() { |
| 607 | let covered = matches!(name, "owner" | "name") && properties.contains_key("repo") |
| 608 | || properties.contains_key(name); |
| 609 | assert!(covered, "{}: {name}", route.path); |
| 610 | } |
| 611 | } |
| 612 | } |
| 613 | |
| 614 | #[test] |
| 615 | fn every_operation_has_a_route() { |
| 616 | for op in Op::ALL { |
| 617 | assert!(ROUTES.iter().any(|route| route.op == op), "{}", op.name()); |
| 618 | } |
| 619 | } |
| 620 | } |