Skip to content

Commit 217c7d7

Browse files
author
Kevin Paulisse
committed
Move --ignore-tags out of CLI
1 parent 5bb72b8 commit 217c7d7

5 files changed

Lines changed: 103 additions & 56 deletions

File tree

lib/octocatalog-diff/catalog-diff/differ.rb

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,32 @@ def ignore(ignores = [])
104104
self
105105
end
106106

107+
# Handle --ignore-tags option, the ability to tag resources within modules/manifests and
108+
# have catalog-diff ignore them.
109+
def ignore_tags
110+
return unless @opts[:ignore_tags].is_a?(Array) && @opts[:ignore_tags].any?
111+
112+
# Go through the "to" catalog and identify any resources that have been tagged with one or more
113+
# specified "ignore tags." Add any such items to the ignore list. The 'to' catalog has the authoritative
114+
# list of dynamic ignores.
115+
@catalog2_raw.resources.each do |resource|
116+
next unless tagged_for_ignore?(resource)
117+
ignore(type: resource['type'], title: resource['title'])
118+
@logger.debug "Ignoring type='#{resource['type']}', title='#{resource['title']}' based on tag in to-catalog"
119+
end
120+
121+
# Go through the "from" catalog and identify any resources that have been tagged with one or more
122+
# specified "ignore tags." Only mark the resources for ignoring if they do not appear in the 'to'
123+
# catalog, thereby allowing the 'to' catalog to be the authoritative ignore list. This allows deleted
124+
# items that were previously ignored to continue to be ignored.
125+
@catalog1_raw.resources.each do |resource|
126+
next if @catalog2_raw.resource(type: resource['type'], title: resource['title'])
127+
next unless tagged_for_ignore?(resource)
128+
ignore(type: resource['type'], title: resource['title'])
129+
@logger.debug "Ignoring type='#{resource['type']}', title='#{resource['title']}' based on tag in from-catalog"
130+
end
131+
end
132+
107133
# Return catalog1 with filter_and_cleanups applied.
108134
# This is in the public section because it's called from spec tests as well
109135
# as being called internally.
@@ -122,6 +148,20 @@ def catalog2
122148

123149
private
124150

151+
# Determine if a resource is tagged with any ignore-tag.
152+
# @param resource [Hash] The resource
153+
# @return [Boolean] true if tagged for ignore, false if not
154+
def tagged_for_ignore?(resource)
155+
return false unless @opts[:ignore_tags].is_a?(Array)
156+
return false unless resource.key?('tags') && resource['tags'].is_a?(Array)
157+
@opts[:ignore_tags].each do |tag|
158+
# tag_with_type will be like: 'ignored_catalog_diff__mymodule__mytype'
159+
tag_with_type = [tag, resource['type'].downcase.gsub(/\W/, '_')].join('__')
160+
return true if resource['tags'].include?(tag) || resource['tags'].include?(tag_with_type)
161+
end
162+
false
163+
end
164+
125165
# Actually perform the catalog diff. This implements the 3-part algorithm described in the
126166
# comment block at the top of this file.
127167
def catdiff

lib/octocatalog-diff/cli/diffs.rb

Lines changed: 1 addition & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -28,52 +28,13 @@ def diffs(catalogs)
2828
differ = OctocatalogDiff::CatalogDiff::Differ.new(diff_opts, catalogs[:from], catalogs[:to])
2929
differ.ignore(attr: 'tags') unless @options.fetch(:include_tags, false)
3030
differ.ignore(@options.fetch(:ignore, []))
31-
32-
# Handle --ignore-tags option, the ability to tag resources within modules/manifests and
33-
# have catalog-diff ignore them.
34-
if @options[:ignore_tags].is_a?(Array) && @options[:ignore_tags].any?
35-
# Go through the "to" catalog and identify any resources that have been tagged with one or more
36-
# specified "ignore tags." Add any such items to the ignore list. The 'to' catalog has the authoritative
37-
# list of dynamic ignores.
38-
catalogs[:to].resources.each do |resource|
39-
next unless tagged_for_ignore?(resource)
40-
differ.ignore(type: resource['type'], title: resource['title'])
41-
@logger.debug "Ignoring type='#{resource['type']}', title='#{resource['title']}' based on tag in to-catalog"
42-
end
43-
44-
# Go through the "from" catalog and identify any resources that have been tagged with one or more
45-
# specified "ignore tags." Only mark the resources for ignoring if they do not appear in the 'to'
46-
# catalog, thereby allowing the 'to' catalog to be the authoritative ignore list. This allows deleted
47-
# items that were previously ignored to continue to be ignored.
48-
catalogs[:from].resources.each do |resource|
49-
next if catalogs[:to].resource(type: resource['type'], title: resource['title'])
50-
next unless tagged_for_ignore?(resource)
51-
differ.ignore(type: resource['type'], title: resource['title'])
52-
@logger.debug "Ignoring type='#{resource['type']}', title='#{resource['title']}' based on tag in from-catalog"
53-
end
54-
end
31+
differ.ignore_tags
5532

5633
# Actually perform the diff
5734
diff_result = differ.diff
5835
@logger.debug 'Success compute diffs between catalogs'
5936
diff_result
6037
end
61-
62-
private
63-
64-
# Determine if a resource is tagged with any ignore-tag.
65-
# @param resource [Hash] The resource
66-
# @return [Boolean] true if tagged for ignore, false if not
67-
def tagged_for_ignore?(resource)
68-
return false unless @options[:ignore_tags].is_a?(Array)
69-
return false unless resource.key?('tags') && resource['tags'].is_a?(Array)
70-
@options[:ignore_tags].each do |tag|
71-
# tag_with_type will be like: 'ignored_catalog_diff__mymodule__mytype'
72-
tag_with_type = [tag, resource['type'].downcase.gsub(/\W/, '_')].join('__')
73-
return true if resource['tags'].include?(tag) || resource['tags'].include?(tag_with_type)
74-
end
75-
false
76-
end
7738
end
7839
end
7940
end

spec/octocatalog-diff/integration/ignore_tags_spec.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,4 +98,21 @@ def build_catalogs(old_repo, new_repo, argv)
9898
end
9999
end
100100
end
101+
102+
it 'should remove tagged-for-ignore resources' do
103+
@logger, @logger_str = OctocatalogDiff::Spec.setup_logger
104+
cat1 = OctocatalogDiff::Catalog.new(json: File.read(OctocatalogDiff::Spec.fixture_path('catalogs/ignore-tags-old.json')))
105+
cat2 = OctocatalogDiff::Catalog.new(json: File.read(OctocatalogDiff::Spec.fixture_path('catalogs/ignore-tags-new.json')))
106+
opts = { ignore_tags: ['ignored_catalog_diff'] }
107+
answer = JSON.parse(File.read(OctocatalogDiff::Spec.fixture_path('diffs/ignore-tags-partial.json')))
108+
obj = OctocatalogDiff::Cli::Diffs.new(opts, @logger)
109+
diffs = obj.diffs(from: cat1, to: cat2)
110+
expect(diffs.size).to eq(8)
111+
answer.each do |x|
112+
expect(diffs).to include(x), "Does not contain: #{x}"
113+
end
114+
expect(@logger_str.string).to match(/Ignoring type='Mymodule::Resource1', title='one' based on tag in to-catalog/)
115+
r = %r{Ignoring type='File', title='/tmp/old-file/ignored/one' based on tag in from-catalog}
116+
expect(@logger_str.string).to match(r)
117+
end
101118
end

spec/octocatalog-diff/tests/catalog-diff/differ_spec.rb

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1287,6 +1287,51 @@
12871287
end
12881288
end
12891289

1290+
describe '#ignore_tags' do
1291+
let(:catalog_1) { OctocatalogDiff::Catalog.new(json: OctocatalogDiff::Spec.fixture_read('catalogs/ignore-tags-old.json')) }
1292+
let(:catalog_2) { OctocatalogDiff::Catalog.new(json: OctocatalogDiff::Spec.fixture_read('catalogs/ignore-tags-new.json')) }
1293+
let(:opts) { { ignore_tags: ['ignored_catalog_diff'] } }
1294+
let(:answer) { JSON.parse(OctocatalogDiff::Spec.fixture_read('diffs/ignore-tags-partial.json')) }
1295+
1296+
it 'should remove tagged-for-ignore resources' do
1297+
logger, logger_str = OctocatalogDiff::Spec.setup_logger
1298+
subject = described_class.new(opts.merge(logger: logger), catalog_1, catalog_2)
1299+
subject.ignore_tags
1300+
1301+
ignore_answer = [
1302+
{ type: 'Mymodule::Resource1', title: 'one', attr: '*' },
1303+
{ type: 'Mymodule::Resource1', title: 'two', attr: '*' },
1304+
{ type: 'Mymodule::Resource1', title: 'three', attr: '*' },
1305+
{ type: 'Mymodule::Resource1', title: 'four', attr: '*' },
1306+
{ type: 'Mymodule::Resource2', title: 'five', attr: '*' },
1307+
{ type: 'File', title: '/tmp/ignored/one', attr: '*' },
1308+
{ type: 'File', title: '/tmp/new-file/ignored/one', attr: '*' },
1309+
{ type: 'File', title: '/tmp/ignored/two', attr: '*' },
1310+
{ type: 'File', title: '/tmp/new-file/ignored/two', attr: '*' },
1311+
{ type: 'File', title: '/tmp/ignored/three', attr: '*' },
1312+
{ type: 'File', title: '/tmp/new-file/ignored/three', attr: '*' },
1313+
{ type: 'File', title: '/tmp/ignored/four', attr: '*' },
1314+
{ type: 'File', title: '/tmp/new-file/ignored/four', attr: '*' },
1315+
{ type: 'File', title: '/tmp/resource2/five', attr: '*' },
1316+
{ type: 'File', title: '/tmp/ignored/five', attr: '*' },
1317+
{ type: 'File', title: '/tmp/new-file/ignored/five', attr: '*' },
1318+
{ type: 'File', title: '/tmp/old-file/ignored/one', attr: '*' },
1319+
{ type: 'File', title: '/tmp/old-file/ignored/two', attr: '*' },
1320+
{ type: 'File', title: '/tmp/old-file/ignored/three', attr: '*' },
1321+
{ type: 'File', title: '/tmp/old-file/ignored/four', attr: '*' },
1322+
{ type: 'File', title: '/tmp/old-file/ignored/five', attr: '*' }
1323+
]
1324+
1325+
ignores = subject.instance_variable_get('@ignore')
1326+
expect(ignores.size).to eq(ignore_answer.size)
1327+
ignore_answer.each { |answer| expect(ignores).to include(answer) }
1328+
1329+
expect(logger_str.string).to match(/Ignoring type='Mymodule::Resource1', title='one' based on tag in to-catalog/)
1330+
r = %r{Ignoring type='File', title='/tmp/old-file/ignored/one' based on tag in from-catalog}
1331+
expect(logger_str.string).to match(r)
1332+
end
1333+
end
1334+
12901335
describe '#hashdiff_nested_changes' do
12911336
it 'should return array with proper results' do
12921337
hashdiff_add_remove = [

spec/octocatalog-diff/tests/cli/diffs_spec.rb

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -67,21 +67,5 @@
6767
expect(result).to eq([])
6868
expect(@logger_str.string).not_to match(/WARN/)
6969
end
70-
71-
it 'should remove tagged-for-ignore resources' do
72-
cat1 = OctocatalogDiff::Catalog.new(json: File.read(OctocatalogDiff::Spec.fixture_path('catalogs/ignore-tags-old.json')))
73-
cat2 = OctocatalogDiff::Catalog.new(json: File.read(OctocatalogDiff::Spec.fixture_path('catalogs/ignore-tags-new.json')))
74-
opts = { ignore_tags: ['ignored_catalog_diff'] }
75-
answer = JSON.parse(File.read(OctocatalogDiff::Spec.fixture_path('diffs/ignore-tags-partial.json')))
76-
obj = OctocatalogDiff::Cli::Diffs.new(opts, @logger)
77-
diffs = obj.diffs(from: cat1, to: cat2)
78-
expect(diffs.size).to eq(8)
79-
answer.each do |x|
80-
expect(diffs).to include(x), "Does not contain: #{x}"
81-
end
82-
expect(@logger_str.string).to match(/Ignoring type='Mymodule::Resource1', title='one' based on tag in to-catalog/)
83-
r = %r{Ignoring type='File', title='/tmp/old-file/ignored/one' based on tag in from-catalog}
84-
expect(@logger_str.string).to match(r)
85-
end
8670
end
8771
end

0 commit comments

Comments
 (0)