Skip to content

Commit 4fdda9b

Browse files
authored
[One Workflow] Fix yaml document parsing for executions (#241315)
## Summary Issue: elastic/security-team#14485 Fixes part of the problem with broken decorators in executions. Done: Fixes the step types and triggers decorators. Pending: Fix step highlighting. https://github.com/user-attachments/assets/edf861ad-b715-4842-9d53-0cc794574fa5
1 parent 67499dd commit 4fdda9b

File tree

5 files changed

+36
-18
lines changed

5 files changed

+36
-18
lines changed

src/platform/plugins/shared/workflows_management/public/pages/workflow_detail/ui/workflow_detail_editor.test.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ jest.mock('react-redux', () => ({
4242
}));
4343

4444
// Mock lazy loaded components
45-
const WorkflowYAMLEditorMock = ({ workflowYaml, readOnly, highlightDiff }: any) => (
45+
const WorkflowYAMLEditorMock = ({ workflowYaml, isExecutionYaml, highlightDiff }: any) => (
4646
<div data-test-subj="workflow-yaml-editor">
47-
{readOnly && <span data-test-subj="read-only-indicator">{'Read Only'}</span>}
47+
{isExecutionYaml && <span data-test-subj="read-only-indicator">{'Read Only'}</span>}
4848
{highlightDiff && <span data-test-subj="highlight-diff-indicator">{'Highlight Diff'}</span>}
4949
<div data-test-subj="yaml-content">{workflowYaml || 'No YAML'}</div>
5050
</div>

src/platform/plugins/shared/workflows_management/public/pages/workflow_detail/ui/workflow_detail_editor.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ export const WorkflowDetailEditor = React.memo<WorkflowDetailEditorProps>(({ hig
124124
<WorkflowYAMLEditor
125125
stepExecutions={execution?.stepExecutions}
126126
workflowYaml={overrideYamlValue ?? workflowYaml}
127-
readOnly={activeTab === 'executions'}
127+
isExecutionYaml={activeTab === 'executions'}
128128
highlightDiff={highlightDiff}
129129
selectedExecutionId={selectedExecutionId}
130130
onStepActionClicked={handleStepRun}

src/platform/plugins/shared/workflows_management/public/widgets/workflow_yaml_editor/ui/workflow_yaml_editor.stories.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,21 +86,21 @@ type Story = StoryObj<typeof WorkflowYAMLEditor>;
8686
export const Default: Story = {
8787
args: {
8888
workflowYaml,
89-
readOnly: false,
89+
isExecutionYaml: false,
9090
},
9191
};
9292

9393
export const ReadOnly: Story = {
9494
args: {
9595
workflowYaml,
96-
readOnly: true,
96+
isExecutionYaml: true,
9797
},
9898
};
9999

100100
export const WithStepExecutions: Story = {
101101
args: {
102102
workflowYaml,
103-
readOnly: false,
103+
isExecutionYaml: false,
104104
stepExecutions: [
105105
{
106106
stepId: 'analysis',

src/platform/plugins/shared/workflows_management/public/widgets/workflow_yaml_editor/ui/workflow_yaml_editor.test.tsx

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -231,16 +231,20 @@ steps:
231231
<WorkflowYAMLEditor
232232
{...defaultProps}
233233
workflowYaml={yamlWithAlertTrigger}
234-
readOnly={false}
234+
isExecutionYaml={false}
235235
/>
236236
);
237237

238238
expect(document.querySelector('[data-testid="yaml-editor"]')).toBeInTheDocument();
239239
});
240240

241-
it('renders in readOnly mode', () => {
241+
it('renders in readOnly mode when isExecutionYaml is true', () => {
242242
renderWithProviders(
243-
<WorkflowYAMLEditor {...defaultProps} workflowYaml={yamlWithAlertTrigger} readOnly={true} />
243+
<WorkflowYAMLEditor
244+
{...defaultProps}
245+
workflowYaml={yamlWithAlertTrigger}
246+
isExecutionYaml={true}
247+
/>
244248
);
245249

246250
expect(document.querySelector('[data-testid="yaml-editor"]')).toBeInTheDocument();
@@ -262,7 +266,11 @@ steps:
262266
// Should not throw an error
263267
expect(() => {
264268
renderWithProviders(
265-
<WorkflowYAMLEditor {...defaultProps} workflowYaml={invalidYaml} readOnly={false} />
269+
<WorkflowYAMLEditor
270+
{...defaultProps}
271+
workflowYaml={invalidYaml}
272+
isExecutionYaml={false}
273+
/>
266274
);
267275
}).not.toThrow();
268276

src/platform/plugins/shared/workflows_management/public/widgets/workflow_yaml_editor/ui/workflow_yaml_editor.tsx

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { css } from '@emotion/react';
1414
import type { SchemasSettings } from 'monaco-yaml';
1515
import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react';
1616
import { useDispatch, useSelector } from 'react-redux';
17-
import type YAML from 'yaml';
17+
import YAML from 'yaml';
1818
import { FormattedMessage } from '@kbn/i18n-react';
1919
import { monaco } from '@kbn/monaco';
2020
import { isTriggerType } from '@kbn/workflows';
@@ -116,7 +116,7 @@ const editorOptions: monaco.editor.IStandaloneEditorConstructionOptions = {
116116

117117
export interface WorkflowYAMLEditorProps {
118118
workflowYaml: string;
119-
readOnly?: boolean;
119+
isExecutionYaml?: boolean;
120120
stepExecutions?: WorkflowStepExecutionDto[];
121121
'data-testid'?: string;
122122
highlightDiff?: boolean;
@@ -130,7 +130,7 @@ export interface WorkflowYAMLEditorProps {
130130

131131
export const WorkflowYAMLEditor = ({
132132
workflowYaml,
133-
readOnly = false,
133+
isExecutionYaml = false,
134134
stepExecutions,
135135
highlightDiff = false,
136136
onMount,
@@ -174,9 +174,19 @@ export const WorkflowYAMLEditor = ({
174174
// Refs / Disposables for Monaco providers
175175
const disposablesRef = useRef<monaco.IDisposable[]>([]);
176176
const focusedStepInfo = useSelector(selectFocusedStepInfo);
177-
const yamlDocument = useSelector(selectYamlDocument);
178177
const workflowYamlSchemaLoose = useSelector(selectSchemaLoose);
179-
const yamlDocumentRef = useRef<YAML.Document | null>(null);
178+
// The current yaml document in the editor (could be unsaved)
179+
const currentYamlDocument = useSelector(selectYamlDocument);
180+
181+
const yamlDocument = useMemo(() => {
182+
// if the yaml comes from an execution, we need to parse it to get the correct yaml document
183+
if (isExecutionYaml) {
184+
return YAML.parseDocument(workflowYaml, { keepSourceTokens: true });
185+
}
186+
return currentYamlDocument;
187+
}, [isExecutionYaml, workflowYaml, currentYamlDocument]);
188+
189+
const yamlDocumentRef = useRef<YAML.Document | null>(yamlDocument ?? null);
180190
yamlDocumentRef.current = yamlDocument || null;
181191

182192
const focusedStepInfoRef = useRef<StepInfo | undefined>(focusedStepInfo);
@@ -416,7 +426,7 @@ export const WorkflowYAMLEditor = ({
416426
editor: editorRef.current,
417427
yamlDocument: yamlDocument || null,
418428
isEditorMounted,
419-
readOnly,
429+
readOnly: isExecutionYaml,
420430
});
421431

422432
const updateContainerPosition = (
@@ -486,8 +496,8 @@ export const WorkflowYAMLEditor = ({
486496
const completionProvider = useCompletionProvider();
487497

488498
const options = useMemo(() => {
489-
return { ...editorOptions, readOnly };
490-
}, [readOnly]);
499+
return { ...editorOptions, readOnly: isExecutionYaml };
500+
}, [isExecutionYaml]);
491501

492502
useEffect(() => {
493503
// Monkey patching

0 commit comments

Comments
 (0)