Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions feature/github-repo-importer/pkg/compare/compare.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ func hashNormalizedYamlFile(path string) (string, error) {
removeKey(root, "id")
removeKey(root, "branch_policy_ids")
removeKey(root, "tag_policy_ids")
removeEmptyValues(root)
sortMappingNode(root)
}

Expand Down Expand Up @@ -136,6 +137,34 @@ func removeKey(node *yaml.Node, target string) {
}
}

func removeEmptyValues(node *yaml.Node) {
switch node.Kind {
case yaml.MappingNode:
newContent := make([]*yaml.Node, 0, len(node.Content))
for i := 0; i < len(node.Content); i += 2 {
k := node.Content[i]
v := node.Content[i+1]
if isEmptyScalar(v) {
continue
}
removeEmptyValues(v)
newContent = append(newContent, k, v)
}
node.Content = newContent
case yaml.SequenceNode:
for _, elem := range node.Content {
removeEmptyValues(elem)
}
}
}

func isEmptyScalar(node *yaml.Node) bool {
if node.Kind != yaml.ScalarNode {
return false
}
return node.Tag == "!!null" || (node.Tag == "!!str" && node.Value == "")
}

func sortMappingNode(node *yaml.Node) {
if node.Kind != yaml.MappingNode {
return
Expand Down
18 changes: 18 additions & 0 deletions feature/github-repo-importer/pkg/compare/compare_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,24 @@ func TestHashingYamlFiles(t *testing.T) {
pathOfFresh: "testdata/existing/existing4.yaml",
wantEqual: true,
},
{
name: "empty and null values are equivalent to the key being absent",
pathOfImported: "testdata/imported/imported5.yaml",
pathOfFresh: "testdata/existing/existing5.yaml",
wantEqual: true,
},
Comment thread
mladjan-gadzic marked this conversation as resolved.
{
name: "a real difference is still detected alongside an empty value",
pathOfImported: "testdata/imported/imported6.yaml",
pathOfFresh: "testdata/existing/existing6.yaml",
wantEqual: false,
},
{
name: "a false value on one side only is a difference, not an empty value",
pathOfImported: "testdata/imported/imported7.yaml",
pathOfFresh: "testdata/existing/existing7.yaml",
wantEqual: false,
},
}

for _, tt := range tests {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
description: ""
homepage_url: ""
visibility: public
default_branch: main
has_issues: true
has_downloads: false
allow_auto_merge: false
archived: false
license_template:
rulesets:
- enforcement: active
name: test
rules:
deletion: true
non_fast_forward: true
target: branch
conditions:
ref_name:
include:
- ~DEFAULT_BRANCH
vulnerability_alerts_enabled: true
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
description: Managed by Terraform
homepage_url: ""
visibility: public
default_branch: main
has_issues: true
has_downloads: false
archived: false
vulnerability_alerts_enabled: true
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
visibility: public
default_branch: main
has_issues: true
has_downloads: false
archived: false
vulnerability_alerts_enabled: true
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
visibility: public
default_branch: main
has_issues: true
has_downloads: false
allow_auto_merge: false
archived: false
rulesets:
- enforcement: active
name: test
rules:
deletion: true
non_fast_forward: true
target: branch
conditions:
ref_name:
include:
- ~DEFAULT_BRANCH
vulnerability_alerts_enabled: true
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
description: Edited by hand outside the tool
visibility: public
default_branch: main
has_issues: true
has_downloads: false
archived: false
vulnerability_alerts_enabled: true
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
visibility: public
default_branch: main
has_issues: true
archived: false
vulnerability_alerts_enabled: true
Loading