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.



210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'lib/carson/config.rb', line 210

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_base_path = fetch_string( hash: fetch_hash( hash: data, key: "hooks" ), key: "base_path" )
	@required_hooks = fetch_string_array( hash: fetch_hash( hash: data, key: "hooks" ), key: "required_hooks" )

	@path_groups = fetch_hash( hash: fetch_hash( hash: data, key: "scope" ), key: "path_groups" ).transform_values { |value| normalize_patterns( value: value ) }

	@template_managed_files = fetch_string_array( hash: fetch_hash( hash: data, key: "template" ), key: "managed_files" )
	@template_superseded_files = fetch_optional_string_array( hash: fetch_hash( hash: data, key: "template" ), key: "superseded_files" )
	@template_canonical = fetch_optional_path( hash: fetch_hash( hash: data, key: "template" ), key: "canonical" )
	resolve_canonical_files!
	lint_hash = fetch_hash( hash: data, key: "lint" )
	@lint_policy_source = lint_hash.fetch( "policy_source", "" ).to_s.strip

	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_prefix = fetch_string( hash: review_hash, key: "required_disposition_prefix" )
	@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" )
	style_hash = fetch_hash( hash: data, key: "style" )
	@ruby_indentation = fetch_string( hash: style_hash, key: "ruby_indentation" ).downcase

	govern_hash = fetch_hash( hash: data, key: "govern" )
	@govern_repos = fetch_optional_string_array( hash: govern_hash, key: "repos" ).map { |p| safe_expand_path( p ) }
	govern_merge_hash = fetch_hash( hash: govern_hash, key: "merge" )
	@govern_merge_authority = fetch_optional_boolean( hash: govern_merge_hash, key: "authority", default: true, key_path: "govern.merge.authority" )
	@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.



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

def audit_advisory_check_names
  @audit_advisory_check_names
end

#git_remoteObject

Returns the value of attribute git_remote.



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

def git_remote
  @git_remote
end

#govern_agent_providerObject (readonly)

Returns the value of attribute govern_agent_provider.



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

def govern_agent_provider
  @govern_agent_provider
end

#govern_check_waitObject (readonly)

Returns the value of attribute govern_check_wait.



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

def govern_check_wait
  @govern_check_wait
end

#govern_dispatch_state_pathObject (readonly)

Returns the value of attribute govern_dispatch_state_path.



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

def govern_dispatch_state_path
  @govern_dispatch_state_path
end

#govern_merge_authorityObject (readonly)

Returns the value of attribute govern_merge_authority.



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

def govern_merge_authority
  @govern_merge_authority
end

#govern_merge_methodObject (readonly)

Returns the value of attribute govern_merge_method.



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

def govern_merge_method
  @govern_merge_method
end

#govern_reposObject (readonly)

Returns the value of attribute govern_repos.



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

def govern_repos
  @govern_repos
end

#hooks_base_pathObject (readonly)

Returns the value of attribute hooks_base_path.



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

def hooks_base_path
  @hooks_base_path
end

#lint_policy_sourceObject (readonly)

Returns the value of attribute lint_policy_source.



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

def lint_policy_source
  @lint_policy_source
end

#main_branchObject (readonly)

Returns the value of attribute main_branch.



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

def main_branch
  @main_branch
end

#path_groupsObject (readonly)

Returns the value of attribute path_groups.



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

def path_groups
  @path_groups
end

#protected_branchesObject (readonly)

Returns the value of attribute protected_branches.



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

def protected_branches
  @protected_branches
end

#required_hooksObject (readonly)

Returns the value of attribute required_hooks.



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

def required_hooks
  @required_hooks
end

#review_bot_usernamesObject (readonly)

Returns the value of attribute review_bot_usernames.



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

def review_bot_usernames
  @review_bot_usernames
end

#review_disposition_prefixObject (readonly)

Returns the value of attribute review_disposition_prefix.



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

def review_disposition_prefix
  @review_disposition_prefix
end

#review_max_pollsObject (readonly)

Returns the value of attribute review_max_polls.



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

def review_max_polls
  @review_max_polls
end

#review_poll_secondsObject (readonly)

Returns the value of attribute review_poll_seconds.



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

def review_poll_seconds
  @review_poll_seconds
end

#review_risk_keywordsObject (readonly)

Returns the value of attribute review_risk_keywords.



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

def review_risk_keywords
  @review_risk_keywords
end

#review_sweep_statesObject (readonly)

Returns the value of attribute review_sweep_states.



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

def review_sweep_states
  @review_sweep_states
end

#review_sweep_window_daysObject (readonly)

Returns the value of attribute review_sweep_window_days.



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

def review_sweep_window_days
  @review_sweep_window_days
end

#review_tracking_issue_labelObject (readonly)

Returns the value of attribute review_tracking_issue_label.



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

def review_tracking_issue_label
  @review_tracking_issue_label
end

#review_tracking_issue_titleObject (readonly)

Returns the value of attribute review_tracking_issue_title.



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

def review_tracking_issue_title
  @review_tracking_issue_title
end

#review_wait_secondsObject (readonly)

Returns the value of attribute review_wait_seconds.



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

def review_wait_seconds
  @review_wait_seconds
end

#ruby_indentationObject (readonly)

Returns the value of attribute ruby_indentation.



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

def ruby_indentation
  @ruby_indentation
end

#template_canonicalObject (readonly)

Returns the value of attribute template_canonical.



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

def template_canonical
  @template_canonical
end

#template_managed_filesObject (readonly)

Returns the value of attribute template_managed_files.



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

def template_managed_files
  @template_managed_files
end

#template_superseded_filesObject (readonly)

Returns the value of attribute template_superseded_files.



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

def template_superseded_files
  @template_superseded_files
end

#workflow_styleObject (readonly)

Returns the value of attribute workflow_style.



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

def workflow_style
  @workflow_style
end

Class Method Details

.apply_env_overrides(data:) ⇒ Object



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/carson/config.rb', line 147

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_BASE_PATH", "" ).to_s.strip
	hooks[ "base_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_prefix = ENV.fetch( "CARSON_REVIEW_DISPOSITION_PREFIX", "" ).to_s.strip
	review[ "required_disposition_prefix" ] = disposition_prefix unless disposition_prefix.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?
	lint = fetch_hash_section( data: copy, key: "lint" )
	lint_policy_source_env = ENV.fetch( "CARSON_LINT_POLICY_SOURCE", "" ).to_s.strip
	lint[ "policy_source" ] = lint_policy_source_env unless lint_policy_source_env.empty?
	style = fetch_hash_section( data: copy, key: "style" )
	ruby_indentation = ENV.fetch( "CARSON_RUBY_INDENTATION", "" ).to_s.strip
	style[ "ruby_indentation" ] = ruby_indentation unless ruby_indentation.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?
	merge = fetch_hash_section( data: govern, key: "merge" )
	govern_authority = ENV.fetch( "CARSON_GOVERN_MERGE_AUTHORITY", "" ).to_s.strip
	merge[ "authority" ] = ( govern_authority == "true" ) unless govern_authority.empty?
	govern_method = ENV.fetch( "CARSON_GOVERN_MERGE_METHOD", "" ).to_s.strip
	merge[ "method" ] = govern_method unless govern_method.empty?
	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



136
137
138
139
140
141
142
143
144
145
# File 'lib/carson/config.rb', line 136

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



122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/carson/config.rb', line 122

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



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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/carson/config.rb', line 30

def self.default_data
{
	"git" => {
		"remote" => "origin",
		"main_branch" => "main",
		"protected_branches" => [ "main", "master" ]
	},
	"hooks" => {
		"base_path" => "~/.carson/hooks",
		"required_hooks" => [ "pre-commit", "prepare-commit-msg", "pre-merge-commit", "pre-push" ]
	},
	"scope" => {
		"path_groups" => {
			"tool" => [ "exe/**", "bin/**", "lib/**", "script/**", ".github/**", "templates/.github/**", "hooks/**", "install.sh", "README.md", "RELEASE.md", "VERSION", "carson.gemspec" ],
			"ui" => [ "app/views/**", "app/assets/**", "app/javascript/**", "docs/ui_*.md" ],
			"test" => [ "test/**", "spec/**", "features/**" ],
			"domain" => [ "app/**", "db/**", "config/**" ],
			"docs" => [ "docs/**", "*.md" ]
		}
	},
	"template" => {
		"managed_files" => [ ".github/carson.md", ".github/copilot-instructions.md", ".github/CLAUDE.md", ".github/AGENTS.md", ".github/pull_request_template.md" ],
		"superseded_files" => [ ".github/carson-instructions.md", ".github/workflows/carson-lint.yml", ".github/.mega-linter.yml" ],
		"canonical" => nil
	},
	"lint" => {
		"policy_source" => "wanghailei/lint.git"
	},
	"workflow" => {
		"style" => "branch"
	},
	"review" => {
		"bot_usernames" => [],
		"wait_seconds" => 10,
		"poll_seconds" => 15,
		"max_polls" => 20,
		"required_disposition_prefix" => "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" => [],
		"merge" => {
			"authority" => true,
			"method" => "squash"
		},
		"agent" => {
			"provider" => "auto",
			"codex" => {},
			"claude" => {}
		},
		"dispatch_state_path" => "~/.carson/govern/dispatch_state.json",
		"check_wait" => 30
	},
	"style" => {
		"ruby_indentation" => "tabs"
	}
	}
end

.env_integer(key:, fallback:) ⇒ Object



198
199
200
201
202
203
204
# File 'lib/carson/config.rb', line 198

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



206
207
208
# File 'lib/carson/config.rb', line 206

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

.fetch_hash_section(data:, key:) ⇒ Object

Raises:



191
192
193
194
195
196
# File 'lib/carson/config.rb', line 191

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



112
113
114
115
116
117
118
119
120
# File 'lib/carson/config.rb', line 112

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



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

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



100
101
102
103
104
105
106
107
108
109
110
# File 'lib/carson/config.rb', line 100

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 => e
	raise ConfigError, "invalid global config JSON at #{path} (#{e.message})"
end