Repository: xanzy/go-gitlab
Pull request #804 (2020-03-23 01:09:51)

add group hook. resolves issue #797
Add support for creating group hooks. 

Also changes casing in EnableSSLVerification so it is consistent with project hook. 


Patch:
diff --git a/group_hooks.go b/group_hooks.go
index 6c92517..c502cd4 100644
--- a/group_hooks.go
+++ b/group_hooks.go
@@ -31,13 +31,14 @@ type GroupHook struct {
 	PushEvents               bool       `json:"push_events"`
 	IssuesEvents             bool       `json:"issues_events"`
 	ConfidentialIssuesEvents bool       `json:"confidential_issues_events"`
+	ConfidentialNoteEvents	 bool       `json:"confidential_note_events"`
 	MergeRequestsEvents      bool       `json:"merge_requests_events"`
 	TagPushEvents            bool       `json:"tag_push_events"`
 	NoteEvents               bool       `json:"note_events"`
 	JobEvents                bool       `json:"job_events"`
 	PipelineEvents           bool       `json:"pipeline_events"`
 	WikiPageEvents           bool       `json:"wiki_page_events"`
-	EnableSslVerification    bool       `json:"enable_ssl_verification"`
+	EnableSSLVerification    bool       `json:"enable_ssl_verification"`
 	CreatedAt                *time.Time `json:"created_at"`
 }
 
@@ -63,3 +64,46 @@ func (s *GroupsService) ListGroupHooks(gid interface{}) ([]*GroupHook, *Response
 
 	return gh, resp, err
 }
+
+// AddGroupHookOptions represents the available AddGroupHook() options.
+//
+// GitLab API docs: https://docs.gitlab.com/ee/api/groups.html#add-group-hook
+type AddGroupHookOptions struct {
+	URL                      *string `url:"url,omitempty" json:"url,omitempty"`
+	PushEvents               *bool   `url:"push_events,omitempty"  json:"push_events,omitempty"`
+	IssuesEvents             *bool   `url:"issues_events,omitempty"  json:"issues_events,omitempty"`
+	ConfidentialIssuesEvents *bool   `url:"confidential_issues_events,omitempty"  json:"confidential_issues_events,omitempty"`
+	ConfidentialNoteEvents   *bool   `url:"confidential_note_events,omitempty"  json:"confidential_note_events,omitempty"`
+	MergeRequestsEvents      *bool   `url:"merge_requests_events,omitempty"  json:"merge_requests_events,omitempty"`
+	TagPushEvents            *bool   `url:"tag_push_events,omitempty"  json:"tag_push_events,omitempty"`
+	NoteEvents               *bool   `url:"note_events,omitempty"  json:"note_events,omitempty"`
+	JobEvents                *bool   `url:"job_events,omitempty"  json:"job_events,omitempty"`
+	PipelineEvents           *bool   `url:"pipeline_events,omitempty"  json:"pipeline_events,omitempty"`
+	WikiPageEvents           *bool   `url:"wiki_page_events,omitempty"  json:"wiki_page_events,omitempty"`
+	EnableSSLVerification    *bool   `url:"enable_ssl_verification,omitempty"  json:"enable_ssl_verification,omitempty"`
+	Token                    *string `url:"token,omitempty" json:"token,omitempty"`
+}
+
+// AddGroupHook create a new group scoped webhook.
+//
+// GitLab API docs: https://docs.gitlab.com/ee/api/groups.html#add-group-hook
+func (s *GroupsService) AddGroupHook(gid interface{}, opt *AddGroupHookOptions, options ...OptionFunc) (*GroupHook, *Response, error) {
+	group, err := parseID(gid)
+	if err != nil {
+		return nil, nil, err
+	}
+	u := fmt.Sprintf("groups/%s/hooks", pathEscape(group))
+
+	req, err := s.client.NewRequest("POST", u, opt, options)
+	if err != nil {
+		return nil, nil, err
+	}
+
+	gh := new(GroupHook)
+	resp, err := s.client.Do(req, gh)
+	if err != nil {
+		return nil, resp, err
+	}
+
+	return gh, resp, err
+}


Test patch:
diff --git a/group_hooks_test.go b/group_hooks_test.go
index 725bb58..4a84846 100644
--- a/group_hooks_test.go
+++ b/group_hooks_test.go
@@ -70,7 +70,7 @@ func TestListGroupHooks(t *testing.T) {
 		JobEvents:                true,
 		PipelineEvents:           true,
 		WikiPageEvents:           true,
-		EnableSslVerification:    true,
+		EnableSSLVerification:    true,
 		CreatedAt:                &datePointer,
 	}}
 
@@ -78,3 +78,62 @@ func TestListGroupHooks(t *testing.T) {
 		t.Errorf("listGroupHooks returned \ngot:\n%v\nwant:\n%v", Stringify(groupHooks), Stringify(want))
 	}
 }
+
+func TestAddGroupHook(t *testing.T) {
+	mux, server, client := setup()
+	defer teardown(server)
+
+	mux.HandleFunc("/api/v4/groups/1/hooks", func(w http.ResponseWriter, r *http.Request) {
+		testMethod(t, r, "POST")
+		fmt.Fprint(w, `
+{
+	"id": 1,
+	"url": "http://example.com/hook",
+	"group_id": 3,
+	"push_events": true,
+	"issues_events": true,
+	"confidential_issues_events": true,
+	"merge_requests_events": true,
+	"tag_push_events": true,
+	"note_events": true,
+	"job_events": true,
+	"pipeline_events": true,
+	"wiki_page_events": true,
+	"enable_ssl_verification": true,
+	"created_at": "2012-10-12T17:04:47Z"
+}`)
+	})
+
+	url := "http://www.example.com/hook"
+	opt := &AddGroupHookOptions{
+		URL: &url,
+	}
+
+	groupHooks, _, err := client.Groups.AddGroupHook(1, opt)
+	if err != nil {
+		t.Error(err)
+	}
+
+	datePointer := time.Date(2012, 10, 12, 17, 4, 47, 0, time.UTC)
+	want := &GroupHook{
+		ID:                       1,
+		URL:                      "http://example.com/hook",
+		GroupID:                  3,
+		PushEvents:               true,
+		IssuesEvents:             true,
+		ConfidentialIssuesEvents: true,
+		ConfidentialNoteEvents:   false,
+		MergeRequestsEvents:      true,
+		TagPushEvents:            true,
+		NoteEvents:               true,
+		JobEvents:                true,
+		PipelineEvents:           true,
+		WikiPageEvents:           true,
+		EnableSSLVerification:    true,
+		CreatedAt:                &datePointer,
+	}
+
+	if !reflect.DeepEqual(groupHooks, want) {
+		t.Errorf("AddGroupHook returned \ngot:\n%v\nwant:\n%v", Stringify(groupHooks), Stringify(want))
+	}
+}