Skip to content

Commit de2c8ea

Browse files
authored
Fixes #37986, #37982 - Fix UI Error when importing templates (#192)
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.
1 parent aff0e8e commit de2c8ea

File tree

2 files changed

+15
-9
lines changed

2 files changed

+15
-9
lines changed

app/services/foreman_templates/parse_result.rb

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def to_h(verbose = false)
1616
:additional_errors => @additional_errors,
1717
:additional_info => @additional_info,
1818
:exception => @exception&.message,
19-
:validation_errors => errors,
19+
:validation_errors => transformed_errors,
2020
:file => @template_file,
2121
:type => @template.present? ? @template.class.name.underscore : nil
2222
}
@@ -27,9 +27,7 @@ def to_h(verbose = false)
2727
end
2828

2929
def errors
30-
@template&.errors&.messages&.transform_values do |v|
31-
v.join(', ')
32-
end
30+
@template&.errors
3331
end
3432

3533
def corrupted_metadata
@@ -99,5 +97,13 @@ def calculate_diff(old, new)
9997
nil
10098
end
10199
end
100+
101+
private
102+
103+
def transformed_errors
104+
return {} unless errors
105+
106+
errors.to_hash.transform_values(&:last)
107+
end
102108
end
103109
end

test/unit/template_importer_test.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -222,15 +222,15 @@ def audit_comment
222222

223223
template_res = find_result(results[:results], template.name)
224224
refute template_res.imported
225-
assert_equal template_res.errors[:base], "This template is locked. Please clone it to a new template to customize."
225+
assert_equal template_res.errors.full_messages.first, "This template is locked. Please clone it to a new template to customize."
226226

227227
ptable_res = find_result(results[:results], ptable.name)
228228
refute ptable_res.imported
229-
assert_equal ptable_res.errors[:base], "This template is locked. Please clone it to a new template to customize."
229+
assert_equal ptable_res.errors.full_messages.first, "This template is locked. Please clone it to a new template to customize."
230230

231231
snippet_res = find_result(results[:results], snippet.name)
232232
refute snippet_res.imported
233-
assert_equal snippet_res.errors[:base], "This template is locked. Please clone it to a new template to customize."
233+
assert_equal snippet_res.errors.full_messages.first, "This template is locked. Please clone it to a new template to customize."
234234

235235
assert_equal @template_template, template.template
236236
assert_equal @ptable_layout, ptable.layout
@@ -317,11 +317,11 @@ def keep_options_test_common(lock_setting)
317317

318318
template_res = find_result(results[:results], template.name)
319319
refute template_res.imported
320-
assert_equal template_res.errors[:base], "This template is locked. Please clone it to a new template to customize."
320+
assert_equal template_res.errors.full_messages.first, "This template is locked. Please clone it to a new template to customize."
321321

322322
ptable_res = find_result(results[:results], ptable.name)
323323
refute ptable_res.imported
324-
assert_equal ptable_res.errors[:base], "This template is locked. Please clone it to a new template to customize."
324+
assert_equal ptable_res.errors.full_messages.first, "This template is locked. Please clone it to a new template to customize."
325325
results
326326
end
327327
end

0 commit comments

Comments
 (0)