Skip to content

Commit

Permalink
fix: detect prereleases on GitHub so we can ignore them
Browse files Browse the repository at this point in the history
This is simple enough: we just use the GitHub releases API that does
actually have the info in it.

I have complained about the feed not having the info here:
https://github.com/orgs/community/discussions/88765
  • Loading branch information
lf- committed Jan 19, 2024
1 parent 6e4ee69 commit 3b745ea
Showing 1 changed file with 22 additions and 17 deletions.
39 changes: 22 additions & 17 deletions nix_update/version/github.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
import json
import re
import urllib.request
import xml.etree.ElementTree as ET
from urllib.parse import ParseResult, unquote, urlparse
from xml.etree.ElementTree import Element
from urllib.parse import ParseResult, urlparse

from ..errors import VersionError
from ..utils import info
from .version import Version


def version_from_entry(entry: Element) -> Version:
if entry is None:
raise VersionError("No release found")
link = entry.find("{http://www.w3.org/2005/Atom}link")
assert link is not None
href = link.attrib["href"]
url = urlparse(href)
# TODO: set pre-release flag
return Version(unquote(url.path.split("/")[-1]))
def version_from_entry(entry: dict) -> Version:
href = entry["html_url"]
assert href is not None
prerelease = entry["prerelease"]
draft = entry["draft"]
version = entry["tag_name"]
return Version(version, prerelease=prerelease or draft)


def fetch_github_versions(url: ParseResult) -> list[Version]:
Expand All @@ -27,11 +24,19 @@ def fetch_github_versions(url: ParseResult) -> list[Version]:
owner, repo = parts[1], parts[2]
repo = re.sub(r"\.git$", "", repo)
# TODO fallback to tags?
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")
# https://docs.github.com/en/rest/releases/releases?apiVersion=2022-11-28#list-releases
releases_url = f"https://api.github.com/repos/{owner}/{repo}/releases?per_page=100"
info(f"fetch {releases_url}")
req = urllib.request.Request(
releases_url,
headers={
"Accept": "application/vnd.github+json",
"X-GitHub-Api-Version": "2022-11-28",
},
)
resp = urllib.request.urlopen(req)
releases = json.loads(resp.read())

return [version_from_entry(x) for x in releases]


Expand Down

0 comments on commit 3b745ea

Please sign in to comment.