2016-05-21 16:40:08 +00:00
|
|
|
module Gitlab
|
|
|
|
module Email
|
|
|
|
module Handler
|
|
|
|
class BaseHandler
|
|
|
|
attr_reader :mail, :mail_key
|
|
|
|
|
|
|
|
def initialize(mail, mail_key)
|
|
|
|
@mail = mail
|
|
|
|
@mail_key = mail_key
|
|
|
|
end
|
|
|
|
|
|
|
|
def message
|
|
|
|
@message ||= process_message
|
|
|
|
end
|
|
|
|
|
|
|
|
def author
|
|
|
|
raise NotImplementedError
|
|
|
|
end
|
|
|
|
|
|
|
|
def project
|
|
|
|
raise NotImplementedError
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
2016-06-15 08:18:31 +00:00
|
|
|
|
2016-05-21 16:40:08 +00:00
|
|
|
def validate_permission!(permission)
|
|
|
|
raise UserNotFoundError unless author
|
|
|
|
raise UserBlockedError if author.blocked?
|
|
|
|
raise ProjectNotFound unless author.can?(:read_project, project)
|
|
|
|
raise UserNotAuthorizedError unless author.can?(permission, project)
|
|
|
|
end
|
|
|
|
|
|
|
|
def process_message
|
2016-06-20 09:58:05 +00:00
|
|
|
message = ReplyParser.new(mail).execute.strip
|
|
|
|
add_attachments(message)
|
2016-05-21 16:40:08 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def add_attachments(reply)
|
|
|
|
attachments = Email::AttachmentUploader.new(mail).execute(project)
|
|
|
|
|
|
|
|
reply + attachments.map do |link|
|
|
|
|
"\n\n#{link[:markdown]}"
|
|
|
|
end.join
|
|
|
|
end
|
|
|
|
|
2016-06-20 11:11:42 +00:00
|
|
|
def verify_record!(record:, invalid_exception:, record_name:)
|
2016-05-21 16:40:08 +00:00
|
|
|
return if record.persisted?
|
2016-06-30 15:34:19 +00:00
|
|
|
return if record.errors.key?(:commands_only)
|
2016-05-21 16:40:08 +00:00
|
|
|
|
2016-06-20 11:11:42 +00:00
|
|
|
error_title = "The #{record_name} could not be created for the following reasons:"
|
2016-06-15 12:10:16 +00:00
|
|
|
|
2016-05-21 16:40:08 +00:00
|
|
|
msg = error_title + record.errors.full_messages.map do |error|
|
|
|
|
"\n\n- #{error}"
|
|
|
|
end.join
|
|
|
|
|
2016-06-20 11:11:42 +00:00
|
|
|
raise invalid_exception, msg
|
2016-05-21 16:40:08 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|