diff --git a/antsichaut/antsichaut.py b/antsichaut/antsichaut.py index 45dc6c2..34b85b1 100755 --- a/antsichaut/antsichaut.py +++ b/antsichaut/antsichaut.py @@ -225,6 +225,24 @@ def remove_outdated( if url_found and not_full_match: del current_changes[change_type][idx] + @staticmethod + def _sort_by_semver(data: ChLogType) -> ChLogType: + """Sort releases by semver. + + :param data: The full changelog structure + :return: The sorted releases + """ + if not data or "releases" not in data: + return data + data["releases"] = OrderedDict( + sorted( + data["releases"].items(), + key=lambda t: [int(v) for v in t[0].split(".")], + reverse=True, + ), + ) + return data + def parse_changelog( # noqa: C901, PLR0912 self, changes: list[dict[str, str]], @@ -243,7 +261,8 @@ def parse_changelog( # noqa: C901, PLR0912 # get the new version from the changelog.yaml # by using the last item in the list of releases - new_version = list(dict(dict(data)["releases"]))[-1] + data = self._sort_by_semver(data) + new_version = list(data["releases"].keys())[0] # add changes-key to the release dict dict(data)["releases"][new_version].insert(0, "changes", {}) diff --git a/pyproject.toml b/pyproject.toml index 443763a..536e53d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -83,6 +83,8 @@ disable = [ "fixme", "too-few-public-methods", "unsubscriptable-object", + "protected-access", # covered by ruff + ] enable = [ "useless-suppression", # Identify unneeded pylint disable statements @@ -106,7 +108,8 @@ target-version = "py38" [tool.ruff.per-file-ignores] # S101 allow assert in tests -"tests/**" = ["S101"] +# SLF001 allow private access in tests +"tests/**" = ["S101", "SLF001"] [build-system] requires = ["poetry-core>=1.0.0"] diff --git a/tests/fixtures/changelogs/changelog.yaml b/tests/fixtures/changelogs/changelog.yaml index 4dc852e..e36ba6c 100644 --- a/tests/fixtures/changelogs/changelog.yaml +++ b/tests/fixtures/changelogs/changelog.yaml @@ -3,3 +3,12 @@ releases: 1.0.0: changes: trivial: [] + 1.0.10: + changes: + trivial: [] + 1.0.11: + changes: + trivial: [] + 1.0.2: + changes: + trivial: [] diff --git a/tests/test_basic.py b/tests/test_basic.py index a16eb86..47f81d3 100644 --- a/tests/test_basic.py +++ b/tests/test_basic.py @@ -99,11 +99,35 @@ def _write_changelog(self) -> None: yaml = YAML() change_log = yaml.load(cci.test_str_data) - trivial_changes = change_log["releases"]["1.0.0"]["changes"]["trivial"] assert cci.test_str_data assert all(s in cci.test_str_data for s in ("pull/10", "pull/11", "pull/12")) + + trivial_changes = change_log["releases"]["1.0.11"]["changes"]["trivial"] expected_number_of_entries = 3 assert len(trivial_changes) == expected_number_of_entries assert re.findall(r"pull/(\d+)", cci.test_str_data) == ["12", "11", "10"] + + +def test_sort_semver() -> None: + """Test sorting by semver.""" + cci = ChangelogCIBase( + repository=REPO, + since_version="0.0.0", + to_version="0.0.0", + group_config=GROUP_CONFIG, + ) + changelog = FIXTURE_DIR / "changelogs/changelog.yaml" + + yaml = YAML() + with changelog.open(encoding="utf-8") as file: + data = yaml.load(file) + + original = list(data["releases"].keys()) + + data = cci._sort_by_semver(data) + revised = list(data["releases"].keys()) + + assert original != revised + assert revised == ["1.0.11", "1.0.10", "1.0.2", "1.0.0"]