From bfe83ffaf99166058aa939865d3a38ff01f339de Mon Sep 17 00:00:00 2001 From: Ian Bolton Date: Fri, 30 Jun 2023 13:05:42 -0400 Subject: [PATCH] :bug: Handle uploaded yaml with a single parsed value (#1081) - Fixes a bug with the rules selector that was writing ruleset data to each rule in the map. This caused all uploaded rules to show the ruleset name rather than the rule name. - Fixes an issue with yaml parsing. yaml.load returns an object when only one value is found rather than an array when multiple values are found from an uploaded rule file. This caused the app to blow up & freeze when trying to iterate through the single object. - TODO: Fix the enabled save button on edit with no dirty fields for the custom-target-form #1073 Signed-off-by: ibolton336 --- .../src/app/common/CustomRules/rules-utils.tsx | 16 +++++++++++----- .../migration-targets/custom-target-form.tsx | 13 +++---------- client/src/app/queries/rulesets.ts | 2 +- 3 files changed, 15 insertions(+), 16 deletions(-) diff --git a/client/src/app/common/CustomRules/rules-utils.tsx b/client/src/app/common/CustomRules/rules-utils.tsx index e98cfef23f..8ddf2fcdf8 100644 --- a/client/src/app/common/CustomRules/rules-utils.tsx +++ b/client/src/app/common/CustomRules/rules-utils.tsx @@ -11,15 +11,21 @@ export const checkRuleFileType = (filename: string): RuleFileType => { return "XML"; } else return null; }; +type ParsedYamlElement = { labels?: string[] }; +type ParsedYaml = ParsedYamlElement[] | {}; export const parseRules = (file: IReadFile): ParsedRule => { if (file.data) { if (checkRuleFileType(file.fileName) === "YAML") { - const yamlDoc = yaml.load(file.data) as any[]; - const yamlLabels = yamlDoc?.reduce((acc, parsedLine) => { - const newLabels = parsedLine?.labels ? parsedLine?.labels : []; - return [...acc, ...newLabels]; - }, []); + const yamlDoc: ParsedYaml = yaml.load(file.data) as ParsedYaml; + const yamlList = Array.isArray(yamlDoc) ? yamlDoc : [yamlDoc]; + const yamlLabels = Array.from( + new Set( + yamlList?.flatMap((parsedLine) => { + return parsedLine?.labels ? parsedLine?.labels : []; + }) || [] + ) + ); const allLabels = getLabels(yamlLabels); return { source: allLabels?.sourceLabel, diff --git a/client/src/app/pages/migration-targets/custom-target-form.tsx b/client/src/app/pages/migration-targets/custom-target-form.tsx index 4e70079db6..c859ae457f 100644 --- a/client/src/app/pages/migration-targets/custom-target-form.tsx +++ b/client/src/app/pages/migration-targets/custom-target-form.tsx @@ -113,13 +113,6 @@ export const CustomTargetForm: React.FC = ({ }; }); - const toOptionWithValue = ( - value: IdentityDropdown - ): OptionWithValue => ({ - value, - toString: () => value?.name || "", - }); - const { rulesets } = useFetchRulesets(); const validationSchema: yup.SchemaOf = yup @@ -175,12 +168,12 @@ export const CustomTargetForm: React.FC = ({ }); const getInitialCustomRulesFilesData = () => - ruleset?.rules?.map((ruleset): IReadFile => { - const emptyFile = new File(["empty"], ruleset.name, { + ruleset?.rules?.map((rule): IReadFile => { + const emptyFile = new File(["empty"], rule.name, { type: "placeholder", }); return { - fileName: ruleset.name, + fileName: rule.name, fullFile: emptyFile, loadResult: "success", loadPercentage: 100, diff --git a/client/src/app/queries/rulesets.ts b/client/src/app/queries/rulesets.ts index 4c7f9b8776..7061cdbd4b 100644 --- a/client/src/app/queries/rulesets.ts +++ b/client/src/app/queries/rulesets.ts @@ -31,7 +31,7 @@ export const useFetchRulesets = () => { target: labels.targetLabel, }; return { - ...ruleset, + ...rule, metadata: transformedMetadata, }; });