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.

Constant Summary collapse

CANONICAL_GITHUB_DIRECTORIES =
%w[actions workflows ISSUE_TEMPLATE DISCUSSION_TEMPLATE linters].freeze
CANONICAL_GITHUB_ROOT_FILES =
[
	".mega-linter.yml",
	"AGENTS.md",
	"CLAUDE.md",
	"CODEOWNERS",
	"FUNDING.yml",
	"carson.md",
	"copilot-instructions.md",
	"dependabot.yml",
	"funding.yml",
	"labeler.yml",
	"pull_request_template.md"
].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data:) ⇒ Config

Returns a new instance of Config.



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
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
# File 'lib/carson/config.rb', line 207

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 = resolve_runtime_path(
		path: fetch_string( hash: fetch_hash( hash: data, key: "hooks" ), key: "path" ),
		fallback_leaf: "hooks"
	)
	@managed_hooks = fetch_string_array( hash: fetch_hash( hash: data, key: "hooks" ), key: "managed" )

	template_hash = fetch_hash( hash: data, key: "template" )
	@template_managed_files = fetch_optional_string_array( hash: template_hash, key: "managed_files" )
	@lint_canonical = fetch_optional_path( hash: fetch_hash( hash: data, key: "lint" ), key: "canonical" )
	@lint_canonical ||= fetch_optional_path( hash: template_hash, key: "canonical" )
	@template_canonical = @lint_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_authority = fetch_string( hash: govern_hash, key: "authority" ).downcase
	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
	@govern_state_path = resolve_runtime_path(
		path: govern_hash.fetch( "state_path" ).to_s,
		fallback_leaf: "state.sqlite3"
	)
	@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.



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

def audit_advisory_check_names
  @audit_advisory_check_names
end

#git_remoteObject

Returns the value of attribute git_remote.



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

def git_remote
  @git_remote
end

#govern_agent_providerObject (readonly)

Returns the value of attribute govern_agent_provider.



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

def govern_agent_provider
  @govern_agent_provider
end

#govern_authorityObject (readonly)

Returns the value of attribute govern_authority.



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

def govern_authority
  @govern_authority
end

#govern_check_waitObject (readonly)

Returns the value of attribute govern_check_wait.



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

def govern_check_wait
  @govern_check_wait
end

#govern_merge_methodObject (readonly)

Returns the value of attribute govern_merge_method.



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

def govern_merge_method
  @govern_merge_method
end

#govern_reposObject (readonly)

Returns the value of attribute govern_repos.



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

def govern_repos
  @govern_repos
end

#govern_state_pathObject (readonly)

Returns the value of attribute govern_state_path.



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

def govern_state_path
  @govern_state_path
end

#hooks_pathObject (readonly)

Returns the value of attribute hooks_path.



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

def hooks_path
  @hooks_path
end

#lint_canonicalObject (readonly)

Returns the value of attribute lint_canonical.



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

def lint_canonical
  @lint_canonical
end

#main_branchObject (readonly)

Returns the value of attribute main_branch.



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

def main_branch
  @main_branch
end

#managed_hooksObject (readonly)

Returns the value of attribute managed_hooks.



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

def managed_hooks
  @managed_hooks
end

#protected_branchesObject (readonly)

Returns the value of attribute protected_branches.



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

def protected_branches
  @protected_branches
end

#review_bot_usernamesObject (readonly)

Returns the value of attribute review_bot_usernames.



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

def review_bot_usernames
  @review_bot_usernames
end

#review_dispositionObject (readonly)

Returns the value of attribute review_disposition.



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

def review_disposition
  @review_disposition
end

#review_max_pollsObject (readonly)

Returns the value of attribute review_max_polls.



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

def review_max_polls
  @review_max_polls
end

#review_poll_secondsObject (readonly)

Returns the value of attribute review_poll_seconds.



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

def review_poll_seconds
  @review_poll_seconds
end

#review_risk_keywordsObject (readonly)

Returns the value of attribute review_risk_keywords.



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

def review_risk_keywords
  @review_risk_keywords
end

#review_sweep_statesObject (readonly)

Returns the value of attribute review_sweep_states.



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

def review_sweep_states
  @review_sweep_states
end

#review_sweep_window_daysObject (readonly)

Returns the value of attribute review_sweep_window_days.



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

def review_sweep_window_days
  @review_sweep_window_days
end

#review_tracking_issue_labelObject (readonly)

Returns the value of attribute review_tracking_issue_label.



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

def review_tracking_issue_label
  @review_tracking_issue_label
end

#review_tracking_issue_titleObject (readonly)

Returns the value of attribute review_tracking_issue_title.



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

def review_tracking_issue_title
  @review_tracking_issue_title
end

#review_wait_secondsObject (readonly)

Returns the value of attribute review_wait_seconds.



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

def review_wait_seconds
  @review_wait_seconds
end

#template_canonicalObject (readonly)

Returns the value of attribute template_canonical.



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

def template_canonical
  @template_canonical
end

#template_managed_filesObject (readonly)

Returns the value of attribute template_managed_files.



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

def template_managed_files
  @template_managed_files
end

#workflow_styleObject (readonly)

Returns the value of attribute workflow_style.



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

def workflow_style
  @workflow_style
end

Class Method Details

.apply_env_overrides(data:) ⇒ Object



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
# File 'lib/carson/config.rb', line 148

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_authority = ENV.fetch( "CARSON_GOVERN_AUTHORITY", "" ).to_s.strip
	govern[ "authority" ] = govern_authority unless govern_authority.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



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

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



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

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



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
99
# File 'lib/carson/config.rb', line 44

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" => [],
		"canonical" => nil
	},
	"lint" => {
		"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" => [],
		"authority" => "remote",
		"merge" => {
			"method" => "squash"
		},
		"agent" => {
			"provider" => "auto",
			"codex" => {},
			"claude" => {}
		},
		"state_path" => "~/.carson/state.sqlite3",
		"check_wait" => 30
	}
}
end

.env_integer(key:, fallback:) ⇒ Object



195
196
197
198
199
200
201
# File 'lib/carson/config.rb', line 195

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



203
204
205
# File 'lib/carson/config.rb', line 203

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

.fetch_hash_section(data:, key:) ⇒ Object

Raises:



188
189
190
191
192
193
# File 'lib/carson/config.rb', line 188

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



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

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



37
38
39
40
41
42
# File 'lib/carson/config.rb', line 37

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



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

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