-
Notifications
You must be signed in to change notification settings - Fork 44
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
🐛 Handle uploaded yaml with a single parsed value #1081
Conversation
ibolton336
commented
Jun 30, 2023
- 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 Custom target edit form "save button" always enabled #1073
Codecov ReportPatch coverage has no change and project coverage change:
Additional details and impacted files@@ Coverage Diff @@
## main #1081 +/- ##
==========================================
- Coverage 46.93% 46.92% -0.02%
==========================================
Files 177 177
Lines 4440 4441 +1
Branches 1024 1025 +1
==========================================
Hits 2084 2084
- Misses 2283 2284 +1
Partials 73 73
Flags with carried forward coverage won't be shown. Click here to find out more.
☔ View full report in Codecov by Sentry. |
237ba90
to
4e87ed3
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A couple thoughts -- might not be a problem?
@@ -11,12 +11,13 @@ export const checkRuleFileType = (filename: string): RuleFileType => { | |||
return "XML"; | |||
} else return null; | |||
}; | |||
|
|||
type ParsedYaml = any[] | {}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since we are accessing the labels
property on the parsed YAML array elements, can we use a type here that contains that property instead of any
? Maybe something like:
type ParsedYamlElement = { labels?: string[]; };
type ParsedYaml = ParsedYamlElement[] | {};
const yamlLabels = yamlDoc?.reduce((acc, parsedLine) => { | ||
const yamlDoc: ParsedYaml = yaml.load(file.data) as ParsedYaml; | ||
const yamlList = Array.isArray(yamlDoc) ? yamlDoc : [yamlDoc]; | ||
const yamlLabels = yamlList?.reduce((acc, parsedLine) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also (and it's fine if we leave this because the reduce
was pre-existing) if your reduce is just adding elements to an accumulated array, you can simplify it by using flatMap instead. Each iteration of a flatMap returns an array, and the arrays are all flattened/appended together at the end.
const yamlLabels = yamlList?.flatMap((parsedLine) => {
return parsedLine?.labels ? parsedLine?.labels : [];
}) || [];
Note that this is the same as .map(...).flat(1)
.
Also, looking at the behavior of getLabels
, if the same label appears more than once in the YAML it's going to end up duplicated in otherLabels
and allLabels
, right? I wonder if that's a problem. If it is, we could deduplicate the array by using a Set.
const yamlLabels = Array.from(new Set(yamlList?.flatMap((parsedLine) => {
return parsedLine?.labels ? parsedLine?.labels : [];
}) || []));
Signed-off-by: ibolton336 <ibolton@redhat.com>
b71af87
to
c3f3b95
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!