Class: CreateGithubRelease::Changelog

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

Overview

Generate a changelog for a new release

Given an existing changelog and a description of a new release, generate a new changelog that includes the new release.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(existing_changelog, next_release_description) ⇒ Changelog

Create a new changelog object

Examples:

existing_changelog = <<~EXISTING_CHANGELOG.chomp
  # Change Log

  List of changes in each release of this project.

  ## v0.1.0 (2022-10-31)

  * 07a1167 Release v0.1.0 (#1)
EXISTING_CHANGELOG

next_release_description = <<~next_release_DESCRIPTION
  ## v1.0.0 (2022-11-10)

  * f5e69d6 Release v1.0.0 (#4)
next_release_DESCRIPTION

changelog = CreateGithubRelease::Changelog.new(existing_changelog, next_release_description)

expected_new_changelog = <<~CHANGELOG
  # Change Log

  List of changes in each release of this project.

  ## v1.0.0 (2022-11-10)

  * f5e69d6 Release v1.0.0 (#4)

  ## v0.1.0 (2022-10-31)

  * 07a1167 Release v0.1.0 (#1)
CHANGELOG

changelog.front_matter # =>  "# Change Log\n\nList of changes in each release of this project."
changelog.body # => "## v0.1.0 (2022-10-31)\n\n* 07a1167 Release v0.1.0 (#1)"
changelog.next_release_description # => "## v1.0.0 (2022-11-10)\n\n..."
changelog.to_s == expected_new_changelog # => true

Parameters:

  • existing_changelog (String)

    Contents of the changelog as a string

  • next_release_description (String)

    The description of the next release to add to the changelog



55
56
57
58
59
60
# File 'lib/create_github_release/changelog.rb', line 55

def initialize(existing_changelog, next_release_description)
  @existing_changelog = existing_changelog
  @next_release_description = next_release_description

  @lines = existing_changelog.lines.map(&:chomp)
end

Instance Attribute Details

#existing_changelogString (readonly)

The changelog before the new release is added

Examples:

changelog.existing_changelog # => "# Change Log\n\n## v1.0.0...## v0.1.0...\n"

Returns:

  • (String)

    The changelog before the new release is added



181
182
183
# File 'lib/create_github_release/changelog.rb', line 181

def existing_changelog
  @existing_changelog
end

#next_release_descriptionString (readonly)

The description of the new release to add to the changelog

Examples:

changelog.next_release_description # => "# v1.0.0 - 2018-06-30\n\n[Full Changelog](...)..."

Returns:

  • (String)

    The description of the new release to add to the changelog



190
191
192
# File 'lib/create_github_release/changelog.rb', line 190

def next_release_description
  @next_release_description
end

Instance Method Details

#bodyString

The body of the existing changelog

Examples:

Changelog with front matter and a body

changelog_text = <<~CHANGELOG
  This is the front matter
  ## v0.1.0
  ...
CHANGELOG

next_release_description = '## v1.0.0\n\n* 8374b31 Add FizzBuzz'

changelog = CreateGithubRelease::Changelog.new(changelog_text, next_release_description)
changelog.body # => "## v0.1.0\n..."

Changelog without front matter

changelog_text = <<~CHANGELOG
  ## v0.1.0
  ...
CHANGELOG

next_release_description = '## v1.0.0\n\n* 8374b31 Add FizzBuzz'

changelog = CreateGithubRelease::Changelog.new(changelog_text, next_release_description)
changelog.body # => "## v0.1.0\n..."

Changelog without a body

changelog_text = <<~CHANGELOG
  This is the front matter
CHANGELOG

next_release_description = '## v1.0.0\n\n* 8374b31 Add FizzBuzz'

changelog = CreateGithubRelease::Changelog.new(changelog_text, next_release_description)
changelog.body # => ""

An empty changelog (new line only)

changelog_text = <<~CHANGELOG
CHANGELOG

next_release_description = '## v1.0.0\n\n* 8374b31 Add FizzBuzz'

changelog = CreateGithubRelease::Changelog.new(changelog_text, next_release_description)
changelog.body # => ""

An empty changelog (empty string)

changelog_text = ""

next_release_description = '## v1.0.0\n\n* 8374b31 Add FizzBuzz'

changelog = CreateGithubRelease::Changelog.new(changelog_text, next_release_description)
changelog.body # => ""

Returns:

  • (String)

    The body of the existing changelog



168
169
170
171
172
# File 'lib/create_github_release/changelog.rb', line 168

def body
  return '' if body_start == body_end

  lines[body_start..body_end - 1].join("\n")
end

#front_matterString

The front matter of the changelog

This is the part of the changelog up until the body. The body contains the list of releases and is the first line starting with '## '.

Examples:

Changelog with front matter

changelog_text = <<~CHANGELOG
  This is the front matter
  ## v0.1.0
  ...
CHANGELOG

next_release_description = '## v1.0.0\n\n* 8374b31 Add FizzBuzz'

changelog = CreateGithubRelease::Changelog.new(changelog_text, next_release_description)
changelog.front_matter # => "This is the front matter\n"

Changelog without front matter

changelog_text = <<~CHANGELOG
  ## v0.1.0
  ...
CHANGELOG

next_release_description = '## v1.0.0\n\n* 8374b31 Add FizzBuzz'

changelog = CreateGithubRelease::Changelog.new(changelog_text, next_release_description)
changelog.front_matter # => ""

An empty changelog

changelog_text = <<~CHANGELOG
CHANGELOG

next_release_description = '## v1.0.0\n\n* 8374b31 Add FizzBuzz'

changelog = CreateGithubRelease::Changelog.new(changelog_text, next_release_description)
changelog.front_matter # => ""

An empty changelog

changelog_text = ""

next_release_description = '## v1.0.0\n\n* 8374b31 Add FizzBuzz'

changelog = CreateGithubRelease::Changelog.new(changelog_text, next_release_description)
changelog.front_matter # => ""

Returns:

  • (String)

    The front matter of the changelog



108
109
110
111
112
# File 'lib/create_github_release/changelog.rb', line 108

def front_matter
  return '' if front_matter_start == front_matter_end

  lines[front_matter_start..front_matter_end - 1].join("\n")
end

#to_sString

The changelog with the new release

Examples:

Changelog with front matter and a body

changelog_text = <<~CHANGELOG
  This is the front matter
  ## v0.1.0
  ...
CHANGELOG

next_release_description = '## v1.0.0\n\n* 8374b31 Add FizzBuzz'

expected_changelog = <<~CHANGELOG
  This is the front matter

  ## v1.0.0 (2022-11-08)
  ...release description...

  ## v0.1.0
  ...
CHANGELOG

changelog = CreateGithubRelease::Changelog.new(changelog_text, next_release_description)

changelog.to_s == expected_changelog # => true

Changelog without front matter

changelog_text = <<~CHANGELOG
  ## v0.1.0
  ...
CHANGELOG

next_release_description = '## v1.0.0\n\n* 8374b31 Add FizzBuzz'

expected_changelog = <<~CHANGELOG
  ## v1.0.0 (2022-11-08)
  ...release description...

  ## v0.1.0
  ...
CHANGELOG

changelog = CreateGithubRelease::Changelog.new(changelog_text, next_release_description)

changelog.to_s == expected_changelog # => true

Changelog without a body

changelog_text = <<~CHANGELOG
  This is the front matter
CHANGELOG

next_release_description = '## v1.0.0\n\n* 8374b31 Add FizzBuzz'

expected_changelog = <<~CHANGELOG
  This is the front matter

  ## v1.0.0 (2022-11-08)
  ...release description...
CHANGELOG

changelog = CreateGithubRelease::Changelog.new(changelog_text, next_release_description)

changelog.to_s == expected_changelog # => true

A new release without a description

changelog_text = <<~CHANGELOG
  This is the front matter
  ## v0.1.0
  ...
CHANGELOG

next_release_description = '## v1.0.0\n\n* 8374b31 Add FizzBuzz'

expected_changelog = <<~CHANGELOG
  This is the front matter

  ## v1.0.0 (2022-11-08)

  ## v0.1.0
  ...
CHANGELOG

changelog = CreateGithubRelease::Changelog.new(changelog_text, next_release_description)

changelog.to_s == expected_changelog # => true

Returns:

  • (String)

    The changelog with the new release details



279
280
281
# File 'lib/create_github_release/changelog.rb', line 279

def to_s
  formatted_front_matter + next_release_description + formatted_body
end