apollo_commons_ruby 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (48) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +8 -0
  3. data/CODE_OF_CONDUCT.md +74 -0
  4. data/Gemfile +6 -0
  5. data/Gemfile.lock +19 -0
  6. data/README.md +40 -0
  7. data/Rakefile +2 -0
  8. data/apollo_commons_ruby-0.2.0.gem +0 -0
  9. data/apollo_commons_ruby-0.3.0.gem +0 -0
  10. data/apollo_commons_ruby-0.4.0.gem +0 -0
  11. data/apollo_commons_ruby-0.5.0.gem +0 -0
  12. data/apollo_commons_ruby-0.6.0.gem +0 -0
  13. data/apollo_commons_ruby.gemspec +26 -0
  14. data/bin/console +14 -0
  15. data/bin/setup +8 -0
  16. data/lib/apollo_commons_ruby.rb +34 -0
  17. data/lib/apollo_commons_ruby/DomainEvent.rb +122 -0
  18. data/lib/apollo_commons_ruby/FileUtils.rb +41 -0
  19. data/lib/apollo_commons_ruby/MarioEvent.rb +45 -0
  20. data/lib/apollo_commons_ruby/NetworkUtils.rb +67 -0
  21. data/lib/apollo_commons_ruby/Operation.rb +4 -0
  22. data/lib/apollo_commons_ruby/ResolveTemplate.rb +53 -0
  23. data/lib/apollo_commons_ruby/SecretConfig.rb +15 -0
  24. data/lib/apollo_commons_ruby/TemplateBuilder.rb +26 -0
  25. data/lib/apollo_commons_ruby/TemplatesHelper.rb +814 -0
  26. data/lib/apollo_commons_ruby/TemplatesRakefile.rb +816 -0
  27. data/lib/apollo_commons_ruby/ViewGroup.rb +62 -0
  28. data/lib/apollo_commons_ruby/ViewGroupComponent.rb +66 -0
  29. data/lib/apollo_commons_ruby/ViewGroupDefinition.rb +61 -0
  30. data/lib/tasks/add_translations.rake +9 -0
  31. data/lib/tasks/checkValidJson.rake +17 -0
  32. data/lib/tasks/delete_domain_event.rake +7 -0
  33. data/lib/tasks/delete_view_group.rake +7 -0
  34. data/lib/tasks/delete_view_group_component.rake +7 -0
  35. data/lib/tasks/delete_view_group_definition.rake +7 -0
  36. data/lib/tasks/deploy_all_templates.rake +7 -0
  37. data/lib/tasks/runTests.rake +27 -0
  38. data/lib/tasks/update_all.rake +36 -0
  39. data/lib/tasks/update_all_domain_events.rake +24 -0
  40. data/lib/tasks/update_all_view_group_components.rake +18 -0
  41. data/lib/tasks/update_domain_event.rake +11 -0
  42. data/lib/tasks/update_one_template.rake +42 -0
  43. data/lib/tasks/update_translations.rake +24 -0
  44. data/lib/tasks/update_view_group_component.rake +9 -0
  45. data/lib/tasks/update_view_group_definition.rake +7 -0
  46. data/lib/tasks/update_view_group_for_user_0.rake +36 -0
  47. data/lib/version.rb +3 -0
  48. metadata +91 -0
@@ -0,0 +1,816 @@
1
+ require 'fileutils'
2
+ require 'git'
3
+ require 'rubygems'
4
+ require_relative 'ResolveTemplate'
5
+ require_relative 'TemplatesHelper'
6
+ # require "google/cloud/translate"
7
+
8
+ def create_template_from_file (configuration, template_group, language, view, template_name, template_path, is_apollo_config, tenant_id, project_id)
9
+ begin
10
+
11
+ puts "template_name #{template_name}"
12
+ #Parsing Presentation
13
+ presentation_file_path = file_of_name_in_dir_path(template_path,"presentation")
14
+ puts "presentation_file_path #{presentation_file_path}"
15
+ presentation = read_data_from_file_path(presentation_file_path)
16
+
17
+ #template should fail if presentation file is not present at all
18
+ if presentation_file_path == nil
19
+ raise "ERROR: Presentation file not found at path: #{presentation_file_path}"
20
+ end
21
+
22
+ if presentation_file_path.end_with? ".json"
23
+ #if presentation file is json, then enforce proper json
24
+ if !JSON.is_valid_json?(presentation)
25
+ raise "ERROR: Presentation is not a valid JSON"
26
+ end
27
+ presentation = JSON.parse(presentation).to_json
28
+ presentation = ResolveTemplate.new.resolveLang(presentation.to_json, configuration,language)
29
+ presentation = JSON.parse(presentation, :quirks_mode => true)
30
+ end
31
+ if presentation_file_path.end_with? ".html"
32
+ presentation = ResolveTemplate.new.resolveLang(presentation, configuration, language)
33
+ presentation = Base64.encode64(presentation).newline
34
+ else
35
+ presentation = ResolveTemplate.new.resolveLang(presentation, configuration, language)
36
+ presentation = Base64.encode64(presentation.newline).newline
37
+ end
38
+
39
+ #Parsing Default Content
40
+ default_content_file_path = file_of_name_in_dir_path(template_path,"defaultContent")
41
+ #many templates will not support defaultContent, hence initializing a default value
42
+ default_content = {}
43
+ if !(default_content_file_path == nil)
44
+ #Default Content file is present
45
+ begin
46
+ default_content = read_data_from_file_path(default_content_file_path)
47
+ rescue Exception => e
48
+ #Mostly this should not happen, but a sanity check
49
+ puts "ERROR: default content file not found #{e}"
50
+ end
51
+ #If default content is read from file, it should be definitely be of JSON format because i.e. what the API expects
52
+ if !JSON.is_valid_json?(default_content)
53
+ #If not, throw exception
54
+ raise "ERROR: default content is not a valid JSON"
55
+ else
56
+ #Convert json file read to hash so that it is passed to API in json form
57
+ default_content = JSON.parse(default_content)
58
+ default_content = ResolveTemplate.new.resolveEnv(default_content.to_json, configuration, tenant_id, project_id)
59
+ default_content = JSON.parse(default_content)
60
+ end
61
+ end
62
+ default_content_string = default_content.to_json
63
+
64
+ transformer_file_path = file_of_name_in_dir_path(configuration.template_group_dir_path_for_template(template_group), "clientTransformer")
65
+
66
+ isClientTransformerFound = false
67
+ if !(transformer_file_path == nil)
68
+
69
+ isClientTransformerFound = true
70
+
71
+ begin
72
+ client_transformer = get_client_transformer_without_require(transformer_file_path)
73
+ rescue Exception => e
74
+ #Mostly this should not happen, but a sanity check
75
+ puts "ERROR: client transformer file not found #{e}"
76
+ end
77
+
78
+
79
+ if client_transformer == nil
80
+ client_transformer = "";
81
+ end
82
+
83
+ client_transformer = client_transformer.newline.tabs
84
+
85
+ end
86
+
87
+ #Parsing data template
88
+ data_template_file_path = file_of_name_in_dir_path(template_path,"dataTemplate")
89
+ puts "data_template_file_path #{data_template_file_path}"
90
+ #many templates will not support dataTemplate, hence initializing a default value
91
+ data_template = "{}"
92
+ if !(data_template_file_path == nil)
93
+ #data template file is present
94
+ begin
95
+ data_template = read_data_from_file_path(data_template_file_path)
96
+ rescue Exception => e
97
+ #Mostly this should not happen, but a sanity check
98
+ puts "ERROR: data template file not found #{e}"
99
+ end
100
+ if data_template_file_path.end_with? ".json" and !data_template.newline.empty?
101
+ #if data template file is json, then enforce proper json
102
+ if !JSON.is_valid_json?(data_template)
103
+ raise "ERROR: data template is not a valid JSON"
104
+ end
105
+ data_template = "#{data_template}"
106
+ end
107
+ end
108
+
109
+ #Parsing client transformer specific to individual template
110
+ specific_client_transformer_file_path = file_of_name_in_dir_path(template_path,"clientTransformer")
111
+ puts "specific_client_transformer_file_path #{specific_client_transformer_file_path}"
112
+ specific_client_transformer = "{}";
113
+ if !(specific_client_transformer_file_path == nil)
114
+ begin
115
+ specific_client_transformer = get_client_transformer_without_require(specific_client_transformer_file_path)
116
+ rescue Exception => e
117
+ puts "ERROR: specific client transformer file not found #{e}"
118
+ end
119
+ if isClientTransformerFound
120
+ client_transformer = client_transformer + "\n" + specific_client_transformer
121
+ elsif specific_client_transformer
122
+ client_transformer = specific_client_transformer
123
+ isClientTransformerFound = true
124
+ end
125
+ end
126
+
127
+ #Parsing attrs json
128
+ attrs_json_file_path = file_of_name_in_dir_path(template_path, "attrs")
129
+ puts "attrs_json_file_path #{attrs_json_file_path}"
130
+ #many templates will not have attrs json, hence initializing a default value
131
+ attrs_json = "{}"
132
+ if !(attrs_json_file_path == nil)
133
+ #attrs file is present
134
+ begin
135
+ attrs_json = read_data_from_file_path(attrs_json_file_path)
136
+ rescue Exception => e
137
+ #Mostly this should not happen, but a sanity check
138
+ puts "ERROR: attrs file not found #{e}"
139
+ end
140
+ if attrs_json_file_path.end_with? ".json" and !attrs_json.newline.empty?
141
+ #if attrs file is json, then enforce proper json
142
+ if !JSON.is_valid_json?(attrs_json)
143
+ raise "ERROR: attrs is not a valid JSON"
144
+ end
145
+ attrs_json = "#{attrs_json}"
146
+ end
147
+ end
148
+
149
+ if isClientTransformerFound
150
+ client_transformer = client_transformer.newline.tabs
151
+ if client_transformer.scan(/^\s*<%.*%>\s*$/).length == 0
152
+ client_transformer = "<% #{client_transformer} %>"
153
+ end
154
+ if JSON.is_valid_json?(data_template)
155
+ data_template_hash_new = Hash.new
156
+ data_template_hash_new["__transformer"] = client_transformer
157
+ data_template_hash = JSON.parse(data_template)
158
+ data_template_hash_new.merge!(data_template_hash)
159
+ data_template = data_template_hash_new.to_json
160
+ else
161
+ data_template = "#{client_transformer}#{data_template}"
162
+ end
163
+ end
164
+
165
+ data_template = ResolveTemplate.new.resolveEnv(data_template, configuration, tenant_id, project_id)
166
+ data_template = ResolveTemplate.new.resolveLang(data_template, configuration, language)
167
+ data_template = Base64.encode64(data_template.newline).newline
168
+
169
+ if is_apollo_config
170
+ Template.new(template_name, default_content_string, data_template, presentation, attrs_json)
171
+ else
172
+ Template.new(template_name, default_content, data_template, presentation, attrs_json)
173
+ end
174
+ rescue Exception => e
175
+ puts "ERROR: Template loading from #{template_group} failed with error: #{e}"
176
+ end
177
+ end
178
+
179
+ def create_template_mapping_payload(template_group, language, view_type, templateMappings, version)
180
+ body = {
181
+ "templateGroup" => template_group,
182
+ "language" => language,
183
+ "viewType" => view_type,
184
+ "templateMappings" => templateMappings,
185
+ "version" => version
186
+ }
187
+ end
188
+
189
+ def get_parsed_template_group_details(configuration,template_group)
190
+ template_group_details_file_path = configuration.template_group_details_file_path_for_template(template_group)
191
+ template_group_details = read_json_data_from_file_path(template_group_details_file_path)
192
+ return JSON.parse(template_group_details)
193
+ end
194
+
195
+ def verify_and_get_template_version(version)
196
+ allowed_versions = {"v2" => "iOS_v2", "v3" => "iOS_v3"}
197
+ if version == nil
198
+ return nil
199
+ end
200
+
201
+ if allowed_versions.has_value?(version)
202
+ return version
203
+ end
204
+
205
+ if allowed_versions.key?(version)
206
+ return allowed_versions[version]
207
+ end
208
+
209
+ raise "Unsupported template version: #{version}"
210
+ end
211
+
212
+ def perform_git_checks
213
+ current_branch = `git rev-parse --abbrev-ref HEAD`
214
+ if current_branch.eql?("master")
215
+ raise "Rake update in Prod can be performed only from master branch."
216
+ end
217
+
218
+ system("git fetch origin master > /dev/null 2>&1") or raise "Unable to fetch origin/master from git remote"
219
+ master = `git rev-parse master`
220
+ origin_master = `git rev-parse origin/master`
221
+
222
+ if !master.eql?(origin_master)
223
+ raise "Master is not in sync with Origin. Pull changes from remote and retry."
224
+ end
225
+ end
226
+
227
+ def get_client_transformer_without_require(file)
228
+ input_file_path = file.to_s
229
+ final_transformer_output = ""
230
+ line_number_of_require_in_input_file = 0
231
+ line_numbers_to_delete = []
232
+ terminate_processing_of_require_line = false
233
+ #Filter require lines and compute the path to transformer from the line
234
+ File.readlines(input_file_path).each do |require_line|
235
+ line_number_of_require_in_input_file += 1
236
+ is_requires = require_line.index("require ") == 0
237
+ #Iterate through the require lines, until the match is not found.
238
+ #Ignore rest of the lines that match 'require'.
239
+ if is_requires and !terminate_processing_of_require_line then
240
+ #Mark the lines that contain 'require' in the top part of the file
241
+ line_numbers_to_delete << line_number_of_require_in_input_file
242
+ require_line.slice! "require "
243
+ require_line = require_line.strip
244
+ require_line = require_line.gsub('"', '')
245
+ if !file_exists?(require_line)
246
+ raise "ERROR: Transformer file #{require_line} not found"
247
+ end
248
+ transformer_output = read_plain_text_data_from_file_path(require_line)
249
+ if (transformer_output == "")
250
+ raise "ERROR: Transformer file #{require_line} empty"
251
+ end
252
+ #Append transformers in the order of the require statements
253
+ final_transformer_output = final_transformer_output + transformer_output + "\n"
254
+ else
255
+ terminate_processing_of_require_line = true
256
+ end
257
+ end
258
+ current_file_output = read_plain_text_data_from_file_path(input_file_path)
259
+ if final_transformer_output == "" then
260
+ current_file_output
261
+ else
262
+ current_line_number = 0
263
+ #Strip off 'require' commands from the current file's data(does not modify the file) present in the form of a variable
264
+ client_transformer_with_no_require_lines = ""
265
+ current_file_output.each_line do |line|
266
+ current_line_number += 1
267
+ should_remove_line = line_numbers_to_delete.include? current_line_number
268
+ #Remove the line if we had marked it for removal earlier
269
+ client_transformer_with_no_require_lines << line unless should_remove_line
270
+ end
271
+ #Prepend all the transformers to the script to be updated to the script that does not have 'require' command
272
+ client_transformer_with_no_require_lines = final_transformer_output + "\n" + client_transformer_with_no_require_lines
273
+ client_transformer_with_no_require_lines
274
+ end
275
+ end
276
+
277
+ def update_existing_translations(source_path, language_resource_path)
278
+ if (!file_exists?(language_resource_path))
279
+ log_error("Update Failed! No translation resource found at path [ #{language_resource_path} ]")
280
+ abort()
281
+ end
282
+
283
+ if (!file_exists?(source_path))
284
+ log_error("No source file found at path [ #{source_path} ]")
285
+ abort()
286
+ end
287
+
288
+ source_file_plain_text_data = read_plain_text_data_from_file_path(source_path)
289
+
290
+ new_translations = Hash.new
291
+ source_file_plain_text_data.split("\r\n").each { |value|
292
+ key_value_array = value.split(',')
293
+ # puts "Value: #{value} key_value_array: #{key_value_array}"
294
+ # abort()
295
+ if key_value_array.length >= 2
296
+ key = key_value_array[0]
297
+ key_value_array.delete_at(0)
298
+ value = key_value_array.join
299
+
300
+ new_translations[key] = value
301
+ else
302
+ log_error("Terminating! Found incorrect translation key,pair [ #{key_value_array}]")
303
+ abort()
304
+ end
305
+ }
306
+
307
+ old_translations = read_raw_json_data_from_file_path(language_resource_path);
308
+
309
+ new_translations.each { |key, value|
310
+ if old_translations.has_key?(key)
311
+ old_translations[key] = value
312
+ log_info("Updated translation for key [ #{key} ] with value: [ #{value} ]")
313
+ else
314
+ log_error("Key #{key} does not exist in existing translations. Ignoring it")
315
+ end
316
+ }
317
+
318
+ if new_translations.length > 0
319
+ log_info("Writing updated translations to file at path [ #{language_resource_path}]")
320
+ write_data_to_file(language_resource_path, old_translations)
321
+ else
322
+ log_info("Translations not updated due to no new updates")
323
+ end
324
+
325
+ end
326
+
327
+ desc "Available options"
328
+ task :default do
329
+ puts "'rake update_one_template environment=Staging template_group=MCCS-grant-VOUCHER version=v2 scope_id=1001' to build the template for a template group. Version is optional. scope_id is required for templates. It will build for all languages and view_type present in the template group. If you want to build specifically for a view type or language or template_name, specify language or view_type or template_name as query param"
330
+ end
331
+
332
+
333
+ # $LINE_REPLACEMENT_DETAILS_LIST = Array.new
334
+ # $TRANSLATOR = Google::Cloud::Translate.new(
335
+ # project_id: "zeta-pay",
336
+ # credentials: "./keyfile.json"
337
+ # )
338
+
339
+ def get_translated_line(text, target_language)
340
+ return $TRANSLATOR.translate text, to: target_language
341
+ end
342
+
343
+ def is_alpha_char(char)
344
+ return char == "s" || char == "f" || char == "d"
345
+ end
346
+
347
+ def is_numeric_char(char)
348
+ return char == "1" || char == "2" || char == "3" || char == "4" || char == "5" || char == "6" || char == "7" || char == "8" || char == "9"
349
+ end
350
+
351
+ def format_translated_line(text)
352
+ text = text.to_s.gsub("&#39;", " ")
353
+ text = text.to_s.gsub("&quot;", "\"")
354
+ text = text.to_s.gsub("&lt;", "<")
355
+ text = text.to_s.gsub("&gt;", ">")
356
+ index = 0
357
+ output = ""
358
+ length = text.length
359
+ while index < length
360
+ char = text[index]
361
+ if (char == "%" && index + 1 < length && text[index + 1] == " " && index + 2 < length && is_alpha_char(text[index + 2]))
362
+ output = output + " " + "%" + text[index + 2]
363
+ index = index + 2
364
+ elsif (char == "%" && index + 1 < length && text[index + 1] == " " && index + 2 < length && is_numeric_char(text[index + 2]) &&
365
+ index + 3 < length && text[index + 3] == " " && index + 4 < length && text[index + 4] == "$" &&
366
+ index + 5 < length && text[index + 5] == " " && index + 6 < length && is_alpha_char(text[index + 6]))
367
+ output = output + " " + "%" + text[index + 2] + "$" + text[index + 6]
368
+ index = index + 6
369
+ elsif (char == "\\" && index + 1 < length && text[index + 1] == " " && index + 2 < length && text[index + 2] == "n")
370
+ output = output + "\n"
371
+ index = index + 2
372
+ elsif (char == " " && index + 1 < length && text[index + 1] == "%" && index + 2 < length && text[index + 2] == " " &&
373
+ index + 3 < length && text[index + 3] == "%")
374
+ output = output + "%% "
375
+ index = index + 3
376
+ elsif (char == "<" && index + 1 < length && text[index + 1] == "%" && index + 2 < length && text[index + 2] == " " &&
377
+ index + 3 < length && text[index + 3] == "=")
378
+ output = output + "<%= "
379
+ index = index + 3
380
+ else
381
+ output = output + text[index]
382
+ end
383
+ index = index + 1
384
+ end
385
+ return output
386
+ end
387
+
388
+ def modify_file_line(line_replacement_details)
389
+ file_path = line_replacement_details.file_path
390
+ modified_file = IO.readlines(file_path).map do |line|
391
+ get_appropriate_line(line, line_replacement_details)
392
+ end
393
+ File.open(file_path, 'w') do |file|
394
+ file.puts modified_file
395
+ end
396
+ end
397
+
398
+ def get_appropriate_line(line, line_replacement_details)
399
+ if (line.strip.start_with?(line_replacement_details.redundant_line_prefix.strip))
400
+ return line_replacement_details.updated_line
401
+ end
402
+ return line
403
+ end
404
+
405
+ def change_line_in_file(line_replacement_list)
406
+ line_replacement_list.each do |line_replacement_details|
407
+ modify_file_line(line_replacement_details)
408
+ end
409
+ end
410
+
411
+ def get_modified_replacement_property(line_replacement_details)
412
+ file_path = line_replacement_details.file_path
413
+ redundant_line_prefix = line_replacement_details.redundant_line_prefix
414
+ updated_line = line_replacement_details.updated_line.to_s[0...-2]
415
+ return LineReplacementProperties.new(file_path, redundant_line_prefix, updated_line)
416
+ end
417
+
418
+
419
+ def add_translations_for_language(module_name, languages)
420
+ languages.split(",").each do |language|
421
+ if ("en-EN" == language)
422
+ puts "Default language is English. No need for translations"
423
+ next
424
+ end
425
+ target_language = language.split("-")[0]
426
+ target_file = language.split("-")[1] + ".json"
427
+ base_path = "./Resources/"
428
+ File.delete(base_path + target_file) if File.exist?(base_path + target_file)
429
+ puts "Removing file #{target_file}" if File.exist?(base_path + target_file)
430
+ file_name = "EN.json"
431
+ target_file_path = base_path + target_file
432
+ FileUtils.cp(base_path + file_name, target_file_path)
433
+ IO.readlines(target_file_path).map do |line|
434
+ trimmed_line = line.strip
435
+ if trimmed_line.length != 0
436
+ begin
437
+ value = trimmed_line.split(":")[1]
438
+ translated_line = format_translated_line(get_translated_line(value, target_language))
439
+ if (!translated_line.to_s.end_with? ",")
440
+ translated_line = translated_line + ","
441
+ end
442
+ formatted_line = trimmed_line.split(":")[0] + ": " + translated_line.to_s.strip
443
+ puts "Translated Line #{formatted_line}"
444
+ $LINE_REPLACEMENT_DETAILS_LIST.push(LineReplacementProperties.new(target_file_path, line, line.gsub(trimmed_line, formatted_line)))
445
+ rescue
446
+ puts "Translation failed. Using the default line in place"
447
+ $LINE_REPLACEMENT_DETAILS_LIST.push(LineReplacementProperties.new(target_file_path, line, line))
448
+ end
449
+ end
450
+ end
451
+ #Remove ending }
452
+ $LINE_REPLACEMENT_DETAILS_LIST.pop()
453
+ #Modify the last element which has , appended to it
454
+ $LINE_REPLACEMENT_DETAILS_LIST.push(get_modified_replacement_property($LINE_REPLACEMENT_DETAILS_LIST.pop()))
455
+ change_line_in_file($LINE_REPLACEMENT_DETAILS_LIST)
456
+ end
457
+ end
458
+
459
+ def deploy_all_templates()
460
+ # current_dir = Dir.pwd
461
+ # if !current_dir.include? "/Users/ios-ci/.jenkins/jobs/"
462
+ # puts "This rake command is no more supported. Please use jenkins build http://iosci.corp.zeta.in:8080/job/"
463
+ # return nil
464
+ # end
465
+
466
+ environment = ENV['environment']
467
+ if environment == nil
468
+ puts "`environment` has to be passed. Environment = `Prod`/'PreProd'/`Staging`"
469
+ return
470
+ end
471
+
472
+ if environment.eql?("Prod")
473
+ #perform_git_checks
474
+ end
475
+
476
+ require 'ostruct'
477
+ require 'parallel'
478
+ configuration = Configuration.new(environment)
479
+ template_groups = directory_list(configuration.base_folder_path)
480
+ total_tasks_size = 0
481
+ if template_groups
482
+ total_tasks_size = template_groups.size
483
+ end
484
+ puts "Total number of tasks to execute: #{total_tasks_size}"
485
+ puts "\n\n"
486
+ iter = 0
487
+ tasks = []
488
+ template_groups.each do |template_group|
489
+ #template_group = "MCCS-grant-VOUCHER"
490
+ shouldIgnore = false;
491
+ iter+=1
492
+ if shouldIgnore
493
+ puts "Task #{iter}/#{total_tasks_size}: Ignoring for templateGroup #{template_group} because already done in previous build"
494
+ next
495
+ else
496
+ puts "Task #{iter}/#{total_tasks_size}: Scheduling deployment for template_group #{template_group}"
497
+ end
498
+ ENV['environment'] = environment
499
+ ENV['template_group'] = template_group
500
+
501
+ task_to_schedule = OpenStruct.new
502
+ task_to_schedule.environment = ENV['environment']
503
+ task_to_schedule.template_group = ENV['template_group']
504
+ task_to_schedule.language = ENV['language']
505
+ task_to_schedule.view_type = ENV['view_type']
506
+ task_to_schedule.template_name = ENV['template_name']
507
+ task_to_schedule.version = ENV['version']
508
+ task_to_schedule.should_update_inbox = false
509
+ task_to_schedule.tenant_id = ENV['tenant_id']
510
+ task_to_schedule.project_id = ENV['project_id']
511
+ if ENV['scope_id'] === nil
512
+ raise "scope_id is mandatory for deploying templates."
513
+ end
514
+ task_to_schedule.scope_id = ENV['scope_id']
515
+ task_to_schedule.task_id = iter
516
+ tasks << task_to_schedule
517
+ end
518
+ puts "STARTING DEPLOYMENT NOW"
519
+ results = Parallel.map(tasks, progress: "DEPLOYING ALL TEMPLATES", :in_processes => 1) do |task|
520
+ begin
521
+ deploy_one_template(task)
522
+ rescue Exception => e
523
+ puts "Error for template " + task.template_group + " " + e.backtrace.to_s
524
+ File.open("templateScopeError.txt", "a") do |f|
525
+ f.puts(task.template_group )
526
+ end
527
+ end
528
+ end
529
+ puts "DONE!"
530
+ end
531
+
532
+ def deploy_one_template(task)
533
+ environment = task.environment
534
+ template_group = task.template_group
535
+ language = task.language
536
+ view_type = task.view_type
537
+ template_name = task.template_name
538
+ version = task.version
539
+ scope_id = task.scope_id
540
+ should_update_inbox = task.should_update_inbox
541
+ tenant_id = task.tenant_id
542
+ project_id = task.project_id
543
+
544
+ configuration = Configuration.new(environment)
545
+ return if configuration == nil
546
+
547
+ version = verify_and_get_template_version(version)
548
+
549
+ didOnce = false
550
+ template_group_details = get_parsed_template_group_details(configuration,template_group)
551
+
552
+ languages = []
553
+ if language == nil
554
+ languages = template_group_details["supportedLanguages"]
555
+ else
556
+ languages.push(language)
557
+ end
558
+
559
+ specific_language_folders = []
560
+ dir_path = configuration.template_group_dir_path_for_template(template_group)
561
+ specific_language_folders = directory_list(dir_path)
562
+
563
+ if languages.empty?
564
+ return "No language found"
565
+ end
566
+
567
+ puts "languages #{languages}"
568
+
569
+ languageMappings = template_group_details["languageMappings"]
570
+
571
+ languages.each do |language|
572
+ template_language_folder = language
573
+ if !specific_language_folders.include?(language)
574
+ template_language_folder = "common"
575
+ end
576
+
577
+ viewTypes = []
578
+ if view_type == nil
579
+ dir_path = configuration.language_dir_path_for_template(template_group, template_language_folder)
580
+ viewTypes = directory_list(dir_path)
581
+ else
582
+ viewTypes.push(view_type)
583
+ end
584
+
585
+ if viewTypes.empty?
586
+ return "No view types found"
587
+ end
588
+
589
+ puts "viewTypes #{viewTypes}"
590
+
591
+ viewTypes.each do |view_type|
592
+
593
+ allTemplateVariants = Set.new
594
+
595
+ selectedTemplateMappingsInbox = []
596
+ scopeConfigsMercury = []
597
+ dir_path = ""
598
+ if version == nil
599
+ dir_path = configuration.view_type_dir_path_for_template(template_group, template_language_folder, view_type)
600
+ else
601
+ dir_path = configuration.view_type_dir_path_for_template_with_version(template_group, template_language_folder, view_type, version)
602
+ end
603
+ allProbableTemplateNames = all_directory_and_sub_directory_with_character_not_in_dir_path(dir_path, "_")
604
+ puts "For ViewType #{view_type} allProbableTemplateNames #{allProbableTemplateNames}"
605
+ if !(template_name == nil)
606
+ selectedProbableTemplateNames = allProbableTemplateNames.select{|key,value| key == template_name }
607
+ else
608
+ selectedProbableTemplateNames = allProbableTemplateNames
609
+ end
610
+
611
+ if selectedProbableTemplateNames.empty?
612
+ puts "Error: No template names found"
613
+ next
614
+ end
615
+
616
+ if !languageMappings.nil?
617
+ languageMappingForLanguage = languageMappings[language];
618
+ else
619
+ languageMappingForLanguage = nil;
620
+ end
621
+ puts "languageMapping For #{language} = #{languageMappingForLanguage}"
622
+
623
+ selectedProbableTemplateNames.each do |key, value|
624
+ if !language.eql?("EN") && !key.eql?("default") && !languageMappingForLanguage.nil? && !languageMappingForLanguage.include?(key)
625
+ puts "#{key} not included in languageMappings for #{language}"
626
+ next
627
+ end
628
+ template_mapping = template_mapping_in_dir_path_file_path_with_character(dir_path, value.clone, "_")
629
+ key = ResolveTemplate.new.resolveEnv(key.dup, configuration, tenant_id, project_id)
630
+ template_mapping_inbox = ResolveTemplate.new.resolveEnv(template_mapping.dup, configuration, tenant_id, project_id)
631
+ selectedTemplateMappingsInbox.push(TemplateMapping.new(key, value, template_mapping_inbox))
632
+ end
633
+
634
+ if !didOnce
635
+ puts "\n**Updating template group**\n\n"
636
+ if should_update_inbox
637
+ update_template_group_inbox(environment, configuration, template_group, scope_id, tenant_id, project_id)
638
+ end
639
+ upsert_template_group_apollo_config(environment, configuration, scope_id, template_group, view_type, version, tenant_id, project_id)
640
+ didOnce = true
641
+ end
642
+
643
+ puts "\n**Updating Templates for language #{language} view_type #{view_type}**\n"
644
+
645
+ if should_update_inbox
646
+ puts "\n*Insert default template in inbox*\n"
647
+ selectedTemplateMappingsInbox.each do |template_mapping|
648
+ template_mapping_hash = template_mapping.create_hash
649
+ if template_mapping_hash["template_name"] == "default"
650
+ if version == nil
651
+ puts "\n*For template_name #{template_mapping_hash["template_name"]}*\n"
652
+ templates = create_template_from_file(configuration, template_group, language, view_type, template_mapping_hash["template_name"], template_mapping_hash["template_path"], false, tenant_id, project_id).create_hash
653
+ else
654
+ puts "\n*For template_name #{template_mapping_hash["template_name"]}*\n"
655
+ version_path = "#{dir_path}"
656
+ templates = create_template_from_file(configuration, template_group, language, view_type, template_mapping_hash["template_name"], template_mapping_hash["template_path"], false, tenant_id, project_id).create_hash
657
+ end
658
+ end
659
+ request_payload = CreateTemplatePayload.new(template_group, language, view_type, templates, version)
660
+ puts "\n*Insert Call*\n"
661
+ invoke_API(environment, configuration.post_insert_templates_url, HTTP::POST, nil, request_payload.to_json)
662
+ end
663
+ end
664
+
665
+ #Update Templates
666
+ puts "\n*Update templates API Call*\n"
667
+
668
+ selectedTemplateMappingsInbox.each do |template_mapping|
669
+ inbox_template_name = scope_id
670
+ if version == nil
671
+ template_mapping_hash = template_mapping.create_hash
672
+ puts "\n*For template_name #{template_mapping_hash["template_name"]}*\n"
673
+ templates = create_template_from_file(configuration, template_group, language, view_type, inbox_template_name, template_mapping_hash["template_path"], false, tenant_id, project_id).create_hash
674
+ apollo_config_templates = create_template_from_file(configuration, template_group, language, view_type, template_mapping_hash["template_name"], template_mapping_hash["template_path"], true, tenant_id, project_id).create_hash
675
+ else
676
+ template_mapping_hash = template_mapping.create_hash
677
+ puts "\n*For template_name #{template_mapping_hash["template_name"]}*\n"
678
+ version_path = "#{dir_path}"
679
+ templates = create_template_from_file(configuration, template_group, language, view_type, inbox_template_name, template_mapping_hash["template_path"], false, tenant_id, project_id).create_hash
680
+ apollo_config_templates = create_template_from_file(configuration, template_group, language, view_type, template_mapping_hash["template_name"], template_mapping_hash["template_path"], true, tenant_id, project_id).create_hash
681
+ end
682
+ if should_update_inbox
683
+ request_payload = CreateTemplatePayload.new(template_group, language, view_type, templates, version)
684
+ puts "\n*Insert Call*\n"
685
+ invoke_API(environment, configuration.post_insert_templates_url, HTTP::POST, nil, request_payload.to_json)
686
+ puts "\n*Update Call*\n"
687
+ invoke_API(environment, configuration.post_update_templates_url, HTTP::POST, nil, request_payload.to_json)
688
+ end
689
+ upsert_templates_apollo_config(environment, configuration, scope_id, template_group, language, view_type, apollo_config_templates, version)
690
+ end
691
+
692
+ #Upsert Template Mappings
693
+ puts "\n*Upsert template mapping API Call*\n"
694
+ query_params = {
695
+ :templateGroup => template_group,
696
+ :language => language,
697
+ :viewType => view_type
698
+ }
699
+ if should_update_inbox
700
+ get_templates_for_view_response = invoke_API(environment, configuration.get_templates_for_view_url, HTTP::GET, query_params)
701
+ allTemplateMappingsInbox = get_templates_for_view_response["mappings"]
702
+ if allTemplateMappingsInbox == nil
703
+ allTemplateMappingsInbox = {}
704
+ end
705
+ if !allTemplateMappingsInbox.key?(scope_id)
706
+ allTemplateMappingsInbox.merge!("#{scope_id}": scope_id)
707
+ end
708
+ selectedTemplateMappingsInbox.each do |template_mapping|
709
+ template_mapping_hash = template_mapping.create_mapping
710
+ allTemplateMappingsInbox.merge!(template_mapping_hash)
711
+ end
712
+ request_payload = create_template_mapping_payload(template_group, language, view_type, allTemplateMappingsInbox, version)
713
+ invoke_API(environment, configuration.post_upsert_template_mappings_url, HTTP::POST, nil, request_payload.to_json)
714
+ end
715
+
716
+ templateMappingsPayload = {}
717
+ selectedTemplateMappingsInbox.each do |template_mapping|
718
+ template_mapping_hash = template_mapping.create_mapping
719
+ templateMappingsPayload.merge!(template_mapping_hash)
720
+ end
721
+ upsert_template_mappings_apollo_config(environment, configuration, scope_id, template_group, language, view_type, templateMappingsPayload, version)
722
+ puts "DONE FOR #{task.task_id}: #{template_group} \n"
723
+ end
724
+
725
+ end
726
+ end
727
+
728
+ def update_template_group_inbox(environment, configuration, template_group, scope_id, tenant_id, project_id)
729
+ query_params = {
730
+ :templateGroup => template_group
731
+ }
732
+ puts "\n*Fetching template group details API call*\n"
733
+ template_group_details_response = invoke_API(environment, configuration.get_template_group_details_url, HTTP::GET, query_params)
734
+
735
+ shouldMakeUpdateTemplateGroupCall = false
736
+
737
+ if template_group_details_response["templateGroup"] == template_group
738
+ shouldMakeUpdateTemplateGroupCall = true
739
+ end
740
+
741
+ template_group_details = get_parsed_template_group_details(configuration,template_group)
742
+
743
+ transformer_file_path = configuration.transfomer_file_path_for_template(template_group)
744
+ transformer_data = read_data_from_file_path(transformer_file_path)
745
+ transformer_data = ResolveTemplate.new.resolveEnv(transformer_data, configuration, tenant_id, project_id)
746
+
747
+ field_ordering = template_group_details["fieldOrdering"]
748
+ expected_data = template_group_details["expectedData"]
749
+
750
+ # TODO: Handle case where field_ordering is not empty.
751
+ field_ordering = ["appID"]
752
+
753
+ request_payload = {
754
+ :templateGroup => template_group,
755
+ :expectedData => expected_data,
756
+ :fieldOrdering => field_ordering,
757
+ :transformer => transformer_data
758
+ }
759
+
760
+ puts "\n*Update template group API call*\n"
761
+ if shouldMakeUpdateTemplateGroupCall
762
+ invoke_API(environment, configuration.post_update_template_group_url, HTTP::POST, nil, request_payload.to_json)
763
+ else
764
+ invoke_API(environment, configuration.post_insert_template_group_url, HTTP::POST, nil, request_payload.to_json)
765
+ end
766
+ end
767
+
768
+ def upsert_template_group_apollo_config(environment, configuration, scope_id, template_group, view, version, tenant_id, project_id)
769
+
770
+ template_group_details = get_parsed_template_group_details(configuration, template_group)
771
+
772
+ transformer_file_path = configuration.transfomer_file_path_for_template(template_group)
773
+ transformer_data = read_data_from_file_path(transformer_file_path)
774
+ transformer_data = ResolveTemplate.new.resolveEnv(transformer_data, configuration, tenant_id, project_id)
775
+
776
+ field_ordering = template_group_details["fieldOrdering"]
777
+ expected_data = template_group_details["expectedData"]
778
+
779
+ request_payload = {
780
+ :expectedData => expected_data,
781
+ :fieldOrdering => field_ordering,
782
+ :transformer => transformer_data,
783
+ :version => version
784
+ }
785
+
786
+ url = configuration.apollo_config_base_url + "/scopes/#{scope_id}/templateGroups/#{template_group}"
787
+
788
+ puts "\n*[Apollo-config]: Upsert template group API call*\n"
789
+ invoke_API(environment, url, HTTP::POST, nil, request_payload.to_json)
790
+ end
791
+
792
+ def upsert_template_mappings_apollo_config(environment, configuration, scope_id, template_group, language, view, mappings, version)
793
+
794
+ request_payload = {
795
+ :mappings => mappings,
796
+ :version => version
797
+ }
798
+
799
+ url = configuration.apollo_config_base_url + "/scopes/#{scope_id}/templateGroups/#{template_group}/languages/#{language}/views/#{view}/mappings/"
800
+
801
+ puts "\n*[Apollo-config]: Upsert template mappings API call*\n"
802
+ invoke_API(environment, url, HTTP::POST, nil, request_payload.to_json)
803
+ end
804
+
805
+ def upsert_templates_apollo_config(environment, configuration, scope_id, template_group, language, view, templates, version)
806
+
807
+ request_payload = {
808
+ :templates => templates,
809
+ :version => version
810
+ }
811
+
812
+ url = configuration.apollo_config_base_url + "/scopes/#{scope_id}/templateGroups/#{template_group}/languages/#{language}/views/#{view}/templates/"
813
+
814
+ puts "\n*[Apollo-config]: Upsert templates API call*\n"
815
+ invoke_API(environment, url, HTTP::POST, nil, request_payload.to_json)
816
+ end