Skip to content
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

Merged
merged 1 commit into from
Jun 30, 2023

Conversation

ibolton336
Copy link
Member

  • 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

@ibolton336 ibolton336 changed the title Handle uploaded yaml with a single parsed value [WIP] 🐛 Handle uploaded yaml with a single parsed value Jun 30, 2023
@codecov
Copy link

codecov bot commented Jun 30, 2023

Codecov Report

Patch coverage has no change and project coverage change: -0.02 ⚠️

Comparison is base (e23fa09) 46.93% compared to head (4e87ed3) 46.92%.

❗ Current head 4e87ed3 differs from pull request most recent head c3f3b95. Consider uploading reports for the commit c3f3b95 to get more accurate results

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              
Flag Coverage Δ
unitests 46.92% <0.00%> (-0.02%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
client/src/app/common/CustomRules/rules-utils.tsx 9.23% <0.00%> (-0.15%) ⬇️
client/src/app/queries/rulesets.ts 15.00% <ø> (ø)

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

@ibolton336 ibolton336 force-pushed the mass-file-upload-fix branch 2 times, most recently from 237ba90 to 4e87ed3 Compare June 30, 2023 16:29
@ibolton336 ibolton336 marked this pull request as ready for review June 30, 2023 16:30
@ibolton336 ibolton336 changed the title [WIP] 🐛 Handle uploaded yaml with a single parsed value 🐛 Handle uploaded yaml with a single parsed value Jun 30, 2023
@ibolton336 ibolton336 changed the title 🐛 Handle uploaded yaml with a single parsed value 🐛 Handle uploaded yaml with a single parsed value Jun 30, 2023
Copy link
Collaborator

@mturley mturley left a 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[] | {};
Copy link
Collaborator

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) => {
Copy link
Collaborator

@mturley mturley Jun 30, 2023

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>
@ibolton336 ibolton336 force-pushed the mass-file-upload-fix branch from b71af87 to c3f3b95 Compare June 30, 2023 16:59
Copy link
Collaborator

@mturley mturley left a comment

Choose a reason for hiding this comment

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

LGTM!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants