Skip to content

Commit

Permalink
feat: disregard diffs that are just reordering of lines
Browse files Browse the repository at this point in the history
  • Loading branch information
adityathebe committed Oct 7, 2024
1 parent 5ff1e9a commit ed96b99
Show file tree
Hide file tree
Showing 7 changed files with 155 additions and 0 deletions.
4 changes: 4 additions & 0 deletions db/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -779,6 +779,10 @@ func generateConfigChange(ctx api.ScrapeContext, newConf, prev models.ConfigItem
return nil, nil
}

if utils.IsReorderingDiff(diff) {
return nil, nil
}

patch, err := jsonpatch.CreateMergePatch([]byte(*newConf.Config), []byte(*prev.Config))
if err != nil {
return nil, fmt.Errorf("failed to create merge patch: %w", err)
Expand Down
49 changes: 49 additions & 0 deletions utils/diff.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package utils

import (
"bufio"
"strings"
)

// IsReorderingDiff returns true if the diff only contains reordered lines.
func IsReorderingDiff(diff string) bool {
scanner := bufio.NewScanner(strings.NewReader(diff))
lines := make(map[string]struct{})

// discard the headers of the diff.
for scanner.Scan() {
line := scanner.Text()
if strings.HasPrefix(line, "@@") && strings.Contains(line, "@@") {
break
}
}

for scanner.Scan() {
line := scanner.Text()

// For each line addition, we try to find the corresponding line removal
// and vice versa.
// If all lines are paired, then it's a reordering change.

if strings.HasPrefix(line, "+") {
opposite := strings.Replace(line, "+", "-", 1)
if _, ok := lines[opposite]; ok {
delete(lines, opposite)
} else {
lines[line] = struct{}{}
}
}

if strings.HasPrefix(line, "-") {
opposite := strings.Replace(line, "-", "+", 1)
if _, ok := lines[opposite]; ok {
delete(lines, opposite)
} else {
lines[line] = struct{}{}
}
}

}

return len(lines) == 0
}
48 changes: 48 additions & 0 deletions utils/diff_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package utils

import (
"os"
"testing"
)

func TestIsReorderingDiff(t *testing.T) {
tests := []struct {
name string
diff string
want bool
}{
{
name: "reordered lines",
diff: "testdata/reordered.diff",
want: true,
},
{
name: "new line addition with reordered lines",
diff: "testdata/non-reordered.diff",
want: false,
},
{
name: "reordered lines",
diff: "testdata/number-reordered.diff",
want: true,
},
{
name: "config change",
diff: "testdata/config-change.diff",
want: false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
diff, err := os.ReadFile(tt.diff)
if err != nil {
t.Errorf("error reading file: %v", err)
}

if got := IsReorderingDiff(string(diff)); got != tt.want {
t.Errorf("IsDiffAnOrderChange() = %v, want %v", got, tt.want)
}
})
}
}
12 changes: 12 additions & 0 deletions utils/testdata/config-change.diff
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
--- a/config.yml
+++ b/config.yml
@@ -1,7 +1,8 @@
server:
- host: localhost
+ host: 0.0.0.0
port: 8080

database:
name: myapp
- user: admin
+ user: app_user
17 changes: 17 additions & 0 deletions utils/testdata/non-reordered.diff
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
--- before
+++ after
@@ -145,11 +145,11 @@
"conditions": [
{
"status": "True",
- "type": "Initialized"
+ "type": "Ready"
},
{
"status": "True",
- "type": "Ready"
+ "type": "Initialized"
+ "color": "Blue"
},
{
"status": "True",
9 changes: 9 additions & 0 deletions utils/testdata/number-reordered.diff
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
--- before
+++ after
@@ -1,5 +1,5 @@
+Third line
First line
Second line
-Third line
Fourth line
Fifth line
16 changes: 16 additions & 0 deletions utils/testdata/reordered.diff
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
--- before
+++ after
@@ -145,11 +145,11 @@
"conditions": [
{
"status": "True",
- "type": "Initialized"
+ "type": "Ready"
},
{
"status": "True",
- "type": "Ready"
+ "type": "Initialized"
},
{
"status": "True",

0 comments on commit ed96b99

Please sign in to comment.