Class: Readiness::Repos::Triggers

Inherits:
Client
  • Object
show all
Defined in:
lib/support_readiness/repos/triggers.rb

Overview

Defines the class Triggers within the module Readiness::Repos.

Author:

  • Jason Colyer

Since:

  • 1.0.12

Class Method Summary collapse

Methods inherited from Client

auth_error, bad_request_error, convert_actions, convert_conditions, convert_standard_names_to_ids, convert_ticket_form_agent_conditions, convert_ticket_form_brands, convert_ticket_form_end_user_conditions, convert_ticket_form_names_to_ids, convert_view_names_to_ids, convert_view_restrictions, covert_ticket_form_field_ids, create_package!, erb_renderer, handle_request_error, not_found_error, not_processible_error, put_into_archive, recursively_deflate_directory, timestamp_filename, to_clean_json, to_clean_json_with_key, to_hash, to_nearly_clean_json, to_nearly_clean_json_with_key, to_param_string, write_entries

Class Method Details

.compare(zendesk_client, location = 'data', verbose = false) ⇒ Hash

Compares the repo trigger files to the Zendesk instance triggers

Examples:

require 'support_readiness'
config = Readiness::Zendesk::Configuration.new
config.username = 'alice@example.com'
config.token = 'test123abc'
config.url = 'https://example.zendesk.com/api/v2'
client = Readiness::Zendesk::Client.new(config)
diffs = Readiness::Repos::Triggers.compare(client, 'triggers/data', false)
pp diffs[:updates.count]
# => 5
require 'support_readiness'
config = Readiness::Zendesk::Configuration.new
config.username = 'alice@example.com'
config.token = 'test123abc'
config.url = 'https://example.zendesk.com/api/v2'
client = Readiness::Zendesk::Client.new(config)
diffs = Readiness::Repos::Triggers.compare(client, 'triggers/data', true)
# => Detailed diff of Cleanup followup tickets
# => - @position Repo:
# => 13
# =>   @position Zendesk:
# => 12
# => Detailed diff of New Billing ticket created via Webform
# => - @position Repo:
# => 11
# =>   @position Zendesk:
# => 10
# => Compare report:
# => - Creates: 0
# => - Updates: 2
pp diffs[:updates.count]
# => 2

Parameters:

  • zendesk_client (Object)

    An instance of Zendesk::Client

  • location (String) (defaults to: 'data')

    The location (relative or absolute) of the repo’s data folder

  • verbose (Boolean) (defaults to: false)

    Whether you want a detailed report or not

Returns:

  • (Hash)

Author:

  • Jason Colyer

Since:

  • 1.0.12



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/support_readiness/repos/triggers.rb', line 55

def self.compare(zendesk_client, location = 'data', verbose = false)
  @zendesk_client = zendesk_client
  diffs = {
    updates: [],
    creates: []
  }
  from_repo = gather(location)
  from_zendesk = Zendesk::Triggers.list(zendesk_client)
  from_repo.each do |repo|
    zd = from_zendesk.detect { |z| z.title == repo.title }
    if zd.nil?
      diffs[:creates].push(repo)
    else
      comparable = zd.dup
      comparable.id = nil
      comparable.conditions = zd.conditions.sort.to_h
      diffs[:updates].push(update_object(repo, zd)) if to_clean_json(repo) != to_clean_json(comparable)
      detailed_diff(repo, comparable) if verbose && to_clean_json(repo) != to_clean_json(comparable)
    end
  end
  report_diffs(diffs) if verbose
  diffs
end

.convert_managed_content(object, file) ⇒ Object

Performs conversions for a repo file if using managed content

Parameters:

  • object (Hash)

    The Hash derived from parsing a YAML file

Author:

  • Jason Colyer

Since:

  • 1.0.12



221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/support_readiness/repos/triggers.rb', line 221

def self.convert_managed_content(object, file)
  category = file.split("#{@location}/#{object['active'] ? '' : 'in' }active/").last.split('/').first
  path = "#{@location}/managed_content/#{category}/#{object['title']}.md"
  handle_request_error(1, 'Repos', 404, { action: 'Get managed content', id: object['title'] }) unless File.exist?(path)
  object['actions'].each_with_index do |action, index|
    if action['value'].is_a?(String)
      if action['value'] =~ /MANAGED_CONTENT/
        if action['value'] == 'MANAGED_CONTENT'
          object['actions'][index]['value'] = action['value'].gsub('MANAGED_CONTENT', File.read(path).chomp)
        else
          object['actions'][index]['value'] = action['value'].gsub('MANAGED_CONTENT', File.read(path).chomp.gsub("\n", '\\n'))
        end
      end
    elsif action['value'].is_a?(Array)
      action['value'].each_with_index do |value, sub_index|
        if value =~ /MANAGED_CONTENT/
          if value == 'MANAGED_CONTENT'
            object['actions'][index]['value'][sub_index] = value.gsub('MANAGED_CONTENT', File.read(path).chomp)
          else
            object['actions'][index]['value'][sub_index] = value.gsub('MANAGED_CONTENT', File.read(path).chomp.gsub("\n", '\\n'))
          end
        end
      end
    end
  end
  object
end

.convert_managed_webhook(object, file) ⇒ Object

Performs conversions for a repo file if using managed webhooks

Parameters:

  • object (Hash)

    The Hash derived from parsing a YAML file

Author:

  • Jason Colyer

Since:

  • 1.0.12



255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
# File 'lib/support_readiness/repos/triggers.rb', line 255

def self.convert_managed_webhook(object, file)
  category = file.split("#{@location}/#{object['active'] ? '' : 'in' }active/").last.split('/').first
  path = "#{@location}/managed_content/#{category}/#{object['title']}.webhook"
  handle_request_error(1, 'Repos', 404, { action: 'Get managed webhook', id: object['title'] }) unless File.exist?(path)
  object['actions'].each_with_index do |action, index|
    next unless action['value'].is_a?(Array)

    action['value'].each_with_index do |value, sub_index|
      next unless value == 'MANAGED_WEBHOOK'

      converted_value = JSON.parse(File.read(path))
      if converted_value.is_a? Hash
        object['actions'][index]['value'][sub_index] = converted_value.to_json
      else
        object['actions'][index]['value'][sub_index] = converted_value
      end
    end
  end
  object
end

.detailed_diff(repo, zendesk) ⇒ Object

Outputs a comparison report

Parameters:

Author:

  • Jason Colyer

Since:

  • 1.0.12



98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/support_readiness/repos/triggers.rb', line 98

def self.detailed_diff(repo, zendesk)
  puts "Detailed diff of #{repo.title}"
  repo.instance_variables.each do |v|
    next if v == :@id

    if repo.instance_variable_get(v) != zendesk.instance_variable_get(v)
      puts "- #{v} Repo:"
      pp repo.instance_variable_get(v)
      puts "  #{v} Zendesk:"
      pp zendesk.instance_variable_get(v)
    end
  end
end

.gather(location = 'data') ⇒ Array

Parses repo trigger files

Examples:

require 'support_readiness'
repo = Readiness::Repos::Triggers.gather('triggers/data')
pp repo.count
# => 35

Parameters:

  • location (String) (defaults to: 'data')

    The location (relative or absolute) of the repo’s data folder

Returns:

  • (Array)

Author:

  • Jason Colyer

Since:

  • 1.0.12



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/support_readiness/repos/triggers.rb', line 138

def self.gather(location = 'data')
  @brands = Readiness::Zendesk::Brands.list(@zendesk_client)
  @categories = Readiness::Zendesk::TriggerCategories.list(@zendesk_client)
  @groups = Readiness::Zendesk::Groups.list(@zendesk_client)
  @schedules = Readiness::Zendesk::Schedules.list(@zendesk_client)
  @satisfaction_reasons = Readiness::Zendesk::SatisfactionReasons.list(@zendesk_client)
  @targets = Readiness::Zendesk::Targets.list(@zendesk_client)
  @ticket_fields = Readiness::Zendesk::TicketFields.list(@zendesk_client)
  @ticket_forms = Readiness::Zendesk::TicketForms.list(@zendesk_client)
  @webhooks = Readiness::Zendesk::Webhooks.list(@zendesk_client)
  @errors = []
  @location = location
  array = []
  Dir["#{@location}/**/*.yaml"].each do |f|
    object = YAML.safe_load_file(f)
    object = convert_managed_content(object, f) if object['contains_managed_content']
    object['default'] = false
    object = convert_managed_webhook(object, f) if object['contains_managed_webhook']
    object = convert_standard_names_to_ids(object)
    unless (!!Integer(object['category_id']) rescue false)
    #if object['category_id'].to_i.zero?
      trigger_category = Readiness::Zendesk::TriggerCategories.find_by_name(@zendesk_client, object['category_id'], @categories)
      object['category_id'] = trigger_category.id.to_s
    end
    validity_check(f, object)
    object['id'] = nil
    array.push(Zendesk::Triggers.new(object))
  end
  repo_check(array)
  report_errors unless @errors.count.zero?
  array
end

.repo_check(objects) ⇒ Object

Performs basic checks on the repo

Parameters:

  • objects (Array)

    The Array of Hashes derived from parsing the repo files

Author:

  • Jason Colyer

Since:

  • 1.0.12



206
207
208
209
210
211
212
213
# File 'lib/support_readiness/repos/triggers.rb', line 206

def self.repo_check(objects)
  duplicate_names = objects.group_by { |o| o.title }.select { |k, v| v.size > 1 }.map(&:first)
  unless duplicate_names.count.zero?
    duplicate_names.each do |d|
      @errors.push("The title '#{d}' is used in multiple files")
    end
  end
end

.report_diffs(diffs) ⇒ Object

Outputs a comparison report

Parameters:

  • diffs (Hash)

    The returned value of compare

Author:

  • Jason Colyer

Since:

  • 1.0.12



85
86
87
88
89
# File 'lib/support_readiness/repos/triggers.rb', line 85

def self.report_diffs(diffs)
  puts 'Compare report:'
  puts "- Creates: #{diffs[:creates].count}"
  puts "- Updates: #{diffs[:updates].count}"
end

.report_errorsObject

Outputs an error report and exits with a status code of 1

Author:

  • Jason Colyer

Since:

  • 1.0.12



176
177
178
179
180
181
182
183
# File 'lib/support_readiness/repos/triggers.rb', line 176

def self.report_errors
  puts 'The following errors were found in the repo files:'
  @errors.each do |e|
    puts "- #{e}"
  end
  puts 'Rectify the errors and retry. We cannot proceed with those errors occurring.'
  exit 1
end

.update_object(repo, zendesk) ⇒ Object

Creates an instance of Zendesk::Triggers to use for updates

Parameters:

Returns:

  • (Object)

Author:

  • Jason Colyer

Since:

  • 1.0.12



120
121
122
123
124
# File 'lib/support_readiness/repos/triggers.rb', line 120

def self.update_object(repo, zendesk)
  object = repo
  object.id = zendesk.id
  object
end

.validity_check(file, object) ⇒ Object

Performs basic checks on a repo file

Parameters:

  • file (String)

    The path to the repo file

  • object (Hash)

    The Hash derived from parsing a YAML file

Author:

  • Jason Colyer

Since:

  • 1.0.12



192
193
194
195
196
197
198
# File 'lib/support_readiness/repos/triggers.rb', line 192

def self.validity_check(file, object)
  folder = file.split("#{@location}/").last.split('/').first
  @errors.push("Missing position: #{file}") if object['position'].nil?
  @errors.push("Missing title: #{file}") if object['title'].nil?
  @errors.push("Inactive trigger in active folder: #{file}") if folder == 'active' && !object['active']
  @errors.push("Active trigger in inactive folder: #{file}") if folder == 'inactive' && object['active']
end