2018-08-23 09:22:23 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-10-12 17:07:36 +00:00
|
|
|
module Gitlab
|
|
|
|
module Email
|
|
|
|
module Handler
|
|
|
|
module ReplyProcessing
|
2018-12-28 18:53:39 +00:00
|
|
|
# rubocop:disable Gitlab/ModuleWithInstanceVariables
|
2016-10-12 17:07:36 +00:00
|
|
|
def project
|
2018-12-28 18:53:39 +00:00
|
|
|
return @project if instance_variable_defined?(:@project)
|
|
|
|
|
|
|
|
if project_id
|
|
|
|
@project = Project.find_by_id(project_id)
|
|
|
|
@project = nil unless valid_project_slug?(@project)
|
|
|
|
else
|
|
|
|
@project = Project.find_by_full_path(project_path)
|
|
|
|
end
|
|
|
|
|
|
|
|
@project
|
2016-10-12 17:07:36 +00:00
|
|
|
end
|
2018-12-28 18:53:39 +00:00
|
|
|
# rubocop:enable Gitlab/ModuleWithInstanceVariables
|
2016-10-12 17:07:36 +00:00
|
|
|
|
2021-07-01 21:08:38 +00:00
|
|
|
private
|
|
|
|
|
|
|
|
attr_reader :project_id, :project_slug, :project_path, :incoming_email_token
|
|
|
|
|
|
|
|
def author
|
|
|
|
raise NotImplementedError
|
|
|
|
end
|
|
|
|
|
2016-10-12 17:07:36 +00:00
|
|
|
def message
|
|
|
|
@message ||= process_message
|
|
|
|
end
|
|
|
|
|
2018-05-09 20:19:16 +00:00
|
|
|
def message_including_reply
|
|
|
|
@message_with_reply ||= process_message(trim_reply: false)
|
|
|
|
end
|
|
|
|
|
2021-11-19 09:13:48 +00:00
|
|
|
def message_including_reply_or_only_quotes
|
|
|
|
@message_including_reply_or_only_quotes ||= process_message(trim_reply: false, allow_only_quotes: true)
|
|
|
|
end
|
|
|
|
|
2021-08-13 03:10:09 +00:00
|
|
|
def message_with_appended_reply
|
|
|
|
@message_with_appended_reply ||= process_message(append_reply: true)
|
|
|
|
end
|
|
|
|
|
2018-05-09 20:19:16 +00:00
|
|
|
def process_message(**kwargs)
|
2021-08-13 03:10:09 +00:00
|
|
|
message, stripped_text = ReplyParser.new(mail, **kwargs).execute
|
|
|
|
message = message.strip
|
|
|
|
|
2020-07-14 15:09:05 +00:00
|
|
|
message_with_attachments = add_attachments(message)
|
2021-08-13 03:10:09 +00:00
|
|
|
# Support bot is specifically forbidden from using slash commands.
|
|
|
|
message = strip_quick_actions(message_with_attachments)
|
|
|
|
return message unless kwargs[:append_reply]
|
2020-07-14 15:09:05 +00:00
|
|
|
|
2021-08-13 03:10:09 +00:00
|
|
|
append_reply(message, stripped_text)
|
2016-10-12 17:07:36 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def add_attachments(reply)
|
2020-12-10 12:09:43 +00:00
|
|
|
attachments = Email::AttachmentUploader.new(mail).execute(**upload_params)
|
2016-10-12 17:07:36 +00:00
|
|
|
|
|
|
|
reply + attachments.map do |link|
|
|
|
|
"\n\n#{link[:markdown]}"
|
|
|
|
end.join
|
|
|
|
end
|
|
|
|
|
2020-01-14 09:08:19 +00:00
|
|
|
def upload_params
|
|
|
|
{
|
|
|
|
upload_parent: project,
|
|
|
|
uploader_class: FileUploader
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2016-10-12 17:07:36 +00:00
|
|
|
def validate_permission!(permission)
|
|
|
|
raise UserNotFoundError unless author
|
|
|
|
raise UserBlockedError if author.blocked?
|
2018-05-03 21:32:20 +00:00
|
|
|
|
|
|
|
if project
|
|
|
|
raise ProjectNotFound unless author.can?(:read_project, project)
|
|
|
|
end
|
|
|
|
|
2019-01-15 18:53:24 +00:00
|
|
|
raise UserNotAuthorizedError unless author.can?(permission, try(:noteable) || project)
|
2016-10-12 17:07:36 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def verify_record!(record:, invalid_exception:, record_name:)
|
|
|
|
return if record.persisted?
|
|
|
|
return if record.errors.key?(:commands_only)
|
|
|
|
|
|
|
|
error_title = "The #{record_name} could not be created for the following reasons:"
|
|
|
|
|
|
|
|
msg = error_title + record.errors.full_messages.map do |error|
|
|
|
|
"\n\n- #{error}"
|
|
|
|
end.join
|
|
|
|
|
|
|
|
raise invalid_exception, msg
|
|
|
|
end
|
2018-12-28 18:53:39 +00:00
|
|
|
|
|
|
|
def valid_project_slug?(found_project)
|
2021-05-26 18:10:52 +00:00
|
|
|
return false unless found_project
|
|
|
|
|
2018-12-28 18:53:39 +00:00
|
|
|
project_slug == found_project.full_path_slug
|
|
|
|
end
|
2020-07-14 15:09:05 +00:00
|
|
|
|
|
|
|
def strip_quick_actions(content)
|
|
|
|
return content unless author.support_bot?
|
|
|
|
|
2021-08-13 03:10:09 +00:00
|
|
|
quick_actions_extractor.redact_commands(content)
|
|
|
|
end
|
|
|
|
|
|
|
|
def quick_actions_extractor
|
2020-07-14 15:09:05 +00:00
|
|
|
command_definitions = ::QuickActions::InterpretService.command_definitions
|
2021-08-13 03:10:09 +00:00
|
|
|
::Gitlab::QuickActions::Extractor.new(command_definitions)
|
|
|
|
end
|
|
|
|
|
|
|
|
def append_reply(message, reply)
|
|
|
|
return message if message.blank? || reply.blank?
|
|
|
|
|
|
|
|
# Do not append if message only contains slash commands
|
|
|
|
body, _commands = quick_actions_extractor.extract_commands(message)
|
|
|
|
return message if body.empty?
|
2020-07-14 15:09:05 +00:00
|
|
|
|
2021-08-13 03:10:09 +00:00
|
|
|
message + "\n\n<details><summary>...</summary>\n\n#{reply}\n\n</details>"
|
2020-07-14 15:09:05 +00:00
|
|
|
end
|
2016-10-12 17:07:36 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-12-23 18:07:33 +00:00
|
|
|
|
2021-05-11 21:10:21 +00:00
|
|
|
Gitlab::Email::Handler::ReplyProcessing.prepend_mod_with('Gitlab::Email::Handler::ReplyProcessing')
|