Class: Carson::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/carson/config.rb

Overview

Config is built-in only for outsider mode; host repositories do not carry Carson config files.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data:) ⇒ Config

Returns a new instance of Config.



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/carson/config.rb', line 189

def initialize( data: )
	@git_remote = fetch_string( hash: fetch_hash( hash: data, key: "git" ), key: "remote" )
	@main_branch = fetch_string( hash: fetch_hash( hash: data, key: "git" ), key: "main_branch" )
	@protected_branches = fetch_string_array( hash: fetch_hash( hash: data, key: "git" ), key: "protected_branches" )

	@hooks_path = fetch_string( hash: fetch_hash( hash: data, key: "hooks" ), key: "path" )
	@managed_hooks = fetch_string_array( hash: fetch_hash( hash: data, key: "hooks" ), key: "managed" )

	@template_managed_files = fetch_string_array( hash: fetch_hash( hash: data, key: "template" ), key: "managed_files" )
	@template_canonical = fetch_optional_path( hash: fetch_hash( hash: data, key: "template" ), key: "canonical" )
	resolve_canonical_files!

	workflow_hash = fetch_hash( hash: data, key: "workflow" )
	@workflow_style = fetch_string( hash: workflow_hash, key: "style" ).downcase

	review_hash = fetch_hash( hash: data, key: "review" )
	@review_wait_seconds = fetch_non_negative_integer( hash: review_hash, key: "wait_seconds" )
	@review_poll_seconds = fetch_non_negative_integer( hash: review_hash, key: "poll_seconds" )
	@review_max_polls = fetch_positive_integer( hash: review_hash, key: "max_polls" )
	@review_disposition = fetch_string( hash: review_hash, key: "disposition" )
	@review_risk_keywords = fetch_string_array( hash: review_hash, key: "risk_keywords" )
	sweep_hash = fetch_hash( hash: review_hash, key: "sweep" )
	@review_sweep_window_days = fetch_positive_integer( hash: sweep_hash, key: "window_days" )
	@review_sweep_states = fetch_string_array( hash: sweep_hash, key: "states" ).map( &:downcase )
	tracking_issue_hash = fetch_hash( hash: review_hash, key: "tracking_issue" )
	@review_tracking_issue_title = fetch_string( hash: tracking_issue_hash, key: "title" )
	@review_tracking_issue_label = fetch_string( hash: tracking_issue_hash, key: "label" )
	@review_bot_usernames = fetch_optional_string_array( hash: review_hash, key: "bot_usernames" )
	audit_hash = fetch_hash( hash: data, key: "audit" )
	@audit_advisory_check_names = fetch_optional_string_array( hash: audit_hash, key: "advisory_check_names" )

	govern_hash = fetch_hash( hash: data, key: "govern" )
	@govern_repos = fetch_optional_string_array( hash: govern_hash, key: "repos" ).map { |path| safe_expand_path( path ) }
	@govern_auto_merge = fetch_optional_boolean( hash: govern_hash, key: "auto_merge", default: true, key_path: "govern.auto_merge" )
	govern_merge_hash = fetch_hash( hash: govern_hash, key: "merge" )
	@govern_merge_method = fetch_string( hash: govern_merge_hash, key: "method" ).downcase
	govern_agent_hash = fetch_hash( hash: govern_hash, key: "agent" )
	@govern_agent_provider = fetch_string( hash: govern_agent_hash, key: "provider" ).downcase
	dispatch_path = govern_hash.fetch( "dispatch_state_path" ).to_s
	@govern_dispatch_state_path = safe_expand_path( dispatch_path )
	@govern_check_wait = fetch_non_negative_integer( hash: govern_hash, key: "check_wait" )

	validate!
end

Instance Attribute Details

#audit_advisory_check_namesObject (readonly)

Returns the value of attribute audit_advisory_check_names.



11
12
13
# File 'lib/carson/config.rb', line 11

def audit_advisory_check_names
  @audit_advisory_check_names
end

#git_remoteObject

Returns the value of attribute git_remote.



10
11
12
# File 'lib/carson/config.rb', line 10

def git_remote
  @git_remote
end

#govern_agent_providerObject (readonly)

Returns the value of attribute govern_agent_provider.



11
12
13
# File 'lib/carson/config.rb', line 11

def govern_agent_provider
  @govern_agent_provider
end

#govern_auto_mergeObject (readonly)

Returns the value of attribute govern_auto_merge.



11
12
13
# File 'lib/carson/config.rb', line 11

def govern_auto_merge
  @govern_auto_merge
end

#govern_check_waitObject (readonly)

Returns the value of attribute govern_check_wait.



11
12
13
# File 'lib/carson/config.rb', line 11

def govern_check_wait
  @govern_check_wait
end

#govern_dispatch_state_pathObject (readonly)

Returns the value of attribute govern_dispatch_state_path.



11
12
13
# File 'lib/carson/config.rb', line 11

def govern_dispatch_state_path
  @govern_dispatch_state_path
end

#govern_merge_methodObject (readonly)

Returns the value of attribute govern_merge_method.



11
12
13
# File 'lib/carson/config.rb', line 11

def govern_merge_method
  @govern_merge_method
end

#govern_reposObject (readonly)

Returns the value of attribute govern_repos.



11
12
13
# File 'lib/carson/config.rb', line 11

def govern_repos
  @govern_repos
end

#hooks_pathObject (readonly)

Returns the value of attribute hooks_path.



11
12
13
# File 'lib/carson/config.rb', line 11

def hooks_path
  @hooks_path
end

#main_branchObject (readonly)

Returns the value of attribute main_branch.



11
12
13
# File 'lib/carson/config.rb', line 11

def main_branch
  @main_branch
end

#managed_hooksObject (readonly)

Returns the value of attribute managed_hooks.



11
12
13
# File 'lib/carson/config.rb', line 11

def managed_hooks
  @managed_hooks
end

#protected_branchesObject (readonly)

Returns the value of attribute protected_branches.



11
12
13
# File 'lib/carson/config.rb', line 11

def protected_branches
  @protected_branches
end

#review_bot_usernamesObject (readonly)

Returns the value of attribute review_bot_usernames.



11
12
13
# File 'lib/carson/config.rb', line 11

def review_bot_usernames
  @review_bot_usernames
end

#review_dispositionObject (readonly)

Returns the value of attribute review_disposition.



11
12
13
# File 'lib/carson/config.rb', line 11

def review_disposition
  @review_disposition
end

#review_max_pollsObject (readonly)

Returns the value of attribute review_max_polls.



11
12
13
# File 'lib/carson/config.rb', line 11

def review_max_polls
  @review_max_polls
end

#review_poll_secondsObject (readonly)

Returns the value of attribute review_poll_seconds.



11
12
13
# File 'lib/carson/config.rb', line 11

def review_poll_seconds
  @review_poll_seconds
end

#review_risk_keywordsObject (readonly)

Returns the value of attribute review_risk_keywords.



11
12
13
# File 'lib/carson/config.rb', line 11

def review_risk_keywords
  @review_risk_keywords
end

#review_sweep_statesObject (readonly)

Returns the value of attribute review_sweep_states.



11
12
13
# File 'lib/carson/config.rb', line 11

def review_sweep_states
  @review_sweep_states
end

#review_sweep_window_daysObject (readonly)

Returns the value of attribute review_sweep_window_days.



11
12
13
# File 'lib/carson/config.rb', line 11

def review_sweep_window_days
  @review_sweep_window_days
end

#review_tracking_issue_labelObject (readonly)

Returns the value of attribute review_tracking_issue_label.



11
12
13
# File 'lib/carson/config.rb', line 11

def review_tracking_issue_label
  @review_tracking_issue_label
end

#review_tracking_issue_titleObject (readonly)

Returns the value of attribute review_tracking_issue_title.



11
12
13
# File 'lib/carson/config.rb', line 11

def review_tracking_issue_title
  @review_tracking_issue_title
end

#review_wait_secondsObject (readonly)

Returns the value of attribute review_wait_seconds.



11
12
13
# File 'lib/carson/config.rb', line 11

def review_wait_seconds
  @review_wait_seconds
end

#template_canonicalObject (readonly)

Returns the value of attribute template_canonical.



11
12
13
# File 'lib/carson/config.rb', line 11

def template_canonical
  @template_canonical
end

#template_managed_filesObject (readonly)

Returns the value of attribute template_managed_files.



11
12
13
# File 'lib/carson/config.rb', line 11

def template_managed_files
  @template_managed_files
end

#workflow_styleObject (readonly)

Returns the value of attribute workflow_style.



11
12
13
# File 'lib/carson/config.rb', line 11

def workflow_style
  @workflow_style
end

Class Method Details

.apply_env_overrides(data:) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/carson/config.rb', line 130

def self.apply_env_overrides( data: )
	copy = deep_dup_value( value: data )
	hooks = fetch_hash_section( data: copy, key: "hooks" )
	hooks_path = ENV.fetch( "CARSON_HOOKS_PATH", "" ).to_s.strip
	hooks[ "path" ] = hooks_path unless hooks_path.empty?
	workflow = fetch_hash_section( data: copy, key: "workflow" )
	workflow_style = ENV.fetch( "CARSON_WORKFLOW_STYLE", "" ).to_s.strip
	workflow[ "style" ] = workflow_style unless workflow_style.empty?
	review = fetch_hash_section( data: copy, key: "review" )
	review[ "wait_seconds" ] = env_integer( key: "CARSON_REVIEW_WAIT_SECONDS", fallback: review.fetch( "wait_seconds" ) )
	review[ "poll_seconds" ] = env_integer( key: "CARSON_REVIEW_POLL_SECONDS", fallback: review.fetch( "poll_seconds" ) )
	review[ "max_polls" ] = env_integer( key: "CARSON_REVIEW_MAX_POLLS", fallback: review.fetch( "max_polls" ) )
	disposition = ENV.fetch( "CARSON_REVIEW_DISPOSITION", "" ).to_s.strip
	review[ "disposition" ] = disposition unless disposition.empty?
	sweep = fetch_hash_section( data: review, key: "sweep" )
	sweep[ "window_days" ] = env_integer( key: "CARSON_REVIEW_SWEEP_WINDOW_DAYS", fallback: sweep.fetch( "window_days" ) )
	states = env_string_array( key: "CARSON_REVIEW_SWEEP_STATES" )
	sweep[ "states" ] = states unless states.empty?
	bot_usernames = env_string_array( key: "CARSON_REVIEW_BOT_USERNAMES" )
	review[ "bot_usernames" ] = bot_usernames unless bot_usernames.empty?
	audit = fetch_hash_section( data: copy, key: "audit" )
	advisory_names = env_string_array( key: "CARSON_AUDIT_ADVISORY_CHECK_NAMES" )
	audit[ "advisory_check_names" ] = advisory_names unless advisory_names.empty?
	govern = fetch_hash_section( data: copy, key: "govern" )
	govern_repos = env_string_array( key: "CARSON_GOVERN_REPOS" )
	govern[ "repos" ] = govern_repos unless govern_repos.empty?
	govern_auto_merge = ENV.fetch( "CARSON_GOVERN_AUTO_MERGE", "" ).to_s.strip
	govern[ "auto_merge" ] = ( govern_auto_merge == "true" ) unless govern_auto_merge.empty?
	govern_method = ENV.fetch( "CARSON_GOVERN_MERGE_METHOD", "" ).to_s.strip
	unless govern_method.empty?
		govern[ "merge" ] ||= {}
		govern[ "merge" ][ "method" ] = govern_method
	end
	agent = fetch_hash_section( data: govern, key: "agent" )
	govern_provider = ENV.fetch( "CARSON_GOVERN_AGENT_PROVIDER", "" ).to_s.strip
	agent[ "provider" ] = govern_provider unless govern_provider.empty?
	govern[ "check_wait" ] = env_integer( key: "CARSON_GOVERN_CHECK_WAIT", fallback: govern.fetch( "check_wait" ) )
	copy
end

.deep_dup_value(value:) ⇒ Object



119
120
121
122
123
124
125
126
127
128
# File 'lib/carson/config.rb', line 119

def self.deep_dup_value( value: )
	case value
	when Hash
		value.each_with_object( {} ) { |( key, entry ), copy| copy[ key ] = deep_dup_value( value: entry ) }
	when Array
		value.map { |entry| deep_dup_value( value: entry ) }
	else
		value
	end
end

.deep_merge(base:, overlay:) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/carson/config.rb', line 105

def self.deep_merge( base:, overlay: )
	return deep_dup_value( value: base ) unless overlay.is_a?( Hash )

	base.each_with_object( {} ) { |( key, value ), copy| copy[ key ] = deep_dup_value( value: value ) }.tap do |merged|
		overlay.each do |key, value|
			if merged[ key ].is_a?( Hash ) && value.is_a?( Hash )
				merged[ key ] = deep_merge( base: merged[ key ], overlay: value )
			else
				merged[ key ] = deep_dup_value( value: value )
			end
		end
	end
end

.default_dataObject



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
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/carson/config.rb', line 29

def self.default_data
{
	"git" => {
		"remote" => "origin",
		"main_branch" => "main",
		"protected_branches" => [ "main", "master" ]
	},
	"hooks" => {
		"path" => "~/.carson/hooks",
		"managed" => [ "pre-commit", "prepare-commit-msg", "pre-merge-commit", "pre-push" ]
	},
	"template" => {
		"managed_files" => [ ".github/carson.md", ".github/copilot-instructions.md", ".github/CLAUDE.md", ".github/AGENTS.md", ".github/pull_request_template.md" ],
		"canonical" => nil
	},
	"workflow" => {
		"style" => "branch"
	},
	"review" => {
		"bot_usernames" => [ "gemini-code-assist[bot]", "github-actions[bot]", "dependabot[bot]" ],
		"wait_seconds" => 10,
		"poll_seconds" => 15,
		"max_polls" => 20,
		"disposition" => "Disposition:",
		"risk_keywords" => [ "bug", "security", "incorrect", "block", "fail", "regression" ],
		"sweep" => {
			"window_days" => 3,
			"states" => [ "open", "closed" ]
		},
		"tracking_issue" => {
			"title" => "Carson review sweep findings",
			"label" => "carson-review-sweep"
		}
	},
	"audit" => {
		"advisory_check_names" => [ "Scheduled review sweep", "Carson governance", "Tag, release, publish" ]
	},
	"govern" => {
		"repos" => [],
		"auto_merge" => true,
		"merge" => {
			"method" => "squash"
		},
		"agent" => {
			"provider" => "auto",
			"codex" => {},
			"claude" => {}
		},
		"dispatch_state_path" => "~/.carson/govern/dispatch_state.json",
		"check_wait" => 30
	}
}
end

.env_integer(key:, fallback:) ⇒ Object



177
178
179
180
181
182
183
# File 'lib/carson/config.rb', line 177

def self.env_integer( key:, fallback: )
	text = ENV.fetch( key, "" ).to_s.strip
	return fallback if text.empty?
	Integer( text )
rescue ArgumentError, TypeError
	fallback
end

.env_string_array(key:) ⇒ Object



185
186
187
# File 'lib/carson/config.rb', line 185

def self.env_string_array( key: )
	ENV.fetch( key, "" ).split( "," ).map( &:strip ).reject( &:empty? )
end

.fetch_hash_section(data:, key:) ⇒ Object

Raises:



170
171
172
173
174
175
# File 'lib/carson/config.rb', line 170

def self.fetch_hash_section( data:, key: )
	value = data[ key ]
	raise ConfigError, "missing config section #{key}" if value.nil?
	raise ConfigError, "config section #{key} must be an object" unless value.is_a?( Hash )
	value
end

.global_config_path(repo_root:) ⇒ Object



95
96
97
98
99
100
101
102
103
# File 'lib/carson/config.rb', line 95

def self.global_config_path( repo_root: )
	override = ENV.fetch( "CARSON_CONFIG_FILE", "" ).to_s.strip
	return File.expand_path( override ) unless override.empty?

	home = ENV.fetch( "HOME", "" ).to_s.strip
	return "" unless home.start_with?( "/" )

	File.join( home, ".carson", "config.json" )
end

.load(repo_root:) ⇒ Object



22
23
24
25
26
27
# File 'lib/carson/config.rb', line 22

def self.load( repo_root: )
	base_data = default_data
	merged_data = deep_merge( base: base_data, overlay: load_global_config_data( repo_root: repo_root ) )
	data = apply_env_overrides( data: merged_data )
	new( data: data )
end

.load_global_config_data(repo_root:) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
# File 'lib/carson/config.rb', line 83

def self.load_global_config_data( repo_root: )
	path = global_config_path( repo_root: repo_root )
	return {} if path.empty? || !File.file?( path )

	raw = File.read( path )
	parsed = JSON.parse( raw )
	raise ConfigError, "global config must be a JSON object at #{path}" unless parsed.is_a?( Hash )
	parsed
rescue JSON::ParserError => exception
	raise ConfigError, "invalid global config JSON at #{path} (#{exception.message})"
end