Skip to content

Commit

Permalink
Merge pull request #269 from jvanbruegge/api-fallback
Browse files Browse the repository at this point in the history
github: Add fallback to atom feed if project does not use releases
  • Loading branch information
Mic92 authored Aug 16, 2024
2 parents 31bdac9 + bf33dc4 commit 7840def
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 8 deletions.
18 changes: 10 additions & 8 deletions nix_update/version/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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]:
Expand Down
37 changes: 37 additions & 0 deletions tests/test_github.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
1 change: 1 addition & 0 deletions tests/testpkgs/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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 { };
Expand Down
13 changes: 13 additions & 0 deletions tests/testpkgs/github-no-release.nix
Original file line number Diff line number Diff line change
@@ -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=";
};
}

0 comments on commit 7840def

Please sign in to comment.