Skip to content
Merged
Show file tree
Hide file tree
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
14 changes: 13 additions & 1 deletion cli/libs/contentful.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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"
Expand All @@ -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 "<unknown app>"),
"storage": int(requirements.get("disk", 1)),
"appStore": visible,
"production": bool(variables.get("release", False)) and visible,
Expand Down
18 changes: 18 additions & 0 deletions cli/tests/test_contentful.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import sys
import types

import pytest
from typer.testing import CliRunner

from libs import contentful, main
Expand Down Expand Up @@ -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}

Expand Down
2 changes: 2 additions & 0 deletions docs/appstore-release-spec.md
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,8 @@ Thus `dev` is a broader, richer dataset, while `release` is narrower and product

`production` derives from `release` in `apps/<app>/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/<app>.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
Expand Down
Loading