Skip to content

Commit

Permalink
Complete System Hook API for GitLab 14.9
Browse files Browse the repository at this point in the history
  • Loading branch information
timofurrer committed Mar 7, 2022
1 parent 98aef99 commit 31d8a74
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 5 deletions.
32 changes: 29 additions & 3 deletions system_hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,14 @@ type SystemHooksService struct {
//
// GitLab API docs: https://docs.gitlab.com/ce/api/system_hooks.html
type Hook struct {
ID int `json:"id"`
URL string `json:"url"`
CreatedAt *time.Time `json:"created_at"`
ID int `json:"id"`
URL string `json:"url"`
CreatedAt *time.Time `json:"created_at"`
PushEvents bool `json:"push_events"`
TagPushEvents bool `json:"tag_push_events"`
MergeRequestsEvents bool `json:"merge_requests_events"`
RepositoryUpdateEvents bool `json:"repository_update_events"`
EnableSSLVerification bool `json:"enable_ssl_verification"`
}

func (h Hook) String() string {
Expand All @@ -62,6 +67,27 @@ func (s *SystemHooksService) ListHooks(options ...RequestOptionFunc) ([]*Hook, *
return h, resp, err
}

// GetHook get a single system hook.
//
// GitLab API docs:
// https://docs.gitlab.com/ce/api/system_hooks.html#get-system-hook
func (s *SystemHooksService) GetHook(hook int, options ...RequestOptionFunc) (*Hook, *Response, error) {
u := fmt.Sprintf("hooks/%d", hook)

req, err := s.client.NewRequest(http.MethodGet, u, nil, options)
if err != nil {
return nil, nil, err
}

var h *Hook
resp, err := s.client.Do(req, &h)
if err != nil {
return nil, resp, err
}

return h, resp, err
}

// AddHookOptions represents the available AddHook() options.
//
// GitLab API docs:
Expand Down
62 changes: 60 additions & 2 deletions system_hooks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,71 @@ func TestSystemHooksService_ListHooks(t *testing.T) {

mux.HandleFunc("/api/v4/hooks", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
fmt.Fprint(w, `[{"id":1,"url":"https://gitlab.example.com/hook"}]`)
fmt.Fprint(w, `
[
{
"id":1,
"url":"https://gitlab.example.com/hook",
"created_at":"2016-10-31T12:32:15.192Z",
"push_events":true,
"tag_push_events":false,
"merge_requests_events": true,
"repository_update_events": true,
"enable_ssl_verification":true
}
]`)
})

hooks, _, err := client.SystemHooks.ListHooks()
require.NoError(t, err)

want := []*Hook{{ID: 1, URL: "https://gitlab.example.com/hook"}}
createdAt := time.Date(2016, 10, 31, 12, 32, 15, 192000000, time.UTC)
want := []*Hook{{
ID: 1,
URL: "https://gitlab.example.com/hook",
CreatedAt: &createdAt,
PushEvents: true,
TagPushEvents: false,
MergeRequestsEvents: true,
RepositoryUpdateEvents: true,
EnableSSLVerification: true,
}}
require.Equal(t, want, hooks)
}

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

mux.HandleFunc("/api/v4/hooks/1", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
fmt.Fprint(w, `
{
"id":1,
"url":"https://gitlab.example.com/hook",
"created_at":"2016-10-31T12:32:15.192Z",
"push_events":true,
"tag_push_events":false,
"merge_requests_events": true,
"repository_update_events": true,
"enable_ssl_verification":true
}`)
})

hooks, _, err := client.SystemHooks.GetHook(1)
require.NoError(t, err)

createdAt := time.Date(2016, 10, 31, 12, 32, 15, 192000000, time.UTC)
want := &Hook{
ID: 1,
URL: "https://gitlab.example.com/hook",
CreatedAt: &createdAt,
PushEvents: true,
TagPushEvents: false,
MergeRequestsEvents: true,
RepositoryUpdateEvents: true,
EnableSSLVerification: true,
}
require.Equal(t, want, hooks)
}

Expand Down

0 comments on commit 31d8a74

Please sign in to comment.