Fixes #2941 - Zendesk import error

This commit is contained in:
Mantas Masalskis 2020-04-08 08:23:58 +02:00
parent 16b876e3e8
commit dd5ca52e18
6 changed files with 57 additions and 41 deletions

View file

@ -276,7 +276,6 @@ test:integration:zendesk:
script: script:
- bundle exec rake zammad:db:unseeded - bundle exec rake zammad:db:unseeded
- bundle exec rails test test/integration/zendesk_import_test.rb - bundle exec rails test test/integration/zendesk_import_test.rb
allow_failure: true
### OTRS ### OTRS

View file

@ -253,13 +253,21 @@ returns
file.provider file.provider
end end
RESIZABLE_MIME_REGEXP = %r{image/(jpeg|jpg|png)}i.freeze
def self.resizable_mime?(input)
input.match? RESIZABLE_MIME_REGEXP
end
private private
def generate_previews def generate_previews
return true if Setting.get('import_mode') return true if Setting.get('import_mode')
resizable = preferences.slice('Mime-Type', 'Content-Type', 'mime_type', 'content_type') resizable = preferences
.values.grep(%r{image/(jpeg|jpg|png)}i).any? .slice('Mime-Type', 'Content-Type', 'mime_type', 'content_type')
.values
.any? { |mime| self.class.resizable_mime?(mime) }
begin begin
if resizable if resizable

View file

@ -12,7 +12,7 @@ class Sequencer
end end
def resource_collection def resource_collection
"::ZendeskAPI::#{resource_klass}".constantize.incremental_export(client, 1) client.send resource_klass.pluralize.underscore
end end
def resource_iteration def resource_iteration

View file

@ -11,28 +11,17 @@ class Sequencer
def statistics_diff def statistics_diff
%i[Groups Users Organizations Tickets].each_with_object({}) do |object, stats| %i[Groups Users Organizations Tickets].each_with_object({}) do |object, stats|
stats[object] = empty_diff.merge( stats[object] = object_diff(object)
total: request(object).count!
)
end end
end end
# the special "incremental_export" logic is needed because Zendesk def object_diff(object)
# archives records and doesn't return them via e.g. client.tickets collection_name = object.to_s.underscore
# endpoint as described here: collection = client.send collection_name
# https://github.com/zammad/zammad/issues/558#issuecomment-267951351
# Counting via the incremental_export endpoint has the limitations empty_diff.merge(
# that it returns max. 1000. That's why we need to update the total total: collection.count!
# number while importing in the resource loop )
def request(object)
resource_class = "::ZendeskAPI::#{object.singularize}".safe_constantize
if resource_class.respond_to?(:incremental_export)
# read as: ::ZendeskAPI::Ticket.incremental_export(client, 1)
resource_class.incremental_export(client, 1)
else
# read as: client.groups
client.send(object.to_s.underscore.to_sym)
end
end end
end end
end end

View file

@ -19,14 +19,25 @@ class Sequencer
o_id: instance.id, o_id: instance.id,
data: response.body, data: response.body,
filename: resource.file_name, filename: resource.file_name,
preferences: { preferences: store_preferences,
'Content-Type' => resource.content_type
},
created_by_id: 1 created_by_id: 1
) )
rescue => e rescue => e
handle_failure(e) handle_failure(e)
end end
private
def store_preferences
output = { 'Content-Type' => resource.content_type }
if Store.resizable_mime? resource.content_type
output[:resizable] = true
output[:content_preview] = true
end
output
end
end end
end end
end end

View file

@ -59,13 +59,13 @@ class ZendeskImportTest < ActiveSupport::TestCase
}, },
Tickets: { Tickets: {
skipped: 0, skipped: 0,
created: 142, created: 141,
updated: 1, updated: 1,
unchanged: 0, unchanged: 0,
failed: 0, failed: 0,
deactivated: 0, deactivated: 0,
sum: 143, sum: 142,
total: 143 total: 142
} }
} }
@ -78,9 +78,9 @@ class ZendeskImportTest < ActiveSupport::TestCase
assert_equal(3, Group.count, 'groups') assert_equal(3, Group.count, 'groups')
assert_equal(3, Role.count, 'roles') assert_equal(3, Role.count, 'roles')
assert_equal(2, Organization.count, 'organizations') assert_equal(2, Organization.count, 'organizations')
assert_equal(143, Ticket.count, 'tickets') assert_equal(142, Ticket.count, 'tickets')
assert_equal(151, Ticket::Article.count, 'ticket articles') assert_equal(151, Ticket::Article.count, 'ticket articles')
assert_equal(2, Store.count, 'ticket article attachments') assert_equal(3, Store.count, 'ticket article attachments')
# TODO: Macros, Views, Automations... # TODO: Macros, Views, Automations...
end end
@ -328,15 +328,14 @@ class ZendeskImportTest < ActiveSupport::TestCase
id: 2, id: 2,
data: { data: {
title: 'test', title: 'test',
#note: 'This is the first comment. Feel free to delete this sample ticket.', note: nil,
note: 'test email',
create_article_type_id: 1, create_article_type_id: 1,
create_article_sender_id: 2, create_article_sender_id: 2,
article_count: 2, article_count: 2,
state_id: 3, state_id: 3,
group_id: 3, group_id: 3,
priority_id: 3, priority_id: 3,
owner_id: 1, owner_id: User.find_by(login: 'bob.smith@znuny.com').id,
customer_id: 7, customer_id: 7,
organization_id: 2, organization_id: 2,
test_checkbox: true, test_checkbox: true,
@ -350,16 +349,14 @@ class ZendeskImportTest < ActiveSupport::TestCase
id: 3, id: 3,
data: { data: {
title: 'Bob Smith, here is the test ticket you requested', title: 'Bob Smith, here is the test ticket you requested',
note: 'Hello! This is a Zendesk ticket. We are going to go through the basic support ticket operation in Zendesk. note: nil,
If you\'re reading this message in your email, click the ticket number link that immediately follows the line \'You have been assigned to this t',
create_article_type_id: 10, create_article_type_id: 10,
create_article_sender_id: 2, create_article_sender_id: 2,
article_count: 4, article_count: 5,
state_id: 3, state_id: 3,
group_id: 3, group_id: 3,
priority_id: 1, priority_id: 1,
owner_id: 1, owner_id: User.find_by(login: 'bob.smith@znuny.com').id,
customer_id: 8, customer_id: 8,
organization_id: nil, organization_id: nil,
test_checkbox: false, test_checkbox: false,
@ -373,7 +370,7 @@ If you\'re reading this message in your email, click the ticket number link that
id: 5, id: 5,
data: { data: {
title: 'Twitter', title: 'Twitter',
note: "@gabyalanisr Brandon Arely Snuppy Jaz Jerry Liz Irvig &amp; Wera\nY Losa Otrs Yop \npero si quieres Los Que Puedas", note: nil,
create_article_type_id: 6, create_article_type_id: 6,
create_article_sender_id: 2, create_article_sender_id: 2,
article_count: 1, article_count: 1,
@ -389,7 +386,7 @@ If you\'re reading this message in your email, click the ticket number link that
id: 143, id: 143,
data: { data: {
title: 'Basti ist cool', title: 'Basti ist cool',
note: 'Basti ist cool', note: nil,
create_article_type_id: 8, create_article_type_id: 8,
create_article_sender_id: 2, create_article_sender_id: 2,
article_count: 1, article_count: 1,
@ -465,6 +462,18 @@ If you\'re reading this message in your email, click the ticket number link that
}, },
}, },
}, },
{
message_id: 538_901_840_720,
data: {
count: 1,
1 => {
preferences: {
'Content-Type' => 'text/rtf'
},
filename: 'test.rtf',
},
},
},
] ]
checks.each do |check| checks.each do |check|