summaryrefslogtreecommitdiff
path: root/lib/gitlab/email/handler/create_note_handler.rb
blob: 2ae3bf23a74f42f2df9f6d13a0c133b8b2c0e1f6 (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

require 'gitlab/email/handler/base_handler'

module Gitlab
  module Email
    module Handler
      class CreateNoteHandler < BaseHandler
        def can_handle?
          # We want to raise SentNotificationNotFoundError for missing key
          !!(mail_key.nil? || mail_key =~ /\A\w+\z/)
        end

        def execute
          raise SentNotificationNotFoundError unless sent_notification
          raise AutoGeneratedEmailError if mail.header.to_s =~ /auto-(generated|replied)/

          validate_permission!(:create_note)

          raise NoteableNotFoundError unless sent_notification.noteable
          raise EmptyEmailError if message.blank?

          verify_record!(
            create_note,
            InvalidNoteError,
            "The comment could not be created for the following reasons:"
          )
        end

        def author
          sent_notification.recipient
        end

        def project
          sent_notification.project
        end

        def sent_notification
          @sent_notification ||= SentNotification.for(mail_key)
        end

        private
        def create_note
          Notes::CreateService.new(
            project,
            author,
            note:           message,
            noteable_type:  sent_notification.noteable_type,
            noteable_id:    sent_notification.noteable_id,
            commit_id:      sent_notification.commit_id,
            line_code:      sent_notification.line_code
          ).execute
        end
      end
    end
  end
end