Skip to content

Commit 8e88539

Browse files
committed
github: Add fallback to atom feed if project does not use releases
1 parent 31bdac9 commit 8e88539

File tree

4 files changed

+57
-8
lines changed

4 files changed

+57
-8
lines changed

nix_update/version/github.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ def fetch_github_versions(url: ParseResult) -> list[Version]:
2828
parts = url.path.split("/")
2929
owner, repo = parts[1], parts[2]
3030
repo = re.sub(r"\.git$", "", repo)
31-
# TODO fallback to tags?
3231
github_url = f"https://api.github.com/repos/{owner}/{repo}/releases"
3332
token = os.environ.get("GITHUB_TOKEN")
3433
req = urllib.request.Request(
@@ -39,17 +38,20 @@ def fetch_github_versions(url: ParseResult) -> list[Version]:
3938
info(f"trying to fetch {github_url}")
4039
resp = urllib.request.urlopen(req)
4140
releases = json.loads(resp.read())
42-
return [Version(x["tag_name"], x["prerelease"]) for x in releases]
41+
if releases:
42+
return [Version(x["tag_name"], x["prerelease"]) for x in releases]
43+
else:
44+
warn("No GitHub releases found, falling back to tags")
4345
except urllib.error.URLError as e:
4446
warn(
4547
f"Cannot fetch '{github_url}' using GitHub API ({e}), falling back to public atom feed"
4648
)
47-
feed_url = f"https://github.com/{owner}/{repo}/releases.atom"
48-
info(f"fetch {feed_url}")
49-
resp = urllib.request.urlopen(feed_url)
50-
tree = ET.fromstring(resp.read())
51-
releases = tree.findall(".//{http://www.w3.org/2005/Atom}entry")
52-
return [version_from_entry(x) for x in releases]
49+
feed_url = f"https://github.com/{owner}/{repo}/releases.atom"
50+
info(f"fetch {feed_url}")
51+
resp = urllib.request.urlopen(feed_url)
52+
tree = ET.fromstring(resp.read())
53+
releases = tree.findall(".//{http://www.w3.org/2005/Atom}entry")
54+
return [version_from_entry(x) for x in releases]
5355

5456

5557
def fetch_github_snapshots(url: ParseResult, branch: str) -> list[Version]:

tests/test_github.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,39 @@ def test_github_api(helpers: conftest.Helpers) -> None:
4141
assert "https://github.com/sharkdp/fd/compare/v8.0.0...v" in commit
4242

4343

44+
@pytest.mark.skipif(
45+
"GITHUB_TOKEN" not in os.environ, reason="No GITHUB_TOKEN environment variable set"
46+
)
47+
def test_github_empty_fallback(helpers: conftest.Helpers) -> None:
48+
with helpers.testpkgs(init_git=True) as path:
49+
main(["--file", str(path), "--commit", "github-no-release"])
50+
version = subprocess.run(
51+
[
52+
"nix",
53+
"eval",
54+
"--raw",
55+
"--extra-experimental-features",
56+
"nix-command",
57+
"-f",
58+
path,
59+
"github-no-release.version",
60+
],
61+
check=True,
62+
text=True,
63+
stdout=subprocess.PIPE,
64+
).stdout.strip()
65+
assert tuple(map(int, version.split("."))) >= (4, 4, 3)
66+
commit = subprocess.run(
67+
["git", "-C", path, "log", "-1"],
68+
text=True,
69+
stdout=subprocess.PIPE,
70+
check=True,
71+
).stdout.strip()
72+
print(commit)
73+
assert version in commit
74+
assert "github" in commit
75+
assert "https://github.com/ProtonVPN/proton-vpn-gtk-app/compare/v4.3.2...v" in commit
76+
4477
def test_github_feed_fallback(helpers: conftest.Helpers) -> None:
4578
with helpers.testpkgs(init_git=True) as path:
4679
monkeypatch = pytest.MonkeyPatch()

tests/testpkgs/default.nix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
crate = pkgs.callPackage ./crate.nix { };
1111
gitea = pkgs.callPackage ./gitea.nix { };
1212
github = pkgs.callPackage ./github.nix { };
13+
github-no-release = pkgs.callPackage ./github-no-release.nix { };
1314
gitlab = pkgs.callPackage ./gitlab.nix { };
1415
pypi = pkgs.python3.pkgs.callPackage ./pypi.nix { };
1516
sourcehut = pkgs.python3.pkgs.callPackage ./sourcehut.nix { };

tests/testpkgs/github-no-release.nix

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{ stdenv, fetchFromGitHub }:
2+
3+
stdenv.mkDerivation rec {
4+
pname = "proton-vpn";
5+
version = "4.3.2";
6+
7+
src = fetchFromGitHub {
8+
owner = "ProtonVPN";
9+
repo = "${pname}-gtk-app";
10+
rev = "v${version}";
11+
sha256 = "sha256-AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=";
12+
};
13+
}

0 commit comments

Comments
 (0)