Skip to content

Commit

Permalink
Support adding Github hooks without requiring a Github webhook id to …
Browse files Browse the repository at this point in the history
…be hardcoded (#352)
  • Loading branch information
jbarrick-mesosphere authored and VaibhavPage committed Oct 2, 2019
1 parent 0169381 commit 9605b8b
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 9 deletions.
3 changes: 0 additions & 3 deletions examples/event-sources/github.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -52,7 +50,6 @@ data:
contentType: "json"
example-with-secure-connection: |-
id: 456
owner: "argoproj"
repository: "argo"
hook:
Expand Down
52 changes: 52 additions & 0 deletions gateways/community/github/hook_util.go
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
}
66 changes: 66 additions & 0 deletions gateways/community/github/hook_util_test.go
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"}))
}
11 changes: 8 additions & 3 deletions gateways/community/github/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -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.")
}
}

Expand Down Expand Up @@ -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")
Expand Down
3 changes: 0 additions & 3 deletions gateways/community/github/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down

0 comments on commit 9605b8b

Please sign in to comment.