Class: Gitlab::Email::Handler::ServiceDeskHandler

Inherits:
BaseHandler
  • Object
show all
Includes:
ReplyProcessing, Utils::StrongMemoize
Defined in:
lib/gitlab/email/handler/service_desk_handler.rb

Constant Summary collapse

HANDLER_REGEX =
/\A#{HANDLER_ACTION_BASE_REGEX}-issue-\z/.freeze
HANDLER_REGEX_LEGACY =
/\A(?<project_path>[^\+]*)\z/.freeze
PROJECT_KEY_PATTERN =
/\A(?<slug>.+)-(?<key>[a-z0-9_]+)\z/.freeze

Instance Attribute Summary

Attributes inherited from BaseHandler

#mail, #mail_key

Instance Method Summary collapse

Methods included from Utils::StrongMemoize

#clear_memoization, #strong_memoize, #strong_memoized?

Methods inherited from BaseHandler

#metrics_params

Constructor Details

#initialize(mail, mail_key, service_desk_key: nil) ⇒ ServiceDeskHandler

Returns a new instance of ServiceDeskHandler.


17
18
19
20
21
22
23
24
25
26
# File 'lib/gitlab/email/handler/service_desk_handler.rb', line 17

def initialize(mail, mail_key, service_desk_key: nil)
  if service_desk_key
    mail_key ||= service_desk_key
    @service_desk_key = service_desk_key
  end

  super(mail, mail_key)

  match_project_slug || match_legacy_project_slug
end

Instance Method Details

#can_handle?Boolean

Returns:

  • (Boolean)

28
29
30
# File 'lib/gitlab/email/handler/service_desk_handler.rb', line 28

def can_handle?
  Gitlab::ServiceDesk.supported? && (project_id || can_handle_legacy_format? || service_desk_key)
end

#executeObject

Raises:


32
33
34
35
36
37
38
39
40
41
# File 'lib/gitlab/email/handler/service_desk_handler.rb', line 32

def execute
  raise ProjectNotFound if project.nil?

  create_issue_or_note

  if from_address
    add_email_participant
    send_thank_you_email unless reply_email?
  end
end

#match_legacy_project_slugObject


51
52
53
54
55
# File 'lib/gitlab/email/handler/service_desk_handler.rb', line 51

def match_legacy_project_slug
  return unless matched = HANDLER_REGEX_LEGACY.match(mail_key.to_s)

  @project_path = matched[:project_path]
end

#match_project_slugObject


43
44
45
46
47
48
49
# File 'lib/gitlab/email/handler/service_desk_handler.rb', line 43

def match_project_slug
  return if mail_key&.include?('/')
  return unless matched = HANDLER_REGEX.match(mail_key.to_s)

  @project_slug = matched[:project_slug]
  @project_id   = matched[:project_id]&.to_i
end

#metrics_eventObject


57
58
59
# File 'lib/gitlab/email/handler/service_desk_handler.rb', line 57

def metrics_event
  :receive_email_service_desk
end

#projectObject


61
62
63
64
65
66
67
# File 'lib/gitlab/email/handler/service_desk_handler.rb', line 61

def project
  strong_memoize(:project) do
    project_record = super
    project_record ||= project_from_key if service_desk_key
    project_record&.service_desk_enabled? ? project_record : nil
  end
end