Class: Readiness::Repos::TicketFields

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

Overview

Defines the class TicketFields 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 ticket fields files to the Zendesk instance ticket fields

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



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/support_readiness/repos/ticket_fields.rb', line 22

def self.compare(zendesk_client, location = 'data', verbose = false)
  @from_repo = gather(zendesk_client, location)
  @from_zendesk = Zendesk::TicketFields.list(zendesk_client)
  @diffs = {
    field_updates: [],
    field_creates: [],
    option_updates: [],
    option_deletes: []
  }
  @from_repo.each do |repo|
    zd = @from_zendesk.detect { |z| z.title == repo.title }
    if zd.nil?
      @diffs[:field_creates].push(repo)
      unless repo.custom_field_options.nil?
        repo.custom_field_options.each do |o|
          @diffs[:option_updates].push({ "#{repo.title}" => o })
        end
      end
      break
    else
      comparable_zd = zd.dup
      comparable_repo = repo.dup
      comparable_zd.id = nil
      comparable_zd.custom_field_options = nil
      comparable_repo.custom_field_options = nil
      @diffs[:field_updates].push(update_object(repo, zd)) if to_clean_json(comparable_zd) != to_clean_json(comparable_repo)
      detailed_diff(comparable_repo, comparable_zd) if verbose && to_clean_json(comparable_repo) != to_clean_json(comparable_zd)
      next if repo.custom_field_options.nil?

      zd_options = @from_zendesk.detect { |z| z.title == repo.title }.custom_field_options
      repo.custom_field_options.map! { |o| Zendesk::TicketFieldOptions.new(o) } unless repo.custom_field_options.first.is_a? Object
      repo.custom_field_options.each do |repo_option|
        repo_option.position = repo.custom_field_options.index(repo_option) if repo_option.position.nil?
        zd_option = zd.custom_field_options.detect { |zd_o| zd_o.value == repo_option.value }
        if zd_option.nil? || option_difference?(zd_option, repo_option)
          @diffs[:option_updates].push({ "#{repo.title}" => repo_option })
        end
      end
      zd_options.each do |zd_option|
        repo_option = repo.custom_field_options.detect { |rp_o| rp_o.value == zd_option.value }
        if repo_option.nil?
          @diffs[:option_deletes].push({ "#{zd.title}" => zd_option })
        end
      end
    end
  end
  report_diffs if verbose
  @diffs
end

.detailed_diff(repo, zendesk) ⇒ Object

Outputs a comparison report

Parameters:

Author:

  • Jason Colyer

Since:

  • 1.0.12



108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/support_readiness/repos/ticket_fields.rb', line 108

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(client, location = 'data') ⇒ Array

Parses repo ticket field files

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)
repo = Readiness::Repos::TicketFields.gather(client, 'tickets/forms-and-fields/data/fields')
pp repo.count
# => 170

Parameters:

  • client (Object)

    An instance of Zendesk::Client

  • 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



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/support_readiness/repos/ticket_fields.rb', line 154

def self.gather(client, location = 'data')
  @errors = []
  @location = location
  array = []
  Dir["#{@location}/**/*.yaml"].each do |f|
    object = YAML.safe_load_file(f)
    validity_check(f, object)
    object['id'] = nil
    object['title_in_portal'] = (object['visible_in_portal'] ? object['title_in_portal'] : object['title'])
    array.push(Zendesk::TicketFields.new(client, object))
  end
  repo_check(array)
  report_errors unless @errors.count.zero?
  array
end

.option_difference?(zd_option, repo_option) ⇒ Boolean

Deterine if there is a difference between the repo ticket field options to the Zendesk instance ticket field options

Parameters:

Returns:

  • (Boolean)

Author:

  • Jason Colyer

Since:

  • 1.0.38



80
81
82
83
84
85
86
# File 'lib/support_readiness/repos/ticket_fields.rb', line 80

def self.option_difference?(zd_option, repo_option)
  return true if zd_option.name != repo_option.name
  return true if zd_option.position != repo_option.position
  return true if zd_option.value != repo_option.value

  false
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/ticket_fields.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_diffsObject

Outputs a comparison report

Author:

  • Jason Colyer

Since:

  • 1.0.12



93
94
95
96
97
98
99
# File 'lib/support_readiness/repos/ticket_fields.rb', line 93

def self.report_diffs
  puts 'Compare report:'
  puts "- Field Creates:  #{@diffs[:field_creates].count}"
  puts "- Field Updates:  #{@diffs[:field_updates].count}"
  puts "- Option Updates: #{@diffs[:option_updates].count}"
  puts "- Option Deletes: #{@diffs[:option_deletes].count}"
end

.report_errorsObject

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

Author:

  • Jason Colyer

Since:

  • 1.0.12



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

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::TicketFields to use for updates

Parameters:

Returns:

  • (Object)

Author:

  • Jason Colyer

Since:

  • 1.0.12



130
131
132
133
134
# File 'lib/support_readiness/repos/ticket_fields.rb', line 130

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



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

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