Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix crashes #124

Merged
merged 4 commits into from
Mar 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .readthedocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ build:
tools:
python: "3.10"
jobs:
post_create_environment:
- pip install poetry
post_install:
- pip install -U poetry
- poetry config virtualenvs.create false
- poetry install --with docs
- VIRTUAL_ENV=$READTHEDOCS_VIRTUALENV_PATH poetry install --with docs
12 changes: 9 additions & 3 deletions sphinx_github_changelog/changelog.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def compute_changelog(
token: Optional[str], options: Dict[str, str]
) -> List[nodes.Node]:
if not token:
return no_token(changelog_url=options["changelog-url"])
return no_token(changelog_url=options.get("changelog-url"))

owner_repo = extract_github_repo_name(url=options["github"])
releases = extract_releases(owner_repo=owner_repo, token=token)
Expand All @@ -60,7 +60,7 @@ def no_token(changelog_url: Optional[str]) -> List[nodes.Node]:
par += nodes.Text("Changelog was not built because ")
par += nodes.literal("", "sphinx_github_changelog_token")
par += nodes.Text(" parameter is missing in the documentation configuration.")
result = [nodes.warning("", par)]
result: List[nodes.Node] = [nodes.warning("", par)]

if changelog_url:
par2 = nodes.paragraph()
Expand Down Expand Up @@ -100,6 +100,12 @@ 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
) -> Optional[nodes.Node]:
Expand All @@ -109,7 +115,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)
Expand Down
36 changes: 24 additions & 12 deletions tests/unit/test_changelog.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,6 @@ def extract_releases(mocker, release_dict):
)


@pytest.fixture
def options():
def _(kwargs):
return {"changelog-url": None, "github": None, "pypi": None, **kwargs}

return _


def node_to_string(node):
if isinstance(node, list):
return canonicalize(
Expand All @@ -49,16 +41,16 @@ def canonicalize(value):
)


def test_compute_changelog_no_token(options):
nodes = changelog.compute_changelog(token=None, options=options({}))
def test_compute_changelog_no_token():
nodes = changelog.compute_changelog(token=None, options={})
assert len(nodes) == 1

assert "Changelog was not built" in node_to_string(nodes[0])


def test_compute_changelog_token(options, extract_releases):
def test_compute_changelog_token(extract_releases):
nodes = changelog.compute_changelog(
token="token", options=options({"github": "https://github.com/a/b/releases"})
token="token", options={"github": "https://github.com/a/b/releases"}
)
assert "1.0.0: A new hope" in node_to_string(nodes[0])

Expand Down Expand Up @@ -156,6 +148,13 @@ def test_node_for_release_title_tag(release_dict):
)


def test_node_for_release_none_title(release_dict):
release_dict["name"] = None
assert "<title>1.0.0</title>" 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(
changelog.node_for_release(release=release_dict, pypi_name="foo")
Expand Down Expand Up @@ -239,3 +238,16 @@ 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
Loading