Skip to content

Commit

Permalink
fix: skip unparseable versions when calculating next version (#649)
Browse files Browse the repository at this point in the history
  • Loading branch information
bernardcooke53 committed Jul 21, 2023
1 parent 8950c52 commit 88f25ea
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 9 deletions.
2 changes: 2 additions & 0 deletions docs/algorithm.rst
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ Implementation

1. Parse all the Git tags of the repository into semantic versions, and **sort**
in descending (most recent first) order according to `semver precedence`_.
Ignore any tags which do not correspond to valid semantic vesrions accroding
to ``tag_format``.


2. Find the ``merge-base`` of HEAD and the latest tag according to the sort above.
Expand Down
6 changes: 6 additions & 0 deletions docs/multibranch_releases.rst
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ but when merged to the branch configured to produce full releases (``main``), if
released separately the changes from each branch would be released in two versions
that would be considered different according to the semver specification.

.. note::

If you have tags in your Git repository that are not valid semantic versions
(which have then been formatted into your :ref:`tag_format <config-tag-format>`),
these tags will be ignored for the purposes of calculating the next version.

.. _prereleases: https://semver.org/#spec-item-9
.. _build metadata MUST be ignored: https://semver.org/#spec-item-10

Expand Down
1 change: 1 addition & 0 deletions semantic_release/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from semantic_release.errors import (
CommitParseError as CommitParseError,
InvalidConfiguration as InvalidConfiguration,
InvalidVersion as InvalidVersion,
SemanticReleaseBaseError as SemanticReleaseBaseError,
)
from semantic_release.version import (
Expand Down
7 changes: 7 additions & 0 deletions semantic_release/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ class InvalidConfiguration(SemanticReleaseBaseError):
"""


class InvalidVersion(ValueError, SemanticReleaseBaseError):
"""
Raised when Version.parse attempts to parse a string containing
an invalid version.
"""


class NotAReleaseBranch(InvalidConfiguration):
"""
Raised when semantic_release is invoked on a branch which isn't configured for
Expand Down
3 changes: 2 additions & 1 deletion semantic_release/version/algorithm.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
)
from semantic_release.const import DEFAULT_VERSION
from semantic_release.enums import LevelBump
from semantic_release.errors import InvalidVersion
from semantic_release.version.translator import VersionTranslator
from semantic_release.version.version import Version

Expand All @@ -40,7 +41,7 @@ def tags_and_versions(
for tag in tags:
try:
version = translator.from_tag(tag.name)
except NotImplementedError as e:
except (NotImplementedError, InvalidVersion) as e:
log.warning(
"Couldn't parse tag %s as as Version: %s",
tag.name,
Expand Down
8 changes: 1 addition & 7 deletions semantic_release/version/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,12 @@

from semantic_release.const import SEMVER_REGEX
from semantic_release.enums import LevelBump
from semantic_release.errors import InvalidVersion
from semantic_release.helpers import check_tag_format

log = logging.getLogger(__name__)


class InvalidVersion(ValueError):
"""
Raised when Version.parse attempts to parse a string containing
an invalid version.
"""


# Very heavily inspired by semver.version:_comparator, I don't think there's
# a cleaner way to do this
# https://github.com/python-semver/python-semver/blob/b5317af9a7e99e6a86df98320e73be72d5adf0de/src/semver/version.py#L32
Expand Down
12 changes: 12 additions & 0 deletions tests/unit/semantic_release/version/test_algorithm.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,18 @@ def test_sorted_repo_tags_and_versions(tags, sorted_tags):
"v1.0.0",
],
),
(
"v{version}",
("0.3", "0.4"),
[
"v1.0.0-rc.1",
"v1.0.0-beta.2",
"v1.0.0-beta.11",
"v1.0.0-alpha.1",
"v1.0.0-alpha.beta.1",
"v1.0.0",
],
),
(
r"(\w+--)?v{version}",
("v1.1.0-test-test", "test_v1.1.0"),
Expand Down
3 changes: 2 additions & 1 deletion tests/unit/semantic_release/version/test_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
import pytest

from semantic_release.enums import LevelBump
from semantic_release.version.version import InvalidVersion, Version
from semantic_release.errors import InvalidVersion
from semantic_release.version.version import Version

random.seed(0)

Expand Down

0 comments on commit 88f25ea

Please sign in to comment.