Merge pull request #269 from jvanbruegge/api-fallback

github: Add fallback to atom feed if project does not use releases
This commit is contained in:
Jörg Thalheim 2024-08-16 06:30:36 +02:00 committed by GitHub
commit 7840def89c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 61 additions and 8 deletions

View File

@ -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]:

View File

@ -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()

View File

@ -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 { };

View File

@ -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=";
};
}