diff --git a/examples/event-sources/github.yaml b/examples/event-sources/github.yaml index cdc40f72ad..57d702735b 100644 --- a/examples/event-sources/github.yaml +++ b/examples/event-sources/github.yaml @@ -11,8 +11,6 @@ metadata: argo-events-event-source-version: v0.10 data: example: |- - # id of the project - id: 123 # owner of the repo owner: "argoproj" # repository name @@ -52,7 +50,6 @@ data: contentType: "json" example-with-secure-connection: |- - id: 456 owner: "argoproj" repository: "argo" hook: diff --git a/gateways/community/github/hook_util.go b/gateways/community/github/hook_util.go new file mode 100644 index 0000000000..36aa285952 --- /dev/null +++ b/gateways/community/github/hook_util.go @@ -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 +} diff --git a/gateways/community/github/hook_util_test.go b/gateways/community/github/hook_util_test.go new file mode 100644 index 0000000000..595e88ad85 --- /dev/null +++ b/gateways/community/github/hook_util_test.go @@ -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"})) +} diff --git a/gateways/community/github/start.go b/gateways/community/github/start.go index a9563a1098..b31f3e0c52 100644 --- a/gateways/community/github/start.go +++ b/gateways/community/github/start.go @@ -99,7 +99,6 @@ func (rc *RouteConfig) PostStart() error { Events: gc.Events, Active: gh.Bool(gc.Active), Config: hookConfig, - ID: &rc.ges.Id, } rc.client = gh.NewClient(PATTransport.Client()) @@ -132,9 +131,14 @@ func (rc *RouteConfig) PostStart() error { if hook == nil { ctx, cancel = context.WithTimeout(context.Background(), 10*time.Second) defer cancel() - hook, _, err = rc.client.Repositories.GetHook(ctx, gc.Owner, gc.Repository, rc.ges.Id) + hooks, _, err := rc.client.Repositories.ListHooks(ctx, gc.Owner, gc.Repository, nil) if err != nil { - return fmt.Errorf("failed to get existing webhook with id %d. err: %+v", rc.ges.Id, err) + return fmt.Errorf("failed to list existing webhooks. err: %+v", err) + } + + hook = getHook(hooks, formattedUrl, gc.Events) + if hook == nil { + return fmt.Errorf("failed to find existing webhook.") } } @@ -214,6 +218,7 @@ func (rc *RouteConfig) RouteHandler(writer http.ResponseWriter, request *http.Re common.LabelEventSource: r.EventSource.Name, common.LabelEndpoint: r.Webhook.Endpoint, common.LabelPort: r.Webhook.Port, + "hi": "lol", }) logger.Info("request received") diff --git a/gateways/community/github/validate.go b/gateways/community/github/validate.go index ee5f70a6fb..f6f87aa575 100644 --- a/gateways/community/github/validate.go +++ b/gateways/community/github/validate.go @@ -30,9 +30,6 @@ func validateGithub(config interface{}) error { if g == nil { return gwcommon.ErrNilEventSource } - if g.Id == 0 { - return fmt.Errorf("hook id must be not be zero") - } if g.Repository == "" { return fmt.Errorf("repository cannot be empty") }