Sha256: f8b0a5e26cd566f082703b09210816d56dd3dd5855d30623f918d6777f75fdd1

Contents?: true

Size: 1.64 KB

Versions: 3

Compression:

Stored size: 1.64 KB

Contents

module Pronto
  class ConfigFile
    DEFAULT_MESSAGE_FORMAT = '%{msg}'.freeze
    DEFAULT_WARNINGS_PER_REVIEW = 30

    EMPTY = {
      'all' => {
        'exclude' => [],
        'include' => []
      },
      'github' => {
        'slug' => nil,
        'access_token' => nil,
        'api_endpoint' => 'https://api.github.com/',
        'web_endpoint' => 'https://github.com/',
        'review_type' => 'request_changes'
      },
      'gitlab' => {
        'slug' => nil,
        'api_private_token' => nil,
        'api_endpoint' => 'https://gitlab.com/api/v4'
      },
      'bitbucket' => {
        'slug' => nil,
        'username' => nil,
        'password' => nil,
        'api_endpoint' => nil,
        'auto_approve' => false,
        'web_endpoint' => 'https://bitbucket.org/'
      },
      'text' => {
        'format' => '%{color_location} %{color_level}: %{msg}'
      },
      'default_commit' => 'master',
      'runners' => [],
      'formatters' => [],
      'max_warnings' => nil,
      'warnings_per_review' => DEFAULT_WARNINGS_PER_REVIEW,
      'verbose' => false,
      'format' => DEFAULT_MESSAGE_FORMAT
    }.freeze

    attr_reader :path

    def initialize(path = ENV.fetch('PRONTO_CONFIG_FILE', '.pronto.yml'))
      @path = path
    end

    def to_h
      hash = File.exist?(@path) ? YAML.load_file(@path) : {}
      deep_merge(hash)
    end

    private

    def deep_merge(hash)
      merger = proc do |_, oldval, newval|
        if oldval.is_a?(Hash) && newval.is_a?(Hash)
          oldval.merge(newval, &merger)
        else
          oldval.nil? ? newval : oldval
        end
      end

      hash.merge(EMPTY, &merger)
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
pronto-0.11.3 lib/pronto/config_file.rb
pronto-0.11.2 lib/pronto/config_file.rb
pronto-0.11.1 lib/pronto/config_file.rb