forked from argoproj/argo-events
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support adding Github hooks without requiring a Github webhook id to …
…be hardcoded (argoproj#352)
- Loading branch information
1 parent
86d4a3d
commit 8289a31
Showing
5 changed files
with
126 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package github | ||
|
||
import ( | ||
gh "github.com/google/go-github/github" | ||
) | ||
|
||
// sliceEqual returns true if the two provided string slices are equal. | ||
func sliceEqual(first []string, second []string) bool { | ||
if len(first) != len(second) { | ||
return false | ||
} | ||
|
||
if first == nil && second == nil { | ||
return true | ||
} | ||
|
||
if first == nil || second == nil { | ||
return false | ||
} | ||
|
||
for index, _ := range first { | ||
if first[index] != second[index] { | ||
return false | ||
} | ||
} | ||
|
||
return true | ||
} | ||
|
||
// compareHook returns true if the hook matches the url and event. | ||
func compareHook(hook *gh.Hook, url string, event []string) bool { | ||
if hook == nil { | ||
return false | ||
} | ||
|
||
if hook.Config["url"] != url { | ||
return false | ||
} | ||
|
||
return sliceEqual(hook.Events, event) | ||
} | ||
|
||
// getHook returns the hook that matches the url and event, or nil if not found. | ||
func getHook(hooks []*gh.Hook, url string, event []string) *gh.Hook { | ||
for _, hook := range hooks { | ||
if compareHook(hook, url, event) { | ||
return hook | ||
} | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package github | ||
|
||
import ( | ||
"testing" | ||
"github.com/stretchr/testify/assert" | ||
gh "github.com/google/go-github/github" | ||
) | ||
|
||
func TestSliceEqual(t *testing.T) { | ||
assert.True(t, sliceEqual(nil, nil)) | ||
assert.True(t, sliceEqual([]string{"hello"}, []string{"hello"})) | ||
assert.True(t, sliceEqual([]string{"hello", "world"}, []string{"hello", "world"})) | ||
assert.True(t, sliceEqual([]string{}, []string{})) | ||
|
||
assert.False(t, sliceEqual([]string{"hello"}, nil)) | ||
assert.False(t, sliceEqual([]string{"hello"}, []string{})) | ||
assert.False(t, sliceEqual([]string{}, []string{"hello"})) | ||
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"})) | ||
} | ||
|
||
func TestCompareHook(t *testing.T) { | ||
assert.False(t, compareHook(nil, "https://google.com/", []string{})) | ||
|
||
assert.True(t, compareHook(&gh.Hook{ | ||
Config: map[string]interface{}{ | ||
"url": "https://google.com/", | ||
}, | ||
Events: []string{"*"}, | ||
}, "https://google.com/", []string{"*"})) | ||
|
||
assert.False(t, compareHook(&gh.Hook{ | ||
Config: map[string]interface{}{ | ||
"url": "https://google.com/", | ||
}, | ||
Events: []string{"pull_request"}, | ||
}, "https://google.com/", []string{"*"})) | ||
|
||
assert.False(t, compareHook(&gh.Hook{ | ||
Config: map[string]interface{}{ | ||
"url": "https://example.com/", | ||
}, | ||
Events: []string{"pull_request"}, | ||
}, "https://google.com/", []string{"*"})) | ||
} | ||
|
||
func TestGetHook(t *testing.T) { | ||
hooks := []*gh.Hook{ | ||
&gh.Hook{ | ||
Config: map[string]interface{}{ | ||
"url": "https://example.com/", | ||
}, | ||
Events: []string{"pull_request"}, | ||
}, | ||
&gh.Hook{ | ||
Config: map[string]interface{}{ | ||
"url": "https://example.com/", | ||
}, | ||
Events: []string{"*"}, | ||
}, | ||
} | ||
|
||
assert.Equal(t, hooks[1], getHook(hooks, "https://example.com/", []string{"*"})) | ||
assert.Nil(t, getHook(hooks, "https://example.com/", []string{"does_not_exist"})) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters