diff --git a/github/repos_hooks.go b/github/repos_hooks.go index a66287f..8468672 100644 --- a/github/repos_hooks.go +++ b/github/repos_hooks.go @@ -175,3 +175,35 @@ func (s *RepositoriesService) TestHook(owner, repo string, id int) (*Response, e } return s.client.Do(req, nil) } + +// ServiceHook represents a hook that has configuration settings, a list of +// available events, and default events. +type ServiceHook struct { + Name *string `json:"name,omitempty"` + Events []string `json:"events,omitempty"` + SupportedEvents []string `json:"supported_events,omitempty"` + Schema [][]string `json:"schema,omitempty"` +} + +func (s *ServiceHook) String() string { + return Stringify(s) +} + +// ListServiceHooks lists all of the available service hooks. +// +// GitHub API docs: https://developer.github.com/webhooks/#services +func (s *RepositoriesService) ListServiceHooks() ([]ServiceHook, *Response, error) { + u := "hooks" + req, err := s.client.NewRequest("GET", u, nil) + if err != nil { + return nil, nil, err + } + + hooks := new([]ServiceHook) + resp, err := s.client.Do(req, hooks) + if err != nil { + return nil, resp, err + } + + return *hooks, resp, err +} diff --git a/github/repos_hooks_test.go b/github/repos_hooks_test.go index 295d382..b322e17 100644 --- a/github/repos_hooks_test.go +++ b/github/repos_hooks_test.go @@ -171,3 +171,35 @@ func TestRepositoriesService_TestHook_invalidOwner(t *testing.T) { _, err := client.Repositories.TestHook("%", "%", 1) testURLParseError(t, err) } + +func TestRepositoriesService_ListServiceHooks(t *testing.T) { + setup() + defer teardown() + + mux.HandleFunc("/hooks", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "GET") + fmt.Fprint(w, `[{ + "name":"n", + "events":["e"], + "supported_events":["s"], + "schema":[ + ["a", "b"] + ] + }]`) + }) + + hooks, _, err := client.Repositories.ListServiceHooks() + if err != nil { + t.Errorf("Repositories.ListHooks returned error: %v", err) + } + + want := []ServiceHook{{ + Name: String("n"), + Events: []string{"e"}, + SupportedEvents: []string{"s"}, + Schema: [][]string{{"a", "b"}}, + }} + if !reflect.DeepEqual(hooks, want) { + t.Errorf("Repositories.ListServiceHooks returned %+v, want %+v", hooks, want) + } +} diff --git a/tests/integration/repos_test.go b/tests/integration/repos_test.go index 42634c9..11c23f3 100644 --- a/tests/integration/repos_test.go +++ b/tests/integration/repos_test.go @@ -95,3 +95,14 @@ func TestRepositories_BranchesTags(t *testing.T) { t.Fatalf("Repositories.ListTags('git', 'git') returned no tags") } } + +func TestRepositories_ServiceHooks(t *testing.T) { + hooks, _, err := client.Repositories.ListServiceHooks() + if err != nil { + t.Fatalf("Repositories.ListServiceHooks() returned error: %v", err) + } + + if len(hooks) == 0 { + t.Fatalf("Repositories.ListServiceHooks() returned no hooks") + } +}