Browse Source

Fixes #4249 - Zendesk Import: Link to linked Tickets are not working after migration to Zammad.

Dominik Klein 2 years ago
parent
commit
3ce99d9ff5

+ 1 - 0
lib/sequencer/sequence/import/zendesk/ticket/comment.rb

@@ -16,6 +16,7 @@ class Sequencer
                 'Import::Zendesk::Ticket::Comment::To',
                 'Import::Zendesk::Ticket::Comment::Mapping',
                 'Import::Zendesk::Ticket::Comment::InlineImages',
+                'Import::Zendesk::Ticket::Comment::InternalTicketLinks',
                 'Import::Zendesk::Ticket::Comment::UnsetInstance',
                 'Common::ModelClass::Ticket::Article',
                 'Import::Common::Model::FindBy::MessageId',

+ 53 - 0
lib/sequencer/unit/import/zendesk/ticket/comment/internal_ticket_links.rb

@@ -0,0 +1,53 @@
+# Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
+
+class Sequencer
+  class Unit
+    module Import
+      module Zendesk
+        module Ticket
+          module Comment
+            class InternalTicketLinks < Sequencer::Unit::Base
+              include ::Sequencer::Unit::Import::Common::Mapping::Mixin::ProvideMapped
+
+              uses :mapped
+
+              def process
+                return if !contains_internal_ticket_link?(mapped[:body])
+
+                provide_mapped do
+                  {
+                    body: replaced_internal_ticket_links,
+                  }
+                end
+              end
+
+              private
+
+              def contains_internal_ticket_link?(string)
+                return false if string.blank?
+
+                string.include?('/agent/tickets/')
+              end
+
+              def replaced_internal_ticket_links
+                body_html = Nokogiri::HTML(mapped[:body])
+
+                body_html.css('a').each do |node|
+                  next if !contains_internal_ticket_link?(node['href'])
+
+                  node.attributes['href'].value = convert_link(node['href'])
+                end
+
+                body_html.to_html
+              end
+
+              def convert_link(link)
+                link.sub! '/agent/tickets/', '/#ticket/zoom/'
+              end
+            end
+          end
+        end
+      end
+    end
+  end
+end

+ 2 - 2
spec/lib/sequencer/sequence/import/zendesk/ticket/comment_spec.rb

@@ -22,7 +22,7 @@ RSpec.describe ::Sequencer::Sequence::Import::Zendesk::Ticket::Comment, sequence
           'id'          => 31_964_468_581,
           'type'        => 'Comment',
           'author_id'   => 1_150_734_731,
-          'html_body'   => "<div class=\"zd-comment\" dir=\"auto\"><p dir=\"auto\">This is the latest comment for this ticket. You also changed the ticket status to Pending.</p><span style=\"opacity: 1;\"><img src=\"#{inline_image_url}\"></span></div>",
+          'html_body'   => "<div class=\"zd-comment\" dir=\"auto\"><p dir=\"auto\">This is the latest comment for this ticket. You also changed the ticket status to Pending.</p><span style=\"opacity: 1;\"><img src=\"#{inline_image_url}\"></span><a href=\"/agent/tickets/1\" rel=\"ticket\">#1</a></p></div>",
           'public'      => true,
           'attachments' => [
             {
@@ -87,7 +87,7 @@ RSpec.describe ::Sequencer::Sequence::Import::Zendesk::Ticket::Comment, sequence
       {
         from:       'john.doe@example.com',
         to:         'zendesk@example.com',
-        body:       "\n<div dir=\"auto\">\n<p dir=\"auto\">This is the latest comment for this ticket. You also changed the ticket status to Pending.</p>\n<span><img src=\"data:image/png;base64,MTIz\"></span>\n</div>\n",
+        body:       "\n<div dir=\"auto\">\n<p dir=\"auto\">This is the latest comment for this ticket. You also changed the ticket status to Pending.</p>\n<span><img src=\"data:image/png;base64,MTIz\"></span><a href=\"/#ticket/zoom/1\" rel=\"ticket\">#1</a>\n</div>\n",
         created_at: Time.zone.parse('2018-09-28T12:00:00Z'),
         updated_at: Time.zone.parse('2018-09-28T12:00:00Z'),
       }