Skip to content

fix: validate bumped version against tag pattern #1137

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions git-cliff-core/src/changelog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,18 @@ impl<'a> Changelog<'a> {
if last_release.version.is_none() {
let next_version = last_release
.calculate_next_version_with_config(&self.config.bump)?;

// Validate against tag pattern if configured
if let Some(tag_pattern) = &self.config.git.tag_pattern {
if !tag_pattern.is_match(&next_version) {
return Err(Error::ChangelogError(format!(
"Bumped version '{}' does not match configured tag pattern '{}'",
next_version,
tag_pattern
)));
Comment on lines +584 to +588
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return Err(Error::ChangelogError(format!(
"Bumped version '{}' does not match configured tag pattern '{}'",
next_version,
tag_pattern
)));
return Err(Error::ChangelogError(format!(
"Bumped version '{next_version}' does not match \
configured tag pattern '{tag_version}'",
)));

}
}

debug!("Bumping the version to {next_version}");
last_release.version = Some(next_version.to_string());
last_release.timestamp = SystemTime::now()
Expand Down Expand Up @@ -1428,4 +1440,35 @@ chore(deps): fix broken deps
.assert_eq(str::from_utf8(&out).unwrap_or_default());
Ok(())
}

#[test]
fn test_bump_version_with_tag_pattern() -> Result<()> {
let (mut config, releases) = get_test_data();

// Configure a tag pattern that won't match the default version
config.git.tag_pattern = Some(Regex::new("foo-[0-9]+\\.[0-9]+\\.[0-9]+").unwrap());

let mut changelog = Changelog::new(releases.clone(), &config, None)?;

// Test that bumping version fails when it doesn't match the pattern
match changelog.bump_version() {
Err(Error::ChangelogError(msg)) => {
assert!(msg.contains("does not match configured tag pattern"));
}
other => panic!("Expected ChangelogError, got {:?}", other),
}

// Now test with a pattern that will match
config.git.tag_pattern = Some(Regex::new("v[0-9]+\\.[0-9]+\\.[0-9]+").unwrap());
let mut changelog = Changelog::new(releases, &config, None)?;

// This should succeed
if let Ok(Some(version)) = changelog.bump_version() {
assert!(config.git.tag_pattern.as_ref().unwrap().is_match(&version));
} else {
panic!("Expected successful version bump");
}

Ok(())
}
}
Loading