From 03c2ff9a172b99dafed809e6e687caac46b9bbfa Mon Sep 17 00:00:00 2001 From: Cyborus Date: Mon, 8 Jul 2024 22:22:39 -0400 Subject: fix: null team reviews --- generator/src/openapi.rs | 3 ++ generator/src/structs.rs | 14 +++++- src/generated/structs.rs | 1 + src/lib.rs | 12 +++++ swagger.v1.json | 5 ++- tests/repo.rs | 113 ++++++++++++++++++++++++++++++++++++++++++++++- 6 files changed, 145 insertions(+), 3 deletions(-) diff --git a/generator/src/openapi.rs b/generator/src/openapi.rs index 35dd9ba..0678b59 100644 --- a/generator/src/openapi.rs +++ b/generator/src/openapi.rs @@ -1079,6 +1079,9 @@ pub struct Schema { pub xml: Option, pub external_docs: Option, pub example: Option, + + #[serde(flatten)] + pub extensions: BTreeMap, } impl JsonDeref for Schema { diff --git a/generator/src/structs.rs b/generator/src/structs.rs index 4c78fac..6a74dc1 100644 --- a/generator/src/structs.rs +++ b/generator/src/structs.rs @@ -1,7 +1,7 @@ use crate::openapi::*; use eyre::WrapErr; use heck::ToPascalCase; -use std::fmt::Write; +use std::{collections::BTreeMap, fmt::Write}; pub fn create_structs(spec: &OpenApiV2) -> eyre::Result { let mut s = String::new(); @@ -51,6 +51,13 @@ pub fn create_struct_for_definition( let mut subtypes = Vec::new(); + let parse_with = schema + .extensions + .get("x-parse-with") + .map(|ex| serde_json::from_value::>(ex.clone())) + .transpose()? + .unwrap_or_default(); + let docs = create_struct_docs(schema)?; let mut fields = String::new(); let required = schema.required.as_deref().unwrap_or_default(); @@ -90,6 +97,11 @@ pub fn create_struct_for_definition( if field_ty == "Option" { fields.push_str("#[serde(with = \"time::serde::rfc3339::option\")]\n"); } + if let Some(parse_method) = parse_with.get(prop_name) { + fields.push_str("#[serde(deserialize_with = \"crate::"); + fields.push_str(parse_method); + fields.push_str("\")]\n"); + } if let MaybeRef::Value { value } = &prop_schema { if let Some(desc) = &value.description { for line in desc.lines() { diff --git a/src/generated/structs.rs b/src/generated/structs.rs index f7b8ce8..5b97ac9 100644 --- a/src/generated/structs.rs +++ b/src/generated/structs.rs @@ -2034,6 +2034,7 @@ pub struct PullRequest { #[serde(deserialize_with = "crate::none_if_blank_url")] pub patch_url: Option, pub pin_order: Option, + #[serde(deserialize_with = "crate::requested_reviewers_ignore_null")] pub requested_reviewers: Option>, pub state: Option, pub title: Option, diff --git a/src/lib.rs b/src/lib.rs index d9b877e..abbb8a5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -358,6 +358,18 @@ where .or(Ok(None)) } +fn requested_reviewers_ignore_null<'de, D, DE>( + deserializer: D, +) -> Result>, DE> +where + D: Deserializer<'de>, + DE: serde::de::Error, +{ + let list: Option>> = + Option::deserialize(deserializer).map_err(DE::custom)?; + Ok(list.map(|list| list.into_iter().filter_map(|x| x).collect::>())) +} + fn parse_ssh_url(raw_url: &String) -> Result { // in case of a non-standard ssh-port (not 22), the ssh url coming from the forgejo API // is actually parseable by the url crate, so try to do that first diff --git a/swagger.v1.json b/swagger.v1.json index e706eb8..fad9e63 100644 --- a/swagger.v1.json +++ b/swagger.v1.json @@ -20667,7 +20667,10 @@ "$ref": "#/definitions/User" } }, - "x-go-package": "code.gitea.io/gitea/modules/structs" + "x-go-package": "code.gitea.io/gitea/modules/structs", + "x-parse-with": { + "requested_reviewers": "requested_reviewers_ignore_null" + } }, "PullRequestMeta": { "description": "PullRequestMeta PR info if an issue is a PR", diff --git a/tests/repo.rs b/tests/repo.rs index 004cb8e..16566da 100644 --- a/tests/repo.rs +++ b/tests/repo.rs @@ -20,7 +20,7 @@ impl Git { } } -async fn basic_repo(api: &forgejo_api::Forgejo, git: &Git, name: &str) -> Repository { +async fn setup_local_repo(git: &Git) { git.run(&["config", "--global", "init.defaultBranch", "main"]); git.run(&["init"]); git.run(&["config", "user.name", "TestingAdmin"]); @@ -30,7 +30,10 @@ async fn basic_repo(api: &forgejo_api::Forgejo, git: &Git, name: &str) -> Reposi .unwrap(); git.run(&["add", "."]); git.run(&["commit", "-m", "initial commit"]); +} +async fn basic_repo(api: &forgejo_api::Forgejo, git: &Git, name: &str) -> Repository { + setup_local_repo(git).await; let repo_opt = CreateRepoOption { auto_init: Some(false), default_branch: Some("main".into()), @@ -68,6 +71,51 @@ async fn basic_repo(api: &forgejo_api::Forgejo, git: &Git, name: &str) -> Reposi remote_repo } +async fn basic_org_repo( + api: &forgejo_api::Forgejo, + git: &Git, + org: &str, + name: &str, +) -> Repository { + setup_local_repo(git).await; + + let repo_opt = CreateRepoOption { + auto_init: Some(false), + default_branch: Some("main".into()), + description: Some("Test Repo".into()), + gitignores: Some("".into()), + issue_labels: Some("".into()), + license: Some("".into()), + name: name.into(), + object_format_name: None, + private: Some(false), + readme: None, + template: Some(false), + trust_model: Some(CreateRepoOptionTrustModel::Default), + }; + let remote_repo = api.create_org_repo(org, repo_opt).await.unwrap(); + assert!( + remote_repo.has_pull_requests.unwrap(), + "repo does not accept pull requests" + ); + assert!( + remote_repo.owner.as_ref().unwrap().login.as_ref().unwrap() == org, + "repo owner is not \"TestingAdmin\"" + ); + assert!( + remote_repo.name.as_ref().unwrap() == name, + "repo name is not \"{name}\"" + ); + + let mut remote_url = remote_repo.clone_url.clone().unwrap(); + remote_url.set_username("TestingAdmin").unwrap(); + remote_url.set_password(Some("password")).unwrap(); + git.run(&["remote", "add", "origin", remote_url.as_str()]); + git.run(&["push", "-u", "origin", "main"]); + + remote_repo +} + #[tokio::test] async fn pull_request() { let api = common::login(); @@ -274,3 +322,66 @@ async fn delete_repo() { .await .expect("failed to delete repo"); } + +#[tokio::test] +async fn team_pr_review_request() { + let api = common::login(); + + let org_opt = CreateOrgOption { + description: Some("Testing team review requests".into()), + email: None, + full_name: None, + location: None, + repo_admin_change_team_access: None, + username: "team-review-org".into(), + visibility: None, + website: None, + }; + api.org_create(org_opt).await.unwrap(); + + let git = Git::new("./test_repos/team-pr-review"); + let _ = basic_org_repo(&api, &git, "team-review-org", "team-pr-review").await; + + git.run(&["switch", "-c", "test"]); + tokio::fs::write( + "./test_repos/team-pr-review/example.rs", + "fn add_one(x: u32) -> u32 { x + 1 }", + ) + .await + .unwrap(); + git.run(&["add", "."]); + git.run(&["commit", "-m", "egg"]); + git.run(&["push", "-u", "origin", "test"]); + + let pr_opt = CreatePullRequestOption { + assignee: None, + assignees: Some(vec!["TestingAdmin".into()]), + base: Some("main".into()), + body: Some("This is a test PR".into()), + due_date: None, + head: Some("test".into()), + labels: None, + milestone: None, + title: Some("test pr".into()), + }; + + // Wait for... something to happen, or else creating a PR will return 404 + tokio::time::sleep(std::time::Duration::from_secs(3)).await; + api.repo_create_pull_request("team-review-org", "team-pr-review", pr_opt) + .await + .expect("couldn't create pr"); + + let review_opt = PullReviewRequestOptions { + reviewers: None, + team_reviewers: Some(vec!["Owners".into()]), + }; + api.repo_create_pull_review_requests("team-review-org", "team-pr-review", 1, review_opt) + .await + .expect("couldn't request review"); + + let pr = api + .repo_get_pull_request("team-review-org", "team-pr-review", 1) + .await + .expect("couldn't get pr"); + assert_eq!(pr.requested_reviewers, Some(Vec::new())); +} -- cgit v1.2.3