Skip to content

Commit

Permalink
fix(GithubEventSource): Compare events ignoring order and duplicate (a…
Browse files Browse the repository at this point in the history
…rgoproj#1124)

* fix(GithubEventSource): Sort events before compare

Signed-off-by: Derek Wang <whynowy@gmail.com>
  • Loading branch information
whynowy authored Mar 23, 2021
1 parent 88dc8b1 commit 35f136d
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 8 deletions.
32 changes: 24 additions & 8 deletions eventsources/sources/github/hook_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,40 @@ import (

// sliceEqual returns true if the two provided string slices are equal.
func sliceEqual(first []string, second []string) bool {
if len(first) != len(second) {
if len(first) == 0 && len(second) == 0 {
return true
}
if len(first) == 0 || len(second) == 0 {
return false
}

if first == nil && second == nil {
return true
tmp := make(map[string]int)
for _, i := range first {
tmp[i] = 1
}

if first == nil || second == nil {
return false
for _, i := range second {
v, ok := tmp[i]
if !ok || v == -1 {
tmp[i] = -1
} else {
tmp[i] = 2
}
}

if v, ok := tmp["*"]; ok {
// If both slices contain "*", return true directly
return v == 2
}

for index := range first {
if first[index] != second[index] {
for _, v := range tmp {
// -1: only exists in second
// 1: only exists in first
// 2: exists in both
if v < 2 {
return false
}
}

return true
}

Expand Down
12 changes: 12 additions & 0 deletions eventsources/sources/github/hook_util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,18 @@ func TestSliceEqual(t *testing.T) {
assert.False(t, sliceEqual([]string{"hello"}, []string{"hello", "world"}))
assert.False(t, sliceEqual([]string{"hello", "world"}, []string{"hello"}))
assert.False(t, sliceEqual([]string{"hello", "world"}, []string{"hello", "moon"}))
assert.True(t, sliceEqual([]string{"hello", "world"}, []string{"world", "hello"}))
assert.True(t, sliceEqual([]string{"hello", "*"}, []string{"*"}))
assert.True(t, sliceEqual([]string{"hello", "*"}, []string{"*", "world"}))
assert.True(t, sliceEqual([]string{"hello", "world", "hello"}, []string{"hello", "hello", "world", "world"}))
assert.True(t, sliceEqual([]string{"world", "hello"}, []string{"hello", "hello", "world", "world"}))
assert.True(t, sliceEqual([]string{"hello", "hello", "world", "world"}, []string{"world", "hello"}))
assert.False(t, sliceEqual([]string{"hello"}, []string{"*", "hello"}))
assert.False(t, sliceEqual([]string{"hello", "*"}, []string{"hello"}))
assert.False(t, sliceEqual([]string{"*", "hello", "*"}, []string{"hello"}))
assert.False(t, sliceEqual([]string{"hello"}, []string{"world", "world"}))
assert.False(t, sliceEqual([]string{"hello", "hello"}, []string{"world", "world"}))
assert.True(t, sliceEqual([]string{"*", "hello", "*"}, []string{"*", "world", "hello", "world"}))
}

func TestCompareHook(t *testing.T) {
Expand Down

0 comments on commit 35f136d

Please sign in to comment.