Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: autoupdate for nox #321

Merged
merged 1 commit into from
Nov 28, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 30 additions & 13 deletions noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import difflib
import email.message
import functools
import json
import os
import re
Expand All @@ -20,6 +21,7 @@
import zipfile
from collections.abc import Callable
from pathlib import Path
from typing import Any

import nox

Expand Down Expand Up @@ -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:
"""
Expand All @@ -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:
Expand Down