From 4845f570ee6beb8041fefff604618df40a38e1ba Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Tue, 28 Nov 2023 09:13:07 -0500 Subject: [PATCH] fix: autoupdate for nox Signed-off-by: Henry Schreiner --- noxfile.py | 43 ++++++++++++++++++++++++++++++------------- 1 file changed, 30 insertions(+), 13 deletions(-) diff --git a/noxfile.py b/noxfile.py index 5b9c864d..c5912a2b 100644 --- a/noxfile.py +++ b/noxfile.py @@ -9,6 +9,7 @@ import difflib import email.message +import functools import json import os import re @@ -20,6 +21,7 @@ import zipfile from collections.abc import Callable from pathlib import Path +from typing import Any import nox @@ -401,6 +403,32 @@ def pc_bump(session: nox.Session) -> None: page.write_text(txt) +@functools.lru_cache(maxsize=None) # noqa: UP033 +def get_latest_version_tag(repo: str, old_version: str) -> dict[str, Any] | None: + auth = os.environ.get("GITHUB_TOKEN", os.environ.get("GITHUB_API_TOKEN", "")) + request = urllib.request.Request( + f"https://api.github.com/repos/{repo}/tags?per_page=100" + ) + request.add_header("Accept", "application/vnd.github+json") + request.add_header("X-GitHub-Api-Version", "2022-11-28") + if auth: + request.add_header("Authorization", f"Bearer: {auth}") + response = urllib.request.urlopen(request) + results = json.loads(response.read()) + if not results: + msg = f"No results for {repo}" + raise RuntimeError(msg) + tags = [ + x["name"] + for x in results + if x["name"].count(".") == old_version.count(".") + and x["name"].startswith("v") == old_version.startswith("v") + ] + if tags: + return tags[0] + return None + + @nox.session(venv_backend="none") def gha_bump(session: nox.Session) -> None: """ @@ -415,23 +443,12 @@ def gha_bump(session: nox.Session) -> None: # This assumes there is a single version per action old_versions = {m[1]: m[2] for m in GHA_VERS.finditer(full_txt)} - versions = {} for repo, old_version in old_versions.items(): session.log(f"{repo}: {old_version}") - if repo not in versions: - response = urllib.request.urlopen( - f"https://api.github.com/repos/{repo}/tags" - ) - versions[repo] = json.loads(response.read()) - tags = [ - x["name"] - for x in versions[repo] - if x["name"].count(".") == old_version.count(".") - ] - if not tags: + new_version = get_latest_version_tag(repo, old_version) + if not new_version: continue - new_version = tags[0] if new_version != old_version: session.log(f"Convert {repo}: {old_version} -> {new_version}") for page in pages: