forked from prontolabs/pronto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig_file.rb
68 lines (61 loc) · 1.64 KB
/
config_file.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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