From 739c78bad16db13c1e5db331bdec1f398d16941b Mon Sep 17 00:00:00 2001 From: Kevin Veen-Birkenbach Date: Mon, 5 Oct 2026 02:38:59 +0200 Subject: [PATCH] feat(update): bump addon pins through their declared source The version-source updater only read meta/services.yml. An addon pinned in meta/addons/.yml stayed where it was: the ONLYOFFICE connector for Nextcloud sat on v10.1.2, which does not install on Nextcloud 35, until someone bumped it by hand. The updater now reads addon files too. An addon declares its source in the `update:` block it already has, beside monitored, catalog and upstream_id. A monitored addon of the github-releases catalog needs nothing more: its upstream_id names the repository whose tags are its versions. The update-version-sources job opens the PRs, one per role. Every pinned addon is wired: - nextcloud onlyoffice carries its own version pin now. Its archive URL stays a literal that carries the pin, because test_pinned_addon_archives_available reads it as written - wordpress mcp-adapter, moodle auth_oidc and jellyfin sso-authentication follow the tags of their repository, moodle within its v4.5 series - the three xwiki extensions read their Maven metadata - pretix-oidc reads the PyPI simple index. The JSON API answers 404 behind the package cache the test stack routes pypi.org through - joomla's plugin is built here and jellyfin's ldap-authentication is bound to the md5 of its archive; both are opted out with that reason A bump rewrites the pin and the config.archive line together, and an archive that does not carry the pin fails test_version_keys, so the two cannot drift apart again. test_version_pins_are_watched covers addon pins as well: a new one without a source or a reasoned opt-out fails the lint. Co-Authored-By: Claude Opus 5.5 (1M context) --- .github/workflows/cron-update.yml | 2 +- cli/contributing/update/README.md | 44 ++++- cli/contributing/update/source/__main__.py | 2 +- .../design/role/services/layout.md | 2 +- .../meta/addons/ldap-authentication.yml | 1 + .../meta/addons/sso-authentication.yml | 2 + .../meta/addons/plg_system_keycloak.yml | 1 + .../web-app-moodle/meta/addons/auth_oidc.yml | 1 + .../meta/addons/onlyoffice.yml | 5 +- .../meta/addons/pretix-oidc.yml | 4 + roles/web-app-xwiki/meta/addons/ai-llm.yml | 4 + .../meta/addons/ldap-authenticator.yml | 4 + .../meta/addons/oidc-authenticator.yml | 4 + .../test_version_pins_are_watched.py | 53 +++++- tests/unit/python/utils/update/test_source.py | 130 +++++++++++++- utils/update/addons.py | 23 ++- utils/update/source.py | 167 +++++++++++++++--- 17 files changed, 408 insertions(+), 41 deletions(-) diff --git a/.github/workflows/cron-update.yml b/.github/workflows/cron-update.yml index bc1599d217..57f610fc05 100644 --- a/.github/workflows/cron-update.yml +++ b/.github/workflows/cron-update.yml @@ -108,7 +108,7 @@ jobs: UPDATE_BRANCH_PREFIX: update/version-sources UPDATE_COMMIT_MESSAGE: "update(versions): update pins with a declared source" UPDATE_PR_TITLE: "update(versions): update pins with a declared source" - UPDATE_PR_BODY: "Automated update of version pins declaring their upstream in an `update:` block of `roles/*/meta/services.yml`." + UPDATE_PR_BODY: "Automated update of version pins declaring their upstream in an `update:` block of `roles/*/meta/services.yml` or `roles/*/meta/addons/*.yml`." UPDATE_BASE_BRANCH: main run: bash scripts/github/update/open/role_prs.sh roles diff --git a/cli/contributing/update/README.md b/cli/contributing/update/README.md index 336bb06b76..d6c0605940 100644 --- a/cli/contributing/update/README.md +++ b/cli/contributing/update/README.md @@ -8,13 +8,12 @@ A pin the Docker updater (`image` + `version`) and the repository updater (`repository` + `ref`) do not reach names its upstream next to itself: ```yaml -lmstudio: - app_version: 0.0.25-1 +stalwart: + webui_version: "v1.0.5" update: - key: app_version - type: http_regex - url: https://lmstudio.ai/install.sh - pattern: 'APP_VERSION="([0-9][0-9.-]*)"' + key: webui_version + type: git_tags + repository: https://github.com/stalwartlabs/webui.git ``` `key` names the pinned key and defaults to `version`; an entity with several @@ -32,6 +31,39 @@ pins declares a list of such blocks. Types: on Docker Hub), `strip: true` removes it again so a pin without the upstream's `v` stays without it. +An addon pins its version at the root of `meta/addons/.yml` and declares +the same fields in its `update:` block, beside `monitored`, `catalog` and +`upstream_id`: + +```yaml +version: "9.15.7" +update: + type: http_regex + url: https://maven.xwiki.org/releases/org/xwiki/contrib/ldap/ldap-authenticator/maven-metadata.xml + pattern: '([0-9][0-9.]*)' +``` + +A monitored addon of the `github-releases` catalog omits `type`. Its +`upstream_id` is the `owner/repository` whose tags are its versions: + +```yaml +version: "v10.2.1" +update: + monitored: true + catalog: github-releases + upstream_id: ONLYOFFICE/onlyoffice-nextcloud +``` + +An addon's `config.archive` stays a literal URL that carries the pinned version: + +```yaml +config: + archive: "https://github.com/ONLYOFFICE/onlyoffice-nextcloud/releases/download/v10.2.1/onlyoffice.tar.gz" +``` + +The updater rewrites both lines together. A URL that does not carry the pin +fails `tests/lint/ansible/services/test_version_keys.py`. + Run `python -m cli.contributing.update.source --dry-run` to list pending bumps; the `update-version-sources` job of `.github/workflows/cron-update.yml` opens the PRs, and `tests/external/update/source/` warns about them. diff --git a/cli/contributing/update/source/__main__.py b/cli/contributing/update/source/__main__.py index 0fc8202170..aa74e763c1 100755 --- a/cli/contributing/update/source/__main__.py +++ b/cli/contributing/update/source/__main__.py @@ -13,7 +13,7 @@ def main() -> int: parser = argparse.ArgumentParser( description=( "Update version pins that declare their upstream in an `update:` " - "block of roles/*/meta/services.yml." + "block of roles/*/meta/services.yml or roles/*/meta/addons/*.yml." ) ) parser.add_argument( diff --git a/docs/contributing/design/role/services/layout.md b/docs/contributing/design/role/services/layout.md index 26869529e0..59df2b1f7b 100644 --- a/docs/contributing/design/role/services/layout.md +++ b/docs/contributing/design/role/services/layout.md @@ -432,7 +432,7 @@ config: {} # optional, opaque, role-interpreted runtime payload | `bridges` | no | list | — | Non-empty list of in-repo service keys; each MUST resolve to a service block in the same role's `meta/services.yml`. Omit when there is no cross-role dependency. | | `version` | no | string | `""` | A pin MUST be a quoted string, never an unquoted number. `""` tracks the app default. | | `group` | no | string | — | Free grouping label (e.g. Odoo `core`/`optional`). MUST NOT affect enablement. | -| `update` | no | map | — | `monitored` (bool, default `false`), `catalog` (a supported adapter), `upstream_id` (defaults to the addon id). | +| `update` | no | map | — | `monitored` (bool, default `false`), `catalog` (a supported adapter), `upstream_id` (defaults to the addon id). A `version` pin MUST also name its version source here, or carry `# nocheck: unwatched-version` with a reason: either `type` with its fields, or `monitored: true` with `catalog: github-releases` and the `owner/repository` as `upstream_id`. See [Declared version sources](../../../../../cli/contributing/update/README.md#declared-version-sources). | | `config` | no | map | — | Opaque, role-interpreted runtime payload. Lint does not constrain its inner shape beyond requiring a mapping. Secrets inside `config` MUST resolve through `lookup('config', application_id, 'secrets.credentials.')` and MUST NOT be inlined literally. | Any credential an addon needs is declared in `meta/secrets.yml` `credentials:` diff --git a/roles/web-app-jellyfin/meta/addons/ldap-authentication.yml b/roles/web-app-jellyfin/meta/addons/ldap-authentication.yml index 72b63d43fa..0d6ff6109b 100644 --- a/roles/web-app-jellyfin/meta/addons/ldap-authentication.yml +++ b/roles/web-app-jellyfin/meta/addons/ldap-authentication.yml @@ -3,6 +3,7 @@ enabled: "{{ lookup('config', 'web-app-jellyfin', 'services.ldap.enabled') | boo required: false mechanism: plugin source: upstream +# nocheck: unwatched-version Reason: the pin is bound to config.tag, config.url and the md5 of that release archive, which a version bump alone cannot follow version: "23.0.0.0" update: monitored: true diff --git a/roles/web-app-jellyfin/meta/addons/sso-authentication.yml b/roles/web-app-jellyfin/meta/addons/sso-authentication.yml index d09e1c3499..d5e4f8ae2c 100644 --- a/roles/web-app-jellyfin/meta/addons/sso-authentication.yml +++ b/roles/web-app-jellyfin/meta/addons/sso-authentication.yml @@ -8,6 +8,8 @@ update: monitored: true catalog: github-releases upstream_id: 9p4/jellyfin-plugin-sso + match: "v" + strip: true config: tag: "manifest-release" manifest: "https://raw.githubusercontent.com/9p4/jellyfin-plugin-sso/manifest-release/manifest.json" diff --git a/roles/web-app-joomla/meta/addons/plg_system_keycloak.yml b/roles/web-app-joomla/meta/addons/plg_system_keycloak.yml index 8eeec78887..217db5e036 100644 --- a/roles/web-app-joomla/meta/addons/plg_system_keycloak.yml +++ b/roles/web-app-joomla/meta/addons/plg_system_keycloak.yml @@ -3,6 +3,7 @@ enabled: "{{ lookup('config', 'web-app-joomla', 'services.sso.enabled') | bool } required: false mechanism: plugin source: built +# nocheck: unwatched-version Reason: the plugin is built in this repository from files/joomla-oidc-plugin, so the version is ours and has no upstream version: "1.0.0" bridges: - sso diff --git a/roles/web-app-moodle/meta/addons/auth_oidc.yml b/roles/web-app-moodle/meta/addons/auth_oidc.yml index 7315cb50f4..1e33ee4776 100644 --- a/roles/web-app-moodle/meta/addons/auth_oidc.yml +++ b/roles/web-app-moodle/meta/addons/auth_oidc.yml @@ -10,3 +10,4 @@ update: monitored: true catalog: github-releases upstream_id: microsoft/moodle-auth_oidc + match: "v4.5." diff --git a/roles/web-app-nextcloud/meta/addons/onlyoffice.yml b/roles/web-app-nextcloud/meta/addons/onlyoffice.yml index 53b96bb514..e1ee56880b 100644 --- a/roles/web-app-nextcloud/meta/addons/onlyoffice.yml +++ b/roles/web-app-nextcloud/meta/addons/onlyoffice.yml @@ -5,10 +5,11 @@ mechanism: plugin source: upstream bridges: - onlyoffice +version: "v10.2.1" update: monitored: true - catalog: nextcloud-appstore - upstream_id: onlyoffice + catalog: github-releases + upstream_id: ONLYOFFICE/onlyoffice-nextcloud config: archive: "https://github.com/ONLYOFFICE/onlyoffice-nextcloud/releases/download/v10.2.1/onlyoffice.tar.gz" incompatible_plugins: diff --git a/roles/web-app-pretix/meta/addons/pretix-oidc.yml b/roles/web-app-pretix/meta/addons/pretix-oidc.yml index c92eccfa12..d8eb8546b7 100644 --- a/roles/web-app-pretix/meta/addons/pretix-oidc.yml +++ b/roles/web-app-pretix/meta/addons/pretix-oidc.yml @@ -4,5 +4,9 @@ required: false mechanism: plugin source: built version: "2.3.1" +update: + type: http_regex + url: https://pypi.org/simple/pretix-oidc/ + pattern: "pretix[_-]oidc-([0-9][0-9.]*)\\.tar\\.gz" bridges: - sso diff --git a/roles/web-app-xwiki/meta/addons/ai-llm.yml b/roles/web-app-xwiki/meta/addons/ai-llm.yml index 6935b2b7ed..18e7bdec6c 100644 --- a/roles/web-app-xwiki/meta/addons/ai-llm.yml +++ b/roles/web-app-xwiki/meta/addons/ai-llm.yml @@ -6,6 +6,10 @@ source: upstream bridges: - litellm version: "0.7" +update: + type: http_regex + url: https://maven.xwiki.org/releases/org/xwiki/contrib/llm/application-ai-llm-models-ui/maven-metadata.xml + pattern: "([0-9][0-9.]*)" group: ai-llm config: id: org.xwiki.contrib.llm:application-ai-llm-models-ui diff --git a/roles/web-app-xwiki/meta/addons/ldap-authenticator.yml b/roles/web-app-xwiki/meta/addons/ldap-authenticator.yml index a08ab7a774..3a6372c637 100644 --- a/roles/web-app-xwiki/meta/addons/ldap-authenticator.yml +++ b/roles/web-app-xwiki/meta/addons/ldap-authenticator.yml @@ -6,6 +6,10 @@ source: upstream bridges: - ldap version: "9.15.7" +update: + type: http_regex + url: https://maven.xwiki.org/releases/org/xwiki/contrib/ldap/ldap-authenticator/maven-metadata.xml + pattern: "([0-9][0-9.]*)" group: ldap config: id: org.xwiki.contrib.ldap:ldap-authenticator diff --git a/roles/web-app-xwiki/meta/addons/oidc-authenticator.yml b/roles/web-app-xwiki/meta/addons/oidc-authenticator.yml index 1aab849e54..e80ea23ca7 100644 --- a/roles/web-app-xwiki/meta/addons/oidc-authenticator.yml +++ b/roles/web-app-xwiki/meta/addons/oidc-authenticator.yml @@ -6,6 +6,10 @@ source: upstream bridges: - sso version: "2.19.6" +update: + type: http_regex + url: https://maven.xwiki.org/releases/org/xwiki/contrib/oidc/oidc-authenticator/maven-metadata.xml + pattern: "([0-9][0-9.]*)" group: oidc config: id: org.xwiki.contrib.oidc:oidc-authenticator diff --git a/tests/lint/repository/test_version_pins_are_watched.py b/tests/lint/repository/test_version_pins_are_watched.py index 9e3ee644f4..1f927e5494 100644 --- a/tests/lint/repository/test_version_pins_are_watched.py +++ b/tests/lint/repository/test_version_pins_are_watched.py @@ -6,9 +6,10 @@ resolver (an ``update:`` block naming the upstream). A pin outside all three is frozen silently, and nothing tells the next reader whether that was a decision or an oversight. -Scope: every ``roles/*/meta/services.yml`` value whose key names a version and -whose content is a semver. Moving tags (``latest``, ``stable``, a branch name) -are not pins and are ignored. +Scope: every value in ``roles/*/meta/services.yml`` and at the root of +``roles/*/meta/addons/*.yml`` whose key names a version and whose content is a +semver. Moving tags (``latest``, ``stable``, a branch name) are not pins and +are ignored. Per-line opt-out: ``# nocheck: unwatched-version`` above the pin, with a reason, for a version that must stay where it is. An existing @@ -25,6 +26,7 @@ from utils.annotations.suppress import is_suppressed_at from utils.cache.files import read_text from utils.cache.yaml import load_yaml from utils.roles.mapping import ROLE_FILE_META_SERVICES +from utils.update.addons import iter_addon_files from utils.update.base import is_semver from utils.update.docker import collect_entries as docker_entries from utils.update.repository import collect_entries as repository_entries @@ -33,9 +35,14 @@ from utils.update.source import collect_entries as source_entries from . import PROJECT_ROOT _RULE = "unwatched-version" +_OPT_OUTS = (_RULE, "docker-version", "repository-version") _VERSION_KEY = re.compile(r"(^|_)(version|release|ref|tag)$") +def _addon_label(addon_id: str) -> str: + return f"addons/{addon_id}" + + def _watched() -> set[tuple[str, str, str]]: """Return ``(role, entity, key)`` of every pin an updater already bumps.""" watched = {(e.role, e.service, "version") for e in docker_entries(PROJECT_ROOT)} @@ -43,13 +50,46 @@ def _watched() -> set[tuple[str, str, str]]: (e.role, e.entity_path[-1] if e.entity_path else "", "ref") for e in repository_entries(PROJECT_ROOT) } - watched |= {(e.role, e.entity, e.key) for e in source_entries(PROJECT_ROOT)} + watched |= { + (e.role, _addon_label(e.entity) if e.addon else e.entity, e.key) + for e in source_entries(PROJECT_ROOT) + } return watched +def _addon_findings(watched: set[tuple[str, str, str]]) -> list[str]: + findings: list[str] = [] + for role, addon_path in iter_addon_files(PROJECT_ROOT / "roles"): + spec = load_yaml(str(addon_path)) + if not isinstance(spec, dict): + continue + label = _addon_label(addon_path.stem) + lines = read_text(str(addon_path)).splitlines() + for key, value in spec.items(): + text = str(value) + if not _VERSION_KEY.search(str(key)) or not is_semver(text): + continue + if (role, label, str(key)) in watched: + continue + number = next( + ( + index + for index, line in enumerate(lines, start=1) + if re.match(rf"^{re.escape(str(key))}\s*:", line) + ), + 0, + ) + if number and any( + is_suppressed_at(lines, number, rule) for rule in _OPT_OUTS + ): + continue + findings.append(f"- {role}/{label}.{key} = {text}") + return findings + + def _findings() -> list[str]: watched = _watched() - findings: list[str] = [] + findings: list[str] = _addon_findings(watched) for config_path in sorted( (PROJECT_ROOT / "roles").glob(f"*/{ROLE_FILE_META_SERVICES}") ): @@ -76,8 +116,7 @@ def _findings() -> list[str]: 0, ) if number and any( - is_suppressed_at(lines, number, rule) - for rule in (_RULE, "docker-version", "repository-version") + is_suppressed_at(lines, number, rule) for rule in _OPT_OUTS ): continue findings.append(f"- {role}/{entity}.{key} = {text}") diff --git a/tests/unit/python/utils/update/test_source.py b/tests/unit/python/utils/update/test_source.py index 773047bc65..ea546543c3 100644 --- a/tests/unit/python/utils/update/test_source.py +++ b/tests/unit/python/utils/update/test_source.py @@ -5,7 +5,7 @@ import unittest import unittest.mock as mock from pathlib import Path -from utils.roles.mapping import ROLE_FILE_META_SERVICES +from utils.roles.mapping import ROLE_DIR_META_ADDONS, ROLE_FILE_META_SERVICES from utils.update import source as module SERVICES = """\ @@ -33,6 +33,57 @@ frozen: """ +ADDONS = { + "released": """\ +--- +enabled: true +version: "v1.2.0" +update: + monitored: true + catalog: github-releases + upstream_id: example/released +config: + archive: "https://example.test/released/download/v1.2.0/app.tar.gz" +""", + "declared": """\ +--- +enabled: true +version: "3.4" +update: + type: http_regex + url: https://example.test/metadata.xml + pattern: '([0-9.]+)' +""", + "unmonitored": """\ +--- +enabled: true +version: "1.0.0" +update: + monitored: false + catalog: github-releases + upstream_id: example/unmonitored +""", + "catalogued": """\ +--- +enabled: true +version: "1.0.0" +update: + monitored: true + catalog: wordpress-org +""", + "frozen": """\ +--- +enabled: true +# nocheck: unwatched-version Reason: bound to a checksum +version: "2.0.0" +update: + monitored: true + catalog: github-releases + upstream_id: example/frozen +""", +} + + def _repo(services: str = SERVICES) -> Path: tmp = Path(tempfile.mkdtemp()) config = tmp / "roles" / "web-app-example" / ROLE_FILE_META_SERVICES @@ -41,6 +92,15 @@ def _repo(services: str = SERVICES) -> Path: return tmp +def _addon_repo(addons: dict[str, str] | None = None) -> Path: + tmp = Path(tempfile.mkdtemp()) + directory = tmp / "roles" / "web-app-example" / ROLE_DIR_META_ADDONS + directory.mkdir(parents=True) + for addon_id, content in (ADDONS if addons is None else addons).items(): + (directory / f"{addon_id}.yml").write_text(content, encoding="utf-8") + return tmp + + class TestCollectEntries(unittest.TestCase): def test_each_declared_pin_is_collected_and_a_suppressed_one_is_not(self) -> None: entries = module.collect_entries(_repo()) @@ -92,5 +152,73 @@ class TestOutdated(unittest.TestCase): self.assertEqual(module.outdated([self._entry(root)], root), []) +class TestAddons(unittest.TestCase): + def test_only_a_declared_or_github_released_addon_pin_is_collected(self) -> None: + entries = module.collect_entries(_addon_repo()) + + self.assertEqual( + [ + (e.entity, e.key, e.current, e.source["type"], e.line, e.addon) + for e in entries + ], + [ + ("declared", "version", "3.4", "http_regex", 3, True), + ("released", "version", "v1.2.0", "git_tags", 3, True), + ], + ) + self.assertEqual( + entries[1].source["repository"], "https://github.com/example/released.git" + ) + + def test_an_addon_pin_moves_together_with_the_archive_that_carries_it(self) -> None: + root = _addon_repo({"released": ADDONS["released"]}) + with mock.patch.object(module, "candidates", return_value=["v1.3.0"]): + updates = module.find_outdated_updates(root) + module.apply_updates(updates) + + addon = ( + root / "roles" / "web-app-example" / ROLE_DIR_META_ADDONS / "released.yml" + ) + written = addon.read_text() # nocheck: cache-read just rewritten here + self.assertIn('version: "v1.3.0"', written) + self.assertIn("released/download/v1.3.0/app.tar.gz", written) + self.assertIn("upstream_id: example/released", written) + + def test_an_archive_that_does_not_carry_the_pin_is_reported(self) -> None: + drifted = ADDONS["released"].replace('version: "v1.2.0"', 'version: "v1.1.0"') + root = _addon_repo({"drifted": drifted}) + + self.assertEqual( + module.invalid_declarations(root), + [ + ( + "web-app-example/addons/drifted.version: config.archive does " + "not carry the pinned version, so a bump would move only one " + "of the two" + ), + ], + ) + + def test_an_addon_block_that_cannot_resolve_is_reported(self) -> None: + root = _addon_repo( + {"broken": "---\nenabled: true\nupdate:\n type: carrier_pigeon\n"} + ) + + self.assertEqual( + module.invalid_declarations(root), + [ + ( + "web-app-example/addons/broken: update.key 'version' names no " + "key of the addon" + ), + ( + "web-app-example/addons/broken.version: unknown update.type " + "'carrier_pigeon', expected one of git_tags, registry_tags, npm, " + "http_regex, script" + ), + ], + ) + + if __name__ == "__main__": unittest.main() diff --git a/utils/update/addons.py b/utils/update/addons.py index 0e06df475d..ab275ec32c 100644 --- a/utils/update/addons.py +++ b/utils/update/addons.py @@ -45,7 +45,26 @@ ADDON_KEYS: frozenset[str] = frozenset( } ) -UPDATE_KEYS: frozenset[str] = frozenset({"monitored", "catalog", "upstream_id"}) +VERSION_SOURCE_KEYS: frozenset[str] = frozenset( + { + "key", + "type", + "repository", + "image", + "package", + "url", + "pattern", + "path", + "match", + "strip", + } +) + +UPDATE_KEYS: frozenset[str] = ( + frozenset({"monitored", "catalog", "upstream_id"}) | VERSION_SOURCE_KEYS +) + +GITHUB_RELEASES_CATALOG = "github-releases" SUPPORTED_CATALOGS: frozenset[str] = frozenset( { @@ -60,7 +79,7 @@ SUPPORTED_CATALOGS: frozenset[str] = frozenset( "gnome-extensions", "chrome-webstore", "firefox-amo", - "github-releases", + GITHUB_RELEASES_CATALOG, } ) diff --git a/utils/update/source.py b/utils/update/source.py index 852cd03736..92fbd7561d 100644 --- a/utils/update/source.py +++ b/utils/update/source.py @@ -5,19 +5,24 @@ The Docker updater watches ``image`` plus ``version``, the repository updater ``release``, a tag in a registry neither updater speaks - declares where its upstream lives:: - lmstudio: - app_version: 0.0.25-1 + stalwart: + webui_version: "v1.0.5" update: - key: app_version - type: http_regex - url: https://lmstudio.ai/install.sh - pattern: 'APP_VERSION="([0-9][0-9.-]*)"' + key: webui_version + type: git_tags + repository: https://github.com/stalwartlabs/webui.git ``key`` names the pinned key and defaults to ``version``; an entity with several pins declares a list of such blocks. The types are ``git_tags`` (repository), ``registry_tags`` (image), ``npm`` (package), ``http_regex`` (url, pattern) and ``script`` (path, run with the current version and printing the latest one). + +An addon in ``meta/addons/.yml`` declares the same fields in its +``update:`` block, beside ``monitored``, ``catalog`` and ``upstream_id``. A +monitored addon of the ``github-releases`` catalog needs no ``type``: its +``upstream_id`` already names the repository whose tags are its versions. An +addon's ``config.archive`` URL carries the pinned version and moves with it. """ from __future__ import annotations @@ -35,6 +40,7 @@ from utils.annotations.suppress import is_suppressed_at from utils.cache.files import read_text from utils.cache.yaml import load_yaml from utils.roles.mapping import ROLE_FILE_META_SERVICES +from utils.update.addons import GITHUB_RELEASES_CATALOG, iter_addon_files from utils.update.base import ( latest_semver, version_depth, @@ -57,6 +63,7 @@ if TYPE_CHECKING: from pathlib import Path NOCHECK_MARKER = "version-source" +UNWATCHED_MARKER = "unwatched-version" UPDATE_KEY = "update" DEFAULT_KEY = "version" TIMEOUT_SECONDS = 30 @@ -66,6 +73,19 @@ USER_AGENT = "infinito-nexus-version-source" @dataclass(frozen=True) class VersionSourceEntry: + """One pin and the upstream it declares. + + Args: + role: role that carries the pin. + entity: service entity in ``meta/services.yml``, or the addon id. + key: pinned key. + current: pinned version. + source: the ``update:`` block. + config_path: file that carries the pin. + line: 1-indexed line of the pin. + addon: whether the pin sits in ``meta/addons/.yml``. + """ + role: str entity: str key: str @@ -73,6 +93,7 @@ class VersionSourceEntry: source: dict[str, Any] config_path: Path line: int + addon: bool = False @dataclass(frozen=True) @@ -233,6 +254,80 @@ def _key_line(lines: list[str], entity: str, key: str) -> int | None: return None +def _top_level_line(lines: list[str], key: str) -> int | None: + """Return the 1-indexed line of ``key`` at the root of an addon file.""" + for number, line in enumerate(lines, start=1): + if re.match(rf"^{re.escape(key)}\s*:", line): + return number + return None + + +def _archive_index(lines: list[str]) -> int | None: + """Return the 0-indexed line of ``config.archive`` in an addon file.""" + for index, line in enumerate(lines): + if re.match(r"^\s+archive\s*:", line): + return index + return None + + +def _archive(spec: Any) -> str: + """Return the ``config.archive`` URL of an addon, or an empty string.""" + config = spec.get("config") if isinstance(spec, dict) else None + return str(config.get("archive", "")) if isinstance(config, dict) else "" + + +def addon_sources(spec: Any) -> list[dict[str, Any]]: + """Return the version sources an addon declares. + + Args: + spec: root mapping of a ``meta/addons/.yml`` file. + """ + declared = spec.get(UPDATE_KEY) if isinstance(spec, dict) else None + if not isinstance(declared, dict): + return [] + if "type" in declared: + return [declared] + if ( + declared.get("monitored") + and declared.get("catalog") == GITHUB_RELEASES_CATALOG + and declared.get("upstream_id") + ): + repository = f"https://github.com/{declared['upstream_id']}.git" + return [{**declared, "type": "git_tags", "repository": repository}] + return [] + + +def _declaration_problems( + label: str, config: dict[str, Any], source: Any, owner: str +) -> list[str]: + """Return what keeps one ``update:`` block from resolving. + + Args: + label: ``/`` as it appears in the message. + config: mapping that carries the pin. + source: the ``update:`` block. + owner: what ``config`` is, ``entity`` or ``addon``. + """ + if not isinstance(source, dict): + return [f"{label}: update entry is not a mapping"] + problems: list[str] = [] + key = str(source.get("key", DEFAULT_KEY)) + if key not in config: + problems.append(f"{label}: update.key '{key}' names no key of the {owner}") + if source.get("type") not in TYPES: + problems.append( + f"{label}.{key}: unknown update.type " + f"'{source.get('type')}', expected one of {', '.join(TYPES)}" + ) + archive = _archive(config) if owner == "addon" else "" + if archive and str(config.get(key, "")) not in archive: + problems.append( + f"{label}.{key}: config.archive does not carry the pinned version, " + "so a bump would move only one of the two" + ) + return problems + + def invalid_declarations(repo_root: Path) -> list[str]: """Return one message per ``update:`` block that cannot be resolved. @@ -253,22 +348,49 @@ def invalid_declarations(repo_root: Path) -> list[str]: continue sources = declared if isinstance(declared, list) else [declared] for source in sources: - if not isinstance(source, dict): - problems.append(f"{role}/{entity}: update entry is not a mapping") - continue - key = str(source.get("key", DEFAULT_KEY)) - if key not in config: - problems.append( - f"{role}/{entity}: update.key '{key}' names no key of the entity" - ) - if source.get("type") not in TYPES: - problems.append( - f"{role}/{entity}.{key}: unknown update.type " - f"'{source.get('type')}', expected one of {', '.join(TYPES)}" - ) + problems += _declaration_problems( + f"{role}/{entity}", config, source, "entity" + ) + for role, addon_path in iter_addon_files(repo_root / "roles"): + spec = load_yaml(str(addon_path)) + for source in addon_sources(spec): + problems += _declaration_problems( + f"{role}/addons/{addon_path.stem}", spec, source, "addon" + ) return problems +def _addon_entries(repo_root: Path) -> list[VersionSourceEntry]: + entries: list[VersionSourceEntry] = [] + for role, addon_path in iter_addon_files(repo_root / "roles"): + spec = load_yaml(str(addon_path)) + lines = read_text(str(addon_path)).splitlines() + for source in addon_sources(spec): + key = str(source.get("key", DEFAULT_KEY)) + current = str(spec.get(key, "")).strip() + line = _top_level_line(lines, key) + if not current or line is None: + continue + if any( + is_suppressed_at(lines, line, marker) + for marker in (NOCHECK_MARKER, UNWATCHED_MARKER) + ): + continue + entries.append( + VersionSourceEntry( + role=role, + entity=addon_path.stem, + key=key, + current=current, + source=source, + config_path=addon_path, + line=line, + addon=True, + ) + ) + return entries + + def collect_entries(repo_root: Path) -> list[VersionSourceEntry]: """Return every declared version source that is not suppressed.""" entries: list[VersionSourceEntry] = [] @@ -301,7 +423,7 @@ def collect_entries(repo_root: Path) -> list[VersionSourceEntry]: line=line, ) ) - return entries + return entries + _addon_entries(repo_root) def outdated( @@ -335,6 +457,11 @@ def apply_updates(updates: Iterable[VersionSourceUpdate]) -> list[Path]: ).splitlines(keepends=True) index = update.entry.line - 1 lines[index] = lines[index].replace(update.entry.current, update.latest, 1) + archive = _archive_index(lines) if update.entry.addon else None + if archive is not None: + lines[archive] = lines[archive].replace( + update.entry.current, update.latest, 1 + ) path.write_text("".join(lines), encoding="utf-8") if path not in changed: changed.append(path)