Class: Spreen::Wiki::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/spreen/wiki/configuration.rb,
sig/generated/spreen/wiki/configuration.rbs

Overview

Resolves the runtime configuration for a wiki checkout. Every value is looked up with the same precedence: explicit keyword argument, then the .spreen.yml file under base_path (or config_path), then the ORGANISATION_NAME environment variable (organisation only), then the built-in defaults.

Constant Summary collapse

CONFIG_FILENAME =

Returns:

  • (::String)
'.spreen.yml'
EMPTY_HASH =

: Hash[String, untyped]

Returns:

  • (Hash[String, untyped])
{}.freeze
DEFAULT_TEMPLATE_DIR =

Returns:

  • (Object)
File.expand_path(File.join(__dir__.to_s, 'templates'))
DEFAULT_EXCLUDED_DIRS =

Returns:

  • (Object)
%w[spreen-wiki wikis-by-owner].freeze
DEFAULT_LABELS =

Built-in labels: how the first line of a wiki page is parsed (regexp), which namespace collects pages without a declaration (no_declaration), which namespaces the count report covers (unknown_namespaces) and which namespace the LLM export targets (llm_target_namespace). A config file can override any of these, add languages or add group_by criteria.

Returns:

  • (Object)
{
  'Owner' => {
    'regexp' => '[Oo]wner:\s?',
    'languages' => {
      'English' => {
        'no_declaration' => 'Unowned',
        'unknown_namespaces' => ['Unknown Owner nor Necessity', 'Unowned but Necessary', 'Unowned'],
        'llm_target_namespace' => 'Unknown Owner nor Necessity'
      },
      'Japanese' => {
        'no_declaration' => 'Owner記名なし',
        'unknown_namespaces' => [
          'Ownerチームが不明だが必要なページ群',
          'Ownerチーム・要or不要が不明なページ群',
          'Owner記名なし'
        ],
        'llm_target_namespace' => 'Ownerチーム・要or不要が不明なページ群'
      }
    }
  },
  'Category' => {
    'regexp' => '[Cc]ategory:\s?',
    'languages' => {
      'English' => {
        'no_declaration' => 'Uncategorised',
        'unknown_namespaces' => ['Uncategorised'],
        'llm_target_namespace' => 'Uncategorised'
      },
      'Japanese' => {
        'no_declaration' => 'Category記載なし',
        'unknown_namespaces' => ['Category記載なし'],
        'llm_target_namespace' => 'Category記載なし'
      }
    }
  }
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_path:, config_path: nil, organisation: nil, repository: nil, wiki_url: nil, owner_base_url: nil, excluded_dirs: nil, template_dir: nil) ⇒ Configuration

Returns a new instance of Configuration.

Parameters:

  • base_path: (String)
  • config_path: (String, nil) (defaults to: nil)
  • organisation: (String, nil) (defaults to: nil)
  • repository: (String, nil) (defaults to: nil)
  • wiki_url: (String, nil) (defaults to: nil)
  • owner_base_url: (String, nil) (defaults to: nil)
  • excluded_dirs: (Array[String], nil) (defaults to: nil)
  • template_dir: (String, nil) (defaults to: nil)


82
83
84
85
86
87
88
89
90
91
92
# File 'lib/spreen/wiki/configuration.rb', line 82

def initialize(base_path:, config_path: nil, organisation: nil, repository: nil, wiki_url: nil,
               owner_base_url: nil, excluded_dirs: nil, template_dir: nil)
  @file           = load_file(config_path || File.join(base_path, CONFIG_FILENAME))
  @organisation   = resolve_organisation(organisation)
  @repository     = resolve(repository, 'repository')
  @wiki_url       = resolve_wiki_url(wiki_url)
  @owner_base_url = resolve_owner_base_url(owner_base_url)
  @excluded_dirs  = excluded_dirs || file.fetch('exclude', DEFAULT_EXCLUDED_DIRS)
  @template_dir   = template_dir || file.fetch('template_dir', DEFAULT_TEMPLATE_DIR)
  @labels         = deep_merge(DEFAULT_LABELS, file.fetch('labels', EMPTY_HASH))
end

Instance Attribute Details

#excluded_dirsObject (readonly)

Returns the value of attribute excluded_dirs.

Returns:

  • (Object)


61
62
63
# File 'lib/spreen/wiki/configuration.rb', line 61

def excluded_dirs
  @excluded_dirs
end

#fileObject (readonly)

Returns the value of attribute file.

Returns:

  • (Object)


141
142
143
# File 'lib/spreen/wiki/configuration.rb', line 141

def file
  @file
end

#labelsObject (readonly)

Returns the value of attribute labels.

Returns:

  • (Object)


141
142
143
# File 'lib/spreen/wiki/configuration.rb', line 141

def labels
  @labels
end

#organisationObject (readonly)

Returns the value of attribute organisation.

Returns:

  • (Object)


61
62
63
# File 'lib/spreen/wiki/configuration.rb', line 61

def organisation
  @organisation
end

#owner_base_urlObject (readonly)

Returns the value of attribute owner_base_url.

Returns:

  • (Object)


61
62
63
# File 'lib/spreen/wiki/configuration.rb', line 61

def owner_base_url
  @owner_base_url
end

#repositoryObject (readonly)

Returns the value of attribute repository.

Returns:

  • (Object)


61
62
63
# File 'lib/spreen/wiki/configuration.rb', line 61

def repository
  @repository
end

#template_dirObject (readonly)

Returns the value of attribute template_dir.

Returns:

  • (Object)


61
62
63
# File 'lib/spreen/wiki/configuration.rb', line 61

def template_dir
  @template_dir
end

#wiki_urlObject (readonly)

Returns the value of attribute wiki_url.

Returns:

  • (Object)


61
62
63
# File 'lib/spreen/wiki/configuration.rb', line 61

def wiki_url
  @wiki_url
end

Instance Method Details

#deep_merge(base, override) ⇒ Hash[String, untyped]

Parameters:

  • base (Hash[String, untyped])
  • override (Hash[String, untyped])

Returns:

  • (Hash[String, untyped])


210
211
212
213
214
# File 'lib/spreen/wiki/configuration.rb', line 210

def deep_merge(base, override)
  base.merge(override) do |_key, old_value, new_value|
    old_value.is_a?(Hash) && new_value.is_a?(Hash) ? deep_merge(old_value, new_value) : new_value
  end
end

#default_owner_base_urlString?

Returns:

  • (String, nil)


203
204
205
# File 'lib/spreen/wiki/configuration.rb', line 203

def default_owner_base_url
  "https://github.com/orgs/#{organisation}/teams/" if organisation
end

#default_wiki_urlString?

Returns:

  • (String, nil)


198
199
200
# File 'lib/spreen/wiki/configuration.rb', line 198

def default_wiki_url
  "https://github.com/#{organisation}/#{repository}/wiki" if organisation && repository
end

#group_bysArray[String]

Returns:

  • (Array[String])


95
96
97
# File 'lib/spreen/wiki/configuration.rb', line 95

def group_bys
  labels.keys
end

#label_for(group_by) ⇒ Hash[String, untyped]

Parameters:

  • group_by (String)

Returns:

  • (Hash[String, untyped])


186
187
188
# File 'lib/spreen/wiki/configuration.rb', line 186

def label_for(group_by)
  labels.fetch(group_by, EMPTY_HASH)
end

#language_labels(group_by, language) ⇒ Hash[String, untyped]

Parameters:

  • group_by (String)
  • language (String)

Returns:

  • (Hash[String, untyped])


193
194
195
# File 'lib/spreen/wiki/configuration.rb', line 193

def language_labels(group_by, language)
  label_for(group_by).fetch('languages', EMPTY_HASH).fetch(language, EMPTY_HASH)
end

#languages(group_by) ⇒ Array[String]

Parameters:

  • group_by (String)

Returns:

  • (Array[String])


101
102
103
# File 'lib/spreen/wiki/configuration.rb', line 101

def languages(group_by)
  label_for(group_by).fetch('languages', EMPTY_HASH).keys
end

#llm_target_namespace(group_by, language) ⇒ String

Parameters:

  • group_by (String)
  • language (String)

Returns:

  • (String)


128
129
130
# File 'lib/spreen/wiki/configuration.rb', line 128

def llm_target_namespace(group_by, language)
  language_labels(group_by, language).fetch('llm_target_namespace', '')
end

#load_file(path) ⇒ Hash[String, untyped]

Parameters:

  • path (String)

Returns:

  • (Hash[String, untyped])


178
179
180
181
182
# File 'lib/spreen/wiki/configuration.rb', line 178

def load_file(path)
  return {} unless File.exist?(path)

  YAML.safe_load_file(path) || {}
end

#no_declaration(group_by, language) ⇒ String

Parameters:

  • group_by (String)
  • language (String)

Returns:

  • (String)


114
115
116
# File 'lib/spreen/wiki/configuration.rb', line 114

def no_declaration(group_by, language)
  language_labels(group_by, language).fetch('no_declaration', '')
end

#path_to_template(group_by, language) ⇒ String

Parameters:

  • group_by (String)
  • language (String)

Returns:

  • (String)


135
136
137
# File 'lib/spreen/wiki/configuration.rb', line 135

def path_to_template(group_by, language)
  File.join(template_dir, group_by.downcase, "#{language.downcase}.md")
end

#presence(value) ⇒ String?

Treats empty strings the same as nil so that callers can pass values straight from optional environment variables or CLI flags.

Parameters:

  • value (String, nil)

Returns:

  • (String, nil)


147
148
149
# File 'lib/spreen/wiki/configuration.rb', line 147

def presence(value)
  value unless value.nil? || value.empty?
end

#resolve(explicit, key) ⇒ String?

Parameters:

  • explicit (String, nil)
  • key (String)

Returns:

  • (String, nil)


154
155
156
# File 'lib/spreen/wiki/configuration.rb', line 154

def resolve(explicit, key)
  presence(explicit) || presence(file[key])
end

#resolve_organisation(explicit) ⇒ String?

Parameters:

  • explicit (String, nil)

Returns:

  • (String, nil)


160
161
162
# File 'lib/spreen/wiki/configuration.rb', line 160

def resolve_organisation(explicit)
  resolve(explicit, 'organisation') || presence(ENV.fetch('ORGANISATION_NAME', nil))
end

#resolve_owner_base_url(explicit) ⇒ String?

Parameters:

  • explicit (String, nil)

Returns:

  • (String, nil)


172
173
174
# File 'lib/spreen/wiki/configuration.rb', line 172

def resolve_owner_base_url(explicit)
  resolve(explicit, 'owner_base_url') || default_owner_base_url
end

#resolve_wiki_url(explicit) ⇒ String?

Parameters:

  • explicit (String, nil)

Returns:

  • (String, nil)


166
167
168
# File 'lib/spreen/wiki/configuration.rb', line 166

def resolve_wiki_url(explicit)
  resolve(explicit, 'wiki_url') || default_wiki_url
end

#target_regexp(group_by) ⇒ Regexp

Parameters:

  • group_by (String)

Returns:

  • (Regexp)


107
108
109
# File 'lib/spreen/wiki/configuration.rb', line 107

def target_regexp(group_by)
  Regexp.new(label_for(group_by).fetch('regexp', ''))
end

#unknown_namespaces(group_by, language) ⇒ Array[String]

Parameters:

  • group_by (String)
  • language (String)

Returns:

  • (Array[String])


121
122
123
# File 'lib/spreen/wiki/configuration.rb', line 121

def unknown_namespaces(group_by, language)
  language_labels(group_by, language).fetch('unknown_namespaces', [])
end