BigW Consortium Gitlab

incoming_email.rb 891 Bytes
Newer Older
Douwe Maan committed
1
module Gitlab
2
  module IncomingEmail
Douwe Maan committed
3 4
    class << self
      def enabled?
5 6 7 8 9
        config.enabled && address_formatted_correctly?
      end

      def address_formatted_correctly?
        config.address &&
10
          config.address.include?("%{key}")
Douwe Maan committed
11 12
      end

13 14
      def reply_address(key)
        config.address.gsub('%{key}', key)
Douwe Maan committed
15 16
      end

17
      def key_from_address(address)
18 19
        regex = address_regex
        return unless regex
Douwe Maan committed
20

21
        match = address.match(regex)
Douwe Maan committed
22 23 24 25 26 27
        return unless match

        match[1]
      end

      def config
28
        Gitlab.config.incoming_email
Douwe Maan committed
29 30
      end

31 32
      private

Douwe Maan committed
33
      def address_regex
34 35 36 37
        wildcard_address = config.address
        return nil unless wildcard_address

        regex = Regexp.escape(wildcard_address)
38
        regex = regex.gsub(Regexp.escape('%{key}'), "(.+)")
39
        Regexp.new(regex).freeze
Douwe Maan committed
40 41 42 43
      end
    end
  end
end