BigW Consortium Gitlab

base_handler.rb 1.53 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
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
Lin Jen-Shin committed
25

26 27 28 29 30 31 32 33
        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
34 35
          message = ReplyParser.new(mail).execute.strip
          add_attachments(message)
36 37 38 39 40 41 42 43 44 45
        end

        def add_attachments(reply)
          attachments = Email::AttachmentUploader.new(mail).execute(project)

          reply + attachments.map do |link|
            "\n\n#{link[:markdown]}"
          end.join
        end

46
        def verify_record!(record:, invalid_exception:, record_name:)
47
          return if record.persisted?
48
          return if record.errors.key?(:commands_only)
49

50
          error_title = "The #{record_name} could not be created for the following reasons:"
Lin Jen-Shin committed
51

52 53 54 55
          msg = error_title + record.errors.full_messages.map do |error|
            "\n\n- #{error}"
          end.join

56
          raise invalid_exception, msg
57 58 59 60 61
        end
      end
    end
  end
end