Skip to content

Commit

Permalink
Revert "Removing testhook, edithook, and pinghook from the code and t…
Browse files Browse the repository at this point in the history
…he test file (google#1111)" (google#1115)

This reverts commit cf38b2b.
The version will be bumped.
  • Loading branch information
gmlewis authored and joshuabezaleel committed Feb 15, 2019
1 parent 53d096a commit 35aea75
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 0 deletions.
42 changes: 42 additions & 0 deletions github/repos_hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,24 @@ func (s *RepositoriesService) GetHook(ctx context.Context, owner, repo string, i
return h, resp, nil
}

// EditHook updates a specified Hook.
//
// GitHub API docs: https://developer.github.com/v3/repos/hooks/#edit-a-hook
func (s *RepositoriesService) EditHook(ctx context.Context, owner, repo string, id int64, hook *Hook) (*Hook, *Response, error) {
u := fmt.Sprintf("repos/%v/%v/hooks/%d", owner, repo, id)
req, err := s.client.NewRequest("PATCH", u, hook)
if err != nil {
return nil, nil, err
}
h := new(Hook)
resp, err := s.client.Do(ctx, req, h)
if err != nil {
return nil, resp, err
}

return h, resp, nil
}

// DeleteHook deletes a specified Hook.
//
// GitHub API docs: https://developer.github.com/v3/repos/hooks/#delete-a-hook
Expand All @@ -180,3 +198,27 @@ func (s *RepositoriesService) DeleteHook(ctx context.Context, owner, repo string
}
return s.client.Do(ctx, req, nil)
}

// PingHook triggers a 'ping' event to be sent to the Hook.
//
// GitHub API docs: https://developer.github.com/v3/repos/hooks/#ping-a-hook
func (s *RepositoriesService) PingHook(ctx context.Context, owner, repo string, id int64) (*Response, error) {
u := fmt.Sprintf("repos/%v/%v/hooks/%d/pings", owner, repo, id)
req, err := s.client.NewRequest("POST", u, nil)
if err != nil {
return nil, err
}
return s.client.Do(ctx, req, nil)
}

// TestHook triggers a test Hook by github.
//
// GitHub API docs: https://developer.github.com/v3/repos/hooks/#test-a-push-hook
func (s *RepositoriesService) TestHook(ctx context.Context, owner, repo string, id int64) (*Response, error) {
u := fmt.Sprintf("repos/%v/%v/hooks/%d/tests", owner, repo, id)
req, err := s.client.NewRequest("POST", u, nil)
if err != nil {
return nil, err
}
return s.client.Do(ctx, req, nil)
}
73 changes: 73 additions & 0 deletions github/repos_hooks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,43 @@ func TestRepositoriesService_GetHook_invalidOwner(t *testing.T) {
testURLParseError(t, err)
}

func TestRepositoriesService_EditHook(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()

input := &Hook{}

mux.HandleFunc("/repos/o/r/hooks/1", func(w http.ResponseWriter, r *http.Request) {
v := new(Hook)
json.NewDecoder(r.Body).Decode(v)

testMethod(t, r, "PATCH")
if !reflect.DeepEqual(v, input) {
t.Errorf("Request body = %+v, want %+v", v, input)
}

fmt.Fprint(w, `{"id":1}`)
})

hook, _, err := client.Repositories.EditHook(context.Background(), "o", "r", 1, input)
if err != nil {
t.Errorf("Repositories.EditHook returned error: %v", err)
}

want := &Hook{ID: Int64(1)}
if !reflect.DeepEqual(hook, want) {
t.Errorf("Repositories.EditHook returned %+v, want %+v", hook, want)
}
}

func TestRepositoriesService_EditHook_invalidOwner(t *testing.T) {
client, _, _, teardown := setup()
defer teardown()

_, _, err := client.Repositories.EditHook(context.Background(), "%", "%", 1, nil)
testURLParseError(t, err)
}

func TestRepositoriesService_DeleteHook(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()
Expand All @@ -124,3 +161,39 @@ func TestRepositoriesService_DeleteHook_invalidOwner(t *testing.T) {
_, err := client.Repositories.DeleteHook(context.Background(), "%", "%", 1)
testURLParseError(t, err)
}

func TestRepositoriesService_PingHook(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()

mux.HandleFunc("/repos/o/r/hooks/1/pings", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "POST")
})

_, err := client.Repositories.PingHook(context.Background(), "o", "r", 1)
if err != nil {
t.Errorf("Repositories.PingHook returned error: %v", err)
}
}

func TestRepositoriesService_TestHook(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()

mux.HandleFunc("/repos/o/r/hooks/1/tests", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "POST")
})

_, err := client.Repositories.TestHook(context.Background(), "o", "r", 1)
if err != nil {
t.Errorf("Repositories.TestHook returned error: %v", err)
}
}

func TestRepositoriesService_TestHook_invalidOwner(t *testing.T) {
client, _, _, teardown := setup()
defer teardown()

_, err := client.Repositories.TestHook(context.Background(), "%", "%", 1)
testURLParseError(t, err)
}

0 comments on commit 35aea75

Please sign in to comment.