Class: MailReceiver::Receiver
- Inherits:
-
Object
- Object
- MailReceiver::Receiver
- Includes:
- BodyParser
- Defined in:
- lib/mail-receiver/receiver.rb
Instance Method Summary collapse
- #body ⇒ Object
- #current_user ⇒ Object
- #from ⇒ Object
-
#hash_data ⇒ Object
foo+p=chair/chair&id=123 => { p: chair/chair, id: 123 }.
-
#initialize(message, opts = {}) ⇒ Receiver
constructor
A new instance of Receiver.
- #inspect ⇒ Object
- #issue_id ⇒ Object
- #logger ⇒ Object
- #mail ⇒ Object
- #merge_request? ⇒ Boolean
-
#prefix ⇒ Object
[email protected] => foo.
- #process! ⇒ Object
- #process_issue! ⇒ Object
- #process_mr! ⇒ Object
- #project ⇒ Object
- #project_slug ⇒ Object
- #target_id ⇒ Object
- #to ⇒ Object
Methods included from BodyParser
Constructor Details
#initialize(message, opts = {}) ⇒ Receiver
Returns a new instance of Receiver.
5 6 7 8 |
# File 'lib/mail-receiver/receiver.rb', line 5 def initialize(, opts = {}) @message = @logger = opts[:logger] end |
Instance Method Details
#body ⇒ Object
30 31 32 33 34 |
# File 'lib/mail-receiver/receiver.rb', line 30 def body return @body if defined?(@body) @body = self.extract @body end |
#current_user ⇒ Object
121 122 123 |
# File 'lib/mail-receiver/receiver.rb', line 121 def current_user @current_user ||= User.find_by_any_email(from) end |
#from ⇒ Object
40 41 42 |
# File 'lib/mail-receiver/receiver.rb', line 40 def from @from ||= @message.from.is_a?(Array) ? @message.from.first : @message.from end |
#hash_data ⇒ Object
foo+p=chair/chair&id=123 => { p: chair/chair, id: 123 }
50 51 52 53 54 55 |
# File 'lib/mail-receiver/receiver.rb', line 50 def hash_data return @hash_data if defined?(@hash_data) return {} if not prefix.include?('+') @hash_data = Encoder.decode(prefix.split('+').last) return @hash_data end |
#inspect ⇒ Object
57 58 59 |
# File 'lib/mail-receiver/receiver.rb', line 57 def inspect { project_slug: project_slug, issue_id: issue_id, target_id: target_id, merge_request: merge_request?, to: to, body: body} end |
#issue_id ⇒ Object
18 19 20 |
# File 'lib/mail-receiver/receiver.rb', line 18 def issue_id hash_data[:id] end |
#logger ⇒ Object
125 126 127 |
# File 'lib/mail-receiver/receiver.rb', line 125 def logger @logger ||= Logger.new($stdout) end |
#mail ⇒ Object
10 11 12 |
# File 'lib/mail-receiver/receiver.rb', line 10 def mail @message end |
#merge_request? ⇒ Boolean
26 27 28 |
# File 'lib/mail-receiver/receiver.rb', line 26 def merge_request? @merge_request ||= (hash_data[:t] || 'i').downcase == 'm' end |
#prefix ⇒ Object
[email protected] => foo
45 46 47 |
# File 'lib/mail-receiver/receiver.rb', line 45 def prefix @prefix ||= to.split('@').first end |
#process! ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/mail-receiver/receiver.rb', line 68 def process! if current_user.blank? logger.warn "Reply user: #{from} not found user in GitLab." return end if project.blank? logger.warn "Project #{project_slug} is not found." return end note_params = merge_request? ? process_mr! : process_issue! return if note_params.blank? note_params[:project_id] = project.id # relation to target Note if target_id target_note = project.notes.find_by_id(target_id) if target_note note_params[:commit_id] = target_note.commit_id note_params[:line_code] = target_note.line_code end end note_params[:note] = body @note = Notes::CreateService.new(project, current_user, note_params).execute logger.info "Note #{@note.id} created." end |
#process_issue! ⇒ Object
110 111 112 113 114 115 116 117 118 119 |
# File 'lib/mail-receiver/receiver.rb', line 110 def process_issue! @issue = project.issues.find_by(iid: self.issue_id) if @issue.blank? logger.warn "Issue #{self.issue_id} not found." return nil end logger.info "Found issue: #{@issue.id}" { noteable_type: 'Issue', noteable_id: @issue.id } end |
#process_mr! ⇒ Object
99 100 101 102 103 104 105 106 107 108 |
# File 'lib/mail-receiver/receiver.rb', line 99 def process_mr! @mr = project.merge_requests.find_by(iid: self.issue_id) if @mr.blank? logger.warn "MergeRequest #{self.issue_id} not found." return nil end logger.info "Found MergeRequest: #{@mr.id}" { noteable_type: 'MergeRequest', noteable_id: @mr.id } end |
#project ⇒ Object
61 62 63 64 65 66 |
# File 'lib/mail-receiver/receiver.rb', line 61 def project @project ||= Project.find_with_namespace(project_slug) rescue => e logger.warn "Project: #{project_slug} record not found." nil end |
#project_slug ⇒ Object
14 15 16 |
# File 'lib/mail-receiver/receiver.rb', line 14 def project_slug hash_data[:p] end |
#target_id ⇒ Object
22 23 24 |
# File 'lib/mail-receiver/receiver.rb', line 22 def target_id hash_data[:n] end |
#to ⇒ Object
36 37 38 |
# File 'lib/mail-receiver/receiver.rb', line 36 def to @to ||= @message.to.is_a?(Array) ? @message.to.first : @message.to end |