Skip to content

feat: PZ Rule Creation: Sample results/query should not clear when user changes rule type [BED-8303]#2904

Merged
dcairnsspecterops merged 8 commits into
mainfrom
BED-8303-rule-state-switching-cypher-object-id
Jun 25, 2026
Merged

feat: PZ Rule Creation: Sample results/query should not clear when user changes rule type [BED-8303]#2904
dcairnsspecterops merged 8 commits into
mainfrom
BED-8303-rule-state-switching-cypher-object-id

Conversation

@dcairnsspecterops

@dcairnsspecterops dcairnsspecterops commented Jun 18, 2026

Copy link
Copy Markdown
Contributor

Description

PZ-rule-type-change.mov

Motivation and Context

Resolves BED-8303

Users do not like losing their work when changing rule types during PX rule creation. We should store their work between rule type changes.

Therefore, this PR changes the logic when:

  1. a user is creating/updating a PZ cypher rule,
  2. runs the query to produce seeds
  3. changes to an object id rule type
  4. then has a better idea and chooses to go back to the cypher rule type

Previously: their sample results and query would be lost, since we did not store seeds in the context between rule type changes.

Now: we will store the sample results in prevSeeds, and use this value to hydrate the rule form. When the user changes rule type, their seeds (which contain queries, etc) will be maintained. This will clear out when the user leaves the rule form.

How Has This Been Tested?

Please describe in detail how you tested your changes.
Include details of your testing environment, and the tests you ran to
see how your change affects other areas of the code, etc.

Screenshots (optional):

Types of changes

  • Chore (a change that does not modify the application functionality)
  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Database Migrations

Checklist:

Summary by CodeRabbit

Bug Fixes

  • Rule form now preserves your prior seed/query results and object selections when switching between rule type options, preventing loss of configured inputs.
  • Restores the appropriate preview/editor state after switching back, so the Cypher sample results and run readiness remain consistent.

Tests

  • Added coverage to confirm that switching between Object ID and Cypher preserves the correct sample results and keeps the editor/run controls in the expected state.

@coderabbitai

coderabbitai Bot commented Jun 18, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review
📝 Walkthrough

Walkthrough

RuleFormState gains prevSeeds and prevSelectedObjects fields. The set-rule-type reducer action is changed from resetting seeds and selectedObjects to an empty array to a swap-and-restore behavior: current values are stored in their previous counterparts and restored from those fields when toggling rule types. SeedSelection removes now-redundant seed and stale-preview reset dispatches from its rule-type change handler. A new integration test validates that seeds and selected objects are preserved across rule-type toggles.

Changes

Seed Retention on Rule-Type Toggle

Layer / File(s) Summary
State shape, initialization, and reducer swap behavior
src/views/PrivilegeZones/Save/RuleForm/types.ts, src/views/PrivilegeZones/Save/RuleForm/RuleForm.tsx, src/views/PrivilegeZones/Save/RuleForm/SeedSelection.tsx
RuleFormState adds prevSeeds: SelectorSeedRequest[] and initializes both prevSeeds and prevSelectedObjects to [] in initialState. The set-rule-type reducer branch swaps seedsprevSeeds and selectedObjectsprevSelectedObjects instead of clearing them. SeedSelection's onValueChange handler drops the now-redundant seeds: [] and staleCypherPreview: true dispatches.
Test coverage of seed preservation across rule-type toggles
src/views/PrivilegeZones/Save/RuleForm/RuleForm.test.tsx
Adds import of SeedTypeObjectId. New test mocks preview-selectors to return different preview members for Object ID vs. Cypher seeds, renders RuleForm, switches rule types multiple times, and asserts that the prior rule type's seeds and sample results are preserved while the inactive set is cleared, and that editor and button state are rehydrated.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~15 minutes

Suggested reviewers

  • nicole-m-martin
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 inconclusive)

Check name Status Explanation Resolution
Description check ❓ Inconclusive The description addresses the motivation and context, explaining the problem and solution clearly, and includes the Jira ticket reference. However, the 'How Has This Been Tested?' section is empty and checklist items remain incomplete. Complete the 'How Has This Been Tested?' section with details of testing approaches, and update the checklist to reflect which items have been completed.
✅ Passed checks (4 passed)
Check name Status Explanation
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Title check ✅ Passed The title accurately describes the main change: preventing sample results and queries from clearing when users change rule types during privilege zone rule creation.

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

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch BED-8303-rule-state-switching-cypher-object-id

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 added the user interface A pull request containing changes affecting the UI code. label Jun 18, 2026

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In
`@packages/javascript/bh-shared-ui/src/views/PrivilegeZones/Save/RuleForm/RuleForm.tsx`:
- Around line 133-147: The issue is that when toggling rule types, the code
restores seeds from prevSeeds but always clears selectedObjects to an empty
array, causing a mismatch between displayed selections and the actual payload.
Similar to how prevSeeds preserves user work across toggles, you need to also
preserve and restore selectedObjects. Add a prevSelectedObjects field to the
state, and when swapping rule types, swap selectedObjects with
prevSelectedObjects just as you do with seeds and prevSeeds. This ensures that
when returning to ObjectID rule type, the previously selected objects are
restored along with their corresponding seeds.
🪄 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: Repository YAML (base), Organization UI (inherited)

Review profile: CHILL

Plan: Pro

Run ID: ec2651db-0901-4b88-9149-9f6cfd79d2f6

📥 Commits

Reviewing files that changed from the base of the PR and between d979c54 and 8458cce.

📒 Files selected for processing (3)
  • packages/javascript/bh-shared-ui/src/views/PrivilegeZones/Save/RuleForm/RuleForm.tsx
  • packages/javascript/bh-shared-ui/src/views/PrivilegeZones/Save/RuleForm/SeedSelection.tsx
  • packages/javascript/bh-shared-ui/src/views/PrivilegeZones/Save/RuleForm/types.ts
💤 Files with no reviewable changes (1)
  • packages/javascript/bh-shared-ui/src/views/PrivilegeZones/Save/RuleForm/SeedSelection.tsx

* when we introduce a third rule type, simple toggling will be insufficient
* and we will need to store all 3 seed types explicitly.
* That's because we reuse seeds state for _both_ rule types,
* even though we store `selectedObjects` separately

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

It might be worth clarifying in this comment that cypher queries will not be preserved if "Run" is not clicked first.

Also minor typo: Tthis on line 141

@maffkipp

Copy link
Copy Markdown
Contributor

Would it be possible to add a test covering this behavior in RuleForm.test.tsx?

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In
`@packages/javascript/bh-shared-ui/src/views/PrivilegeZones/Save/RuleForm/RuleForm.test.tsx`:
- Around line 486-488: The test selector for the Cypher textbox relies on
className matching which is brittle and will break if styling changes. Add a
data-testid attribute to the textbox wrapper in the PrivilegeZonesCypherEditor
component (e.g., 'privilege-zones_cypher-editor-input'), then replace all
occurrences of the className-based selection pattern that retrieves the textbox
with screen.getByTestId() or screen.queryByTestId() using the new test ID for a
stable, functional selector that is decoupled from CSS class names.
🪄 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: Repository YAML (base), Organization UI (inherited)

Review profile: CHILL

Plan: Pro

Run ID: 6ab8a0c4-5a49-4daa-8132-8418b10c2db6

📥 Commits

Reviewing files that changed from the base of the PR and between 401eaff and 4bb8c8a.

📒 Files selected for processing (1)
  • packages/javascript/bh-shared-ui/src/views/PrivilegeZones/Save/RuleForm/RuleForm.test.tsx

Comment on lines +486 to +488
const textBoxes = screen.getAllByRole('textbox');
const cypherTextBox = textBoxes.find((box) => box.className === 'flex-1');
await user.click(cypherTextBox!);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

📐 Maintainability & Code Quality | 🟡 Minor

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Verify whether a stable selector already exists for the cypher editor input.
rg -n "className === 'flex-1'" packages/javascript/bh-shared-ui/src/views/PrivilegeZones/Save/RuleForm/RuleForm.test.tsx
rg -n "PrivilegeZonesCypherEditor" packages/javascript/bh-shared-ui/src/views/PrivilegeZones/Save/RuleForm -g "*.tsx"
rg -n "data-testid|aria-label|label|role=" packages/javascript/bh-shared-ui/src/views/PrivilegeZones/Save/RuleForm -g "*.tsx"

Repository: SpecterOps/BloodHound

Length of output: 6600


🏁 Script executed:

fd -t f "PrivilegeZonesCypherEditor" packages/javascript/bh-shared-ui/src/views/PrivilegeZones --maxdepth 3

Repository: SpecterOps/BloodHound

Length of output: 396


🏁 Script executed:

cat -n packages/javascript/bh-shared-ui/src/views/PrivilegeZones/PrivilegeZonesCypherEditor.tsx | head -100

Repository: SpecterOps/BloodHound

Length of output: 280


🏁 Script executed:

cat -n packages/javascript/bh-shared-ui/src/views/PrivilegeZones/PrivilegeZonesCypherEditor/PrivilegeZonesCypherEditor.tsx

Repository: SpecterOps/BloodHound

Length of output: 8670


🏁 Script executed:

cat -n packages/javascript/bh-shared-ui/src/views/PrivilegeZones/Save/RuleForm/RuleForm.test.tsx | sed -n '480,495p'

Repository: SpecterOps/BloodHound

Length of output: 951


🏁 Script executed:

cat -n packages/javascript/bh-shared-ui/src/views/PrivilegeZones/Save/RuleForm/RuleForm.test.tsx | sed -n '360,370p; 395,405p; 545,555p'

Repository: SpecterOps/BloodHound

Length of output: 1574


Add a stable selector for the Cypher textbox instead of relying on class-name matching.

The pattern box.className === 'flex-1' at lines 365, 400, 487, and 551 is style-coupled and will break on non-functional UI changes. The PrivilegeZonesCypherEditor component already exposes role='textbox' on the editor wrapper; consider adding a dedicated data-testid (e.g., data-testid='privilege-zones_cypher-editor-input') to the textbox wrapper in the component and querying it directly in tests, or using a more specific role-based selector combined with accessible naming.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In
`@packages/javascript/bh-shared-ui/src/views/PrivilegeZones/Save/RuleForm/RuleForm.test.tsx`
around lines 486 - 488, The test selector for the Cypher textbox relies on
className matching which is brittle and will break if styling changes. Add a
data-testid attribute to the textbox wrapper in the PrivilegeZonesCypherEditor
component (e.g., 'privilege-zones_cypher-editor-input'), then replace all
occurrences of the className-based selection pattern that retrieves the textbox
with screen.getByTestId() or screen.queryByTestId() using the new test ID for a
stable, functional selector that is decoupled from CSS class names.

@dcairnsspecterops dcairnsspecterops changed the title feat: PX Rule Creation: Sample results/query should not clear when user changes rule type [BED-8303] feat: PZ Rule Creation: Sample results/query should not clear when user changes rule type [BED-8303] Jun 23, 2026
@dcairnsspecterops dcairnsspecterops merged commit 42d97b2 into main Jun 25, 2026
12 of 13 checks passed
@dcairnsspecterops dcairnsspecterops deleted the BED-8303-rule-state-switching-cypher-object-id branch June 25, 2026 00:03
@github-actions github-actions Bot locked and limited conversation to collaborators Jun 25, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

user interface A pull request containing changes affecting the UI code.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants