From bf33dc45ce26f36233fd58790f90dc8710ca7198 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20van=20Br=C3=BCgge?= Date: Thu, 15 Aug 2024 13:10:17 +0100 Subject: [PATCH] github: Add fallback to atom feed if project does not use releases --- nix_update/version/github.py | 18 ++++++++------ tests/test_github.py | 37 ++++++++++++++++++++++++++++ tests/testpkgs/default.nix | 1 + tests/testpkgs/github-no-release.nix | 13 ++++++++++ 4 files changed, 61 insertions(+), 8 deletions(-) create mode 100644 tests/testpkgs/github-no-release.nix diff --git a/nix_update/version/github.py b/nix_update/version/github.py index 227def9..2dba1cf 100644 --- a/nix_update/version/github.py +++ b/nix_update/version/github.py @@ -28,7 +28,6 @@ def fetch_github_versions(url: ParseResult) -> list[Version]: parts = url.path.split("/") owner, repo = parts[1], parts[2] repo = re.sub(r"\.git$", "", repo) - # TODO fallback to tags? github_url = f"https://api.github.com/repos/{owner}/{repo}/releases" token = os.environ.get("GITHUB_TOKEN") req = urllib.request.Request( @@ -39,17 +38,20 @@ def fetch_github_versions(url: ParseResult) -> list[Version]: info(f"trying to fetch {github_url}") resp = urllib.request.urlopen(req) releases = json.loads(resp.read()) - return [Version(x["tag_name"], x["prerelease"]) for x in releases] + if releases: + return [Version(x["tag_name"], x["prerelease"]) for x in releases] + else: + warn("No GitHub releases found, falling back to tags") except urllib.error.URLError as e: warn( f"Cannot fetch '{github_url}' using GitHub API ({e}), falling back to public atom feed" ) - feed_url = f"https://github.com/{owner}/{repo}/releases.atom" - info(f"fetch {feed_url}") - resp = urllib.request.urlopen(feed_url) - tree = ET.fromstring(resp.read()) - releases = tree.findall(".//{http://www.w3.org/2005/Atom}entry") - return [version_from_entry(x) for x in releases] + feed_url = f"https://github.com/{owner}/{repo}/releases.atom" + info(f"fetch {feed_url}") + resp = urllib.request.urlopen(feed_url) + tree = ET.fromstring(resp.read()) + releases = tree.findall(".//{http://www.w3.org/2005/Atom}entry") + return [version_from_entry(x) for x in releases] def fetch_github_snapshots(url: ParseResult, branch: str) -> list[Version]: diff --git a/tests/test_github.py b/tests/test_github.py index 586b0d4..3a39c46 100644 --- a/tests/test_github.py +++ b/tests/test_github.py @@ -41,6 +41,43 @@ def test_github_api(helpers: conftest.Helpers) -> None: assert "https://github.com/sharkdp/fd/compare/v8.0.0...v" in commit +@pytest.mark.skipif( + "GITHUB_TOKEN" not in os.environ, reason="No GITHUB_TOKEN environment variable set" +) +def test_github_empty_fallback(helpers: conftest.Helpers) -> None: + with helpers.testpkgs(init_git=True) as path: + main(["--file", str(path), "--commit", "github-no-release"]) + version = subprocess.run( + [ + "nix", + "eval", + "--raw", + "--extra-experimental-features", + "nix-command", + "-f", + path, + "github-no-release.version", + ], + check=True, + text=True, + stdout=subprocess.PIPE, + ).stdout.strip() + assert tuple(map(int, version.split("."))) >= (4, 4, 3) + commit = subprocess.run( + ["git", "-C", path, "log", "-1"], + text=True, + stdout=subprocess.PIPE, + check=True, + ).stdout.strip() + print(commit) + assert version in commit + assert "github" in commit + assert ( + "https://github.com/ProtonVPN/proton-vpn-gtk-app/compare/v4.3.2...v" + in commit + ) + + def test_github_feed_fallback(helpers: conftest.Helpers) -> None: with helpers.testpkgs(init_git=True) as path: monkeypatch = pytest.MonkeyPatch() diff --git a/tests/testpkgs/default.nix b/tests/testpkgs/default.nix index f22c83e..3e37c52 100644 --- a/tests/testpkgs/default.nix +++ b/tests/testpkgs/default.nix @@ -10,6 +10,7 @@ crate = pkgs.callPackage ./crate.nix { }; gitea = pkgs.callPackage ./gitea.nix { }; github = pkgs.callPackage ./github.nix { }; + github-no-release = pkgs.callPackage ./github-no-release.nix { }; gitlab = pkgs.callPackage ./gitlab.nix { }; pypi = pkgs.python3.pkgs.callPackage ./pypi.nix { }; sourcehut = pkgs.python3.pkgs.callPackage ./sourcehut.nix { }; diff --git a/tests/testpkgs/github-no-release.nix b/tests/testpkgs/github-no-release.nix new file mode 100644 index 0000000..346423c --- /dev/null +++ b/tests/testpkgs/github-no-release.nix @@ -0,0 +1,13 @@ +{ stdenv, fetchFromGitHub }: + +stdenv.mkDerivation rec { + pname = "proton-vpn"; + version = "4.3.2"; + + src = fetchFromGitHub { + owner = "ProtonVPN"; + repo = "${pname}-gtk-app"; + rev = "v${version}"; + sha256 = "sha256-AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA="; + }; +}