From b05b9f85927cc431f56a76fdd2ec88328f73e5e8 Mon Sep 17 00:00:00 2001 From: Jan David Date: Tue, 12 Dec 2023 17:27:48 +0100 Subject: [PATCH] Skip serializing default GitHub host (#88) The configuration enables users to set a custom endpoint for GitHub. This can be useful for users of GitHub Enterprise, but is mostly used to mock GitHub in tests. The setting is now excluded from the rendered configuration if it points to github.com, which cleans up the configuration. --- src/cli/configuration/mod.rs | 1 - src/github/configuration.rs | 43 ++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/src/cli/configuration/mod.rs b/src/cli/configuration/mod.rs index 6d95e3a..08f79fa 100644 --- a/src/cli/configuration/mod.rs +++ b/src/cli/configuration/mod.rs @@ -249,7 +249,6 @@ mod tests { r#" library: github: - instance: https://api.github.com/ owner: jdno repository: flowcrafter workflows: diff --git a/src/github/configuration.rs b/src/github/configuration.rs index b9ce937..148dccb 100644 --- a/src/github/configuration.rs +++ b/src/github/configuration.rs @@ -10,6 +10,7 @@ use crate::github::repository::Repository; #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] pub struct GitHubConfiguration { #[cfg_attr(feature = "serde", serde(default = "default_instance"))] + #[cfg_attr(feature = "serde", serde(skip_serializing_if = "is_default_instance"))] #[builder(default = default_instance())] instance: Url, @@ -44,6 +45,10 @@ fn default_instance() -> Url { Url::parse("https://api.github.com").expect("failed to parse hard-coded GitHub URL 🤯") } +fn is_default_instance(instance: &Url) -> bool { + instance == &default_instance() +} + #[cfg(test)] mod tests { use super::*; @@ -101,6 +106,44 @@ mod tests { assert_send::(); } + #[cfg(feature = "serde")] + #[test] + fn trait_serialize_with_default_instance() { + let configuration = GitHubConfiguration::builder() + .owner("jdno") + .repository("flowcrafter") + .build(); + + let yaml = indoc!( + r#" + owner: jdno + repository: flowcrafter + "# + ); + + assert_eq!(yaml, serde_yaml::to_string(&configuration).unwrap()); + } + + #[cfg(feature = "serde")] + #[test] + fn trait_serialize_with_custom_instance() { + let configuration = GitHubConfiguration::builder() + .instance(Url::parse("https://github.example.com").unwrap()) + .owner("jdno") + .repository("flowcrafter") + .build(); + + let yaml = indoc!( + r#" + instance: https://github.example.com/ + owner: jdno + repository: flowcrafter + "# + ); + + assert_eq!(yaml, serde_yaml::to_string(&configuration).unwrap()); + } + #[test] fn trait_sync() { fn assert_sync() {}