diff --git a/sphinx_github_changelog/changelog.py b/sphinx_github_changelog/changelog.py index 35e5380..5aff59f 100644 --- a/sphinx_github_changelog/changelog.py +++ b/sphinx_github_changelog/changelog.py @@ -99,6 +99,10 @@ def extract_pypi_package_name(url: Optional[str]) -> Optional[str]: return stripped_url[len(prefix) :] # noqa +def get_release_title(title: Optional[str], tag: str): + if not title: + return tag + return title if tag in title else f"{tag}: {title}" def node_for_release( release: Dict[str, Any], pypi_name: Optional[str] = None @@ -109,7 +113,7 @@ def node_for_release( tag = release["tagName"] title = release["name"] date = release["publishedAt"][:10] - title = title if tag in title else f"{tag}: {title}" + title = get_release_title(title=title, tag=tag) # Section id_section = nodes.make_id("release-" + tag) diff --git a/tests/unit/test_changelog.py b/tests/unit/test_changelog.py index b5ed70a..274ae75 100644 --- a/tests/unit/test_changelog.py +++ b/tests/unit/test_changelog.py @@ -155,6 +155,13 @@ def test_node_for_release_title_tag(release_dict): changelog.node_for_release(release=release_dict, pypi_name=None) ) +def test_node_for_release_none_title(release_dict): + + release_dict["name"] = None + assert "1.0.0" in node_to_string( + changelog.node_for_release(release=release_dict, pypi_name=None) + ) + def test_node_for_release_title_pypy(release_dict): value = node_to_string( @@ -239,3 +246,15 @@ def test_github_call_http_error_connection(requests_mock): changelog.github_call(url=url, token="token", query="") assert str(exc_info.value) == "Could not retrieve changelog from github: bar" + +@pytest.mark.parametrize( + "title, tag, expected", + [ + ("Foo", "1.0.0", "1.0.0: Foo"), + ("1.0.0: Foo", "1.0.0", "1.0.0: Foo"), + ("Fix 1.0.0", "1.0.1", "1.0.1: Fix 1.0.0"), + (None, "1.0.0", "1.0.0"), + ], +) +def test_get_release_title(title, tag, expected): + assert changelog.get_release_title(title=title, tag=tag) == expected