Class: Carson::Config
- Inherits:
-
Object
- Object
- Carson::Config
- 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
-
#audit_advisory_check_names ⇒ Object
readonly
Returns the value of attribute audit_advisory_check_names.
-
#git_remote ⇒ Object
Returns the value of attribute git_remote.
-
#govern_agent_provider ⇒ Object
readonly
Returns the value of attribute govern_agent_provider.
-
#govern_auto_merge ⇒ Object
readonly
Returns the value of attribute govern_auto_merge.
-
#govern_check_wait ⇒ Object
readonly
Returns the value of attribute govern_check_wait.
-
#govern_dispatch_state_path ⇒ Object
readonly
Returns the value of attribute govern_dispatch_state_path.
-
#govern_merge_method ⇒ Object
readonly
Returns the value of attribute govern_merge_method.
-
#govern_repos ⇒ Object
readonly
Returns the value of attribute govern_repos.
-
#hooks_path ⇒ Object
readonly
Returns the value of attribute hooks_path.
-
#main_branch ⇒ Object
readonly
Returns the value of attribute main_branch.
-
#managed_hooks ⇒ Object
readonly
Returns the value of attribute managed_hooks.
-
#protected_branches ⇒ Object
readonly
Returns the value of attribute protected_branches.
-
#review_bot_usernames ⇒ Object
readonly
Returns the value of attribute review_bot_usernames.
-
#review_disposition ⇒ Object
readonly
Returns the value of attribute review_disposition.
-
#review_max_polls ⇒ Object
readonly
Returns the value of attribute review_max_polls.
-
#review_poll_seconds ⇒ Object
readonly
Returns the value of attribute review_poll_seconds.
-
#review_risk_keywords ⇒ Object
readonly
Returns the value of attribute review_risk_keywords.
-
#review_sweep_states ⇒ Object
readonly
Returns the value of attribute review_sweep_states.
-
#review_sweep_window_days ⇒ Object
readonly
Returns the value of attribute review_sweep_window_days.
-
#review_tracking_issue_label ⇒ Object
readonly
Returns the value of attribute review_tracking_issue_label.
-
#review_tracking_issue_title ⇒ Object
readonly
Returns the value of attribute review_tracking_issue_title.
-
#review_wait_seconds ⇒ Object
readonly
Returns the value of attribute review_wait_seconds.
-
#template_canonical ⇒ Object
readonly
Returns the value of attribute template_canonical.
-
#template_managed_files ⇒ Object
readonly
Returns the value of attribute template_managed_files.
-
#workflow_style ⇒ Object
readonly
Returns the value of attribute workflow_style.
Class Method Summary collapse
- .apply_env_overrides(data:) ⇒ Object
- .deep_dup_value(value:) ⇒ Object
- .deep_merge(base:, overlay:) ⇒ Object
- .default_data ⇒ Object
- .env_integer(key:, fallback:) ⇒ Object
- .env_string_array(key:) ⇒ Object
- .fetch_hash_section(data:, key:) ⇒ Object
- .global_config_path(repo_root:) ⇒ Object
- .load(repo_root:) ⇒ Object
- .load_global_config_data(repo_root:) ⇒ Object
Instance Method Summary collapse
-
#initialize(data:) ⇒ Config
constructor
A new instance of Config.
Constructor Details
#initialize(data:) ⇒ Config
Returns a new instance of Config.
183 184 185 186 187 188 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 |
# File 'lib/carson/config.rb', line 183 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 { |p| ( p ) } @govern_auto_merge = fetch_optional_boolean( hash: govern_hash, key: "auto_merge", default: true, key_path: "govern.auto_merge" ) @govern_merge_method = fetch_string( hash: govern_hash, key: "merge_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 = ( dispatch_path ) @govern_check_wait = fetch_non_negative_integer( hash: govern_hash, key: "check_wait" ) validate! end |
Instance Attribute Details
#audit_advisory_check_names ⇒ Object (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_remote ⇒ Object
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_provider ⇒ Object (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_auto_merge ⇒ Object (readonly)
Returns the value of attribute govern_auto_merge.
10 11 12 |
# File 'lib/carson/config.rb', line 10 def govern_auto_merge @govern_auto_merge end |
#govern_check_wait ⇒ Object (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_path ⇒ Object (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_method ⇒ Object (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_repos ⇒ Object (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_path ⇒ Object (readonly)
Returns the value of attribute hooks_path.
10 11 12 |
# File 'lib/carson/config.rb', line 10 def hooks_path @hooks_path end |
#main_branch ⇒ Object (readonly)
Returns the value of attribute main_branch.
10 11 12 |
# File 'lib/carson/config.rb', line 10 def main_branch @main_branch end |
#managed_hooks ⇒ Object (readonly)
Returns the value of attribute managed_hooks.
10 11 12 |
# File 'lib/carson/config.rb', line 10 def managed_hooks @managed_hooks end |
#protected_branches ⇒ Object (readonly)
Returns the value of attribute protected_branches.
10 11 12 |
# File 'lib/carson/config.rb', line 10 def protected_branches @protected_branches end |
#review_bot_usernames ⇒ Object (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 ⇒ Object (readonly)
Returns the value of attribute review_disposition.
10 11 12 |
# File 'lib/carson/config.rb', line 10 def review_disposition @review_disposition end |
#review_max_polls ⇒ Object (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_seconds ⇒ Object (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_keywords ⇒ Object (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_states ⇒ Object (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_days ⇒ Object (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_label ⇒ Object (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_title ⇒ Object (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_seconds ⇒ Object (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 |
#template_canonical ⇒ Object (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_files ⇒ Object (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 |
#workflow_style ⇒ Object (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
127 128 129 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 |
# File 'lib/carson/config.rb', line 127 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 govern[ "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
116 117 118 119 120 121 122 123 124 125 |
# File 'lib/carson/config.rb', line 116 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
102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/carson/config.rb', line 102 def self.deep_merge( base:, overlay: ) return deep_dup_value( value: base ) unless .is_a?( Hash ) base.each_with_object( {} ) { |( key, value ), copy| copy[ key ] = deep_dup_value( value: value ) }.tap do |merged| .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_data ⇒ Object
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 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/carson/config.rb', line 28 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
171 172 173 174 175 176 177 |
# File 'lib/carson/config.rb', line 171 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
179 180 181 |
# File 'lib/carson/config.rb', line 179 def self.env_string_array( key: ) ENV.fetch( key, "" ).split( "," ).map( &:strip ).reject( &:empty? ) end |
.fetch_hash_section(data:, key:) ⇒ Object
164 165 166 167 168 169 |
# File 'lib/carson/config.rb', line 164 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
92 93 94 95 96 97 98 99 100 |
# File 'lib/carson/config.rb', line 92 def self.global_config_path( repo_root: ) override = ENV.fetch( "CARSON_CONFIG_FILE", "" ).to_s.strip return File.( 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
21 22 23 24 25 26 |
# File 'lib/carson/config.rb', line 21 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
80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/carson/config.rb', line 80 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.})" end |