From d813b0f4f543a1a1c011ad789e534e4e83e4f790 Mon Sep 17 00:00:00 2001 From: adamlazik1 Date: Wed, 6 Nov 2024 12:44:24 +0000 Subject: [PATCH] Fixes #37986, #37982 - Fix UI Error when importing templates Importing templates through UI causes an error and the page with import results does not load. Steps to Reproduce: 1. Have foreman instance with the templates plugin on nightly (error originates in one of the newest commits so it is not on stream yet). 2. Navigate to Hosts > Templates > Sync Templates 3. Import templates from https://github.com/theforeman/community-templates.git Actual behavior: The page with results does not load and two popups are displayed. One saying that templates have been imported succesfully and another saying that there has been an error. Expected behavior: Page with results loads normally and displays templates correctly. --- app/services/foreman_templates/parse_result.rb | 14 ++++++++++---- test/unit/template_importer_test.rb | 10 +++++----- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/app/services/foreman_templates/parse_result.rb b/app/services/foreman_templates/parse_result.rb index 48721324..9280acf8 100644 --- a/app/services/foreman_templates/parse_result.rb +++ b/app/services/foreman_templates/parse_result.rb @@ -16,7 +16,7 @@ def to_h(verbose = false) :additional_errors => @additional_errors, :additional_info => @additional_info, :exception => @exception&.message, - :validation_errors => errors, + :validation_errors => transformed_errors, :file => @template_file, :type => @template.present? ? @template.class.name.underscore : nil } @@ -27,9 +27,7 @@ def to_h(verbose = false) end def errors - @template&.errors&.messages&.transform_values do |v| - v.join(', ') - end + @template&.errors end def corrupted_metadata @@ -99,5 +97,13 @@ def calculate_diff(old, new) nil end end + + private + + def transformed_errors + return {} unless errors + + errors.to_hash.transform_values(&:last) + end end end diff --git a/test/unit/template_importer_test.rb b/test/unit/template_importer_test.rb index daf8780c..18b0d551 100644 --- a/test/unit/template_importer_test.rb +++ b/test/unit/template_importer_test.rb @@ -222,15 +222,15 @@ def audit_comment template_res = find_result(results[:results], template.name) refute template_res.imported - assert_equal template_res.errors[:base], "This template is locked. Please clone it to a new template to customize." + assert_equal template_res.errors.full_messages.first, "This template is locked. Please clone it to a new template to customize." ptable_res = find_result(results[:results], ptable.name) refute ptable_res.imported - assert_equal ptable_res.errors[:base], "This template is locked. Please clone it to a new template to customize." + assert_equal ptable_res.errors.full_messages.first, "This template is locked. Please clone it to a new template to customize." snippet_res = find_result(results[:results], snippet.name) refute snippet_res.imported - assert_equal snippet_res.errors[:base], "This template is locked. Please clone it to a new template to customize." + assert_equal snippet_res.errors.full_messages.first, "This template is locked. Please clone it to a new template to customize." assert_equal @template_template, template.template assert_equal @ptable_layout, ptable.layout @@ -317,11 +317,11 @@ def keep_options_test_common(lock_setting) template_res = find_result(results[:results], template.name) refute template_res.imported - assert_equal template_res.errors[:base], "This template is locked. Please clone it to a new template to customize." + assert_equal template_res.errors.full_messages.first, "This template is locked. Please clone it to a new template to customize." ptable_res = find_result(results[:results], ptable.name) refute ptable_res.imported - assert_equal ptable_res.errors[:base], "This template is locked. Please clone it to a new template to customize." + assert_equal ptable_res.errors.full_messages.first, "This template is locked. Please clone it to a new template to customize." results end end