Skip to content

fix(LaunchDarkly): Imported matches conditions only match from the start of the value - #8840

Open
yoarajota wants to merge 1 commit into
Flagsmith:mainfrom
yoarajota:fix/launchdarkly-matches-search-semantics
Open

yoarajota wants to merge 1 commit into
Flagsmith:mainfrom
yoarajota:fix/launchdarkly-matches-search-semantics

Conversation

@yoarajota

Copy link
Copy Markdown
Contributor
  • I have read the Contributing Guide.
  • I have added information to docs/ if required so people know about the feature.
  • I have filled in the "Changes" section below.
  • I have filled in the "How did you test this code" section below.

Changes

The LaunchDarkly importer passes matches patterns through unchanged as REGEX conditions. LaunchDarkly evaluates matches with a search (clause_regex.search(context_value) in ldclient/impl/operators.py of the Python server SDK), so the pattern can match anywhere in the value. Flagsmith evaluates REGEX with re.compile(pattern).match(value) (flag_engine.segments.evaluator._evaluate_regex), which only matches from the start. An imported matches rule therefore misses values that LaunchDarkly accepts.

On main (d031dbe), a segment with the rule email matches "gmail\.com" imports as the condition gmail\.com. An identity with email = "user@gmail.com" is in the segment in LaunchDarkly and not in the imported Flagsmith segment. The same happens with ^gmail|corp and user@corp.com.

  • matches patterns now import as .*(?:<pattern>), so they match anywhere in the value. The group keeps alternation scoped to the pattern.
  • Leading inline flags such as (?i) are kept at the start of the expression. Python 3.11+ raises re.error for global flags that are not at the start, so a plain wrap would break patterns that work today.
  • Updated the two existing expectations for the foo[0-9]{0,1} condition.

This follows the startsWith and endsWith fix. . does not match a newline, so a trait value with a newline before the match is still not found; I left that as is, consistent with the endsWith pattern.

Review effort: 1/5

How did you test this code?

Added test_process_import_request__matches_clause__matches_anywhere_in_value. It rewrites the dynamic-list fixture segment to use a matches clause, runs process_import_request, creates an identity with an email trait and checks membership through Identity.get_segments().

pattern email expected in segment
gmail\.com gmail.com yes
gmail\.com user@gmail.com yes
gmail\.com user@example.org no
^gmail|corp user@corp.com yes
^gmail|corp user@gmail.com no
(?i)GMAIL\.com user@gmail.com yes

With integrations/launch_darkly/services.py checked out from main, three cases fail (user@gmail.com, user@corp.com, (?i)GMAIL\.com) and three pass. With the fix, all six pass.

Also ran, against the test-db Postgres settings from docker/api/docker-compose.local.yml:

  • pytest tests/unit/integrations/launch_darkly/: 71 passed.
  • mypy integrations/launch_darkly/ tests/unit/integrations/launch_darkly/: no issues.
  • ruff check and ruff format --check on both paths: clean.

The full make test suite and pre-commit were not run.

@yoarajota
yoarajota requested a review from a team as a code owner October 25, 2026 03:07
@yoarajota
yoarajota requested review from emyller and removed request for a team October 25, 2026 03:07
@vercel

vercel Bot commented Oct 25, 2026

Copy link
Copy Markdown

@yoarajota is attempting to deploy a commit to the Flagsmith Team on Vercel.

A member of the Team first needs to authorize it.

@github-actions github-actions Bot added the api Issue related to the REST API label Oct 25, 2026
@coderabbitai

coderabbitai Bot commented Oct 25, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

Walkthrough

The LaunchDarkly matches conversion now uses a helper that preserves leading inline flags and allows patterns to match anywhere in a value. Tests cover substring matching, alternation, case-insensitive matching, and updated expected regex representations.

Priority: Normal

Estimated code review effort: 2 (Simple) | ~10 minutes

Merge Risk: Low · up to a2cc3

Imported LaunchDarkly rules can miss a match after a newline in a trait value. The narrow discrepancy should be fixed or explicitly accepted before merging.

Finishing Touches: 💡 1
🛠️ Fix failing CI checks: 💡
  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 1


Review info
Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: ASSERTIVE
  • Plan: Advanced
  • Run ID: a8b13a4f-c65d-4f8a-9cc0-d2106c5976cb
Commits

Reviewing files that changed from the base of the PR and between d031dbe and a2cc33e.

Files selected for processing: 2
  • api/integrations/launch_darkly/services.py
  • api/tests/unit/integrations/launch_darkly/test_services.py

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.

# start only. Leading inline flags such as `(?i)` must stay at the start of the expression.
flags = re.match(r"(?:\(\?[a-zA-Z]+\))+", pattern)
prefix = flags.group() if flags else ""
return f"{prefix}.*(?:{pattern[len(prefix) :]})"

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Minor · Functional Correctness · Quick win

Match across newlines in the prefix.

If a trait value is "first\nbar" and the LaunchDarkly pattern is bar, LaunchDarkly can find bar, but .*(?:bar) cannot reach it. . does not match a newline by default. Use a newline-matching prefix, such as (?s:.*), without changing the flags of the imported pattern. Add a multiline case to the segment-membership test. (launchdarkly.com)

Proposed change
--- "a/api/integrations/launch_darkly/services.py"
+++ "b/api/integrations/launch_darkly/services.py"
@@ -179,7 +179,7 @@
     # start only. Leading inline flags such as `(?i)` must stay at the start of the expression.
     flags = re.match(r"(?:\(\?[a-zA-Z]+\))+", pattern)
     prefix = flags.group() if flags else ""
-    return f"{prefix}.*(?:{pattern[len(prefix) :]})"
+    return f"{prefix}(?s:.*)(?:{pattern[len(prefix) :]})"
 
 
 def _convert_ld_values(values: list[str], ld_operator: str) -> list[str]:
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
return f"{prefix}.*(?:{pattern[len(prefix) :]})"
return f"{prefix}(?s:.*)(?:{pattern[len(prefix) :]})"

@codecov

codecov Bot commented Oct 25, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 97.98%. Comparing base (c582d5b) to head (a2cc33e).
⚠️ Report is 8 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #8840      +/-   ##
==========================================
- Coverage   98.88%   97.98%   -0.90%     
==========================================
  Files        1683     1683              
  Lines       70275    70288      +13     
==========================================
- Hits        69489    68875     -614     
- Misses        786     1413     +627     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

This branch has not been deployed

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

Labels

api Issue related to the REST API

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant