Merge pull request #320 from ITV/filter-tags
Added tag exclusion with a filter (string or regex)
This commit is contained in:
commit
519ca91cd7
|
@ -129,26 +129,34 @@ module GitHubChangelogGenerator
|
|||
end
|
||||
|
||||
# @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)
|
||||
return all_tags unless @options[:exclude_tags]
|
||||
|
||||
apply_exclude_tags(all_tags)
|
||||
if @options[:exclude_tags]
|
||||
apply_exclude_tags(all_tags)
|
||||
elsif @options[:exclude_tags_regex]
|
||||
apply_exclude_tags_regex(all_tags)
|
||||
else
|
||||
all_tags
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def apply_exclude_tags(all_tags)
|
||||
if @options[:exclude_tags].is_a?(Regexp)
|
||||
filter_tags_with_regex(all_tags)
|
||||
filter_tags_with_regex(all_tags, @options[:exclude_tags])
|
||||
else
|
||||
filter_exact_tags(all_tags)
|
||||
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)
|
||||
all_tags.reject { |tag| @options[:exclude_tags] =~ tag.name }
|
||||
all_tags.reject { |tag| regex =~ tag.name }
|
||||
end
|
||||
|
||||
def filter_exact_tags(all_tags)
|
||||
|
|
|
@ -122,6 +122,9 @@ module GitHubChangelogGenerator
|
|||
opts.on("--exclude-tags x,y,z", Array, "Change log will exclude specified tags") do |list|
|
||||
options[:exclude_tags] = list
|
||||
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|
|
||||
options[:since_tag] = v
|
||||
end
|
||||
|
|
|
@ -76,31 +76,47 @@ describe GitHubChangelogGenerator::Generator do
|
|||
describe "#filter_excluded_tags" do
|
||||
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)) }
|
||||
it { is_expected.to be_a Array }
|
||||
it { is_expected.to match_array(tags_mash_from_strings(%w(1 2))) }
|
||||
end
|
||||
|
||||
context "with invalid excluded tags" do
|
||||
context "with non-matching string" do
|
||||
let(:generator) { GitHubChangelogGenerator::Generator.new(exclude_tags: %w(invalid tags)) }
|
||||
it { is_expected.to be_a Array }
|
||||
it { is_expected.to match_array(tags_mash_from_strings(%w(1 2 3))) }
|
||||
end
|
||||
|
||||
context "with regex exclude_tags" do
|
||||
context "with matching regex" do
|
||||
let(:generator) { GitHubChangelogGenerator::Generator.new(exclude_tags: /[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 in exclude_tags" do
|
||||
context "with non-matching regex" do
|
||||
let(:generator) { GitHubChangelogGenerator::Generator.new(exclude_tags: /[abc]/) }
|
||||
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_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
|
||||
context "with filled array" do
|
||||
subject { generator.filter_since_tag(tags_mash_from_strings(%w(1 2 3))) }
|
||||
|
|
Loading…
Reference in New Issue
Block a user