Skip to content

Fix replicating template messages #794

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Apr 15, 2020
Merged
48 changes: 33 additions & 15 deletions lib/github_changelog_generator/generator/generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ class ChangelogGeneratorError < StandardError
# @see GitHubChangelogGenerator::Section
class Generator
attr_accessor :options, :filtered_tags, :tag_section_mapping, :sorted_tags
CREDIT_LINE = <<~CREDIT
\\* *This Changelog was automatically generated \
by [github_changelog_generator]\
(https://github.com/github-changelog-generator/github-changelog-generator)*
CREDIT

# A Generator responsible for all logic, related with changelog generation from ready-to-parse issues
#
Expand All @@ -46,22 +51,14 @@ def compound_changelog
fetch_and_filter_tags
fetch_issues_and_pr

log = ""
log += @options[:frontmatter] if @options[:frontmatter]
log += "#{options[:header]}\n\n"

log += if @options[:unreleased_only]
generate_entry_between_tags(@filtered_tags[0], nil)
else
generate_entries_for_all_tags
end

log = if @options[:unreleased_only]
generate_entry_between_tags(@filtered_tags[0], nil)
else
generate_entries_for_all_tags
end
log += File.read(@options[:base]) if File.file?(@options[:base])

credit_line = "\n\n\\* *This Changelog was automatically generated by [github_changelog_generator](https://github.com/github-changelog-generator/github-changelog-generator)*"
log.gsub!(/#{credit_line}(\n)?/, "") # Remove old credit lines
log += "#{credit_line}\n"

log = remove_old_fixed_string(log)
log = insert_fixed_string(log)
@log = log
end

Expand Down Expand Up @@ -151,5 +148,26 @@ def fetch_issues_and_pr
add_first_occurring_tag_to_prs(@sorted_tags, @pull_requests)
nil
end

# Remove the previously assigned fixed message.
# @param log [String] Old lines are fixed
def remove_old_fixed_string(log)
log.gsub!(/#{Regexp.escape(@options[:frontmatter])}/, "") if @options[:frontmatter]
log.gsub!(/#{Regexp.escape(@options[:header])}\n{,2}/, "")
log.gsub!(/\n{,2}#{Regexp.escape(CREDIT_LINE)}/, "") # Remove old credit lines
log
end

# Add template messages to given string. Previously added
# messages of the same wording are removed.
# @param log [String]
def insert_fixed_string(log)
ins = ""
ins += @options[:frontmatter] if @options[:frontmatter]
ins += "#{@options[:header]}\n\n"
log.insert(0, ins)
log += "\n\n#{CREDIT_LINE}"
log
end
end
end
47 changes: 47 additions & 0 deletions spec/unit/generator/generator_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# frozen_string_literal: true

require "github_changelog_generator/generator/generator"

RSpec.describe GitHubChangelogGenerator::Generator do
let(:header) { "# Changelog" }
let(:generator) { described_class.new({ header: header }) }
let(:content) do
<<~'BASE'
## [1.3.10](https://github.com/xxx/yyy/tree/1.3.10) (2015-03-18)

[Full Changelog](https://github.com/xxx/yyy/compare/1.3.9...1.3.10)

**Fixed bugs:**


BASE
end
let(:footer) do
<<~CREDIT
\\* *This Changelog was automatically generated \
by [github_changelog_generator]\
(https://github.com/github-changelog-generator/github-changelog-generator)*
CREDIT
end

context "when the given base file has previously appended template messages" do
describe "#remove_old_fixed_string" do
it "removes old template headers and footers" do
log = +"#{header}\n\n#{header}\n#{header}#{content}\n\n#{footer}\n#{footer}#{footer}"

expect(generator.send(:remove_old_fixed_string, log)).to eq content
end
end
end

context "when plain contents string was given" do
describe "#insert_fixed_string" do
it "append template messages at header and footer" do
log = String.new(content)
ans = "#{header}\n\n#{content}\n\n#{footer}"

expect(generator.send(:insert_fixed_string, log)).to eq ans
end
end
end
end