Skip to content

Commit

Permalink
fix: previous version and tag creation
Browse files Browse the repository at this point in the history
  • Loading branch information
bennetrr committed Apr 12, 2024
1 parent df37278 commit b6682fb
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 53 deletions.
45 changes: 16 additions & 29 deletions src/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -452,35 +446,28 @@ 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,
args.bumping_suffix,
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)
else:
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)
Expand All @@ -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()
Expand Down
59 changes: 35 additions & 24 deletions tests/test_two_versions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

Expand All @@ -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
)

Expand All @@ -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
)

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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
)

Expand All @@ -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
)

Expand All @@ -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
)

Expand All @@ -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')
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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
)

Expand All @@ -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
)

Expand All @@ -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
)

Expand All @@ -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')
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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')
Expand Down

0 comments on commit b6682fb

Please sign in to comment.