From 3cdca619a99351c78f0b6e68ea877ddad8dfb2a9 Mon Sep 17 00:00:00 2001 From: David Auer Date: Mon, 17 Feb 2025 02:41:11 +0100 Subject: [PATCH] pip: Add changelog links from pypi to checker data --- pip/flatpak-pip-generator | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/pip/flatpak-pip-generator b/pip/flatpak-pip-generator index 013181a3..b9048fbe 100755 --- a/pip/flatpak-pip-generator +++ b/pip/flatpak-pip-generator @@ -78,6 +78,24 @@ def get_pypi_url(name: str, filename: str) -> str: return source['url'] raise Exception('Failed to extract url from {}'.format(url)) +def get_pypi_changelog(name: str) -> str: + url = 'https://pypi.org/pypi/{}/json'.format(name) + with urllib.request.urlopen(url) as response: + body = json.loads(response.read().decode('utf-8')) + urls = {k.casefold(): v for k, v in body['info']['project_urls'].items()} + # Keys as recognized by pypi: + # https://github.com/pypi/warehouse/blob/main/warehouse/templates/packaging/detail.html#L24 + search_keys = {"changelog", "change log", "changes", "release notes", "news", "what's new", "history"} + keys = search_keys.intersection(urls.keys()) + if len(keys) == 1: + return urls[keys.pop()] + elif len(keys) > 1: + print(f"Warning: Got multiple potential keys for the changelog, picking one at random from {keys}.") + return urls[keys.pop()] + else: + print(f"No changelog url found for '{name}'.") + + def get_tar_package_url_pypi(name: str, version: str) -> str: url = 'https://pypi.org/pypi/{}/{}/json'.format(name, version) @@ -354,6 +372,7 @@ with tempfile.TemporaryDirectory(prefix=tempdir_prefix) as tempdir: name = name.casefold() is_pypi = True url = get_pypi_url(name, filename) + changelog = get_pypi_changelog(name) source = OrderedDict([ ('type', 'file'), ('url', url), @@ -361,7 +380,10 @@ with tempfile.TemporaryDirectory(prefix=tempdir_prefix) as tempdir: if opts.checker_data: source['x-checker-data'] = { 'type': 'pypi', - 'name': name} + 'name': name, + } + if changelog: + source['x-checker-data']['changelog-url-template'] = changelog if url.endswith(".whl"): source['x-checker-data']['packagetype'] = 'bdist_wheel' is_vcs = False