Skip to content

Commit bd2c08a

Browse files
author
Kevin Paulisse
committed
Use API diff object in absent file filter
1 parent 587f529 commit bd2c08a

1 file changed

Lines changed: 13 additions & 14 deletions

File tree

lib/octocatalog-diff/catalog-diff/filter/absent_file.rb

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# frozen_string_literal: true
22

3+
require_relative '../../api/v1/diff'
34
require_relative '../filter'
45

56
require 'set'
@@ -9,10 +10,12 @@ module CatalogDiff
910
class Filter
1011
# Filter out changes in parameters when the "to" resource has ensure => absent.
1112
class AbsentFile < OctocatalogDiff::CatalogDiff::Filter
13+
KEEP_ATTRIBUTES = (Set.new %w(ensure backup force provider)).freeze
14+
1215
# Constructor: Since this filter requires knowledge of the entire array of diffs,
1316
# override the inherited method to store those diffs in an instance variable.
1417
def initialize(diffs, _logger = nil)
15-
@diffs = diffs
18+
@diffs = diffs.map { |x| OctocatalogDiff::API::V1::Diff.new(x) }
1619
@results = nil
1720
end
1821

@@ -21,7 +24,7 @@ def initialize(diffs, _logger = nil)
2124
# Return true if the difference is in a resource where `ensure => absent` has been
2225
# declared. Return false if they this is not the case.
2326
#
24-
# @param diff [internal diff format] Difference
27+
# @param diff [OctocatalogDiff::API::V1::Diff] Difference
2528
# @param _options [Hash] Additional options (there are none for this filter)
2629
# @return [Boolean] true if this difference is a YAML file with identical objects, false otherwise
2730
def filtered?(diff, _options = {})
@@ -37,27 +40,23 @@ def build_results
3740
# Which files can we ignore?
3841
@files_to_ignore = Set.new
3942
@diffs.each do |diff|
40-
next unless diff[0] == '~' || diff[0] == '!'
41-
next unless diff[1] =~ /^File\f([^\f]+)\fparameters\fensure$/
42-
next unless ['absent', 'false', false].include?(diff[3])
43-
@files_to_ignore.add Regexp.last_match(1)
43+
next unless diff.change? && diff.type == 'File' && diff.structure == %w(parameters ensure)
44+
next unless ['absent', 'false', false].include?(diff.new_value)
45+
@files_to_ignore.add diff.title
4446
end
4547

4648
# Based on that, which diffs can we ignore?
47-
@results = Set.new @diffs.reject { |diff| keep_diff?(diff) }
49+
@results = Set.new @diffs.reject { |diff| keep_diff?(diff) }.map(&:raw)
4850
end
4951

5052
# Private: Determine whether to keep a particular diff.
5153
# @param diff [OctocatalogDiff::API::V1::Diff] Difference under consideration
5254
# @return [Boolean] true = keep, false = discard
5355
def keep_diff?(diff)
54-
keep = %w(ensure backup force provider)
55-
if (diff[0] == '!' || diff[0] == '~') && diff[1] =~ /^File\f(.+)\fparameters\f(.+)$/
56-
if @files_to_ignore.include?(Regexp.last_match(1)) && !keep.include?(Regexp.last_match(2))
57-
return false
58-
end
59-
end
60-
true
56+
return true unless diff.change? && diff.type == 'File' && diff.structure.first == 'parameters'
57+
return true unless @files_to_ignore.include?(diff.title)
58+
return true if KEEP_ATTRIBUTES.include?(diff.structure.last)
59+
false
6160
end
6261
end
6362
end

0 commit comments

Comments
 (0)