Skip to content

Commit

Permalink
fix(hooks): account for multiple hooks in repo disable (#151)
Browse files Browse the repository at this point in the history
* fix: account for multiple hooks

* also activate repo after repair
  • Loading branch information
wass3r committed May 13, 2020
1 parent 41688f2 commit cb74efc
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 8 deletions.
15 changes: 15 additions & 0 deletions api/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,21 @@ func RepairRepo(c *gin.Context) {
return
}

// if the repo was previously inactive, mark it as active
if !r.GetActive() {
r.SetActive(true)

// send API call to update the repo
err = database.FromContext(c).UpdateRepo(r)
if err != nil {
retErr := fmt.Errorf("unable to set repo %s to active: %w", r.GetFullName(), err)

util.HandleError(c, http.StatusInternalServerError, retErr)

return
}
}

c.JSON(http.StatusOK, fmt.Sprintf("Repo %s repaired", r.GetFullName()))
}

Expand Down
21 changes: 13 additions & 8 deletions source/github/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,15 @@ func (c *client) Disable(u *library.User, org, name string) error {
return err
}

// since 0 might be a real value (though unlikely?)
var id *int64
// accounting for situations in which multiple hooks have been
// associated with this vela instance, which causes some
// disable, repair, enable operations to act in undesirable ways
var ids []int64

// iterate through each element in the hooks
for _, hook := range hooks {
// skip if the hook has no ID
if hook.ID == nil {
if hook.GetID() == 0 {
continue
}

Expand All @@ -95,17 +97,20 @@ func (c *client) Disable(u *library.User, org, name string) error {

// capture hook ID if the hook url matches
if hookURL == fmt.Sprintf("%s/webhook", c.LocalHost) {
id = hook.ID
ids = append(ids, hook.GetID())
}
}

// skip if we got no hook ID
if id == nil {
// skip if we have no hook IDs
if len(ids) == 0 {
return nil
}

// send API call to delete the webhook
_, err = client.Repositories.DeleteHook(ctx, org, name, *id)
// go through all found hook IDs and delete them
for _, id := range ids {
// send API call to delete the webhook
_, err = client.Repositories.DeleteHook(ctx, org, name, id)
}

return err
}
Expand Down
46 changes: 46 additions & 0 deletions source/github/repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,52 @@ func TestGithub_Disable_HooksButNotFound(t *testing.T) {
}
}

func TestGithub_Disable_MultipleHooks(t *testing.T) {
// setup context
gin.SetMode(gin.TestMode)

resp := httptest.NewRecorder()
_, engine := gin.CreateTestContext(resp)
count := 0
wantCount := 2

// setup mock server
engine.GET("/api/v3/repos/:org/:repo/hooks", func(c *gin.Context) {
c.Header("Content-Type", "application/json")
c.Status(http.StatusOK)
c.File("testdata/hooks_multi.json")
})
engine.DELETE("/api/v3/repos/:org/:repo/hooks/:hook_id", func(c *gin.Context) {
count++
c.Status(http.StatusNoContent)
})

s := httptest.NewServer(engine)
defer s.Close()

// setup types
u := new(library.User)
u.SetName("foo")
u.SetToken("bar")

client, _ := NewTest(s.URL, "https://foo.bar.com")

// run test
err := client.Disable(u, "foo", "bar")

if count != wantCount {
t.Errorf("Count returned %d, want %d", count, wantCount)
}

if resp.Code != http.StatusOK {
t.Errorf("Disable returned %v, want %v", resp.Code, http.StatusOK)
}

if err != nil {
t.Errorf("Disable returned err: %v", err)
}
}

func TestGithub_Enable(t *testing.T) {
// setup context
gin.SetMode(gin.TestMode)
Expand Down
59 changes: 59 additions & 0 deletions source/github/testdata/hooks_multi.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
[
{
"id": 1,
"url": "https://api.github.com/repos/foo/bar/hooks/1",
"test_url": "https://api.github.com/repos/foo/bar/hooks/1/test",
"ping_url": "https://api.github.com/repos/foo/bar/hooks/1/pings",
"name": "web",
"events": [
"push",
"pull_request",
"deployment"
],
"active": true,
"config": {
"url": "https://foo.bar.com/webhook",
"content_type": "form"
},
"updated_at": "2011-09-06T20:39:23Z",
"created_at": "2011-09-06T17:26:27Z"
},
{
"id": 2,
"url": "https://api.github.com/repos/foo/bar/hooks/2",
"test_url": "https://api.github.com/repos/foo/bar/hooks/2/test",
"ping_url": "https://api.github.com/repos/foo/bar/hooks/2/pings",
"name": "web",
"events": [
"push",
"pull_request",
"deployment"
],
"active": true,
"config": {
"url": "https://foo.bar.com/webhook",
"content_type": "form"
},
"updated_at": "2011-09-06T20:39:23Z",
"created_at": "2011-09-06T17:26:27Z"
},
{
"id": 3,
"url": "https://api.github.com/repos/foo/bar/hooks/3",
"test_url": "https://api.github.com/repos/foo/bar/hooks/3/test",
"ping_url": "https://api.github.com/repos/foo/bar/hooks/3/pings",
"name": "web",
"events": [
"push",
"pull_request",
"deployment"
],
"active": true,
"config": {
"url": "https://not.foo.bar.com/webhook",
"content_type": "form"
},
"updated_at": "2011-09-06T20:39:23Z",
"created_at": "2011-09-06T17:26:27Z"
}
]

0 comments on commit cb74efc

Please sign in to comment.