Verified Commit ce5d7878 authored by Gatla Vishweshwar Reddy's avatar Gatla Vishweshwar Reddy Committed by GitLab
Browse files

feat: add support for group Datadog integration

Changelog: Improvements
parent 2920bc25
Loading
Loading
Loading
Loading
+71 −0
Original line number Diff line number Diff line
@@ -182,3 +182,74 @@ func (s *IntegrationsService) DeleteGroupMattermostSlashCommandsIntegration(gid
	)
	return resp, err
}

// GroupDatadogIntegration represents a Datadog integration for a group.
//
// GitLab API docs:
// https://docs.gitlab.com/api/group_integrations/#datadog
type GroupDatadogIntegration struct {
	Integration
	APIURL             string `json:"api_url"`
	DatadogEnv         string `json:"datadog_env"`
	DatadogService     string `json:"datadog_service"`
	DatadogSite        string `json:"datadog_site"`
	DatadogTags        string `json:"datadog_tags"`
	ArchiveTraceEvents *bool  `json:"archive_trace_events"`
}

// GroupDatadogIntegrationOptions represents the available options for
// creating or updating a Datadog integration for a group.
//
// GitLab API docs:
// https://docs.gitlab.com/api/group_integrations/#datadog
type GroupDatadogIntegrationOptions struct {
	APIKey               *string `url:"api_key,omitempty" json:"api_key,omitempty"`
	APIURL               *string `url:"api_url,omitempty" json:"api_url,omitempty"`
	DatadogEnv           *string `url:"datadog_env,omitempty" json:"datadog_env,omitempty"`
	DatadogService       *string `url:"datadog_service,omitempty" json:"datadog_service,omitempty"`
	DatadogSite          *string `url:"datadog_site,omitempty" json:"datadog_site,omitempty"`
	DatadogTags          *string `url:"datadog_tags,omitempty" json:"datadog_tags,omitempty"`
	ArchiveTraceEvents   *bool   `url:"archive_trace_events,omitempty" json:"archive_trace_events,omitempty"`
	UseInheritedSettings *bool   `url:"use_inherited_settings,omitempty" json:"use_inherited_settings,omitempty"`
}

// GetGroupDatadogIntegration retrieves the Datadog integration for a group.
//
// GitLab API docs:
// https://docs.gitlab.com/api/group_integrations/#datadog
func (s *IntegrationsService) GetGroupDatadogIntegration(gid any, options ...RequestOptionFunc) (*GroupDatadogIntegration, *Response, error) {
	return do[*GroupDatadogIntegration](
		s.client,
		withPath("groups/%s/integrations/datadog", GroupID{gid}),
		withMethod(http.MethodGet),
		withRequestOpts(options...),
	)
}

// SetGroupDatadogIntegration creates or updates the Datadog integration for a group.
//
// GitLab API docs:
// https://docs.gitlab.com/api/group_integrations/#datadog
func (s *IntegrationsService) SetGroupDatadogIntegration(gid any, opt *GroupDatadogIntegrationOptions, options ...RequestOptionFunc) (*GroupDatadogIntegration, *Response, error) {
	return do[*GroupDatadogIntegration](
		s.client,
		withPath("groups/%s/integrations/datadog", GroupID{gid}),
		withMethod(http.MethodPut),
		withAPIOpts(opt),
		withRequestOpts(options...),
	)
}

// DeleteGroupDatadogIntegration removes the Datadog integration from a group.
//
// GitLab API docs:
// https://docs.gitlab.com/api/group_integrations/#datadog
func (s *IntegrationsService) DeleteGroupDatadogIntegration(gid any, options ...RequestOptionFunc) (*Response, error) {
	_, resp, err := do[none](
		s.client,
		withPath("groups/%s/integrations/datadog", GroupID{gid}),
		withMethod(http.MethodDelete),
		withRequestOpts(options...),
	)
	return resp, err
}
+126 −0
Original line number Diff line number Diff line
@@ -268,3 +268,129 @@ func TestDeleteGroupMattermostSlashCommandsIntegration(t *testing.T) {
	assert.NoError(t, err)
	assert.NotNil(t, resp)
}

func TestGetGroupDatadogIntegration(t *testing.T) {
	t.Parallel()
	mux, client := setup(t)

	// GIVEN a group with a Datadog integration
	mux.HandleFunc("/api/v4/groups/1/integrations/datadog", func(w http.ResponseWriter, r *http.Request) {
		testMethod(t, r, http.MethodGet)
		mustWriteJSONResponse(t, w, map[string]any{
			"id":                   1,
			"title":                "Datadog",
			"slug":                 "datadog",
			"created_at":           "2023-01-01T00:00:00.000Z",
			"updated_at":           "2023-01-02T00:00:00.000Z",
			"active":               true,
			"api_url":              "https://api.datadoghq.com",
			"datadog_env":          "production",
			"datadog_service":      "gitlab-production",
			"datadog_site":         "datadoghq.com",
			"datadog_tags":         "env:prod\nteam:platform",
			"archive_trace_events": true,
		})
	})

	// WHEN GetGroupDatadogIntegration is called
	gdi, resp, err := client.Integrations.GetGroupDatadogIntegration(1)

	// THEN the integration is returned without error
	assert.NoError(t, err)
	assert.NotNil(t, resp)

	createdAt, _ := time.Parse(time.RFC3339, "2023-01-01T00:00:00.000Z")
	updatedAt, _ := time.Parse(time.RFC3339, "2023-01-02T00:00:00.000Z")
	archiveTraceEvents := true

	want := &GroupDatadogIntegration{
		Integration: Integration{
			ID:        1,
			Title:     "Datadog",
			Slug:      "datadog",
			CreatedAt: &createdAt,
			UpdatedAt: &updatedAt,
			Active:    true,
		},
		APIURL:             "https://api.datadoghq.com",
		DatadogEnv:         "production",
		DatadogService:     "gitlab-production",
		DatadogSite:        "datadoghq.com",
		DatadogTags:        "env:prod\nteam:platform",
		ArchiveTraceEvents: &archiveTraceEvents,
	}

	assert.Equal(t, want, gdi)
}

func TestSetGroupDatadogIntegration(t *testing.T) {
	t.Parallel()
	mux, client := setup(t)

	// GIVEN a group where we want to set the Datadog integration
	mux.HandleFunc("/api/v4/groups/1/integrations/datadog", func(w http.ResponseWriter, r *http.Request) {
		testMethod(t, r, http.MethodPut)
		mustWriteJSONResponse(t, w, map[string]any{
			"id":                   1,
			"title":                "Datadog",
			"slug":                 "datadog",
			"created_at":           "2023-01-01T00:00:00.000Z",
			"updated_at":           "2023-01-02T00:00:00.000Z",
			"active":               true,
			"api_url":              "https://api.datadoghq.com",
			"datadog_site":         "datadoghq.com",
			"archive_trace_events": true,
		})
	})

	opt := &GroupDatadogIntegrationOptions{
		APIKey:             Ptr("secret-api-key"),
		APIURL:             Ptr("https://api.datadoghq.com"),
		DatadogSite:        Ptr("datadoghq.com"),
		ArchiveTraceEvents: Ptr(true),
	}

	// WHEN SetGroupDatadogIntegration is called
	gdi, resp, err := client.Integrations.SetGroupDatadogIntegration(1, opt)

	// THEN the updated integration is returned without error
	assert.NoError(t, err)
	assert.NotNil(t, resp)

	createdAt, _ := time.Parse(time.RFC3339, "2023-01-01T00:00:00.000Z")
	updatedAt, _ := time.Parse(time.RFC3339, "2023-01-02T00:00:00.000Z")
	archiveTraceEvents := true

	want := &GroupDatadogIntegration{
		Integration: Integration{
			ID:        1,
			Title:     "Datadog",
			Slug:      "datadog",
			CreatedAt: &createdAt,
			UpdatedAt: &updatedAt,
			Active:    true,
		},
		APIURL:             "https://api.datadoghq.com",
		DatadogSite:        "datadoghq.com",
		ArchiveTraceEvents: &archiveTraceEvents,
	}

	assert.Equal(t, want, gdi)
}

func TestDeleteGroupDatadogIntegration(t *testing.T) {
	t.Parallel()
	mux, client := setup(t)

	// GIVEN a group with a Datadog integration
	mux.HandleFunc("/api/v4/groups/1/integrations/datadog", func(w http.ResponseWriter, r *http.Request) {
		testMethod(t, r, http.MethodDelete)
	})

	// WHEN DeleteGroupDatadogIntegration is called
	resp, err := client.Integrations.DeleteGroupDatadogIntegration(1)

	// THEN the integration is deleted without error
	assert.NoError(t, err)
	assert.NotNil(t, resp)
}
+18 −0
Original line number Diff line number Diff line
@@ -27,6 +27,24 @@ type (
		// https://docs.gitlab.com/api/group_integrations/#list-all-active-integrations
		ListActiveGroupIntegrations(gid any, opt *ListActiveIntegrationsOptions, options ...RequestOptionFunc) ([]*Integration, *Response, error)

		// GetGroupDatadogIntegration retrieves the Datadog integration for a group.
		//
		// GitLab API docs:
		// https://docs.gitlab.com/api/group_integrations/#datadog
		GetGroupDatadogIntegration(gid any, options ...RequestOptionFunc) (*GroupDatadogIntegration, *Response, error)

		// SetGroupDatadogIntegration creates or updates the Datadog integration for a group.
		//
		// GitLab API docs:
		// https://docs.gitlab.com/api/group_integrations/#datadog
		SetGroupDatadogIntegration(gid any, opt *GroupDatadogIntegrationOptions, options ...RequestOptionFunc) (*GroupDatadogIntegration, *Response, error)

		// DeleteGroupDatadogIntegration removes the Datadog integration from a group.
		//
		// GitLab API docs:
		// https://docs.gitlab.com/api/group_integrations/#datadog
		DeleteGroupDatadogIntegration(gid any, options ...RequestOptionFunc) (*Response, error)

		// SetUpGroupHarbor sets up the Harbor integration for a group.
		//
		// GitLab API docs:
+134 −0
Original line number Diff line number Diff line
@@ -39,6 +39,50 @@ func (m *MockIntegrationsServiceInterface) EXPECT() *MockIntegrationsServiceInte
	return m.recorder
}

// DeleteGroupDatadogIntegration mocks base method.
func (m *MockIntegrationsServiceInterface) DeleteGroupDatadogIntegration(gid any, options ...gitlab.RequestOptionFunc) (*gitlab.Response, error) {
	m.ctrl.T.Helper()
	varargs := []any{gid}
	for _, a := range options {
		varargs = append(varargs, a)
	}
	ret := m.ctrl.Call(m, "DeleteGroupDatadogIntegration", varargs...)
	ret0, _ := ret[0].(*gitlab.Response)
	ret1, _ := ret[1].(error)
	return ret0, ret1
}

// DeleteGroupDatadogIntegration indicates an expected call of DeleteGroupDatadogIntegration.
func (mr *MockIntegrationsServiceInterfaceMockRecorder) DeleteGroupDatadogIntegration(gid any, options ...any) *MockIntegrationsServiceInterfaceDeleteGroupDatadogIntegrationCall {
	mr.mock.ctrl.T.Helper()
	varargs := append([]any{gid}, options...)
	call := mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DeleteGroupDatadogIntegration", reflect.TypeOf((*MockIntegrationsServiceInterface)(nil).DeleteGroupDatadogIntegration), varargs...)
	return &MockIntegrationsServiceInterfaceDeleteGroupDatadogIntegrationCall{Call: call}
}

// MockIntegrationsServiceInterfaceDeleteGroupDatadogIntegrationCall wrap *gomock.Call
type MockIntegrationsServiceInterfaceDeleteGroupDatadogIntegrationCall struct {
	*gomock.Call
}

// Return rewrite *gomock.Call.Return
func (c *MockIntegrationsServiceInterfaceDeleteGroupDatadogIntegrationCall) Return(arg0 *gitlab.Response, arg1 error) *MockIntegrationsServiceInterfaceDeleteGroupDatadogIntegrationCall {
	c.Call = c.Call.Return(arg0, arg1)
	return c
}

// Do rewrite *gomock.Call.Do
func (c *MockIntegrationsServiceInterfaceDeleteGroupDatadogIntegrationCall) Do(f func(any, ...gitlab.RequestOptionFunc) (*gitlab.Response, error)) *MockIntegrationsServiceInterfaceDeleteGroupDatadogIntegrationCall {
	c.Call = c.Call.Do(f)
	return c
}

// DoAndReturn rewrite *gomock.Call.DoAndReturn
func (c *MockIntegrationsServiceInterfaceDeleteGroupDatadogIntegrationCall) DoAndReturn(f func(any, ...gitlab.RequestOptionFunc) (*gitlab.Response, error)) *MockIntegrationsServiceInterfaceDeleteGroupDatadogIntegrationCall {
	c.Call = c.Call.DoAndReturn(f)
	return c
}

// DeleteGroupMattermostIntegration mocks base method.
func (m *MockIntegrationsServiceInterface) DeleteGroupMattermostIntegration(gid any, options ...gitlab.RequestOptionFunc) (*gitlab.Response, error) {
	m.ctrl.T.Helper()
@@ -391,6 +435,51 @@ func (c *MockIntegrationsServiceInterfaceDisableProjectGoogleChatCall) DoAndRetu
	return c
}

// GetGroupDatadogIntegration mocks base method.
func (m *MockIntegrationsServiceInterface) GetGroupDatadogIntegration(gid any, options ...gitlab.RequestOptionFunc) (*gitlab.GroupDatadogIntegration, *gitlab.Response, error) {
	m.ctrl.T.Helper()
	varargs := []any{gid}
	for _, a := range options {
		varargs = append(varargs, a)
	}
	ret := m.ctrl.Call(m, "GetGroupDatadogIntegration", varargs...)
	ret0, _ := ret[0].(*gitlab.GroupDatadogIntegration)
	ret1, _ := ret[1].(*gitlab.Response)
	ret2, _ := ret[2].(error)
	return ret0, ret1, ret2
}

// GetGroupDatadogIntegration indicates an expected call of GetGroupDatadogIntegration.
func (mr *MockIntegrationsServiceInterfaceMockRecorder) GetGroupDatadogIntegration(gid any, options ...any) *MockIntegrationsServiceInterfaceGetGroupDatadogIntegrationCall {
	mr.mock.ctrl.T.Helper()
	varargs := append([]any{gid}, options...)
	call := mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetGroupDatadogIntegration", reflect.TypeOf((*MockIntegrationsServiceInterface)(nil).GetGroupDatadogIntegration), varargs...)
	return &MockIntegrationsServiceInterfaceGetGroupDatadogIntegrationCall{Call: call}
}

// MockIntegrationsServiceInterfaceGetGroupDatadogIntegrationCall wrap *gomock.Call
type MockIntegrationsServiceInterfaceGetGroupDatadogIntegrationCall struct {
	*gomock.Call
}

// Return rewrite *gomock.Call.Return
func (c *MockIntegrationsServiceInterfaceGetGroupDatadogIntegrationCall) Return(arg0 *gitlab.GroupDatadogIntegration, arg1 *gitlab.Response, arg2 error) *MockIntegrationsServiceInterfaceGetGroupDatadogIntegrationCall {
	c.Call = c.Call.Return(arg0, arg1, arg2)
	return c
}

// Do rewrite *gomock.Call.Do
func (c *MockIntegrationsServiceInterfaceGetGroupDatadogIntegrationCall) Do(f func(any, ...gitlab.RequestOptionFunc) (*gitlab.GroupDatadogIntegration, *gitlab.Response, error)) *MockIntegrationsServiceInterfaceGetGroupDatadogIntegrationCall {
	c.Call = c.Call.Do(f)
	return c
}

// DoAndReturn rewrite *gomock.Call.DoAndReturn
func (c *MockIntegrationsServiceInterfaceGetGroupDatadogIntegrationCall) DoAndReturn(f func(any, ...gitlab.RequestOptionFunc) (*gitlab.GroupDatadogIntegration, *gitlab.Response, error)) *MockIntegrationsServiceInterfaceGetGroupDatadogIntegrationCall {
	c.Call = c.Call.DoAndReturn(f)
	return c
}

// GetGroupDiscordSettings mocks base method.
func (m *MockIntegrationsServiceInterface) GetGroupDiscordSettings(gid any, options ...gitlab.RequestOptionFunc) (*gitlab.DiscordIntegration, *gitlab.Response, error) {
	m.ctrl.T.Helper()
@@ -1021,6 +1110,51 @@ func (c *MockIntegrationsServiceInterfaceListActiveGroupIntegrationsCall) DoAndR
	return c
}

// SetGroupDatadogIntegration mocks base method.
func (m *MockIntegrationsServiceInterface) SetGroupDatadogIntegration(gid any, opt *gitlab.GroupDatadogIntegrationOptions, options ...gitlab.RequestOptionFunc) (*gitlab.GroupDatadogIntegration, *gitlab.Response, error) {
	m.ctrl.T.Helper()
	varargs := []any{gid, opt}
	for _, a := range options {
		varargs = append(varargs, a)
	}
	ret := m.ctrl.Call(m, "SetGroupDatadogIntegration", varargs...)
	ret0, _ := ret[0].(*gitlab.GroupDatadogIntegration)
	ret1, _ := ret[1].(*gitlab.Response)
	ret2, _ := ret[2].(error)
	return ret0, ret1, ret2
}

// SetGroupDatadogIntegration indicates an expected call of SetGroupDatadogIntegration.
func (mr *MockIntegrationsServiceInterfaceMockRecorder) SetGroupDatadogIntegration(gid, opt any, options ...any) *MockIntegrationsServiceInterfaceSetGroupDatadogIntegrationCall {
	mr.mock.ctrl.T.Helper()
	varargs := append([]any{gid, opt}, options...)
	call := mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SetGroupDatadogIntegration", reflect.TypeOf((*MockIntegrationsServiceInterface)(nil).SetGroupDatadogIntegration), varargs...)
	return &MockIntegrationsServiceInterfaceSetGroupDatadogIntegrationCall{Call: call}
}

// MockIntegrationsServiceInterfaceSetGroupDatadogIntegrationCall wrap *gomock.Call
type MockIntegrationsServiceInterfaceSetGroupDatadogIntegrationCall struct {
	*gomock.Call
}

// Return rewrite *gomock.Call.Return
func (c *MockIntegrationsServiceInterfaceSetGroupDatadogIntegrationCall) Return(arg0 *gitlab.GroupDatadogIntegration, arg1 *gitlab.Response, arg2 error) *MockIntegrationsServiceInterfaceSetGroupDatadogIntegrationCall {
	c.Call = c.Call.Return(arg0, arg1, arg2)
	return c
}

// Do rewrite *gomock.Call.Do
func (c *MockIntegrationsServiceInterfaceSetGroupDatadogIntegrationCall) Do(f func(any, *gitlab.GroupDatadogIntegrationOptions, ...gitlab.RequestOptionFunc) (*gitlab.GroupDatadogIntegration, *gitlab.Response, error)) *MockIntegrationsServiceInterfaceSetGroupDatadogIntegrationCall {
	c.Call = c.Call.Do(f)
	return c
}

// DoAndReturn rewrite *gomock.Call.DoAndReturn
func (c *MockIntegrationsServiceInterfaceSetGroupDatadogIntegrationCall) DoAndReturn(f func(any, *gitlab.GroupDatadogIntegrationOptions, ...gitlab.RequestOptionFunc) (*gitlab.GroupDatadogIntegration, *gitlab.Response, error)) *MockIntegrationsServiceInterfaceSetGroupDatadogIntegrationCall {
	c.Call = c.Call.DoAndReturn(f)
	return c
}

// SetGroupMattermostIntegration mocks base method.
func (m *MockIntegrationsServiceInterface) SetGroupMattermostIntegration(gid any, opt *gitlab.GroupMattermostIntegrationOptions, options ...gitlab.RequestOptionFunc) (*gitlab.GroupMattermostIntegration, *gitlab.Response, error) {
	m.ctrl.T.Helper()