From a48cd063eae9d309016cdd61d9bbc1994a9643af Mon Sep 17 00:00:00 2001 From: Defame1297 Date: Wed, 2 Dec 2026 08:51:21 +0000 Subject: [PATCH] fix(ci): treat only a NAME_UNKNOWN 404 as an image with no tags next_image_version.py read any 404 on the first tags/list page as a repository not pushed yet, so a wrong registry path or a proxy's 404 page would restart the image at .0 instead of failing. The OCI distribution spec answers a missing repository with 404 and the error code NAME_UNKNOWN, so only that answer now means no tags. RegistryError carried only the readable text of the body, so it now also carries the body's OCI error codes. The tests cover a plain 404 page and a 404 with another code, both of which now raise. Assisted-by: Claude Code --- scripts/next_image_version.py | 20 ++++++++++++++++---- tests/scripts/test_next_image_version.py | 15 +++++++++++++++ 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/scripts/next_image_version.py b/scripts/next_image_version.py index ae915ea..ea345c8 100644 --- a/scripts/next_image_version.py +++ b/scripts/next_image_version.py @@ -70,9 +70,18 @@ USER_AGENT = "kamino-next-image-version" class RegistryError(Exception): - def __init__(self, url, status, text, challenge=""): + def __init__(self, url, status, text, challenge="", codes=()): super().__init__(f"GET {url}: HTTP {status}: {text}") - self.status, self.challenge = status, challenge + self.status, self.challenge, self.codes = status, challenge, codes + + +def _error_codes(raw): + """The OCI error codes of an error body (`{"errors": [{"code": ...}]}`), or () for any other body.""" + try: + errors = json.loads(raw).get("errors") or [] + return tuple(e.get("code") for e in errors if isinstance(e, dict)) + except (ValueError, AttributeError, TypeError): + return () def _readable(raw): @@ -92,7 +101,9 @@ def _fetch(url, headers): except urllib.error.HTTPError as exc: raw = exc.read(20000).decode("utf-8", errors="replace") challenge = exc.headers.get("WWW-Authenticate", "") - raise RegistryError(url, exc.code, _readable(raw), challenge) from exc + raise RegistryError( + url, exc.code, _readable(raw), challenge, _error_codes(raw) + ) from exc class Registry: @@ -139,7 +150,8 @@ class Registry: body, headers = self._get(path) except RegistryError as exc: # A repository not pushed yet is 404 NAME_UNKNOWN (OCI distribution spec): no tags. - if exc.status == 404 and not tags: + # Any other 404, such as a wrong registry path, must fail rather than restart at .0. + if exc.status == 404 and "NAME_UNKNOWN" in exc.codes and not tags: return [] raise tags += body.get("tags") or [] diff --git a/tests/scripts/test_next_image_version.py b/tests/scripts/test_next_image_version.py index 0923464..11804e3 100644 --- a/tests/scripts/test_next_image_version.py +++ b/tests/scripts/test_next_image_version.py @@ -105,6 +105,7 @@ class FakeRegistry(http.server.BaseHTTPRequestHandler): no_realm = False empty_token = False unknown = False # the repository does not exist yet + not_found = None # a 404 body without NAME_UNKNOWN agents = None # a fresh list per test, set in setUp def do_GET(self): @@ -128,6 +129,8 @@ class FakeRegistry(http.server.BaseHTTPRequestHandler): b"{}", extra={"WWW-Authenticate": f'Bearer realm="{realm}",service="reg"'}, ) + elif self.not_found is not None: + self.reply(404, self.not_found, "text/plain") elif self.unknown: body = {"errors": [{"code": "NAME_UNKNOWN", "message": "unknown"}]} self.reply(404, json.dumps(body).encode()) @@ -155,6 +158,7 @@ class RegistryClient(unittest.TestCase): def setUp(self): FakeRegistry.refuse = FakeRegistry.refuse_token = FakeRegistry.no_realm = False FakeRegistry.empty_token = FakeRegistry.unknown = False + FakeRegistry.not_found = None FakeRegistry.agents = [] server = QuietServer(("127.0.0.1", 0), FakeRegistry) threading.Thread(target=server.serve_forever, daemon=True).start() @@ -176,6 +180,17 @@ class RegistryClient(unittest.TestCase): niv.next_version("0.1", self.registry.tags(), "c", no_label), "0.1.0" ) + def test_a_404_without_name_unknown_is_a_registry_error(self): + for body in ( + b"404 page not found", + b'{"errors":[{"code":"NOT_FOUND","message":"no such route"}]}', + ): + with self.subTest(body=body): + FakeRegistry.not_found = body + with self.assertRaises(niv.RegistryError) as caught: + self.registry.tags() + self.assertEqual(caught.exception.status, 404) + def test_a_missing_manifest_is_still_a_registry_error(self): FakeRegistry.unknown = True with self.assertRaises(niv.RegistryError) as caught: