Class: Readiness::TicketProcessor::LinkTagger

Inherits:
Client
  • Object
show all
Defined in:
lib/support_readiness/ticket_processor/link_tagger.rb

Overview

Defines the class LinkTagger within the module Zendesk.

Author:

  • Jason Colyer

Since:

  • 1.0.44

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

.call_domainsObject

Since:

  • 1.0.44



111
112
113
114
115
116
117
118
# File 'lib/support_readiness/ticket_processor/link_tagger.rb', line 111

def self.call_domains
  [
    'calendly.com',
    'gitlab.zoom.us',
    'gitlabmtgs.webex.com',
    'teams.microsoft.com'
  ]
end

.process!(zendesk_client, gitlab_client, ticket_id) ⇒ Object

Process a Link Tagging request

Author:

  • Jason Colyer

Since:

  • 1.0.44



18
19
20
21
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/support_readiness/ticket_processor/link_tagger.rb', line 18

def self.process!(zendesk_client, gitlab_client, ticket_id)
  @zendesk_client = zendesk_client
  @gitlab_client = gitlab_client
  @ticket_id = ticket_id
  @user = Readiness::Zendesk::Users.find(@zendesk_client, ENV.fetch('COMMENT_AUTHOR'))
  puts 'No such user, so no comment to review' if @user.is_a? Hash
  exit 0 if @user.is_a? Hash
  puts 'User is not an agent, so not proceeding' unless %w[admin agent].include? @user.role
  exit 0 unless %w[admin agent].include? @user.role
  @ticket = Readiness::Zendesk::Tickets.find(@zendesk_client, @ticket_id)
  puts 'No ticket found, so no tags to add' if @ticket.is_a? Hash
  exit 0 if @ticket.is_a? Hash

  comments = Readiness::Zendesk::Tickets.comments(@zendesk_client, @ticket)
  public_comments = comments.select { |c| c['public'] }
  comment_to_check = public_comments.select { |c| c['author_id'] == @user.id }.last
  if comment_to_check.nil?
    public_comments.reverse.each do |comment|
      new_user = Readiness::Zendesk::Users.find(@zendesk_client, comment['author_id'])
      next unless %w[admin agent].include? new_user.role

      comment_to_check = comment
      break
    end
  end
  if comment_to_check.nil?
    puts 'Cannot determine a comment to check, so exiting...'
    exit 0
  end
  tags_to_add = []
  Nokogiri::HTML(comment_to_check['html_body']).css('a').each do |p|
    next if p['href'].nil? || p['href'].empty?

    link = p['href'].split('?').first.split('#').first
    domain = link.split('/')[2]
    if domain == 'gitlab.com'
      if link =~ %r{/-/issues/[0-9]+}
        slug = link.split("#{domain}/").last.split(%r{/-/issues/[0-9]+}).first
        project = Readiness::GitLab::Projects.find(@gitlab_client, CGI.escape(slug))
        unless project.is_a? Hash
          iid = link.split('/').last
          issue = Readiness::GitLab::Issues.find(@gitlab_client, project, iid)
          unless issue.is_a? Hash
            tags_to_add.push('gitlab_issue_link')
            tags_to_add.push("#{slug.gsub('/', '_')}_issues_#{iid}")
            tags_to_add.push("issue~#{slug.gsub('/', '~')}_#{iid}")
            tags_to_add.push("issue_#{project.id}_#{iid}")
          end
        end
      elsif link =~ %r{/-/merge_requests/[0-9]+}
        slug = link.split("#{domain}/").last.split(%r{/-/merge_requests/[0-9]+}).first
        project = Readiness::GitLab::Projects.find(@gitlab_client, CGI.escape(slug))
        unless project.is_a? Hash
          iid = link.split('/').last
          mr = Readiness::GitLab::MergeRequests.find(@gitlab_client, project, iid)
          unless mr['message'] || mr['error']
            tags_to_add.push('gitlab_merge_request_link')
            tags_to_add.push("#{slug.gsub('/', '_')}_merge_requests_#{iid}")
            tags_to_add.push("mergerequest~#{slug.gsub('/', '~')}_#{iid}")
            tags_to_add.push("mergerequest_#{project.id}_#{iid}")
          end
        end
      end
    elsif domain == 'docs.gitlab.com'
      tags_to_add.push('docs_link')
      path = link.split('docs.gitlab.com/').last.split('/').join('_')
      tags_to_add.push("doc_#{path}")
    elsif domain == 'handbook.gitlab.com'
      tags_to_add.push('hb_link')
      path = link.split('handbook.gitlab.com/handbook/').last.split('/').join('_')
      tags_to_add.push("hb_#{path}")
    elsif domain == 'support.gitlab.com'
      if link =~ %r{articles}
        tags_to_add.push('kb_link') unless link =~ %r{4911284066204}
      end
    elsif call_domains.include? domain
      tags_to_add.push('agent_offered_call')
    end
  end
  if tags_to_add.uniq.count.zero?
    puts 'No tags to add on this ticket'
    exit 0
  else
    print "Adding #{tags_to_add.uniq.count} tag(s) to ticket..."
    @ticket = Readiness::Zendesk::Tickets.find(@zendesk_client, @ticket_id)
    new_ticket = Readiness::Zendesk::Tickets.new
    new_ticket.id = @ticket.id
    new_ticket.tags = @ticket.tags + tags_to_add.uniq
    Readiness::Zendesk::Tickets.update!(@zendesk_client, new_ticket)
    puts 'done'
  end
end