8000 fix(bump): bump don't throw error on no bump types commits by Wassim-AB · Pull Request #271 · cocogitto/cocogitto · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

fix(bump): bump don't throw error on no bump types commits #271

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

Merged
merged 1 commit into from
May 11, 2023
Merged
Show file tree
Hide file tree
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
3 changes: 1 addition & 2 deletions src/command/bump/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,10 @@ impl<'a> HookRunOptions<'a> {
}

fn ensure_tag_is_greater_than_previous(current: &Tag, next: &Tag) -> Result<()> {
if next <= current {
if next < current {
let comparison = format!("{current} <= {next}").red();
let cause_key = "cause:".red();
let cause = format!("{cause_key} version MUST be greater than current one: {comparison}");

bail!("{}:\n\t{}\n", "SemVer Error".red().to_string(), cause);
};

Expand Down
14 changes: 14 additions & 0 deletions src/command/bump/monorepo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ impl CocoGitto {
// Get package bumps
let bumps = self.get_packages_bumps(pre_release)?;

if bumps.is_empty() {
print!("No conventional commits found for your packages that required a bump. Changelogs will be updated on the next bump.\nPre-Hooks and Post-Hooks have been skiped.\n");
return Ok(());
}

if dry_run {
for bump in bumps {
println!("{}", bump.new_version.prefixed_tag)
Expand Down Expand Up @@ -131,6 +136,11 @@ impl CocoGitto {
// Get package bumps
let bumps = self.get_packages_bumps(pre_release)?;

if bumps.is_empty() {
print!("No conventional commits found for your packages that required a bump. Changelogs will be updated on the next bump.\nPre-Hooks and Post-Hooks have been skiped.\n");
return Ok(());
}

// Get the greatest package increment among public api packages
let increment_from_package_bumps = bumps
.iter()
Expand Down Expand Up @@ -392,6 +402,10 @@ impl CocoGitto {

let mut next_version = next_version.unwrap();

if next_version == old {
continue;
}

if let Some(pre_release) = pre_release {
next_version.version.pre = Prerelease::new(pre_release)?;
}
Expand Down
6 changes: 6 additions & 0 deletions src/command/bump/package.rs
8000
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,13 @@ impl CocoGitto {
let current_tag = self.repository.get_latest_package_tag(package_name);
let current_tag = tag_or_fallback_to_zero(current_tag)?;
let mut next_version = current_tag.bump(increment, &self.repository)?;
if current_tag == next_version {
print!("No conventional commits found for {package_name} that required a bump. Changelog will be updated on the next bump.\nPre-Hooks and Post-Hooks have been skiped.\n");
return Ok(());
}

ensure_tag_is_greater_than_previous(&current_tag, &next_version)?;

if let Some(pre_release) = pre_release {
next_version.version.pre = Prerelease::new(pre_release)?;
}
Expand Down
4 changes: 4 additions & 0 deletions src/command/bump/standard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ impl CocoGitto {
let current_tag = self.repository.get_latest_tag();
let current_tag = tag_or_fallback_to_zero(current_tag)?;
let mut tag = current_tag.bump(increment, &self.repository)?;
if current_tag == tag {
print!("No conventional commits for your repository that required a bump. Changelogs will be updated on the next bump.\nPre-Hooks and Post-Hooks have been skiped.\n");
return Ok(());
}

ensure_tag_is_greater_than_previous(&current_tag, &tag)?;

Expand Down
84 changes: 68 additions & 16 deletions src/conventional/bump.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ pub(crate) trait Bump {
fn major_bump(&self) -> Self;
fn minor_bump(&self) -> Self;
fn patch_bump(&self) -> Self;
fn no_bump(&self) -> Self;
fn auto_bump(&self, repository: &Repository) -> Result<Self, BumpError>
where
Self: Sized;
Expand Down Expand Up @@ -66,6 +67,11 @@ impl Bump for Tag {
next.reset_metadata()
}

fn no_bump(&self) -> Self {
let next = self.clone();
next.reset_metadata()
}

fn auto_bump(&self, repository: &Repository) -> Result<Self, BumpError> {
self.get_version_from_commit_history(repository)
}
Expand Down Expand Up @@ -111,6 +117,7 @@ impl Tag {
IncrementCommand::Major => Ok(self.major_bump()),
IncrementCommand::Minor => Ok(self.minor_bump()),
IncrementCommand::Patch => Ok(self.patch_bump()),
IncrementCommand::NoBump => Ok(self.no_bump()),
IncrementCommand::Auto => self.auto_bump(repository),
IncrementCommand::AutoPackage(package) => self.auto_package_bump(repository, &package),
IncrementCommand::AutoMonoRepoGlobal(package_increment) => {
Expand Down Expand Up @@ -160,6 +167,7 @@ impl Tag {
Increment::Major => self.major_bump(),
Increment::Minor => self.minor_bump(),
Increment::Patch => self.patch_bump(),
Increment::NoBump => self.no_bump(),
})
}

Expand Down Expand Up @@ -201,6 +209,7 @@ impl Tag {
Increment::Major => self.major_bump(),
Increment::Minor => self.minor_bump(),
Increment::Patch => self.patch_bump(),
Increment::NoBump => self.no_bump(),
})
}

Expand Down Expand Up @@ -241,6 +250,7 @@ impl Tag {
Increment::Major => self.major_bump(),
Increment::Minor => self.minor_bump(),
Increment::Patch => self.patch_bump(),
Increment::NoBump => self.no_bump(),
})
}

Expand All @@ -267,12 +277,18 @@ impl Tag {
.any(|commit| commit.message.commit_type == CommitType::BugFix)
};

// At this point, it is not a major, minor or patch bump but we might have found conventional commits
// -> Must be only chore, docs, refactor ... which means commits that don't require bump but shouldn't throw error
let no_bump_required = !commits.is_empty();

if is_major_bump() {
Ok(Increment::Major)
} else if is_minor_bump() {
Ok(Increment::Minor)
} else if is_patch_bump() {
Ok(Increment::Patch)
} else if no_bump_required {
Ok(Increment::NoBump)
} else {
Err(BumpError::NoCommitFound)
}
Expand Down Expand Up @@ -359,6 +375,20 @@ mod test {
Ok(())
}

#[sealed_test]
fn no_bump() -> Result<()> {
// Arrange
let repository = Repository::init(".")?;
let base_version = Tag::from_str("1.0.0", None)?;

// Act
let tag = base_version.bump(IncrementCommand::NoBump, &repository)?;

// Assert
assert_that!(tag.version).is_equal_to(Version::new(1, 0, 0));
Ok(())
}

#[test]
fn should_get_next_auto_version_patch() -> Result<()> {
// Arrange
Expand All @@ -376,6 +406,42 @@ mod test {
Ok(())
}

#[test]
fn should_not_bump_versions_due_to_non_bump_commits() -> Result<()> {
// Arrange
let revert = Commit::commit_fixture(CommitType::Revert, false);
let perf = Commit::commit_fixture(CommitType::Performances, false);
let documentation = Commit::commit_fixture(CommitType::Documentation, false);
let chore = Commit::commit_fixture(CommitType::Chore, false);
let style = Commit::commit_fixture(CommitType::Style, false);
let refactor = Commit::commit_fixture(CommitType::Refactor, false);
let test = Commit::commit_fixture(CommitType::Test, false);
let build = Commit::commit_fixture(CommitType::Build, false);
let ci = Commit::commit_fixture(CommitType::Ci, false);

let base_version = Tag::from_str("1.0.0", None)?;

// Act
let increment = base_version.version_increment_from_commit_history(&[
revert,
perf,
documentation,
chore,
style,
refactor,
test,
build,
ci,
]);

// Assert
assert_that!(increment)
.is_ok()
.is_equal_to(Increment::NoBump);

Ok(())
}

#[test]
fn increment_minor_version_should_set_patch_to_zero() -> Result<()> {
// Arrange
Expand Down Expand Up @@ -472,7 +538,7 @@ mod test {
}

#[test]
fn should_fail_without_feature_bug_fix_or_breaking_change_commit() -> Result<()> {
fn should_not_fail_without_feature_bug_fix_or_breaking_change_commit() -> Result<()> {
// Arrange
let chore = Commit::commit_fixture(CommitType::Chore, false);
let docs = Commit::commit_fixture(CommitType::Documentation, false);
Expand All @@ -482,21 +548,7 @@ mod test {
let version = base_version.version_increment_from_commit_history(&[chore, docs]);

// Assert
let result = version.unwrap_err().to_string();
let result = result.as_str();

assert_eq!(
result,
r#"failed to bump version

cause: No conventional commit found to b 10B25 ump current version.
Only feature, bug fix and breaking change commits will trigger an automatic bump.

suggestion: Please see https://conventionalcommits.org/en/v1.0.0/#summary for more information.
Alternatively consider using `cog bump <--version <VERSION>|--auto|--major|--minor>`

"#
);
assert_that!(version).is_ok().is_equal_to(Increment::NoBump);

Ok(())
}
Expand Down
6 changes: 6 additions & 0 deletions src/conventional/version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pub enum IncrementCommand {
Minor,
Patch,
Auto,
NoBump,
AutoPackage(String),
AutoMonoRepoGlobal(Option<Increment>),
Manual(String),
Expand All @@ -16,6 +17,7 @@ pub enum Increment {
Major,
Minor,
Patch,
NoBump,
}

impl From<Increment> for IncrementCommand {
Expand All @@ -24,6 +26,7 @@ impl From<Increment> for IncrementCommand {
Increment::Major => IncrementCommand::Major,
Increment::Minor => IncrementCommand::Minor,
Increment::Patch => IncrementCommand::Patch,
Increment::NoBump => IncrementCommand::NoBump,
}
}
}
Expand All @@ -43,6 +46,9 @@ impl PartialOrd for Increment {
(Increment::Minor, _) => Some(Ordering::Greater),
(_, Increment::Minor) => Some(Ordering::Less),
(Increment::Patch, Increment::Patch) => Some(Ordering::Equal),
(Increment::NoBump, Increment::NoBump) => Some(Ordering::Equal),
(Increment::Patch, Increment::NoBump) => Some(Ordering::Greater),
(Increment::NoBump, Increment::Patch) => Some(Ordering::Less),
}
}
}
Expand Down
Loading
0