debian-mirror-gitlab/lib/gitlab/email/handler/reply_processing.rb

104 lines
2.9 KiB
Ruby
Raw Normal View History

2018-11-20 20:47:30 +05:30
# frozen_string_literal: true
2017-08-17 22:00:37 +05:30
module Gitlab
module Email
module Handler
module ReplyProcessing
private
2019-02-15 15:39:39 +05:30
attr_reader :project_id, :project_slug, :project_path, :incoming_email_token
2017-08-17 22:00:37 +05:30
def author
raise NotImplementedError
end
2019-02-15 15:39:39 +05:30
# rubocop:disable Gitlab/ModuleWithInstanceVariables
2017-08-17 22:00:37 +05:30
def project
2019-02-15 15:39:39 +05:30
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
2017-08-17 22:00:37 +05:30
end
2019-02-15 15:39:39 +05:30
# rubocop:enable Gitlab/ModuleWithInstanceVariables
2017-08-17 22:00:37 +05:30
def message
@message ||= process_message
end
2018-11-08 19:23:39 +05:30
def message_including_reply
@message_with_reply ||= process_message(trim_reply: false)
end
def process_message(**kwargs)
message = ReplyParser.new(mail, **kwargs).execute.strip
2020-07-28 23:09:34 +05:30
message_with_attachments = add_attachments(message)
# Support bot is specifically forbidden
# from using slash commands.
strip_quick_actions(message_with_attachments)
2017-08-17 22:00:37 +05:30
end
def add_attachments(reply)
2020-03-09 13:42:32 +05:30
attachments = Email::AttachmentUploader.new(mail).execute(upload_params)
2017-08-17 22:00:37 +05:30
reply + attachments.map do |link|
"\n\n#{link[:markdown]}"
end.join
end
2020-03-09 13:42:32 +05:30
def upload_params
{
upload_parent: project,
uploader_class: FileUploader
}
end
2017-08-17 22:00:37 +05:30
def validate_permission!(permission)
raise UserNotFoundError unless author
raise UserBlockedError if author.blocked?
2018-10-15 14:42:47 +05:30
if project
raise ProjectNotFound unless author.can?(:read_project, project)
end
2019-02-02 18:00:53 +05:30
raise UserNotAuthorizedError unless author.can?(permission, try(:noteable) || project)
2017-08-17 22:00:37 +05:30
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
2019-02-15 15:39:39 +05:30
def valid_project_slug?(found_project)
project_slug == found_project.full_path_slug
end
2020-07-28 23:09:34 +05:30
def strip_quick_actions(content)
return content unless author.support_bot?
command_definitions = ::QuickActions::InterpretService.command_definitions
extractor = ::Gitlab::QuickActions::Extractor.new(command_definitions)
extractor.redact_commands(content)
end
2017-08-17 22:00:37 +05:30
end
end
end
end
2020-03-09 13:42:32 +05:30
Gitlab::Email::Handler::ReplyProcessing.prepend_if_ee('::EE::Gitlab::Email::Handler::ReplyProcessing')