Class: ShopifyCLI::Changelog

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

Constant Summary collapse

CHANGELOG_FILE =
File.join(ShopifyCLI::ROOT, "CHANGELOG.md")
CHANGE_CATEGORIES =
%w(Added Changed Deprecated Removed Fixed Security)

Instance Method Summary collapse

Constructor Details

#initializeChangelog

Returns a new instance of Changelog.



10
11
12
# File 'lib/shopify_cli/changelog.rb', line 10

def initialize
  load(File.read(CHANGELOG_FILE))
end

Instance Method Details

#add_change(category, change) ⇒ Object



37
38
39
# File 'lib/shopify_cli/changelog.rb', line 37

def add_change(category, change)
  changes["Unreleased"][:changes][category] << change
end

#entry(pr_id:, desc:) ⇒ Object



41
42
43
# File 'lib/shopify_cli/changelog.rb', line 41

def entry(pr_id:, desc:)
  "* [##{pr_id}](https://github.com/Shopify/shopify-cli/pull/#{pr_id}): #{desc}"
end

#full_contentsObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/shopify_cli/changelog.rb', line 45

def full_contents
  sorted_changes = changes.each_key.sort_by do |change|
    if change == "Unreleased"
      [Float::INFINITY] * 3 # end of the list
    else
      major, minor, patch = change.split(".").map(&:to_i)
      [major, minor, patch]
    end
  end.reverse
  [
    heading,
    *sorted_changes.each.map { |version| release_notes_with_header(version) }.join,
    remainder,
  ].map { |section| section.chomp << "\n" }.join
end

#release_notes(version) ⇒ Object



28
29
30
31
32
33
34
35
# File 'lib/shopify_cli/changelog.rb', line 28

def release_notes(version)
  changes[version][:changes].map do |change_category, changes|
    <<~CHANGES
      ### #{change_category}
      #{changes.map { |change| entry(**change) }.join("\n")}
    CHANGES
  end.join("\n")
end

#save!Object



61
62
63
# File 'lib/shopify_cli/changelog.rb', line 61

def save!
  File.write(CHANGELOG_FILE, full_contents)
end

#update!Object



21
22
23
24
25
26
# File 'lib/shopify_cli/changelog.rb', line 21

def update!
  pr = pr_for_current_branch
  category = CLI::UI::Prompt.ask("What type of change?", options: CHANGE_CATEGORIES)
  add_change(category, { pr_id: pr.number, desc: pr.title })
  save!
end

#update_version!(new_version) ⇒ Object



14
15
16
17
18
19
# File 'lib/shopify_cli/changelog.rb', line 14

def update_version!(new_version)
  changes[new_version] = changes["Unreleased"]
  changes[new_version][:date] = Date.today.iso8601
  changes["Unreleased"] = { changes: [], date: nil }
  save!
end