From b6682fba6f1a1a9e675a69b0a381f4a1f8c1ee65 Mon Sep 17 00:00:00 2001 From: bennetrr Date: Fri, 12 Apr 2024 13:54:04 +0200 Subject: [PATCH] fix: previous version and tag creation --- src/app.py | 45 +++++++++++------------------ tests/test_two_versions.py | 59 ++++++++++++++++++++++---------------- 2 files changed, 51 insertions(+), 53 deletions(-) diff --git a/src/app.py b/src/app.py index 09da55e..91d6d4f 100644 --- a/src/app.py +++ b/src/app.py @@ -293,46 +293,40 @@ def get_new_version( repo = git.Repo(os.getcwd()) # Previous version is the latest version that was made, possibly on another branch / channel. # It is used to get the next version. - previous_version_tag = get_current_version_tag(repo, prefix, reference_version_suffix, bumping_suffix) + reference_version_tag = get_current_version_tag(repo, prefix, reference_version_suffix, bumping_suffix) # Current version is the latest version on this branch / channel. # This is the version returned as previous version at the end of the script. current_version_tag = get_current_version_tag(repo, prefix, suffix, bumping_suffix) + current_version_tag_name = current_version_tag.name if current_version_tag is not None else None - if previous_version_tag is None: - previous_version: str | None = None + if reference_version_tag is None: + reference_version: str | None = None else: - previous_version = previous_version_tag.name.removeprefix(prefix) + reference_version = reference_version_tag.name.removeprefix(prefix) if reference_version_suffix is not None: - previous_version = previous_version.replace(f'-{reference_version_suffix}', '', 1) + reference_version = reference_version.replace(f'-{reference_version_suffix}', '', 1) - if current_version_tag is None: - current_version: str | None = None - else: - current_version = current_version_tag.name.removeprefix(prefix) - if reference_version_suffix is not None: - current_version = current_version.replace(f'-{suffix}', '', 1) - - next_version, version_bumped = get_next_version_from_commits(repo, previous_version_tag, previous_version) + next_version, version_bumped = get_next_version_from_commits(repo, reference_version_tag, reference_version) logger.debug( 'current_version=%s, next_version=%s, version_bumped=%s', - previous_version, next_version, version_bumped + reference_version, next_version, version_bumped ) # Example case: No change that requires a semantic version increase if not version_bumped: logger.info('No changes detected, version stays the same.') - return current_version, next_version, version_bumped + return current_version_tag_name, next_version, version_bumped # Example case: Hotfix if only_bump_suffix: logger.info('Only the suffix will be incremented.') - return current_version, increment_suffix(previous_version, bumping_suffix), version_bumped + return current_version_tag_name, increment_suffix(reference_version, bumping_suffix), version_bumped # Example case: New Release logger.info('Semantic Version will be incremented.') - return current_version, next_version, version_bumped + return current_version_tag_name, next_version, version_bumped def get_new_version_hash_based( @@ -452,12 +446,12 @@ def main() -> None: # endregion if args.mode == 'hash-based': - previous_version, new_version, version_bumped = get_new_version_hash_based( + previous_version_tag_name, new_version, version_bumped = get_new_version_hash_based( args.prefix, args.reference_version_suffix ) else: - previous_version, new_version, version_bumped = get_new_version( + previous_version_tag_name, new_version, version_bumped = get_new_version( args.prefix, args.suffix, args.reference_version_suffix, @@ -465,12 +459,6 @@ def main() -> None: args.only_bump_suffix ) - if args.reference_version_suffix is not None and previous_version is not None: - if '-' in previous_version: - previous_version = previous_version.replace('-', f'-{args.reference_version_suffix}-', 1) - else: - previous_version += f'-{args.reference_version_suffix}' - if args.suffix is not None: if '-' in new_version: new_version = new_version.replace('-', f'-{args.suffix}-', 1) @@ -478,9 +466,8 @@ def main() -> None: new_version += f'-{args.suffix}' new_version_tag_name = f'{args.prefix}{new_version}' - previous_version_tag_name = f'{args.prefix}{previous_version}' if previous_version else '' - new_tag_needed = (version_bumped or ('0.0.0' not in new_version and previous_version != new_version)) + new_tag_needed = (version_bumped or ('0.0.0' not in new_version_tag_name and previous_version_tag_name != new_version_tag_name)) if args.create_tag and new_tag_needed: create_tag(new_version_tag_name) @@ -490,8 +477,8 @@ def main() -> None: set_github_output('version', new_version) set_github_output('version-name', new_version_tag_name) - set_github_output('previous-version', previous_version or '') - set_github_output('previous-version-name', previous_version_tag_name) + set_github_output('previous-version', (previous_version_tag_name or '').removeprefix(args.prefix)) + set_github_output('previous-version-name', previous_version_tag_name or '') set_github_output('tag-created', str(new_tag_needed).lower()) print_github_actions_output() diff --git a/tests/test_two_versions.py b/tests/test_two_versions.py index 5f50f8b..bd05531 100644 --- a/tests/test_two_versions.py +++ b/tests/test_two_versions.py @@ -671,8 +671,8 @@ def test_fix_then_chore(self) -> None: expected_output_chore_release = ActionOutputs( version='0.0.1-pre', version_name='v0.0.1-pre', - previous_version='', - previous_version_name='', + previous_version='0.0.1-pre', + previous_version_name='v0.0.1-pre', tag_created=False ) @@ -686,8 +686,8 @@ def test_fix_then_chore(self) -> None: expected_output_chore_beta = ActionOutputs( version='0.0.1-beta', version_name='v0.0.1-beta', - previous_version='', - previous_version_name='', + previous_version='0.0.1-beta', + previous_version_name='v0.0.1-beta', tag_created=False ) @@ -701,8 +701,8 @@ def test_fix_then_chore(self) -> None: expected_output_chore_prod = ActionOutputs( version='0.0.1', version_name='v0.0.1', - previous_version='', - previous_version_name='', + previous_version='0.0.1', + previous_version_name='v0.0.1', tag_created=False ) @@ -751,10 +751,10 @@ def test_fix_then_chore(self) -> None: # Chore self.assertEqual(expected_output_chore_release, actual_output_chore_release) - self.assertEqual(expected_output_fix_release.version_name, tag_chore_release) + self.assertEqual(expected_output_fix_prod.version_name, tag_chore_release) self.assertEqual(expected_output_chore_beta, actual_output_chore_beta) - self.assertEqual(expected_output_fix_beta.version_name, tag_chore_beta) + self.assertEqual(expected_output_fix_prod.version_name, tag_chore_beta) self.assertEqual(expected_output_chore_prod, actual_output_chore_prod) self.assertEqual(expected_output_fix_prod.version_name, tag_chore_prod) @@ -871,6 +871,7 @@ def test_fix_then_fix(self) -> None: tag_fix1_prod = self.repo.get_latest_tag_name() # Fix 2 + self.repo.checkout('main') self.repo.commit(CommitMessages.FIX) self.repo.merge('main', 'release') @@ -1018,6 +1019,7 @@ def test_fix_then_feat(self) -> None: tag_fix_prod = self.repo.get_latest_tag_name() # Feature + self.repo.checkout('main') self.repo.commit(CommitMessages.FEATURE) self.repo.merge('main', 'release') @@ -1165,6 +1167,7 @@ def test_fix_then_breaking(self) -> None: tag_fix_prod = self.repo.get_latest_tag_name() # Breaking + self.repo.checkout('main') self.repo.commit(CommitMessages.BREAKING_FEATURE) self.repo.merge('main', 'release') @@ -1260,8 +1263,8 @@ def test_feat_then_chore(self) -> None: expected_output_chore_release = ActionOutputs( version='0.1.0-pre', version_name='v0.1.0-pre', - previous_version='', - previous_version_name='', + previous_version='0.1.0-pre', + previous_version_name='v0.1.0-pre', tag_created=False ) @@ -1275,8 +1278,8 @@ def test_feat_then_chore(self) -> None: expected_output_chore_beta = ActionOutputs( version='0.1.0-beta', version_name='v0.1.0-beta', - previous_version='', - previous_version_name='', + previous_version='0.1.0-beta', + previous_version_name='v0.1.0-beta', tag_created=False ) @@ -1290,8 +1293,8 @@ def test_feat_then_chore(self) -> None: expected_output_chore_prod = ActionOutputs( version='0.1.0', version_name='v0.1.0', - previous_version='', - previous_version_name='', + previous_version='0.1.0', + previous_version_name='v0.1.0', tag_created=False ) @@ -1312,6 +1315,7 @@ def test_feat_then_chore(self) -> None: tag_feat_prod = self.repo.get_latest_tag_name() # Chore + self.repo.checkout('main') self.repo.commit(CommitMessages.CHORE) self.repo.merge('main', 'release') @@ -1339,10 +1343,10 @@ def test_feat_then_chore(self) -> None: # Chore self.assertEqual(expected_output_chore_release, actual_output_chore_release) - self.assertEqual(expected_output_feat_release.version_name, tag_chore_release) + self.assertEqual(expected_output_feat_prod.version_name, tag_chore_release) self.assertEqual(expected_output_chore_beta, actual_output_chore_beta) - self.assertEqual(expected_output_feat_beta.version_name, tag_chore_beta) + self.assertEqual(expected_output_feat_prod.version_name, tag_chore_beta) self.assertEqual(expected_output_chore_prod, actual_output_chore_prod) self.assertEqual(expected_output_feat_prod.version_name, tag_chore_prod) @@ -1459,6 +1463,7 @@ def test_feat_then_fix(self) -> None: tag_feat_prod = self.repo.get_latest_tag_name() # Fix + self.repo.checkout('main') self.repo.commit(CommitMessages.FIX) self.repo.merge('main', 'release') @@ -1606,6 +1611,7 @@ def test_feat_then_feat(self) -> None: tag_feat1_prod = self.repo.get_latest_tag_name() # Feature 2 + self.repo.checkout('main') self.repo.commit(CommitMessages.FEATURE) self.repo.merge('main', 'release') @@ -1753,6 +1759,7 @@ def test_feat_then_breaking(self) -> None: tag_feat_prod = self.repo.get_latest_tag_name() # Breaking + self.repo.checkout('main') self.repo.commit(CommitMessages.BREAKING_FEATURE) self.repo.merge('main', 'release') @@ -1848,8 +1855,8 @@ def test_breaking_then_chore(self) -> None: expected_output_chore_release = ActionOutputs( version='1.0.0-pre', version_name='v1.0.0-pre', - previous_version='', - previous_version_name='', + previous_version='1.0.0-pre', + previous_version_name='v1.0.0-pre', tag_created=False ) @@ -1863,8 +1870,8 @@ def test_breaking_then_chore(self) -> None: expected_output_chore_beta = ActionOutputs( version='1.0.0-beta', version_name='v1.0.0-beta', - previous_version='', - previous_version_name='', + previous_version='1.0.0-beta', + previous_version_name='v1.0.0-beta', tag_created=False ) @@ -1878,8 +1885,8 @@ def test_breaking_then_chore(self) -> None: expected_output_chore_prod = ActionOutputs( version='1.0.0', version_name='v1.0.0', - previous_version='', - previous_version_name='', + previous_version='1.0.0', + previous_version_name='v1.0.0', tag_created=False ) @@ -1900,6 +1907,7 @@ def test_breaking_then_chore(self) -> None: tag_breaking_prod = self.repo.get_latest_tag_name() # Chore + self.repo.checkout('main') self.repo.commit(CommitMessages.CHORE) self.repo.merge('main', 'release') @@ -1927,10 +1935,10 @@ def test_breaking_then_chore(self) -> None: # Chore self.assertEqual(expected_output_chore_release, actual_output_chore_release) - self.assertEqual(expected_output_breaking_release.version_name, tag_chore_release) + self.assertEqual(expected_output_breaking_prod.version_name, tag_chore_release) self.assertEqual(expected_output_chore_beta, actual_output_chore_beta) - self.assertEqual(expected_output_breaking_beta.version_name, tag_chore_beta) + self.assertEqual(expected_output_breaking_prod.version_name, tag_chore_beta) self.assertEqual(expected_output_chore_prod, actual_output_chore_prod) self.assertEqual(expected_output_breaking_prod.version_name, tag_chore_prod) @@ -2047,6 +2055,7 @@ def test_breaking_then_fix(self) -> None: tag_breaking_prod = self.repo.get_latest_tag_name() # Fix + self.repo.checkout('main') self.repo.commit(CommitMessages.FIX) self.repo.merge('main', 'release') @@ -2194,6 +2203,7 @@ def test_breaking_then_feat(self) -> None: tag_breaking_prod = self.repo.get_latest_tag_name() # Feature + self.repo.checkout('main') self.repo.commit(CommitMessages.FEATURE) self.repo.merge('main', 'release') @@ -2341,6 +2351,7 @@ def test_breaking_then_breaking(self) -> None: tag_breaking1_prod = self.repo.get_latest_tag_name() # Breaking 2 + self.repo.checkout('main') self.repo.commit(CommitMessages.BREAKING_FEATURE) self.repo.merge('main', 'release')