Skip to content

Commit

Permalink
Merge pull request #2027 from houstonj1/feat/harbor-integration
Browse files Browse the repository at this point in the history
Add support for Harbor Integration
  • Loading branch information
svanharmelen authored Oct 10, 2024
2 parents 8733605 + e6a0bbf commit b5e0812
Show file tree
Hide file tree
Showing 2 changed files with 149 additions and 0 deletions.
97 changes: 97 additions & 0 deletions services.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 bool `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,omitempty" json:"use_inherited_settings,omitempty"`
}

// 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:
Expand Down
52 changes: 52 additions & 0 deletions services_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,58 @@ 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"),
UseInheritedSettings: Ptr(false),
}

_, 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)

Expand Down

0 comments on commit b5e0812

Please sign in to comment.