Skip to content

Make builtin Jinja2 filters allowed on workers#943

Merged
gmazoyer merged 3 commits intoinfrahub-developfrom
gma-20260413-builtinfilters-worker
Apr 14, 2026
Merged

Make builtin Jinja2 filters allowed on workers#943
gmazoyer merged 3 commits intoinfrahub-developfrom
gma-20260413-builtinfilters-worker

Conversation

@gmazoyer
Copy link
Copy Markdown
Contributor

@gmazoyer gmazoyer commented Apr 13, 2026

Why & What

This changes only marks Jinja2-builtin filters as allowed to be used in templates rendered by workers. Previously, the code rendering Jinja2 templates on worker was not validating which filters were allowed to be used.

When introducing allowed contexts for filters, and validating which ones can be used by the worker code, we can introduce what seems to be a regression from a user-perspective.

This should also be documented in the release notes Infrahub 1.9.

Summary by CodeRabbit

  • New Features

    • Expanded Jinja2 template filter availability in Worker environments. Eighteen filters (including attr, batch, dictsort, groupby, items, map, sort, and more) are now supported in Worker contexts, previously available only in Local contexts.
  • Documentation

    • Updated template filter support matrix to reflect expanded filter availability.

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Apr 13, 2026

Walkthrough

Eighteen Jinja2 builtin filters were updated to allow execution in WORKER context in addition to LOCAL context. The allowed_contexts property for filters including attr, batch, dictsort, groupby, items, map, pprint, random, reject, rejectattr, safe, select, selectattr, sort, tojson, unique, urlize, and xmlattr was changed from ExecutionContext.LOCAL to ExecutionContext.WORKER | ExecutionContext.LOCAL. The corresponding documentation table was updated to reflect these context permissions, and unit tests were modified to validate LOCAL-only filter behavior using fqdn_to_ip instead of safe.

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ 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%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately summarizes the main change—enabling Jinja2 builtin filters for worker template execution.
Description check ✅ Passed The description covers the core intent (enabling builtin filters on workers), acknowledges the regression context, and notes documentation requirements.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.


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.

@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 13, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.

@@                 Coverage Diff                  @@
##           infrahub-develop     #943      +/-   ##
====================================================
- Coverage             82.63%   81.45%   -1.19%     
====================================================
  Files                   133      133              
  Lines                 13250    11253    -1997     
  Branches               2306     1690     -616     
====================================================
- Hits                  10949     9166    -1783     
+ Misses                 1652     1542     -110     
+ Partials                649      545     -104     
Flag Coverage Δ
integration-tests 42.00% <ø> (-2.91%) ⬇️
python-3.10 54.24% <ø> (-2.48%) ⬇️
python-3.11 54.24% <ø> (-2.48%) ⬇️
python-3.12 54.26% <ø> (-2.48%) ⬇️
python-3.13 54.24% <ø> (-2.49%) ⬇️
python-3.14 55.85% <ø> (-2.73%) ⬇️
python-filler-3.12 22.89% <ø> (-0.03%) ⬇️

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

Files with missing lines Coverage Δ
infrahub_sdk/template/filters.py 100.00% <ø> (ø)

... and 16 files with indirect coverage changes

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

@cloudflare-workers-and-pages
Copy link
Copy Markdown

cloudflare-workers-and-pages Bot commented Apr 13, 2026

Deploying infrahub-sdk-python with  Cloudflare Pages  Cloudflare Pages

Latest commit: 81343a5
Status: ✅  Deploy successful!
Preview URL: https://5555bc34.infrahub-sdk-python.pages.dev
Branch Preview URL: https://gma-20260413-builtinfilters.infrahub-sdk-python.pages.dev

View logs

@github-actions github-actions Bot added the type/documentation Improvements or additions to documentation label Apr 13, 2026
@gmazoyer gmazoyer marked this pull request as ready for review April 14, 2026 07:47
@gmazoyer gmazoyer requested a review from a team as a code owner April 14, 2026 07:47
Copy link
Copy Markdown

@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: 1

🧹 Nitpick comments (1)
tests/unit/sdk/test_infrahub_filters.py (1)

102-111: Add a direct WORKER-positive regression test for a migrated builtin filter.

These updated tests verify LOCAL-only behavior, but they no longer directly assert the core regression fix (a builtin like safe now allowed in WORKER).

Proposed test addition
 class TestValidateContext:
@@
     def test_context_local_allows_local_only_filters(self) -> None:
         jinja = Jinja2Template(template="{{ data | fqdn_to_ip }}")
         jinja.validate(context=ExecutionContext.LOCAL)
+
+    def test_context_worker_allows_worker_enabled_builtin_filter(self) -> None:
+        jinja = Jinja2Template(template="{{ data | safe }}")
+        jinja.validate(context=ExecutionContext.WORKER)
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@tests/unit/sdk/test_infrahub_filters.py` around lines 102 - 111, Add a
positive WORKER test that asserts a migrated builtin filter (e.g., "safe") is
allowed in WORKER context: create a new test function (similar to
test_context_worker_blocks_local_only_filters and
test_context_local_allows_local_only_filters) that builds a Jinja2Template with
template="{{ data | safe }}", calls
jinja.validate(context=ExecutionContext.WORKER) and ensures it does not raise
(i.e., no pytest.raises), verifying the migrated builtin is permitted in WORKER.
Reference Jinja2Template.validate and ExecutionContext.WORKER when adding the
test.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@docs/docs/python-sdk/reference/templating.mdx`:
- Around line 39-91: The templating reference table in
docs/python-sdk/reference/templating.mdx was edited directly but is generated;
revert this manual change and regenerate the SDK docs instead of editing the
file by running the docs generation task (uv run invoke docs-generate) to
produce updated files (including docs/python-sdk/reference/templating.mdx and
the related docs/python-sdk/reference/config.mdx and
docs/python-sdk/sdk_ref/**/*.mdx), then commit the regenerated output so the
table changes persist and don’t drift from source generation.

---

Nitpick comments:
In `@tests/unit/sdk/test_infrahub_filters.py`:
- Around line 102-111: Add a positive WORKER test that asserts a migrated
builtin filter (e.g., "safe") is allowed in WORKER context: create a new test
function (similar to test_context_worker_blocks_local_only_filters and
test_context_local_allows_local_only_filters) that builds a Jinja2Template with
template="{{ data | safe }}", calls
jinja.validate(context=ExecutionContext.WORKER) and ensures it does not raise
(i.e., no pytest.raises), verifying the migrated builtin is permitted in WORKER.
Reference Jinja2Template.validate and ExecutionContext.WORKER when adding the
test.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 03d7d327-c291-4b46-a4e6-cf695acb1947

📥 Commits

Reviewing files that changed from the base of the PR and between 0629c5a and 81343a5.

📒 Files selected for processing (3)
  • docs/docs/python-sdk/reference/templating.mdx
  • infrahub_sdk/template/filters.py
  • tests/unit/sdk/test_infrahub_filters.py

Comment thread docs/docs/python-sdk/reference/templating.mdx
@gmazoyer gmazoyer merged commit 44a7ccc into infrahub-develop Apr 14, 2026
21 checks passed
@gmazoyer gmazoyer deleted the gma-20260413-builtinfilters-worker branch April 14, 2026 16:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type/documentation Improvements or additions to documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants