From ce294e84dde526335c882a70a88294633eb50daf Mon Sep 17 00:00:00 2001 From: James Houston Date: Wed, 9 Oct 2024 22:08:02 -0400 Subject: [PATCH 1/5] Add support for Harbor Integration --- services.go | 97 ++++++++++++++++++++++++++++++++++++++++++++++++ services_test.go | 51 +++++++++++++++++++++++++ 2 files changed, 148 insertions(+) diff --git a/services.go b/services.go index 0059714c7..d9c25fe96 100644 --- a/services.go +++ b/services.go @@ -767,6 +767,103 @@ func (s *ServicesService) DeleteGithubService(pid interface{}, options ...Reques return s.client.Do(req, nil) } +// HarborService represents the Harbor service settings. +// +// GitLab API docs: +// https://docs.gitlab.com/ee/api/integrations.html#harbor +type HarborService struct { + Service + Properties *HarborServiceProperties `json:"properties"` +} + +// HarborServiceProperties represents Harbor specific properties. +// +// GitLab API docs: +// https://docs.gitlab.com/ee/api/integrations.html#harbor +type HarborServiceProperties struct { + URL string `json:"url"` + ProjectName string `json:"project_name"` + Username string `json:"username"` + Password string `json:"password"` + UseInheritedSettings BoolValue `json:"use_inherited_settings"` +} + +// GetHarborService gets Harbor service settings for a project. +// +// GitLab API docs: +// https://docs.gitlab.com/ee/api/integrations.html#get-harbor-settings +func (s *ServicesService) GetHarborService(pid interface{}, options ...RequestOptionFunc) (*HarborService, *Response, error) { + project, err := parseID(pid) + if err != nil { + return nil, nil, err + } + u := fmt.Sprintf("projects/%s/integrations/harbor", PathEscape(project)) + + req, err := s.client.NewRequest(http.MethodGet, u, nil, options) + if err != nil { + return nil, nil, err + } + + svc := new(HarborService) + resp, err := s.client.Do(req, svc) + if err != nil { + return nil, resp, err + } + + return svc, resp, nil +} + +// SetHarborServiceOptions represents the available SetHarborService(). +// options. +// +// GitLab API docs: +// https://docs.gitlab.com/ee/api/integrations.html#set-up-harbor +type SetHarborServiceOptions struct { + URL *string `url:"url,omitempty" json:"url,omitempty"` + ProjectName *string `url:"project_name,omitempty" json:"project_name,omitempty"` + Username *string `url:"username,omitempty" json:"username,omitempty"` + Password *string `url:"password,omitempty" json:"password,omitempty"` + UseInheritedSettings *bool `url:"use_inherited_settings" json:"use_inherited_settings"` +} + +// SetHarborService sets Harbor service for a project. +// +// GitLab API docs: +// https://docs.gitlab.com/ee/api/integrations.html#set-up-harbor +func (s *ServicesService) SetHarborService(pid interface{}, opt *SetHarborServiceOptions, options ...RequestOptionFunc) (*Response, error) { + project, err := parseID(pid) + if err != nil { + return nil, err + } + u := fmt.Sprintf("projects/%s/integrations/harbor", PathEscape(project)) + + req, err := s.client.NewRequest(http.MethodPut, u, opt, options) + if err != nil { + return nil, err + } + + return s.client.Do(req, nil) +} + +// DeleteHarborService deletes Harbor service for a project. +// +// GitLab API docs: +// https://docs.gitlab.com/ee/api/integrations.html#disable-harbor +func (s *ServicesService) DeleteHarborService(pid interface{}, options ...RequestOptionFunc) (*Response, error) { + project, err := parseID(pid) + if err != nil { + return nil, err + } + u := fmt.Sprintf("projects/%s/integrations/harbor", PathEscape(project)) + + req, err := s.client.NewRequest(http.MethodDelete, u, nil, options) + if err != nil { + return nil, err + } + + return s.client.Do(req, nil) +} + // SlackApplication represents GitLab for slack application settings. // // GitLab API docs: diff --git a/services_test.go b/services_test.go index 36614cecf..224406e47 100644 --- a/services_test.go +++ b/services_test.go @@ -327,6 +327,57 @@ func TestDeleteEmailsOnPushService(t *testing.T) { } } +func TestGetHarborService(t *testing.T) { + mux, client := setup(t) + + mux.HandleFunc("/api/v4/projects/1/integrations/harbor", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, http.MethodGet) + fmt.Fprint(w, `{"id":1}`) + }) + want := &HarborService{Service: Service{ID: 1}} + + service, _, err := client.Services.GetHarborService(1) + if err != nil { + t.Fatalf("Services.GetHarborService returns an error: %v", err) + } + if !reflect.DeepEqual(want, service) { + t.Errorf("Services.GetHarborService returned %+v, want %+v", service, want) + } +} + +func TestSetHarborService(t *testing.T) { + mux, client := setup(t) + + mux.HandleFunc("/api/v4/projects/1/integrations/harbor", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, http.MethodPut) + }) + + opt := &SetHarborServiceOptions{ + URL: Ptr("url"), + ProjectName: Ptr("project"), + Username: Ptr("user"), + Password: Ptr("pass"), + } + + _, err := client.Services.SetHarborService(1, opt) + if err != nil { + t.Fatalf("Services.SetHarborService returns an error: %v", err) + } +} + +func TestDeleteHarborService(t *testing.T) { + mux, client := setup(t) + + mux.HandleFunc("/api/v4/projects/1/integrations/harbor", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, http.MethodDelete) + }) + + _, err := client.Services.DeleteHarborService(1) + if err != nil { + t.Fatalf("Services.DeleteHarborService returns an error: %v", err) + } +} + func TestGetSlackApplication(t *testing.T) { mux, client := setup(t) From 66f30b3d17de16f513d09adde784f86857ccb88c Mon Sep 17 00:00:00 2001 From: James Houston Date: Wed, 9 Oct 2024 22:20:07 -0400 Subject: [PATCH 2/5] fix missing omitempty on SetHarborServiceOptions --- services.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services.go b/services.go index d9c25fe96..a8484ee00 100644 --- a/services.go +++ b/services.go @@ -823,7 +823,7 @@ type SetHarborServiceOptions struct { ProjectName *string `url:"project_name,omitempty" json:"project_name,omitempty"` Username *string `url:"username,omitempty" json:"username,omitempty"` Password *string `url:"password,omitempty" json:"password,omitempty"` - UseInheritedSettings *bool `url:"use_inherited_settings" json:"use_inherited_settings"` + UseInheritedSettings *bool `url:"use_inherited_settings,omitempty" json:"use_inherited_settings,omitempty"` } // SetHarborService sets Harbor service for a project. From 3ac57b9e9e677848575511906346d922bc618b1c Mon Sep 17 00:00:00 2001 From: James Houston Date: Wed, 9 Oct 2024 22:28:42 -0400 Subject: [PATCH 3/5] remove extra period --- services.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services.go b/services.go index a8484ee00..ab1a30473 100644 --- a/services.go +++ b/services.go @@ -813,7 +813,7 @@ func (s *ServicesService) GetHarborService(pid interface{}, options ...RequestOp return svc, resp, nil } -// SetHarborServiceOptions represents the available SetHarborService(). +// SetHarborServiceOptions represents the available SetHarborService() // options. // // GitLab API docs: From 1bdf26e781b6dcce3d8aac36c975c6b489a189be Mon Sep 17 00:00:00 2001 From: James Houston Date: Wed, 9 Oct 2024 22:35:34 -0400 Subject: [PATCH 4/5] add missing field to test --- services_test.go | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/services_test.go b/services_test.go index 224406e47..53596eaca 100644 --- a/services_test.go +++ b/services_test.go @@ -353,10 +353,11 @@ func TestSetHarborService(t *testing.T) { }) opt := &SetHarborServiceOptions{ - URL: Ptr("url"), - ProjectName: Ptr("project"), - Username: Ptr("user"), - Password: Ptr("pass"), + URL: Ptr("url"), + ProjectName: Ptr("project"), + Username: Ptr("user"), + Password: Ptr("pass"), + UseInheritedSettings: Ptr(false), } _, err := client.Services.SetHarborService(1, opt) From e6a0bbf23a3b4a746cf66de7c0064f3e843e6dc6 Mon Sep 17 00:00:00 2001 From: James Houston Date: Thu, 10 Oct 2024 12:28:49 -0400 Subject: [PATCH 5/5] change UseInheritedSettings from BoolValue to bool --- services.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/services.go b/services.go index ab1a30473..56eeca86e 100644 --- a/services.go +++ b/services.go @@ -781,11 +781,11 @@ type HarborService struct { // GitLab API docs: // https://docs.gitlab.com/ee/api/integrations.html#harbor type HarborServiceProperties struct { - URL string `json:"url"` - ProjectName string `json:"project_name"` - Username string `json:"username"` - Password string `json:"password"` - UseInheritedSettings BoolValue `json:"use_inherited_settings"` + URL string `json:"url"` + ProjectName string `json:"project_name"` + Username string `json:"username"` + Password string `json:"password"` + UseInheritedSettings bool `json:"use_inherited_settings"` } // GetHarborService gets Harbor service settings for a project.