From 84c1546bc913f8a781c65077930182c7ef24a9c8 Mon Sep 17 00:00:00 2001 From: zhaojing1987 Date: Wed, 18 Nov 2026 10:12:07 +0800 Subject: [PATCH] fix(contentful): round fractional memory requirements up Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- cli/libs/contentful.py | 14 +++++++++++++- cli/tests/test_contentful.py | 18 ++++++++++++++++++ docs/appstore-release-spec.md | 2 ++ 3 files changed, 33 insertions(+), 1 deletion(-) diff --git a/cli/libs/contentful.py b/cli/libs/contentful.py index 26f460c97..3c841b0f9 100644 --- a/cli/libs/contentful.py +++ b/cli/libs/contentful.py @@ -1,6 +1,7 @@ from __future__ import annotations import json +from decimal import Decimal, InvalidOperation, ROUND_CEILING from libs import catalog from libs.credentials import resolve_secret @@ -26,6 +27,17 @@ def load_draft(app_name: str, drafts_dir: str) -> dict: return catalog.load_catalog(app_name, drafts_dir) +def _catalog_memory(value: object, app_name: str) -> int: + error = f"{app_name}: requirements.memory must be a positive, finite number" + try: + memory = Decimal(str(value)) + except InvalidOperation as exc: + raise ValueError(error) from exc + if not memory.is_finite() or memory <= 0: + raise ValueError(error) + return int(memory.to_integral_value(rounding=ROUND_CEILING)) + + def build_machine_fields(variables: dict, active: bool = True) -> dict: requirements = variables.get("requirements") or {} visible = active and variables.get("scope", "public") != "internal" @@ -38,7 +50,7 @@ def build_machine_fields(variables: dict, active: bool = True) -> dict: if edition.get("dist") ], "vcpu": int(requirements.get("cpu", 1)), - "memory": int(requirements.get("memory", 1)), + "memory": _catalog_memory(requirements.get("memory", 1), variables.get("name") or ""), "storage": int(requirements.get("disk", 1)), "appStore": visible, "production": bool(variables.get("release", False)) and visible, diff --git a/cli/tests/test_contentful.py b/cli/tests/test_contentful.py index 990458b7c..4e7db903c 100644 --- a/cli/tests/test_contentful.py +++ b/cli/tests/test_contentful.py @@ -5,6 +5,7 @@ import sys import types +import pytest from typer.testing import CliRunner from libs import contentful, main @@ -60,6 +61,23 @@ def test_build_machine_fields_title_falls_back_to_name(): assert fields["title"] == "demo" +@pytest.mark.parametrize(("memory", "expected"), [("1.5", 2), (1.5, 2), ("2.0", 2), (2, 2)]) +def test_build_machine_fields_rounds_fractional_memory_up(memory, expected): + variables = {"name": "youtrack", "requirements": {"memory": memory}} + + fields = contentful.build_machine_fields(variables) + + assert fields["memory"] == expected + assert isinstance(fields["memory"], int) + assert variables["requirements"]["memory"] == memory + + +@pytest.mark.parametrize("memory", ["invalid", "NaN", "Infinity", 0, -1, True]) +def test_build_machine_fields_rejects_invalid_memory_with_app_context(memory): + with pytest.raises(ValueError, match=r"demo: requirements\.memory must be a positive, finite number"): + contentful.build_machine_fields({"name": "demo", "requirements": {"memory": memory}}) + + def test_build_machine_fields_hides_archived_and_internal_apps(): variables = {"name": "demo", "release": True} diff --git a/docs/appstore-release-spec.md b/docs/appstore-release-spec.md index 2d3e27d25..78fb55e77 100644 --- a/docs/appstore-release-spec.md +++ b/docs/appstore-release-spec.md @@ -356,6 +356,8 @@ Thus `dev` is a broader, richer dataset, while `release` is narrower and product `production` derives from `release` in `apps//variables.json`; `appStore` derives from whether the app is active and public in scope. `catalog-push --apply` writes both fields when creating or updating Contentful entries, so they need not be repeated in `metadata/catalog/.json`. +The Contentful `memory` payload uses whole units. `catalog-push` rounds a positive fractional `requirements.memory` up (for example, `"1.5"` becomes `2`) without changing the app's declaration. Invalid or non-positive values fail validation. + Changes to `metadata/catalog/*.json` receive dry-run validation in PRs. After merging to `main`, the `Sync data to Contentful` workflow runs `catalog-push --apply`. ### 3.3 Additional Product Fields