Skip to content

Commit

Permalink
Order releases by semver (#35)
Browse files Browse the repository at this point in the history
* Order releases by semver

* Update fixture

* ergh
  • Loading branch information
cidrblock authored May 15, 2023
1 parent b05cf38 commit 92e4ecb
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 3 deletions.
21 changes: 20 additions & 1 deletion antsichaut/antsichaut.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]],
Expand All @@ -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", {})
Expand Down
5 changes: 4 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"]
Expand Down
9 changes: 9 additions & 0 deletions tests/fixtures/changelogs/changelog.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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: []
26 changes: 25 additions & 1 deletion tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]

0 comments on commit 92e4ecb

Please sign in to comment.