BigW Consortium Gitlab

mail_room.rb 1.38 KB
Newer Older
1 2
require 'yaml'
require 'json'
3
require_relative 'redis/queues' unless defined?(Gitlab::Redis::Queues)
4 5 6

module Gitlab
  module MailRoom
7 8 9 10 11 12 13 14 15
    DEFAULT_CONFIG = {
      enabled: false,
      port: 143,
      ssl: false,
      start_tls: false,
      mailbox: 'inbox',
      idle_timeout: 60
    }.freeze

16 17 18 19 20 21 22 23 24
    class << self
      def enabled?
        config[:enabled] && config[:address]
      end

      def config
        @config ||= fetch_config
      end

25 26
      def reset_config!
        @config = nil
27 28
      end

29 30 31
      private

      def fetch_config
32
        return {} unless File.exist?(config_file)
33

34 35 36 37
        config = YAML.load_file(config_file)[rails_env].deep_symbolize_keys[:incoming_email] || {}
        config = DEFAULT_CONFIG.merge(config) do |_key, oldval, newval|
          newval.nil? ? oldval : newval
        end
38 39

        if config[:enabled] && config[:address]
40 41
          gitlab_redis_queues = Gitlab::Redis::Queues.new(rails_env)
          config[:redis_url] = gitlab_redis_queues.url
42

43 44
          if gitlab_redis_queues.sentinels?
            config[:sentinels] = gitlab_redis_queues.sentinels
45
          end
46
        end
47 48

        config
49 50
      end

51 52 53 54
      def rails_env
        @rails_env ||= ENV['RAILS_ENV'] || ENV['RACK_ENV'] || 'development'
      end

55
      def config_file
56
        ENV['MAIL_ROOM_GITLAB_CONFIG_FILE'] || File.expand_path('../../../config/gitlab.yml', __FILE__)
57 58 59 60
      end
    end
  end
end