From b59377a9851a93d7f47dcc8f74232ec8e1f043e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Orhun=20Parmaks=C4=B1z?= Date: Sun, 4 Aug 2024 17:59:14 +0300 Subject: [PATCH] feat(remote): activate integration if remote is set manually --- git-cliff-core/src/changelog.rs | 52 ++++++++++++++++++--------------- git-cliff-core/src/config.rs | 21 +++++++++---- git-cliff-core/src/repo.rs | 14 +++++---- git-cliff/src/lib.rs | 12 ++++++++ 4 files changed, 63 insertions(+), 36 deletions(-) diff --git a/git-cliff-core/src/changelog.rs b/git-cliff-core/src/changelog.rs index dd13670dbc..d15aed1b74 100644 --- a/git-cliff-core/src/changelog.rs +++ b/git-cliff-core/src/changelog.rs @@ -206,9 +206,9 @@ impl<'a> Changelog<'a> { #[cfg(feature = "github")] fn get_github_metadata(&self) -> Result { use crate::remote::github; - if self - .body_template - .contains_variable(github::TEMPLATE_VARIABLES) || + if self.config.remote.github.is_custom || + self.body_template + .contains_variable(github::TEMPLATE_VARIABLES) || self.footer_template .as_ref() .map(|v| v.contains_variable(github::TEMPLATE_VARIABLES)) @@ -262,9 +262,9 @@ impl<'a> Changelog<'a> { #[cfg(feature = "gitlab")] fn get_gitlab_metadata(&self) -> Result { use crate::remote::gitlab; - if self - .body_template - .contains_variable(gitlab::TEMPLATE_VARIABLES) || + if self.config.remote.gitlab.is_custom || + self.body_template + .contains_variable(gitlab::TEMPLATE_VARIABLES) || self.footer_template .as_ref() .map(|v| v.contains_variable(gitlab::TEMPLATE_VARIABLES)) @@ -326,9 +326,9 @@ impl<'a> Changelog<'a> { #[cfg(feature = "gitea")] fn get_gitea_metadata(&self) -> Result { use crate::remote::gitea; - if self - .body_template - .contains_variable(gitea::TEMPLATE_VARIABLES) || + if self.config.remote.gitea.is_custom || + self.body_template + .contains_variable(gitea::TEMPLATE_VARIABLES) || self.footer_template .as_ref() .map(|v| v.contains_variable(gitea::TEMPLATE_VARIABLES)) @@ -379,9 +379,9 @@ impl<'a> Changelog<'a> { #[cfg(feature = "bitbucket")] fn get_bitbucket_metadata(&self) -> Result { use crate::remote::bitbucket; - if self - .body_template - .contains_variable(bitbucket::TEMPLATE_VARIABLES) || + if self.config.remote.bitbucket.is_custom || + self.body_template + .contains_variable(bitbucket::TEMPLATE_VARIABLES) || self.footer_template .as_ref() .map(|v| v.contains_variable(bitbucket::TEMPLATE_VARIABLES)) @@ -791,24 +791,28 @@ mod test { }, remote: RemoteConfig { github: Remote { - owner: String::from("coolguy"), - repo: String::from("awesome"), - token: None, + owner: String::from("coolguy"), + repo: String::from("awesome"), + token: None, + is_custom: false, }, gitlab: Remote { - owner: String::from("coolguy"), - repo: String::from("awesome"), - token: None, + owner: String::from("coolguy"), + repo: String::from("awesome"), + token: None, + is_custom: false, }, gitea: Remote { - owner: String::from("coolguy"), - repo: String::from("awesome"), - token: None, + owner: String::from("coolguy"), + repo: String::from("awesome"), + token: None, + is_custom: false, }, bitbucket: Remote { - owner: String::from("coolguy"), - repo: String::from("awesome"), - token: None, + owner: String::from("coolguy"), + repo: String::from("awesome"), + token: None, + is_custom: false, }, }, bump: Bump::default(), diff --git a/git-cliff-core/src/config.rs b/git-cliff-core/src/config.rs index 0f34ef4e9d..a0454661a3 100644 --- a/git-cliff-core/src/config.rs +++ b/git-cliff-core/src/config.rs @@ -138,12 +138,20 @@ pub struct RemoteConfig { #[derive(Debug, Default, Clone, Serialize, Deserialize)] pub struct Remote { /// Owner of the remote. - pub owner: String, + pub owner: String, /// Repository name. - pub repo: String, + pub repo: String, /// Access token. #[serde(skip_serializing)] - pub token: Option, + pub token: Option, + /// Whether if the remote is set manually. + #[serde(skip_deserializing, default = "default_true")] + pub is_custom: bool, +} + +/// Returns `true` for serde's `default` attribute. +fn default_true() -> bool { + true } impl fmt::Display for Remote { @@ -162,9 +170,10 @@ impl Remote { /// Constructs a new instance. pub fn new>(owner: S, repo: S) -> Self { Self { - owner: owner.into(), - repo: repo.into(), - token: None, + owner: owner.into(), + repo: repo.into(), + token: None, + is_custom: false, } } diff --git a/git-cliff-core/src/repo.rs b/git-cliff-core/src/repo.rs index 6738ed960f..9e032e9376 100644 --- a/git-cliff-core/src/repo.rs +++ b/git-cliff-core/src/repo.rs @@ -244,9 +244,10 @@ impl Repository { (segments.get(1), segments.first()) { return Ok(Remote { - owner: owner.to_string(), - repo: repo.trim_end_matches(".git").to_string(), - token: None, + owner: owner.to_string(), + repo: repo.trim_end_matches(".git").to_string(), + token: None, + is_custom: false, }); } } @@ -360,9 +361,10 @@ mod test { let remote = repository.upstream_remote()?; assert_eq!( Remote { - owner: String::from("orhun"), - repo: String::from("git-cliff"), - token: None, + owner: String::from("orhun"), + repo: String::from("git-cliff"), + token: None, + is_custom: false, }, remote ); diff --git a/git-cliff/src/lib.rs b/git-cliff/src/lib.rs index 8627cc25e5..42f5f06a4c 100644 --- a/git-cliff/src/lib.rs +++ b/git-cliff/src/lib.rs @@ -115,6 +115,7 @@ fn process_repository<'a>( debug!("No GitHub remote is set, using remote: {}", remote); config.remote.github.owner = remote.owner; config.remote.github.repo = remote.repo; + config.remote.github.is_custom = remote.is_custom; } Err(e) => { debug!("Failed to get remote from GitHub repository: {:?}", e); @@ -126,6 +127,7 @@ fn process_repository<'a>( debug!("No GitLab remote is set, using remote: {}", remote); config.remote.gitlab.owner = remote.owner; config.remote.gitlab.repo = remote.repo; + config.remote.gitlab.is_custom = remote.is_custom; } Err(e) => { debug!("Failed to get remote from GitLab repository: {:?}", e); @@ -137,6 +139,7 @@ fn process_repository<'a>( debug!("No Gitea remote is set, using remote: {}", remote); config.remote.gitea.owner = remote.owner; config.remote.gitea.repo = remote.repo; + config.remote.gitea.is_custom = remote.is_custom; } Err(e) => { debug!("Failed to get remote from Gitea repository: {:?}", e); @@ -148,6 +151,7 @@ fn process_repository<'a>( debug!("No Bitbucket remote is set, using remote: {}", remote); config.remote.bitbucket.owner = remote.owner; config.remote.bitbucket.repo = remote.repo; + config.remote.bitbucket.is_custom = remote.is_custom; } Err(e) => { debug!("Failed to get remote from Bitbucket repository: {:?}", e); @@ -465,14 +469,22 @@ pub fn run(mut args: Opt) -> Result<()> { if let Some(ref remote) = args.github_repo { config.remote.github.owner = remote.0.owner.to_string(); config.remote.github.repo = remote.0.repo.to_string(); + config.remote.github.is_custom = true; } if let Some(ref remote) = args.gitlab_repo { config.remote.gitlab.owner = remote.0.owner.to_string(); config.remote.gitlab.repo = remote.0.repo.to_string(); + config.remote.gitlab.is_custom = true; } if let Some(ref remote) = args.bitbucket_repo { config.remote.bitbucket.owner = remote.0.owner.to_string(); config.remote.bitbucket.repo = remote.0.repo.to_string(); + config.remote.bitbucket.is_custom = true; + } + if let Some(ref remote) = args.gitea_repo { + config.remote.gitea.owner = remote.0.owner.to_string(); + config.remote.gitea.repo = remote.0.repo.to_string(); + config.remote.gitea.is_custom = true; } if args.no_exec { if let Some(ref mut preprocessors) = config.git.commit_preprocessors {