Merge pull request #320 from ITV/filter-tags

Added tag exclusion with a filter (string or regex)
This commit is contained in:
Petr Korolev 2016-03-28 13:04:06 +03:00
commit 519ca91cd7
3 changed files with 38 additions and 11 deletions

View File

@ -129,26 +129,34 @@ module GitHubChangelogGenerator
end end
# @param [Array] all_tags all tags # @param [Array] all_tags all tags
# @return [Array] filtered tags according :exclude_tags option # @return [Array] filtered tags according :exclude_tags or :exclude_tags_regex option
def filter_excluded_tags(all_tags) def filter_excluded_tags(all_tags)
return all_tags unless @options[:exclude_tags] if @options[:exclude_tags]
apply_exclude_tags(all_tags)
apply_exclude_tags(all_tags) elsif @options[:exclude_tags_regex]
apply_exclude_tags_regex(all_tags)
else
all_tags
end
end end
private private
def apply_exclude_tags(all_tags) def apply_exclude_tags(all_tags)
if @options[:exclude_tags].is_a?(Regexp) if @options[:exclude_tags].is_a?(Regexp)
filter_tags_with_regex(all_tags) filter_tags_with_regex(all_tags, @options[:exclude_tags])
else else
filter_exact_tags(all_tags) filter_exact_tags(all_tags)
end end
end end
def filter_tags_with_regex(all_tags) def apply_exclude_tags_regex(all_tags)
filter_tags_with_regex(all_tags, Regexp.new(@options[:exclude_tags_regex]))
end
def filter_tags_with_regex(all_tags, regex)
warn_if_nonmatching_regex(all_tags) warn_if_nonmatching_regex(all_tags)
all_tags.reject { |tag| @options[:exclude_tags] =~ tag.name } all_tags.reject { |tag| regex =~ tag.name }
end end
def filter_exact_tags(all_tags) def filter_exact_tags(all_tags)

View File

@ -122,6 +122,9 @@ module GitHubChangelogGenerator
opts.on("--exclude-tags x,y,z", Array, "Change log will exclude specified tags") do |list| opts.on("--exclude-tags x,y,z", Array, "Change log will exclude specified tags") do |list|
options[:exclude_tags] = list options[:exclude_tags] = list
end end
opts.on("--exclude-tags-regex [REGEX]", "Apply a regular expression on tag names so that they can be excluded, for example: --exclude-tags-regex \".*\+\d{1,}\" ") do |last|
options[:exclude_tags_regex] = last
end
opts.on("--since-tag x", "Change log will start after specified tag") do |v| opts.on("--since-tag x", "Change log will start after specified tag") do |v|
options[:since_tag] = v options[:since_tag] = v
end end

View File

@ -76,31 +76,47 @@ describe GitHubChangelogGenerator::Generator do
describe "#filter_excluded_tags" do describe "#filter_excluded_tags" do
subject { generator.filter_excluded_tags(tags_mash_from_strings(%w(1 2 3))) } subject { generator.filter_excluded_tags(tags_mash_from_strings(%w(1 2 3))) }
context "with valid excluded tags" do context "with matching string" do
let(:generator) { GitHubChangelogGenerator::Generator.new(exclude_tags: %w(3)) } let(:generator) { GitHubChangelogGenerator::Generator.new(exclude_tags: %w(3)) }
it { is_expected.to be_a Array } it { is_expected.to be_a Array }
it { is_expected.to match_array(tags_mash_from_strings(%w(1 2))) } it { is_expected.to match_array(tags_mash_from_strings(%w(1 2))) }
end end
context "with invalid excluded tags" do context "with non-matching string" do
let(:generator) { GitHubChangelogGenerator::Generator.new(exclude_tags: %w(invalid tags)) } let(:generator) { GitHubChangelogGenerator::Generator.new(exclude_tags: %w(invalid tags)) }
it { is_expected.to be_a Array } it { is_expected.to be_a Array }
it { is_expected.to match_array(tags_mash_from_strings(%w(1 2 3))) } it { is_expected.to match_array(tags_mash_from_strings(%w(1 2 3))) }
end end
context "with regex exclude_tags" do context "with matching regex" do
let(:generator) { GitHubChangelogGenerator::Generator.new(exclude_tags: /[23]/) } let(:generator) { GitHubChangelogGenerator::Generator.new(exclude_tags: /[23]/) }
it { is_expected.to be_a Array } it { is_expected.to be_a Array }
it { is_expected.to match_array(tags_mash_from_strings(%w(1))) } it { is_expected.to match_array(tags_mash_from_strings(%w(1))) }
end end
context "with non-matching regex in exclude_tags" do context "with non-matching regex" do
let(:generator) { GitHubChangelogGenerator::Generator.new(exclude_tags: /[abc]/) } let(:generator) { GitHubChangelogGenerator::Generator.new(exclude_tags: /[abc]/) }
it { is_expected.to be_a Array } it { is_expected.to be_a Array }
it { is_expected.to match_array(tags_mash_from_strings(%w(1 2 3))) } it { is_expected.to match_array(tags_mash_from_strings(%w(1 2 3))) }
end end
end end
describe "#filter_excluded_tags_regex" do
subject { generator.filter_excluded_tags(tags_mash_from_strings(%w(1 2 3))) }
context "with matching regex" do
let(:generator) { GitHubChangelogGenerator::Generator.new(exclude_tags_regex: '[23]') }
it { is_expected.to be_a Array }
it { is_expected.to match_array(tags_mash_from_strings(%w(1))) }
end
context "with non-matching regex" do
let(:generator) { GitHubChangelogGenerator::Generator.new(exclude_tags_regex: '[45]') }
it { is_expected.to be_a Array }
it { is_expected.to match_array(tags_mash_from_strings(%w(1 2 3))) }
end
end
describe "#filter_since_tag" do describe "#filter_since_tag" do
context "with filled array" do context "with filled array" do
subject { generator.filter_since_tag(tags_mash_from_strings(%w(1 2 3))) } subject { generator.filter_since_tag(tags_mash_from_strings(%w(1 2 3))) }