BigW Consortium Gitlab

incoming_email.rb 1.23 KB
Newer Older
Douwe Maan committed
1
module Gitlab
2
  module IncomingEmail
3 4
    WILDCARD_PLACEHOLDER = '%{key}'.freeze

Douwe Maan committed
5
    class << self
6
      FALLBACK_MESSAGE_ID_REGEX = /\Areply\-(.+)@#{Gitlab.config.gitlab.host}\Z/.freeze
7

8 9
      def enabled?
        config.enabled && config.address
Douwe Maan committed
10 11
      end

12 13 14 15 16 17 18 19
      def supports_wildcard?
        config.address && config.address.include?(WILDCARD_PLACEHOLDER)
      end

      def supports_issue_creation?
        enabled? && supports_wildcard?
      end

20
      def reply_address(key)
21
        config.address.gsub(WILDCARD_PLACEHOLDER, key)
Douwe Maan committed
22 23
      end

24
      def key_from_address(address)
25 26
        regex = address_regex
        return unless regex
Douwe Maan committed
27

28
        match = address.match(regex)
Douwe Maan committed
29 30 31 32 33
        return unless match

        match[1]
      end

34 35
      def key_from_fallback_message_id(mail_id)
        match = mail_id.match(FALLBACK_MESSAGE_ID_REGEX)
36 37 38 39 40
        return unless match

        match[1]
      end

Douwe Maan committed
41
      def config
42
        Gitlab.config.incoming_email
Douwe Maan committed
43 44
      end

45 46
      private

Douwe Maan committed
47
      def address_regex
48 49 50 51
        wildcard_address = config.address
        return nil unless wildcard_address

        regex = Regexp.escape(wildcard_address)
52
        regex = regex.gsub(Regexp.escape('%{key}'), "(.+)")
53
        Regexp.new(regex).freeze
Douwe Maan committed
54 55 56 57
      end
    end
  end
end