Skip to content

Commit

Permalink
Merge pull request github-changelog-generator#320 from ITV/filter-tags
Browse files Browse the repository at this point in the history
Added tag exclusion with a filter (string or regex)
  • Loading branch information
skywinder committed Mar 28, 2016
2 parents 2e2cc16 + 507ffdb commit 519ca91
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 11 deletions.
22 changes: 15 additions & 7 deletions lib/github_changelog_generator/generator/generator_tags.rb
Original file line number Diff line number Diff line change
Expand Up @@ -129,26 +129,34 @@ def filter_between_tags(all_tags)
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)
Expand Down
3 changes: 3 additions & 0 deletions lib/github_changelog_generator/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@ def self.setup_parser(options)
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
Expand Down
24 changes: 20 additions & 4 deletions spec/unit/generator/generator_tags_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,31 +76,47 @@ def tags_mash_from_strings(tags_strings)
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))) }
Expand Down

0 comments on commit 519ca91

Please sign in to comment.