From d0b09ec4734bf5dfc2456529ff4a30222828e3e2 Mon Sep 17 00:00:00 2001 From: Kevin Veen-Birkenbach Date: Thu, 8 Oct 2026 11:07:12 +0200 Subject: [PATCH] fix(update): keep a sourced ref from giving its line to the next entity The repository updater assigns ref: lines to entities by position: every entity that pins a value takes the next line carrying that value. A ref that declares its own source was dropped before it took its line, so a second entity pinning the same value was given the sourced entity's line, and an update of it would have rewritten the wrong ref. A sourced ref now takes its line first and is skipped afterwards, the same order the nocheck marker already follows. Co-Authored-By: Claude Opus 5.5 (1M context) --- .../python/utils/update/test_repository.py | 61 +++++++++++-------- utils/update/repository.py | 43 +++++++------ 2 files changed, 60 insertions(+), 44 deletions(-) diff --git a/tests/unit/python/utils/update/test_repository.py b/tests/unit/python/utils/update/test_repository.py index f51d73927d..b5588e476b 100644 --- a/tests/unit/python/utils/update/test_repository.py +++ b/tests/unit/python/utils/update/test_repository.py @@ -76,30 +76,6 @@ class TestWalkRepoRefPairs(unittest.TestCase): self.assertEqual(len(pairs), 1) self.assertEqual(pairs[0][0], ("both",)) - def test_a_ref_with_its_own_source_is_left_to_that_source(self) -> None: - source = {"type": "http_regex", "url": "https://example.invalid/tags"} - data = { - "sourced": { - "repository": "https://example.invalid/a.git", - "ref": "v1.0.0", - "update": {**source, "key": "ref"}, - }, - "listed": { - "repository": "https://example.invalid/b.git", - "ref": "v2.0.0", - "update": [{**source, "key": "ref"}], - }, - "other-key": { - "repository": "https://example.invalid/c.git", - "ref": "v3.0.0", - "update": {**source, "key": "app_version"}, - }, - } - - pairs = list(walk_repo_ref_pairs(data, ())) - - self.assertEqual([pair[0] for pair in pairs], [("other-key",)]) - def test_walks_lists(self) -> None: data = { "repos": [ @@ -242,6 +218,43 @@ class TestApplyUpdates(unittest.TestCase): self.assertEqual(read_text(str(config_b)), " ref: v2.1.0\n") +SOURCED_FIRST = ( + "sourced:\n" + " repository: https://example.test/a.git\n" + " ref: v1.0.0\n" + " update:\n" + " key: ref\n" + " type: http_regex\n" + " url: https://example.test/tags\n" + " pattern: 'a (v[0-9.]+)'\n" + "plain:\n" + " repository: https://example.test/b.git\n" + " ref: v1.0.0\n" +) + + +class TestSourcedRef(unittest.TestCase): + def test_a_sourced_ref_is_skipped_and_keeps_its_own_line(self) -> None: + with tempfile.TemporaryDirectory() as tmpdir: + root = Path(tmpdir) + config = root / "roles" / "web-app-example" / ROLE_FILE_META_SERVICES + config.parent.mkdir(parents=True) + config.write_text(SOURCED_FIRST, encoding="utf-8") + + entries = collect_entries(root) + + self.assertEqual( + [(entry.entity_path, entry.line) for entry in entries], + [(("plain",), 11)], + ) + + apply_updates([RepositoryRefUpdate(entry=entries[0], latest="v1.1.0")]) + + written = config.read_text().splitlines() # nocheck: cache-read just rewritten here + self.assertEqual(written[2], " ref: v1.0.0") + self.assertEqual(written[10], " ref: v1.1.0") + + class TestCollectEntriesCoversAddons(unittest.TestCase): def _role(self, root: Path) -> Path: role = root / "roles" / "web-app-example" diff --git a/utils/update/repository.py b/utils/update/repository.py index d517cb0876..0b81bf2612 100644 --- a/utils/update/repository.py +++ b/utils/update/repository.py @@ -74,29 +74,32 @@ class RepositoryRefUpdate: latest: str +def _walk( + node, path: tuple[str, ...] +) -> Iterator[tuple[tuple[str, ...], str, str, dict]]: + """Yield ``(entity_path, repository, ref, entity)`` for every dict in + the tree that declares both keys with truthy string values.""" + if isinstance(node, dict): + repo = node.get("repository") + ref = node.get("ref") + if isinstance(repo, str) and repo and isinstance(ref, str) and ref: + yield (path, repo.strip(), ref.strip(), node) + for key, value in node.items(): + if key in ("repository", "ref"): + continue + yield from _walk(value, (*path, str(key))) + elif isinstance(node, list): + for index, item in enumerate(node): + yield from _walk(item, (*path, f"[{index}]")) + + def walk_repo_ref_pairs( node, path: tuple[str, ...] ) -> Iterator[tuple[tuple[str, ...], str, str]]: """Yield ``(entity_path, repository, ref)`` for every dict in the tree that declares both keys with truthy string values.""" - if isinstance(node, dict): - repo = node.get("repository") - ref = node.get("ref") - if ( - isinstance(repo, str) - and repo - and isinstance(ref, str) - and ref - and not declares_source(node, "ref") - ): - yield (path, repo.strip(), ref.strip()) - for key, value in node.items(): - if key in ("repository", "ref"): - continue - yield from walk_repo_ref_pairs(value, (*path, str(key))) - elif isinstance(node, list): - for index, item in enumerate(node): - yield from walk_repo_ref_pairs(item, (*path, f"[{index}]")) + for entity_path, repo, ref, _entity in _walk(node, path): + yield (entity_path, repo, ref) def git_ls_remote_tags(url: str) -> list[str]: @@ -181,14 +184,14 @@ def _entries_of_file( suppressed = suppressed_ref_lines(config_path) entries: list[RepositoryRefEntry] = [] - for entity_path, repo, ref in walk_repo_ref_pairs(data, entity_prefix): + for entity_path, repo, ref, entity in _walk(data, entity_prefix): if not is_semver(ref): continue candidates = ref_line_index.get(ref) or [] if not candidates: continue line_no = candidates.pop(0) - if line_no in suppressed: + if line_no in suppressed or declares_source(entity, "ref"): continue entries.append( RepositoryRefEntry(