Skip to content
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
13 changes: 9 additions & 4 deletions autotest/dfns/test_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@ def test_latest_tag_exact_tag():
assert registry._latest is None # no network call, nothing cached


def test_latest_tag_resolves_via_api():
@pytest.mark.parametrize("token", [None, "abc"])
def test_latest_tag_resolves_via_api(monkeypatch, token):
if token:
monkeypatch.setenv("GITHUB_TOKEN", token)
else:
monkeypatch.delenv("GITHUB_TOKEN", raising=False)
registry = RemoteDfnRegistry(release_id="MODFLOW-ORG/modflow6@latest")

mock_response = MagicMock()
Expand All @@ -29,9 +34,9 @@ def test_latest_tag_resolves_via_api():

assert tag == "v6.6.1"
assert registry._latest == "v6.6.1"
mock_open.assert_called_once_with(
"https://api.github.com/repos/MODFLOW-ORG/modflow6/releases/latest"
)
(request,), _ = mock_open.call_args
assert request.full_url == "https://api.github.com/repos/MODFLOW-ORG/modflow6/releases/latest"
assert request.get_header("Authorization") == (f"Bearer {token}" if token else None)


def test_latest_tag_cached():
Expand Down
7 changes: 4 additions & 3 deletions modflow_devtools/dfns/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

from modflow_devtools.dfn import schema as dfn
from modflow_devtools.dfns import schema as dfns
from modflow_devtools.download import get_request

__all__ = [
"DfnRegistry",
Expand Down Expand Up @@ -75,9 +76,9 @@ def latest_tag(self) -> str:
return tag
if self._latest is None:
owner, name = repo.split("/")
with urllib.request.urlopen(
f"https://api.github.com/repos/{owner}/{name}/releases/latest"
) as resp:
# authenticated if GITHUB_TOKEN is set, to bypass the rate limit
url = f"https://api.github.com/repos/{owner}/{name}/releases/latest"
with urllib.request.urlopen(get_request(url)) as resp:
self._latest = json.loads(resp.read())["tag_name"]
return self._latest

Expand Down
Loading