Repository: xanzy/go-gitlab
Pull request #1402 (2022-03-07 09:58:48)

Complete System Hook API for GitLab 14.9
Required for:

* https://github.com/gitlabhq/terraform-provider-gitlab/issues/180


Patch:
diff --git a/system_hooks.go b/system_hooks.go
index bc496fd..006f593 100644
--- a/system_hooks.go
+++ b/system_hooks.go
@@ -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 {
@@ -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:


Test patch:
diff --git a/system_hooks_test.go b/system_hooks_test.go
index 3d96020..62d0321 100644
--- a/system_hooks_test.go
+++ b/system_hooks_test.go
@@ -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)
 }