Skip to content

add breaking-changes section to changelog #530

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 6 commits into from
Oct 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 49 additions & 26 deletions lib/github_changelog_generator/generator/generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,13 @@ def create_log_for_tag(pull_requests, issues, newer_tag, older_tag = nil)
# @param [Array] pull_requests
# @return [String] generated log for issues
def issues_to_log(issues, pull_requests)
log = ""
bugs_a, enhancement_a, issues_a = parse_by_sections(issues, pull_requests)
sections = parse_by_sections(issues, pull_requests)

log += generate_sub_section(enhancement_a, options[:enhancement_prefix])
log += generate_sub_section(bugs_a, options[:bug_prefix])
log += generate_sub_section(issues_a, options[:issue_prefix])
log = ""
log += generate_sub_section(sections[:breaking], options[:breaking_prefix])
log += generate_sub_section(sections[:enhancement], options[:enhancement_prefix])
log += generate_sub_section(sections[:bugs], options[:bug_prefix])
log += generate_sub_section(sections[:issues], options[:issue_prefix])
log
end

Expand All @@ -109,47 +110,69 @@ def issues_to_log(issues, pull_requests)
#
# @param [Array] issues
# @param [Array] pull_requests
# @return [Array] tuple of filtered arrays: (Bugs, Enhancements Issues)
# @return [Hash] Mapping of filtered arrays: (Bugs, Enhancements, Breaking stuff, Issues)
def parse_by_sections(issues, pull_requests)
issues_a = []
enhancement_a = []
bugs_a = []
sections = {
issues: [],
enhancements: [],
bugs: [],
breaking: []
}

issues.each do |dict|
added = false

dict["labels"].each do |label|
if options[:bug_labels].include?(label["name"])
bugs_a.push(dict)
sections[:bugs] << dict
added = true
next
end
if options[:enhancement_labels].include?(label["name"])
enhancement_a.push(dict)
elsif options[:enhancement_labels].include?(label["name"])
sections[:enhancements] << dict
added = true
elsif options[:breaking_labels].include?(label["name"])
sections[:breaking] << dict
added = true
next
end

break if added
end
issues_a.push(dict) unless added

sections[:issues] << dict unless added
end

sort_pull_requests(pull_requests, sections)
end

# This method iterates through PRs and sorts them into sections
#
# @param [Array] pull_requests
# @param [Hash] sections
# @return [Hash] sections
def sort_pull_requests(pull_requests, sections)
added_pull_requests = []
pull_requests.each do |pr|
added = false

pr["labels"].each do |label|
if options[:bug_labels].include?(label["name"])
bugs_a.push(pr)
added_pull_requests.push(pr)
next
end
if options[:enhancement_labels].include?(label["name"])
enhancement_a.push(pr)
added_pull_requests.push(pr)
next
sections[:bugs] << pr
added_pull_requests << pr
added = true
elsif options[:enhancement_labels].include?(label["name"])
sections[:enhancements] << pr
added_pull_requests << pr
added = true
elsif options[:breaking_labels].include?(label["name"])
sections[:breaking] << pr
added_pull_requests << pr
added = true
end

break if added
end
end
added_pull_requests.each { |p| pull_requests.delete(p) }

[bugs_a, enhancement_a, issues_a]
sections
end
end
end
2 changes: 2 additions & 0 deletions lib/github_changelog_generator/options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ class Options < SimpleDelegator
due_tag
enhancement_labels
enhancement_prefix
breaking_labels
breaking_prefix
exclude_labels
exclude_tags
exclude_tags_regex
Expand Down
8 changes: 8 additions & 0 deletions lib/github_changelog_generator/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ def self.setup_parser(options)
opts.on("--enhancement-label [LABEL]", "Setup custom label for enhancements section. Default is \"**Implemented enhancements:**\"") do |v|
options[:enhancement_prefix] = v
end
opts.on("--breaking-label [LABEL]", "Setup custom label for the breaking changes section. Default is \"**Breaking changes:**\"") do |v|
options[:breaking_prefix] = v
end
opts.on("--issues-label [LABEL]", "Setup custom label for closed-issues section. Default is \"**Closed issues:**\"") do |v|
options[:issue_prefix] = v
end
Expand Down Expand Up @@ -127,6 +130,9 @@ def self.setup_parser(options)
opts.on("--enhancement-labels x,y,z", Array, 'Issues with the specified labels will be always added to "Implemented enhancements" section. Default is \'enhancement,Enhancement\'') do |list|
options[:enhancement_labels] = list
end
opts.on("--breaking-labels x,y,z", Array, 'Issues with these labels will be added to a new section, called "Breaking Changes". Default is \'backwards-incompatible\'') do |list|
options[:breaking_labels] = list
end
opts.on("--issue-line-labels x,y,z", Array, 'The specified labels will be shown in brackets next to each matching issue. Use "ALL" to show all labels. Default is [].') do |list|
options[:issue_line_labels] = list
end
Expand Down Expand Up @@ -208,6 +214,7 @@ def self.default_options
enhancement_labels: ["enhancement", "Enhancement", "Type: Enhancement"],
bug_labels: ["bug", "Bug", "Type: Bug"],
exclude_labels: ["duplicate", "question", "invalid", "wontfix", "Duplicate", "Question", "Invalid", "Wontfix", "Meta: Exclude From Changelog"],
breaking_labels: %w[backwards-incompatible breaking],
issue_line_labels: [],
max_issues: nil,
simple_list: false,
Expand All @@ -218,6 +225,7 @@ def self.default_options
issue_prefix: "**Closed issues:**",
bug_prefix: "**Fixed bugs:**",
enhancement_prefix: "**Implemented enhancements:**",
breaking_prefix: "**Breaking changes:**",
http_cache: true
)
end
Expand Down
3 changes: 2 additions & 1 deletion lib/github_changelog_generator/parser_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def extract_pair(line)
end

KNOWN_ARRAY_KEYS = %i[exclude_labels include_labels bug_labels
enhancement_labels issue_line_labels between_tags exclude_tags]
enhancement_labels breaking_labels issue_line_labels between_tags exclude_tags]
KNOWN_INTEGER_KEYS = [:max_issues]

def convert_value(value, option_name)
Expand All @@ -91,6 +91,7 @@ def convert_value(value, option_name)
header_label: :header,
front_matter: :frontmatter,
pr_label: :merge_prefix,
breaking_label: :breaking_prefix,
issues_wo_labels: :add_issues_wo_labels,
pr_wo_labels: :add_pr_wo_labels,
pull_requests: :pulls,
Expand Down
56 changes: 56 additions & 0 deletions spec/unit/generator/generator_generation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,61 @@ module GitHubChangelogGenerator
end.not_to raise_error
end
end

describe "#parse_by_sections" do
def label(name)
{ "name" => name }
end

def issue(title, labels)
{ "title" => "issue #{title}", "labels" => labels.map { |l| label(l) } }
end

def pr(title, labels)
{ "title" => "pr #{title}", "labels" => labels.map { |l| label(l) } }
end

def get_titles(issues)
issues.map { |issue| issue["title"] }
end

let(:options) do
{
bug_labels: ["bug"],
enhancement_labels: ["enhancement"],
breaking_labels: ["breaking"]
}
end

let(:issues) do
[
issue("no labels", []),
issue("enhancement", ["enhancement"]),
issue("bug", ["bug"]),
issue("breaking", ["breaking"]),
issue("all the labels", %w[enhancement bug breaking])
]
end

let(:pull_requests) do
[
pr("no labels", []),
pr("enhancement", ["enhancement"]),
pr("bug", ["bug"]),
pr("breaking", ["breaking"]),
pr("all the labels", %w[enhancement bug breaking])
]
end

it "works" do
sections = described_class.new(options).parse_by_sections(issues, pull_requests)

expect(get_titles(sections[:issues])).to eq(["issue no labels"])
expect(get_titles(sections[:enhancements])).to eq(["issue enhancement", "issue all the labels", "pr enhancement", "pr all the labels"])
expect(get_titles(sections[:bugs])).to eq(["issue bug", "pr bug"])
expect(get_titles(sections[:breaking])).to eq(["issue breaking", "pr breaking"])
expect(get_titles(pull_requests)).to eq(["pr no labels"])
end
end
end
end