Class: Kettle::Changelog::KeyedEntryUpserter

Inherits:
Object
  • Object
show all
Defined in:
lib/kettle/changelog/keyed_entry_upserter.rb

Constant Summary collapse

SECTIONS =
%w[Added Changed Deprecated Removed Fixed Security].freeze
KEY_PREFIX =
"[kc]"

Instance Method Summary collapse

Constructor Details

#initialize(section:, key:, entry:, legacy_prefixes: [], root: Kettle::Dev::CIHelpers.project_root) ⇒ KeyedEntryUpserter

Returns a new instance of KeyedEntryUpserter.



9
10
11
12
13
14
15
16
# File 'lib/kettle/changelog/keyed_entry_upserter.rb', line 9

def initialize(section:, key:, entry:, legacy_prefixes: [], root: Kettle::Dev::CIHelpers.project_root)
  @root = root
  @section = section.to_s
  @key = key.to_s.strip
  @entry = entry
  @legacy_prefixes = Array(legacy_prefixes).map { |prefix| prefix.to_s.strip }.reject(&:empty?)
  @changelog_path = ENV.fetch("K_CHANGELOG_PATH", File.join(@root, "CHANGELOG.md"))
end

Instance Method Details

#runObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/kettle/changelog/keyed_entry_upserter.rb', line 18

def run
  validate!
  source = read_source!
  matches = find_matches(source)
  if matches.count { |match| match.fetch(:kind) == :keyed } > 1
    raise Error, "expected at most one #{key_label} entry in CHANGELOG.md"
  end
  if matches.any? { |match| match.fetch(:kind) == :keyed } && matches.any? { |match| match.fetch(:kind) == :legacy }
    raise Error, "found both keyed and legacy entries for #{key_label} in CHANGELOG.md"
  end

  body = @entry.respond_to?(:call) ? @entry.call(matches) : @entry
  rendered = render_entry(body)
  changed = matches.empty? || matches.any? { |match| normalize_source(match.fetch(:source)) != normalize_source(rendered) } || matches.length > 1
  write_updated_source(source, rendered, matches) if changed

  matched = if matches.empty?
    "inserted"
  elsif matches.first.fetch(:kind) == :legacy
    "legacy"
  else
    "keyed"
  end

  {
    changed: changed,
    key: @key,
    section: @section,
    entry: rendered,
    matched: matched
  }
end