Class: Readiness::Repos::TicketForms

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

Overview

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

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::TicketForms.compare(client, 'tickets/forms-and-fields/data/forms', false)
pp diffs[:updates.count]
# => 1
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::Groups.compare(client, 'groups/data', true)
# => Detailed diff of Support Ops
# => - @description Repo:
# => "For GitLab Support Operations"
# =>   @description Zendesk:
# => "For GitLab Support Ops"
# => Compare report:
# => - Creates: 0
# => - Updates: 1
pp diffs[:updates.count]
# => 1

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



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/support_readiness/repos/ticket_forms.rb', line 50

def self.compare(zendesk_client, location = 'data', verbose = false)
  @zendesk_client = zendesk_client
  diffs = {
    updates: [],
    creates: []
  }
  from_repo = gather(location)
  from_zendesk = Zendesk::TicketForms.list(zendesk_client)
  from_repo.each do |repo|
    zd = from_zendesk.detect { |z| z.name == repo.name }
    if zd.nil?
      diffs[:creates].push(repo)
    else
      comparable_zd = zd.dup
      comparable_repo = repo.dup
      comparable_zd.id = nil
      comparable_zd.agent_conditions.sort_by! { |a| [a['parent_field_id'], a['value']] }
      comparable_zd.agent_conditions.map { |a| a['child_fields'].sort_by! { |c| c['id'] }}
      comparable_zd.end_user_conditions.sort_by! { |a| [a['parent_field_id'], a['value']] }
      comparable_zd.end_user_conditions.map { |a| a['child_fields'].sort_by! { |c| c['id'] }}
      comparable_repo.agent_conditions.sort_by! { |a| [a['parent_field_id'], a['value']] }
      comparable_repo.agent_conditions.map { |a| a['child_fields'].sort_by! { |c| c['id'] }}
      comparable_repo.end_user_conditions.sort_by! { |a| [a['parent_field_id'], a['value']] }
      comparable_repo.end_user_conditions.map { |a| a['child_fields'].sort_by! { |c| c['id'] }}
      diffs[:updates].push(update_object(repo, zd)) if to_clean_json(comparable_repo) != to_clean_json(comparable_zd)
      detailed_diff(comparable_repo, comparable_zd) if verbose && to_clean_json(comparable_repo) != to_clean_json(comparable_zd)
    end
  end
  report_diffs(diffs) if verbose
  diffs
end

.detailed_diff(repo, zendesk) ⇒ Object

Outputs a comparison report

Parameters:

Author:

  • Jason Colyer

Since:

  • 1.0.12



101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/support_readiness/repos/ticket_forms.rb', line 101

def self.detailed_diff(repo, zendesk)
  puts "Detailed diff of #{repo.name}"
  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 ticket form files

Examples:

require 'support_readiness'
repo = Readiness::Repos::TicketForms.gather('groups/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



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/support_readiness/repos/ticket_forms.rb', line 141

def self.gather(location = 'data')
  @brands = Readiness::Zendesk::Brands.list(@zendesk_client)
  @ticket_fields = Readiness::Zendesk::TicketFields.list(@zendesk_client)
  @errors = []
  @location = location
  array = []
  Dir["#{@location}/**/*.yaml"].each do |f|
    object = YAML.safe_load_file(f)
    object = convert_ticket_form_names_to_ids(object)
    validity_check(f, object)
    object['id'] = nil
    array.push(Zendesk::TicketForms.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



194
195
196
197
198
199
200
201
# File 'lib/support_readiness/repos/ticket_forms.rb', line 194

def self.repo_check(objects)
  duplicate_names = objects.group_by { |o| o.name }.select { |k, v| v.size > 1 }.map(&:first)
  unless duplicate_names.count.zero?
    duplicate_names.each do |d|
      @errors.push("The name '#{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



88
89
90
91
92
# File 'lib/support_readiness/repos/ticket_forms.rb', line 88

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



164
165
166
167
168
169
170
171
# File 'lib/support_readiness/repos/ticket_forms.rb', line 164

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

Parameters:

Returns:

  • (Object)

Author:

  • Jason Colyer

Since:

  • 1.0.12



123
124
125
126
127
# File 'lib/support_readiness/repos/ticket_forms.rb', line 123

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



180
181
182
183
184
185
186
# File 'lib/support_readiness/repos/ticket_forms.rb', line 180

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