Skip to content

Commit a677240

Browse files
authored
Merge pull request #84 from github/kpaulisse-json-filter
Add JSON equivalence filter
2 parents 0ab4f22 + b4abbb8 commit a677240

File tree

23 files changed

+347
-3
lines changed

23 files changed

+347
-3
lines changed

.version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.0.0
1+
1.0.1

doc/CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,15 @@
88
</tr>
99
</thead><tbody>
1010
<tr valign=top>
11+
<td>1.0.1</td>
12+
<td>2017-02-14</td>
13+
<td>
14+
<li><a href="https://github.com/github/octocatalog-diff/pull/84">#84</a>: Add JSON equivalence filter</li>
15+
<li><a href="https://github.com/github/octocatalog-diff/pull/83">#83</a>: Retries for Puppet Master retrieval</li>
16+
<li><a href="https://github.com/github/octocatalog-diff/pull/82">#82</a>: Command line option for Puppet Master timeout</li>
17+
</td>
18+
</tr>
19+
<tr valign=top>
1120
<td>1.0.0</td>
1221
<td>2017-02-06</td>
1322
<td>

doc/advanced-filter.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Please note that there are other options to ignore specified diffs, including:
1010
Here is the list of available filters and an explanation of each:
1111

1212
- [Absent File](/doc/advanced-filter.md#absent-file) - Ignore parameter changes of a file that is declared to be absent
13+
- [JSON](/doc/advanced-filter.md#json) - Ignore whitespace differences if JSON parses to the same object
1314
- [YAML](/doc/advanced-filter.md#yaml) - Ignore whitespace/comment differences if YAML parses to the same object
1415

1516
## Absent File
@@ -69,6 +70,20 @@ Wouldn't it be nice if the meaningless information didn't appear, and all you sa
6970
+ absent
7071
```
7172

73+
## JSON
74+
75+
#### Usage
76+
77+
```
78+
--filters JSON
79+
```
80+
81+
#### Description
82+
83+
If a file resource has extension `.json` and a difference in its content is observed, JSON objects are constructed from the previous and new values. If these JSON objects are identical, the difference is ignored.
84+
85+
This allows you to ignore changes in whitespace, comments, etc., that are not meaningful to a machine parsing the file. Note that changes to files may still trigger Puppet to restart services even though these changes are not displayed in the octocatalog-diff output.
86+
7287
## YAML
7388

7489
#### Usage
@@ -81,4 +96,4 @@ Wouldn't it be nice if the meaningless information didn't appear, and all you sa
8196

8297
If a file resource has extension `.yml` or `.yaml` and a difference in its content is observed, YAML objects are constructed from the previous and new values. If these YAML objects are identical, the difference is ignored.
8398

84-
This allows you to ignore changes in whitespace, comments, etc., that are not meaningful to a machine parsing the file. Please note that by filtering these changes, you are ignoring changes to comments, which may be meaningful to humans.
99+
This allows you to ignore changes in whitespace, comments, etc., that are not meaningful to a machine parsing the file. Please note that by filtering these changes, you are ignoring changes to comments, which may be meaningful to humans. Also, changes to files may still trigger Puppet to restart services even though these changes are not displayed in the octocatalog-diff output.

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
require_relative '../api/v1/diff'
22
require_relative 'filter/absent_file'
33
require_relative 'filter/compilation_dir'
4+
require_relative 'filter/json'
45
require_relative 'filter/yaml'
56

67
require 'stringio'
@@ -12,7 +13,7 @@ class Filter
1213
attr_accessor :logger
1314

1415
# List the available filters here (by class name) for use in the validator method.
15-
AVAILABLE_FILTERS = %w(AbsentFile CompilationDir YAML).freeze
16+
AVAILABLE_FILTERS = %w(AbsentFile CompilationDir JSON YAML).freeze
1617

1718
# Public: Determine whether a particular filter exists. This can be used to validate
1819
# a user-submitted filter.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../filter'
4+
5+
require 'json'
6+
7+
module OctocatalogDiff
8+
module CatalogDiff
9+
class Filter
10+
# Filter based on equivalence of JSON objects for file resources with named extensions.
11+
class JSON < OctocatalogDiff::CatalogDiff::Filter
12+
# Public: Actually do the comparison of JSON objects for appropriate resources.
13+
# Return true if the JSON objects are known to be equivalent. Return false if they
14+
# are not equivalent, or if equivalence cannot be determined.
15+
#
16+
# @param diff_in [OctocatalogDiff::API::V1::Diff] Difference
17+
# @param _options [Hash] Additional options (there are none for this filter)
18+
# @return [Boolean] true if this difference is a JSON file with identical objects, false otherwise
19+
def filtered?(diff, _options = {})
20+
# Skip additions or removals - focus only on changes
21+
return false unless diff.change?
22+
23+
# Make sure we are comparing file content for a file ending in .json extension
24+
return false unless diff.type == 'File' && diff.structure == %w(parameters content)
25+
return false unless diff.title =~ /\.json\z/i
26+
27+
# Attempt to convert the old value and new value into JSON objects. Assuming
28+
# that doesn't error out, the return value is whether or not they're equal.
29+
obj_old = ::JSON.parse(diff.old_value)
30+
obj_new = ::JSON.parse(diff.new_value)
31+
obj_old == obj_new
32+
rescue # Rescue everything - if something failed, we aren't sure what's going on, so we'll return false.
33+
false
34+
end
35+
end
36+
end
37+
end
38+
end
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
:backends:
3+
- yaml
4+
:yaml:
5+
:datadir: /var/lib/puppet/environments/%{::environment}/hieradata
6+
:hierarchy:
7+
- roles/%{::role}
8+
- common
9+
:merge_behavior: deeper
10+
:logger: console
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
---
2+
bar::parameter: default
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
---
2+
bar::parameter: bar
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
node default {
2+
include bar
3+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"value": "bar"}

0 commit comments

Comments
 (0)