Skip to content

Commit

Permalink
feat(remote): activate integration if remote is set manually (#782)
Browse files Browse the repository at this point in the history
  • Loading branch information
orhun authored Aug 4, 2024
1 parent 4b0c0eb commit 4b33e7e
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 36 deletions.
52 changes: 28 additions & 24 deletions git-cliff-core/src/changelog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,9 +206,9 @@ impl<'a> Changelog<'a> {
#[cfg(feature = "github")]
fn get_github_metadata(&self) -> Result<crate::remote::RemoteMetadata> {
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))
Expand Down Expand Up @@ -262,9 +262,9 @@ impl<'a> Changelog<'a> {
#[cfg(feature = "gitlab")]
fn get_gitlab_metadata(&self) -> Result<crate::remote::RemoteMetadata> {
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))
Expand Down Expand Up @@ -326,9 +326,9 @@ impl<'a> Changelog<'a> {
#[cfg(feature = "gitea")]
fn get_gitea_metadata(&self) -> Result<crate::remote::RemoteMetadata> {
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))
Expand Down Expand Up @@ -379,9 +379,9 @@ impl<'a> Changelog<'a> {
#[cfg(feature = "bitbucket")]
fn get_bitbucket_metadata(&self) -> Result<crate::remote::RemoteMetadata> {
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))
Expand Down Expand Up @@ -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(),
Expand Down
21 changes: 15 additions & 6 deletions git-cliff-core/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<SecretString>,
pub token: Option<SecretString>,
/// 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 {
Expand All @@ -162,9 +170,10 @@ impl Remote {
/// Constructs a new instance.
pub fn new<S: Into<String>>(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,
}
}

Expand Down
14 changes: 8 additions & 6 deletions git-cliff-core/src/repo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
}
}
Expand Down Expand Up @@ -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
);
Expand Down
12 changes: 12 additions & 0 deletions git-cliff/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 4b33e7e

Please sign in to comment.