feat: Disallow new cypher rule if query yields no results (BED-7739)#2835
feat: Disallow new cypher rule if query yields no results (BED-7739)#2835dcairnsspecterops wants to merge 8 commits into
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Repository YAML (base), Organization UI (inherited) Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (4)
🚧 Files skipped from review as they are similar to previous changes (2)
📝 WalkthroughWalkthroughCypher editor validation is added to the privilege zones rule form: a ChangesCypher Editor Validation Flow
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Suggested labels
Suggested reviewers
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 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 281-291: The onSubmit handler currently shows a notification when
cypherEditorInvalid is true but continues to call handlePatchRule or
handleCreateRule; update the onSubmit callback (the const onSubmit:
SubmitHandler<RuleFormInputs> using cypherEditorInvalid, addNotification,
handleCreateRule, handlePatchRule, ruleId) to return early after
addNotification(CYPHER_MUST_HAVE_RESULTS) when cypherEditorInvalid is true so
that neither handlePatchRule nor handleCreateRule run for invalid Cypher.
In
`@packages/javascript/bh-shared-ui/src/views/PrivilegeZones/Save/RuleForm/SeedSelectionPreview.tsx`:
- Around line 63-74: The effect currently treats any fetch completion
(sampleResultsFetched) as a trigger and may mark cypherEditorInvalid for
non-Cypher rules or failed requests; update the guard so it only runs when the
preview is for a Cypher rule and the sample results successfully returned.
Concretely, add a rule-type check (e.g., ruleType === 'cypher') and replace or
augment sampleResultsFetched with an explicit success flag (e.g.,
sampleResultsStatus === 'success' or !sampleResultsError) before computing
cypherQueryIsEmpty and dispatching CYPHER_MUST_HAVE_RESULTS; keep references to
directObjects, expandedObjects, cypherEditorInvalid, dispatch, addNotification
and CYPHER_MUST_HAVE_RESULTS when applying the change.
🪄 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: ecc1adaa-4232-41af-ac21-7c8e7090a7bb
📒 Files selected for processing (6)
packages/javascript/bh-shared-ui/src/views/PrivilegeZones/PrivilegeZonesCypherEditor/PrivilegeZonesCypherEditor.tsxpackages/javascript/bh-shared-ui/src/views/PrivilegeZones/Save/RuleForm/RuleForm.tsxpackages/javascript/bh-shared-ui/src/views/PrivilegeZones/Save/RuleForm/RuleFormContext.tsxpackages/javascript/bh-shared-ui/src/views/PrivilegeZones/Save/RuleForm/SeedSelectionPreview.tsxpackages/javascript/bh-shared-ui/src/views/PrivilegeZones/Save/RuleForm/rule-form-utils.tspackages/javascript/bh-shared-ui/src/views/PrivilegeZones/Save/RuleForm/types.ts
| const onSubmit: SubmitHandler<RuleFormInputs> = useCallback(() => { | ||
| if (cypherEditorInvalid) { | ||
| addNotification(CYPHER_MUST_HAVE_RESULTS); | ||
| } | ||
|
|
||
| if (ruleId !== '') { | ||
| handlePatchRule(); | ||
| } else { | ||
| handleCreateRule(); | ||
| } | ||
| }, [cypherEditorInvalid, addNotification, handleCreateRule, handlePatchRule, ruleId]); |
There was a problem hiding this comment.
Stop submission when Cypher validation fails.
Line [282]-Line [290] still runs handlePatchRule/handleCreateRule after showing the notification, so invalid Cypher can still be saved.
Proposed fix
const onSubmit: SubmitHandler<RuleFormInputs> = useCallback(() => {
if (cypherEditorInvalid) {
addNotification(CYPHER_MUST_HAVE_RESULTS);
+ return;
}
if (ruleId !== '') {
handlePatchRule();
} else {
handleCreateRule();
}
}, [cypherEditorInvalid, addNotification, handleCreateRule, handlePatchRule, ruleId]);🤖 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.tsx`
around lines 281 - 291, The onSubmit handler currently shows a notification when
cypherEditorInvalid is true but continues to call handlePatchRule or
handleCreateRule; update the onSubmit callback (the const onSubmit:
SubmitHandler<RuleFormInputs> using cypherEditorInvalid, addNotification,
handleCreateRule, handlePatchRule, ruleId) to return early after
addNotification(CYPHER_MUST_HAVE_RESULTS) when cypherEditorInvalid is true so
that neither handlePatchRule nor handleCreateRule run for invalid Cypher.
| useEffect(() => { | ||
| const emptyResults = directObjects?.length === 0 && expandedObjects?.length === 0; | ||
| const undefinedResults = !directObjects && !expandedObjects; | ||
| const cypherQueryIsEmpty = sampleResultsFetched && (emptyResults || undefinedResults); | ||
|
|
||
| if (cypherQueryIsEmpty && !cypherEditorInvalid) { | ||
| addNotification(CYPHER_MUST_HAVE_RESULTS); | ||
| dispatch({ type: 'set-cypher-editor-validation', cypherEditorInvalid: true }); | ||
| } else if (!cypherQueryIsEmpty && cypherEditorInvalid) { | ||
| dispatch({ type: 'set-cypher-editor-validation', cypherEditorInvalid: false }); | ||
| } | ||
| }, [directObjects, expandedObjects, cypherEditorInvalid, dispatch, sampleResultsFetched, addNotification]); |
There was a problem hiding this comment.
Gate invalid-state updates to successful Cypher previews only.
Line [66]-Line [73] uses isFetched and no rule-type guard, so request failures or non-Cypher previews can incorrectly set cypherEditorInvalid and trigger the Cypher-specific toast.
Proposed fix
- const { data: sampleResults, isFetched: sampleResultsFetched } = useQuery({
+ const { data: sampleResults, isSuccess: sampleResultsSuccess } = useQuery({
queryKey: ['privilege-zones', 'preview-selectors', ruleType, seeds, expansion],
queryFn: async ({ signal }) => {
return apiClient
.assetGroupTagsPreviewSelectors({ seeds, expansion }, { signal })
.then((res) => res.data.data['members']);
},
retry: false,
refetchOnWindowFocus: false,
enabled: seeds.length > 0,
});
@@
useEffect(() => {
- const emptyResults = directObjects?.length === 0 && expandedObjects?.length === 0;
- const undefinedResults = !directObjects && !expandedObjects;
- const cypherQueryIsEmpty = sampleResultsFetched && (emptyResults || undefinedResults);
+ const shouldValidateCypher = ruleType === SeedTypeCypher && sampleResultsSuccess;
+ if (!shouldValidateCypher) {
+ if (cypherEditorInvalid) {
+ dispatch({ type: 'set-cypher-editor-validation', cypherEditorInvalid: false });
+ }
+ return;
+ }
+
+ const cypherQueryIsEmpty = (sampleResults?.length ?? 0) === 0;
if (cypherQueryIsEmpty && !cypherEditorInvalid) {
addNotification(CYPHER_MUST_HAVE_RESULTS);
dispatch({ type: 'set-cypher-editor-validation', cypherEditorInvalid: true });
} else if (!cypherQueryIsEmpty && cypherEditorInvalid) {
dispatch({ type: 'set-cypher-editor-validation', cypherEditorInvalid: false });
}
- }, [directObjects, expandedObjects, cypherEditorInvalid, dispatch, sampleResultsFetched, addNotification]);
+ }, [ruleType, sampleResults, sampleResultsSuccess, cypherEditorInvalid, dispatch, addNotification]);🤖 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/SeedSelectionPreview.tsx`
around lines 63 - 74, The effect currently treats any fetch completion
(sampleResultsFetched) as a trigger and may mark cypherEditorInvalid for
non-Cypher rules or failed requests; update the guard so it only runs when the
preview is for a Cypher rule and the sample results successfully returned.
Concretely, add a rule-type check (e.g., ruleType === 'cypher') and replace or
augment sampleResultsFetched with an explicit success flag (e.g.,
sampleResultsStatus === 'success' or !sampleResultsError) before computing
cypherQueryIsEmpty and dispatching CYPHER_MUST_HAVE_RESULTS; keep references to
directObjects, expandedObjects, cypherEditorInvalid, dispatch, addNotification
and CYPHER_MUST_HAVE_RESULTS when applying the change.
Description
Describe your changes in detail
Motivation and Context
Resolves <TICKET_OR_ISSUE_NUMBER>
Why is this change required? What problem does it solve?
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
Checklist:
Summary by CodeRabbit