Skip to content

Commit

Permalink
Skip serializing default GitHub host (#88)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jdno authored Dec 12, 2023
1 parent e580986 commit b05b9f8
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
1 change: 0 additions & 1 deletion src/cli/configuration/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,6 @@ mod tests {
r#"
library:
github:
instance: https://api.github.com/
owner: jdno
repository: flowcrafter
workflows:
Expand Down
43 changes: 43 additions & 0 deletions src/github/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,

Expand Down Expand Up @@ -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::*;
Expand Down Expand Up @@ -101,6 +106,44 @@ mod tests {
assert_send::<GitHubConfiguration>();
}

#[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<T: Sync>() {}
Expand Down

0 comments on commit b05b9f8

Please sign in to comment.