2016-05-18 18:19:25 -04:00
|
|
|
|
|
|
|
require 'gitlab/email/handler/create_note'
|
|
|
|
require 'gitlab/email/handler/create_issue'
|
|
|
|
|
2015-08-20 14:05:06 -04:00
|
|
|
# Inspired in great part by Discourse's Email::Receiver
|
|
|
|
module Gitlab
|
|
|
|
module Email
|
2016-05-18 18:19:25 -04:00
|
|
|
class ProcessingError < StandardError; end
|
|
|
|
class EmailUnparsableError < ProcessingError; end
|
|
|
|
class SentNotificationNotFoundError < ProcessingError; end
|
|
|
|
class ProjectNotFound < ProcessingError; end
|
|
|
|
class EmptyEmailError < ProcessingError; end
|
|
|
|
class AutoGeneratedEmailError < ProcessingError; end
|
|
|
|
class UserNotFoundError < ProcessingError; end
|
|
|
|
class UserBlockedError < ProcessingError; end
|
|
|
|
class UserNotAuthorizedError < ProcessingError; end
|
|
|
|
class NoteableNotFoundError < ProcessingError; end
|
|
|
|
class InvalidNoteError < ProcessingError; end
|
|
|
|
class InvalidIssueError < ProcessingError; end
|
|
|
|
|
2015-08-20 14:05:06 -04:00
|
|
|
class Receiver
|
2016-05-18 18:19:25 -04:00
|
|
|
attr_reader :mail
|
2015-08-20 14:05:06 -04:00
|
|
|
|
|
|
|
def initialize(raw)
|
2016-05-18 18:19:25 -04:00
|
|
|
raise EmptyEmailError if raw.blank?
|
|
|
|
@mail = build_mail(raw)
|
2015-08-20 14:05:06 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
2016-05-18 18:19:25 -04:00
|
|
|
mail_key = extract_mail_key
|
|
|
|
raise SentNotificationNotFoundError unless mail_key
|
2015-08-20 14:05:06 -04:00
|
|
|
|
2016-05-18 18:19:25 -04:00
|
|
|
if handler = find_handler(mail, mail_key)
|
|
|
|
handler.execute
|
|
|
|
elsif mail_key =~ %r{/|\+}
|
|
|
|
# Sent Notification mail_key would not have / or +
|
2016-04-06 17:27:28 -04:00
|
|
|
raise ProjectNotFound
|
2016-03-23 10:20:22 -04:00
|
|
|
else
|
|
|
|
raise SentNotificationNotFoundError
|
|
|
|
end
|
|
|
|
end
|
2015-08-20 14:05:06 -04:00
|
|
|
|
2016-05-18 18:19:25 -04:00
|
|
|
def build_mail(raw)
|
|
|
|
Mail::Message.new(raw)
|
|
|
|
rescue Encoding::UndefinedConversionError,
|
|
|
|
Encoding::InvalidByteSequenceError => e
|
2015-08-20 15:17:59 -04:00
|
|
|
raise EmailUnparsableError, e
|
|
|
|
end
|
|
|
|
|
2016-05-18 18:19:25 -04:00
|
|
|
def extract_mail_key
|
2016-03-17 15:03:51 -04:00
|
|
|
key_from_to_header || key_from_additional_headers
|
2016-03-01 00:29:20 -05:00
|
|
|
end
|
|
|
|
|
2016-03-17 15:03:51 -04:00
|
|
|
def key_from_to_header
|
2016-05-18 18:19:25 -04:00
|
|
|
mail.to.find do |address|
|
2016-03-23 08:05:31 -04:00
|
|
|
key = Gitlab::IncomingEmail.key_from_address(address)
|
2016-05-18 18:19:25 -04:00
|
|
|
break key if key
|
2015-08-20 14:05:06 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-03-17 15:03:51 -04:00
|
|
|
def key_from_additional_headers
|
2016-05-18 18:19:25 -04:00
|
|
|
Array(mail.references).find do |mail_id|
|
2016-05-20 19:21:58 -04:00
|
|
|
key = Gitlab::IncomingEmail.key_from_fallback_message_id(mail_id)
|
2016-05-18 18:19:25 -04:00
|
|
|
break key if key
|
2016-03-01 00:29:20 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-05-18 18:19:25 -04:00
|
|
|
def find_handler(mail, mail_key)
|
|
|
|
[Handler::CreateNote, Handler::CreateIssue].find do |klass|
|
|
|
|
handler = klass.new(mail, mail_key)
|
|
|
|
break handler if handler.can_handle?
|
2015-08-20 14:05:06 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|