2020-07-14 15:09:05 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# handles service desk issue creation emails with these formats:
|
|
|
|
# incoming+gitlab-org-gitlab-ce-20-issue-@incoming.gitlab.com
|
|
|
|
# incoming+gitlab-org/gitlab-ce@incoming.gitlab.com (legacy)
|
|
|
|
module Gitlab
|
|
|
|
module Email
|
|
|
|
module Handler
|
|
|
|
class ServiceDeskHandler < BaseHandler
|
|
|
|
include ReplyProcessing
|
|
|
|
include Gitlab::Utils::StrongMemoize
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
def initialize(mail, mail_key, service_desk_key: nil)
|
2021-10-21 09:09:48 +00:00
|
|
|
if service_desk_key
|
|
|
|
mail_key ||= service_desk_key
|
2020-07-14 15:09:05 +00:00
|
|
|
@service_desk_key = service_desk_key
|
|
|
|
end
|
2021-10-21 09:09:48 +00:00
|
|
|
|
|
|
|
super(mail, mail_key)
|
|
|
|
|
|
|
|
match_project_slug || match_legacy_project_slug
|
2020-07-14 15:09:05 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def can_handle?
|
|
|
|
Gitlab::ServiceDesk.supported? && (project_id || can_handle_legacy_format? || service_desk_key)
|
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
|
|
|
raise ProjectNotFound if project.nil?
|
|
|
|
|
2021-11-24 15:14:19 +00:00
|
|
|
create_issue_or_note
|
2021-02-15 15:08:59 +00:00
|
|
|
|
|
|
|
if from_address
|
|
|
|
add_email_participant
|
2021-11-24 15:14:19 +00:00
|
|
|
send_thank_you_email unless reply_email?
|
2021-02-15 15:08:59 +00:00
|
|
|
end
|
2020-07-14 15:09:05 +00:00
|
|
|
end
|
|
|
|
|
2021-10-21 09:09:48 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
def match_legacy_project_slug
|
|
|
|
return unless matched = HANDLER_REGEX_LEGACY.match(mail_key.to_s)
|
|
|
|
|
|
|
|
@project_path = matched[:project_path]
|
|
|
|
end
|
|
|
|
|
2020-07-14 15:09:05 +00:00
|
|
|
def metrics_event
|
|
|
|
:receive_email_service_desk
|
|
|
|
end
|
|
|
|
|
|
|
|
def project
|
|
|
|
strong_memoize(:project) do
|
2021-10-21 09:09:48 +00:00
|
|
|
project_record = super
|
|
|
|
project_record ||= project_from_key if service_desk_key
|
|
|
|
project_record&.service_desk_enabled? ? project_record : nil
|
2020-07-14 15:09:05 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-07-01 21:08:38 +00:00
|
|
|
private
|
|
|
|
|
|
|
|
attr_reader :project_id, :project_path, :service_desk_key
|
|
|
|
|
2020-07-14 15:09:05 +00:00
|
|
|
def project_from_key
|
|
|
|
return unless match = service_desk_key.match(PROJECT_KEY_PATTERN)
|
|
|
|
|
2021-05-25 15:10:33 +00:00
|
|
|
Project.with_service_desk_key(match[:key]).find do |project|
|
|
|
|
valid_project_key?(project, match[:slug])
|
|
|
|
end
|
2020-07-14 15:09:05 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def valid_project_key?(project, slug)
|
2021-01-12 21:10:47 +00:00
|
|
|
project.present? && slug == project.full_path_slug
|
2020-07-14 15:09:05 +00:00
|
|
|
end
|
|
|
|
|
2021-11-24 15:14:19 +00:00
|
|
|
def create_issue_or_note
|
|
|
|
if reply_email?
|
|
|
|
create_note_from_reply_email
|
|
|
|
else
|
|
|
|
create_issue!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-07-14 15:09:05 +00:00
|
|
|
def create_issue!
|
2021-09-15 12:11:13 +00:00
|
|
|
@issue = ::Issues::CreateService.new(
|
2021-05-12 03:10:21 +00:00
|
|
|
project: project,
|
|
|
|
current_user: User.support_bot,
|
|
|
|
params: {
|
|
|
|
title: mail.subject,
|
|
|
|
description: message_including_template,
|
|
|
|
confidential: true,
|
|
|
|
external_author: from_address
|
2021-06-21 12:07:45 +00:00
|
|
|
},
|
|
|
|
spam_params: nil
|
2020-07-14 15:09:05 +00:00
|
|
|
).execute
|
|
|
|
|
|
|
|
raise InvalidIssueError unless @issue.persisted?
|
|
|
|
|
2021-11-24 15:14:19 +00:00
|
|
|
begin
|
|
|
|
::Issue::Email.create!(issue: @issue, email_message_id: mail.message_id)
|
|
|
|
rescue StandardError => e
|
|
|
|
Gitlab::ErrorTracking.log_exception(e)
|
|
|
|
end
|
|
|
|
|
2020-07-14 15:09:05 +00:00
|
|
|
if service_desk_setting&.issue_template_missing?
|
2021-11-24 15:14:19 +00:00
|
|
|
create_template_not_found_note
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def issue_from_reply_to
|
|
|
|
strong_memoize(:issue_from_reply_to) do
|
|
|
|
next unless mail.in_reply_to
|
|
|
|
|
|
|
|
Issue::Email.find_by_email_message_id(mail.in_reply_to)&.issue
|
2020-07-14 15:09:05 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-11-24 15:14:19 +00:00
|
|
|
def reply_email?
|
|
|
|
issue_from_reply_to.present?
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_note_from_reply_email
|
|
|
|
@issue = issue_from_reply_to
|
|
|
|
|
|
|
|
create_note(message_including_reply)
|
|
|
|
end
|
|
|
|
|
2021-05-06 18:10:06 +00:00
|
|
|
def send_thank_you_email
|
|
|
|
Notify.service_desk_thank_you_email(@issue.id).deliver_later
|
2021-06-21 12:07:45 +00:00
|
|
|
Gitlab::Metrics::BackgroundTransaction.current&.add_event(:service_desk_thank_you_email)
|
2020-07-14 15:09:05 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def message_including_template
|
2021-11-19 09:13:48 +00:00
|
|
|
description = message_including_reply_or_only_quotes
|
2020-07-14 15:09:05 +00:00
|
|
|
template_content = service_desk_setting&.issue_template_content
|
|
|
|
|
|
|
|
if template_content.present?
|
|
|
|
description += " \n" + template_content
|
|
|
|
end
|
|
|
|
|
|
|
|
description
|
|
|
|
end
|
|
|
|
|
|
|
|
def service_desk_setting
|
|
|
|
strong_memoize(:service_desk_setting) do
|
|
|
|
project.service_desk_setting
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-11-24 15:14:19 +00:00
|
|
|
def create_template_not_found_note
|
2020-07-14 15:09:05 +00:00
|
|
|
issue_template_key = service_desk_setting&.issue_template_key
|
|
|
|
|
|
|
|
warning_note = <<-MD.strip_heredoc
|
|
|
|
WARNING: The template file #{issue_template_key}.md used for service desk issues is empty or could not be found.
|
|
|
|
Please check service desk settings and update the file to be used.
|
|
|
|
MD
|
|
|
|
|
2021-11-24 15:14:19 +00:00
|
|
|
create_note(warning_note)
|
|
|
|
end
|
2020-07-14 15:09:05 +00:00
|
|
|
|
2021-11-24 15:14:19 +00:00
|
|
|
def create_note(note)
|
2020-07-14 15:09:05 +00:00
|
|
|
::Notes::CreateService.new(
|
|
|
|
project,
|
|
|
|
User.support_bot,
|
2021-11-24 15:14:19 +00:00
|
|
|
noteable: @issue,
|
|
|
|
note: note
|
2020-07-14 15:09:05 +00:00
|
|
|
).execute
|
|
|
|
end
|
|
|
|
|
|
|
|
def from_address
|
|
|
|
(mail.reply_to || []).first || mail.from.first || mail.sender
|
|
|
|
end
|
|
|
|
|
|
|
|
def can_handle_legacy_format?
|
|
|
|
project_path && project_path.include?('/') && !mail_key.include?('+')
|
|
|
|
end
|
|
|
|
|
|
|
|
def author
|
|
|
|
User.support_bot
|
|
|
|
end
|
2021-02-15 15:08:59 +00:00
|
|
|
|
|
|
|
def add_email_participant
|
2021-11-24 15:14:19 +00:00
|
|
|
return if reply_email? && !Feature.enabled?(:issue_email_participants, @issue.project)
|
|
|
|
|
2021-02-15 15:08:59 +00:00
|
|
|
@issue.issue_email_participants.create(email: from_address)
|
|
|
|
end
|
2020-07-14 15:09:05 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|