blob: da4299ebcb3e6d7b089184fb991f34a4c5a8dfe7 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
|
require 'gitlab/email/handler/create_note'
require 'gitlab/email/handler/create_issue'
# Inspired in great part by Discourse's Email::Receiver
module Gitlab
module Email
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
class Receiver
def initialize(raw)
@raw = raw
end
def execute
raise EmptyEmailError if @raw.blank?
mail = build_mail
mail_key = extract_mail_key(mail)
raise SentNotificationNotFoundError unless mail_key
if handler = find_handler(mail, mail_key)
handler.execute
elsif mail_key =~ %r{/|\+}
# Sent Notification mail_key would not have / or +
raise ProjectNotFound
else
raise SentNotificationNotFoundError
end
end
def build_mail
Mail::Message.new(@raw)
rescue Encoding::UndefinedConversionError,
Encoding::InvalidByteSequenceError => e
raise EmailUnparsableError, e
end
def extract_mail_key(mail)
key_from_to_header(mail) || key_from_additional_headers(mail)
end
def key_from_to_header(mail)
mail.to.find do |address|
key = Gitlab::IncomingEmail.key_from_address(address)
break key if key
end
end
def key_from_additional_headers(mail)
Array(mail.references).find do |mail_id|
key = Gitlab::IncomingEmail.key_from_fallback_message_id(mail_id)
break key if key
end
end
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?
end
end
end
end
end
|