diff --git a/git-cliff-core/src/changelog.rs b/git-cliff-core/src/changelog.rs index 511088b72f..a5c9f24b39 100644 --- a/git-cliff-core/src/changelog.rs +++ b/git-cliff-core/src/changelog.rs @@ -57,6 +57,7 @@ impl<'a> Changelog<'a> { }; changelog.process_commits(); changelog.process_releases(); + changelog.add_remote_data()?; Ok(changelog) } @@ -355,43 +356,22 @@ impl<'a> Changelog<'a> { } } - /// Increments the version for the unreleased changes based on semver. - pub fn bump_version(&mut self) -> Result> { - if let Some(ref mut last_release) = self.releases.iter_mut().next() { - if last_release.version.is_none() { - let next_version = last_release - .calculate_next_version_with_config(&self.config.bump)?; - debug!("Bumping the version to {next_version}"); - last_release.version = Some(next_version.to_string()); - last_release.timestamp = SystemTime::now() - .duration_since(UNIX_EPOCH)? - .as_secs() - .try_into()?; - return Ok(Some(next_version)); - } - } - Ok(None) - } - - /// Generates the changelog and writes it to the given output. - pub fn generate(&self, out: &mut W) -> Result<()> { - debug!("Generating changelog..."); - let mut additional_context = self.additional_context.clone(); - additional_context.insert( + /// Adds remote data (e.g. GitHub commits) to the releases. + pub fn add_remote_data(&mut self) -> Result<()> { + debug!("Adding remote data..."); + self.additional_context.insert( "remote".to_string(), serde_json::to_value(self.config.remote.clone())?, ); #[cfg(feature = "github")] - let (github_commits, github_pull_requests) = if self.config.remote.github.is_set() - { + let (github_commits, github_pull_requests) = if self.config.remote.github.is_set() { self.get_github_metadata() .expect("Could not get github metadata") } else { (vec![], vec![]) }; #[cfg(feature = "gitlab")] - let (gitlab_commits, gitlab_merge_request) = if self.config.remote.gitlab.is_set() - { + let (gitlab_commits, gitlab_merge_request) = if self.config.remote.gitlab.is_set() { self.get_gitlab_metadata() .expect("Could not get gitlab metadata") } else { @@ -405,6 +385,47 @@ impl<'a> Changelog<'a> { } else { (vec![], vec![]) }; + for release in self.releases.iter_mut() { + #[cfg(feature = "github")] + release.update_github_metadata( + github_commits.clone(), + github_pull_requests.clone(), + )?; + #[cfg(feature = "gitlab")] + release.update_gitlab_metadata( + gitlab_commits.clone(), + gitlab_merge_request.clone(), + )?; + #[cfg(feature = "bitbucket")] + release.update_bitbucket_metadata( + bitbucket_commits.clone(), + bitbucket_pull_request.clone(), + )?; + } + Ok(()) + } + + /// Increments the version for the unreleased changes based on semver. + pub fn bump_version(&mut self) -> Result> { + if let Some(ref mut last_release) = self.releases.iter_mut().next() { + if last_release.version.is_none() { + let next_version = last_release + .calculate_next_version_with_config(&self.config.bump)?; + debug!("Bumping the version to {next_version}"); + last_release.version = Some(next_version.to_string()); + last_release.timestamp = SystemTime::now() + .duration_since(UNIX_EPOCH)? + .as_secs() + .try_into()?; + return Ok(Some(next_version)); + } + } + Ok(None) + } + + /// Generates the changelog and writes it to the given output. + pub fn generate(&self, out: &mut W) -> Result<()> { + debug!("Generating changelog..."); let postprocessors = self .config .changelog @@ -421,27 +442,12 @@ impl<'a> Changelog<'a> { } let mut releases = self.releases.clone(); for release in releases.iter_mut() { - #[cfg(feature = "github")] - release.update_github_metadata( - github_commits.clone(), - github_pull_requests.clone(), - )?; - #[cfg(feature = "gitlab")] - release.update_gitlab_metadata( - gitlab_commits.clone(), - gitlab_merge_request.clone(), - )?; - #[cfg(feature = "bitbucket")] - release.update_bitbucket_metadata( - bitbucket_commits.clone(), - bitbucket_pull_request.clone(), - )?; let write_result = write!( out, "{}", self.body_template.render( &release, - Some(&additional_context), + Some(&self.additional_context), &postprocessors )? ); @@ -459,7 +465,7 @@ impl<'a> Changelog<'a> { &Releases { releases: &releases, }, - Some(&additional_context), + Some(&self.additional_context), &postprocessors, )? );