Skip to content

Conversation

@ogenstad
Copy link
Contributor

@ogenstad ogenstad commented Dec 17, 2025

This PR depends on opsmill/infrahub#7904 being merged first.

I wanted to add some integration tests to this but had some issues with testcontainers for the pre-release (opsmill/infrahub#8004). Will add those later.

Summary by CodeRabbit

  • New Features
    • Added ability to order nodes by metadata fields (created_at or updated_at).

✏️ Tip: You can customize this high-level summary in your review settings.

@coderabbitai
Copy link

coderabbitai bot commented Dec 17, 2025

Walkthrough

This pull request introduces functionality to order nodes by metadata fields. It adds a new OrderDirection enum with ASC and DESC values, creates a NodeMetaOrder model with created_at and updated_at fields that enforces mutual exclusivity, and extends the Order model with a node_metadata field. The GraphQL renderer is updated to serialize Order objects and dictionaries. A changelog entry documents this new feature addition.

Pre-merge checks

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'Add ability to order results by metadata created_at or updated_at' directly describes the main change across all modified files, accurately summarizing the feature being introduced.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@cloudflare-workers-and-pages
Copy link

cloudflare-workers-and-pages bot commented Dec 17, 2025

Deploying infrahub-sdk-python with  Cloudflare Pages  Cloudflare Pages

Latest commit: fb52237
Status: ✅  Deploy successful!
Preview URL: https://8734dd29.infrahub-sdk-python.pages.dev
Branch Preview URL: https://pog-metadata-ordering-ihs-17.infrahub-sdk-python.pages.dev

View logs

@ogenstad ogenstad force-pushed the pog-metadata-ordering-IHS-172 branch from bc94b72 to d473f3c Compare December 17, 2025 09:48
@codecov
Copy link

codecov bot commented Dec 17, 2025

Codecov Report

❌ Patch coverage is 77.27273% with 5 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
infrahub_sdk/types.py 75.00% 3 Missing ⚠️
infrahub_sdk/graphql/renderers.py 66.66% 1 Missing and 1 partial ⚠️
@@                Coverage Diff                @@
##           infrahub-develop     #698   +/-   ##
=================================================
  Coverage             76.03%   76.04%           
=================================================
  Files                   113      114    +1     
  Lines                  9742     9779   +37     
  Branches               1490     1496    +6     
=================================================
+ Hits                   7407     7436   +29     
- Misses                 1840     1846    +6     
- Partials                495      497    +2     
Flag Coverage Δ
integration-tests 34.52% <13.63%> (-0.12%) ⬇️
python-3.10 49.87% <13.63%> (-0.10%) ⬇️
python-3.11 49.89% <13.63%> (-0.08%) ⬇️
python-3.12 49.87% <13.63%> (-0.06%) ⬇️
python-3.13 49.87% <13.63%> (-0.06%) ⬇️
python-3.14 51.50% <13.63%> (-0.11%) ⬇️
python-filler-3.12 24.04% <63.63%> (+0.09%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
infrahub_sdk/enums.py 100.00% <100.00%> (ø)
infrahub_sdk/graphql/renderers.py 92.85% <66.66%> (-5.87%) ⬇️
infrahub_sdk/types.py 86.84% <75.00%> (-6.02%) ⬇️

... and 1 file with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@ogenstad ogenstad force-pushed the pog-metadata-ordering-IHS-172 branch 2 times, most recently from ccbdefe to 71de96f Compare December 17, 2025 10:26
@ogenstad ogenstad force-pushed the pog-metadata-ordering-IHS-172 branch from 71de96f to fb52237 Compare December 17, 2025 12:01
@ogenstad ogenstad marked this pull request as ready for review January 2, 2026 13:30
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (1)
infrahub_sdk/graphql/renderers.py (1)

58-86: Consider reducing code duplication.

The rendering logic for Order, BaseModel, and dict is nearly identical (lines 58-67, 68-77, 78-86), differing only in how the data is obtained (model_dump(exclude_none=True), model_dump(), or direct dict access). While the functionality is correct, this duplication could be refactored to improve maintainability.

🔎 Optional refactor to reduce duplication
+def _render_dict_as_graphql(data: dict[str, Any], convert_enum: bool = False) -> str:
+    """Helper to render a dictionary as GraphQL object syntax."""
+    return (
+        "{ "
+        + ", ".join(
+            f"{key}: {convert_to_graphql_as_string(value=val, convert_enum=convert_enum)}"
+            for key, val in data.items()
+        )
+        + " }"
+    )
+
 def convert_to_graphql_as_string(value: Any, convert_enum: bool = False) -> str:
     ...
     if isinstance(value, Order):
-        data = value.model_dump(exclude_none=True)
-        return (
-            "{ "
-            + ", ".join(
-                f"{key}: {convert_to_graphql_as_string(value=val, convert_enum=convert_enum)}"
-                for key, val in data.items()
-            )
-            + " }"
-        )
+        return _render_dict_as_graphql(value.model_dump(exclude_none=True), convert_enum)
     if isinstance(value, BaseModel):
-        data = value.model_dump()
-        return (
-            "{ "
-            + ", ".join(
-                f"{key}: {convert_to_graphql_as_string(value=val, convert_enum=convert_enum)}"
-                for key, val in data.items()
-            )
-            + " }"
-        )
+        return _render_dict_as_graphql(value.model_dump(), convert_enum)
     if isinstance(value, dict):
-        return (
-            "{ "
-            + ", ".join(
-                f"{key}: {convert_to_graphql_as_string(value=val, convert_enum=convert_enum)}"
-                for key, val in value.items()
-            )
-            + " }"
-        )
+        return _render_dict_as_graphql(value, convert_enum)
📜 Review details

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between b3fc3ac and fb52237.

📒 Files selected for processing (4)
  • changelog/+86c0992a.added.md
  • infrahub_sdk/enums.py
  • infrahub_sdk/graphql/renderers.py
  • infrahub_sdk/types.py
🧰 Additional context used
📓 Path-based instructions (3)
**/*.py

📄 CodeRabbit inference engine (AGENTS.md)

**/*.py: Use type hints on all function signatures
Never mix async/sync inappropriately
Never bypass type checking without justification

Files:

  • infrahub_sdk/types.py
  • infrahub_sdk/enums.py
  • infrahub_sdk/graphql/renderers.py
infrahub_sdk/**/*.py

📄 CodeRabbit inference engine (AGENTS.md)

Follow async/sync dual pattern for new features in the Python SDK

Files:

  • infrahub_sdk/types.py
  • infrahub_sdk/enums.py
  • infrahub_sdk/graphql/renderers.py
**/*.{md,mdx}

📄 CodeRabbit inference engine (AGENTS.md)

**/*.{md,mdx}: Use text language for directory structure code blocks in markdown documentation
Add blank lines before and after lists in markdown documentation
Always specify language in fenced code blocks in markdown documentation

Files:

  • changelog/+86c0992a.added.md
🧠 Learnings (1)
📓 Common learnings
Learnt from: wvandeun
Repo: opsmill/infrahub-sdk-python PR: 637
File: infrahub_sdk/ctl/branch.py:290-290
Timestamp: 2025-11-25T13:29:23.062Z
Learning: In infrahub_sdk/ctl/branch.py, the report command uses pc.created_by.updated_at to display "Created at" for proposed changes as a workaround because the SDK doesn't have easy access to the creation time of the proposed change. This will be replaced with proper object-level metadata implementation in version 1.7 of Infrahub.
🧬 Code graph analysis (3)
infrahub_sdk/types.py (1)
infrahub_sdk/enums.py (1)
  • OrderDirection (4-6)
infrahub_sdk/enums.py (1)
infrahub_sdk/protocols_base.py (1)
  • Enum (110-111)
infrahub_sdk/graphql/renderers.py (1)
infrahub_sdk/types.py (1)
  • Order (84-88)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: Cloudflare Pages
🔇 Additional comments (6)
changelog/+86c0992a.added.md (1)

1-1: LGTM!

The changelog entry accurately describes the new feature for ordering nodes by metadata fields.

infrahub_sdk/enums.py (1)

4-6: LGTM! Well-designed enum.

The str inheritance is a good choice as it enables seamless serialization to GraphQL queries while maintaining type safety through the Enum.

infrahub_sdk/graphql/renderers.py (1)

58-67: The exclude_none=True behavior is appropriate for GraphQL.

Handling Order separately with exclude_none=True prevents sending null fields to GraphQL, which aligns with typical GraphQL practices where absent fields are preferred over explicit nulls for optional parameters.

infrahub_sdk/types.py (3)

73-81: LGTM! Validation logic is correct.

The NodeMetaOrder model is well-designed:

  • The mutual exclusivity validation correctly uses truthiness checks (None is falsy, OrderDirection members are truthy)
  • The constraint makes sense for ordering by a single metadata field at a time
  • Using Pydantic's model_validator provides clear error messages at instantiation time

9-9: The noqa: TC001 comment is appropriate.

OrderDirection is used in runtime type annotations for Pydantic fields, not just static type checking, so it cannot be moved into a TYPE_CHECKING block. The suppression is correct.


84-88: Backend API support confirmed; add integration tests for node_metadata ordering.

The dependent backend PR (opsmill/infrahub#7904) has been merged, confirming the backend API now supports the node_metadata ordering parameter.

Integration tests for the new node_metadata ordering functionality are still pending. The commented-out tests in tests/integration/test_infrahub_client.py show the test pattern. Once the testcontainers issue is resolved, add integration tests covering the new node_metadata parameter to ensure the ordering functionality works end-to-end.

@ogenstad ogenstad requested a review from a team January 2, 2026 15:58
Comment on lines +85 to +88
disable: bool | None = Field(
default=None, description="Disable default ordering, can be used to improve performance"
)
node_metadata: NodeMetaOrder | None = Field(default=None, description="Order by node meta fields")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice one integrating into this pydantic model 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants