Skip to content

Commit

Permalink
feat(changelog): make remote data available in context (#703)
Browse files Browse the repository at this point in the history
  • Loading branch information
orhun authored Jun 15, 2024
1 parent b7e323f commit d14774a
Showing 1 changed file with 50 additions and 44 deletions.
94 changes: 50 additions & 44 deletions git-cliff-core/src/changelog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ impl<'a> Changelog<'a> {
};
changelog.process_commits();
changelog.process_releases();
changelog.add_remote_data()?;
Ok(changelog)
}

Expand Down Expand Up @@ -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<Option<String>> {
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<W: Write>(&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 {
Expand All @@ -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<Option<String>> {
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<W: Write>(&self, out: &mut W) -> Result<()> {
debug!("Generating changelog...");
let postprocessors = self
.config
.changelog
Expand All @@ -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
)?
);
Expand All @@ -459,7 +465,7 @@ impl<'a> Changelog<'a> {
&Releases {
releases: &releases,
},
Some(&additional_context),
Some(&self.additional_context),
&postprocessors,
)?
);
Expand Down

0 comments on commit d14774a

Please sign in to comment.