apollo_commons_ruby_cli 1.0.0

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