From cf6caff1a8496291c8c28f712e6017a6de2255e5 Mon Sep 17 00:00:00 2001 From: Yorick Smilda Date: Fri, 8 Dec 2023 15:03:08 +0100 Subject: [PATCH 1/2] Support deleting labels by label-id --- group_labels.go | 16 +++++++--------- group_labels_test.go | 8 ++------ labels.go | 19 ++++++++----------- labels_test.go | 8 ++------ 4 files changed, 19 insertions(+), 32 deletions(-) diff --git a/group_labels.go b/group_labels.go index d19e3ed5a..8f9872640 100644 --- a/group_labels.go +++ b/group_labels.go @@ -136,23 +136,21 @@ func (s *GroupLabelsService) CreateGroupLabel(gid interface{}, opt *CreateGroupL return l, resp, nil } -// DeleteGroupLabelOptions represents the available DeleteGroupLabel() options. -// -// GitLab API docs: -// https://docs.gitlab.com/ee/api/group_labels.html#delete-a-group-label -type DeleteGroupLabelOptions DeleteLabelOptions - // DeleteGroupLabel deletes a group label given by its name. // // GitLab API docs: https://docs.gitlab.com/ee/api/group_labels.html#delete-a-group-label -func (s *GroupLabelsService) DeleteGroupLabel(gid interface{}, opt *DeleteGroupLabelOptions, options ...RequestOptionFunc) (*Response, error) { +func (s *GroupLabelsService) DeleteGroupLabel(gid interface{}, labelID interface{}, options ...RequestOptionFunc) (*Response, error) { group, err := parseID(gid) if err != nil { return nil, err } - u := fmt.Sprintf("groups/%s/labels", PathEscape(group)) + label, err := parseID(labelID) + if err != nil { + return nil, err + } + u := fmt.Sprintf("groups/%s/labels/%s", PathEscape(group), PathEscape(label)) - req, err := s.client.NewRequest(http.MethodDelete, u, opt, options) + req, err := s.client.NewRequest(http.MethodDelete, u, nil, options) if err != nil { return nil, err } diff --git a/group_labels_test.go b/group_labels_test.go index c79ab7824..72e786526 100644 --- a/group_labels_test.go +++ b/group_labels_test.go @@ -49,15 +49,11 @@ func TestCreateGroupGroupLabel(t *testing.T) { func TestDeleteGroupLabel(t *testing.T) { mux, client := setup(t) - mux.HandleFunc("/api/v4/groups/1/labels", func(w http.ResponseWriter, r *http.Request) { + mux.HandleFunc("/api/v4/groups/1/labels/1", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, http.MethodDelete) }) - label := &DeleteGroupLabelOptions{ - Name: Ptr("My / GroupLabel"), - } - - _, err := client.GroupLabels.DeleteGroupLabel("1", label) + _, err := client.GroupLabels.DeleteGroupLabel("1", "1") if err != nil { log.Fatal(err) } diff --git a/labels.go b/labels.go index 42fc778c9..f7c7fd9f5 100644 --- a/labels.go +++ b/labels.go @@ -168,24 +168,21 @@ func (s *LabelsService) CreateLabel(pid interface{}, opt *CreateLabelOptions, op return l, resp, nil } -// DeleteLabelOptions represents the available DeleteLabel() options. +// DeleteLabel deletes a label given by its id. // // GitLab API docs: https://docs.gitlab.com/ee/api/labels.html#delete-a-label -type DeleteLabelOptions struct { - Name *string `url:"name,omitempty" json:"name,omitempty"` -} - -// DeleteLabel deletes a label given by its name. -// -// GitLab API docs: https://docs.gitlab.com/ee/api/labels.html#delete-a-label -func (s *LabelsService) DeleteLabel(pid interface{}, opt *DeleteLabelOptions, options ...RequestOptionFunc) (*Response, error) { +func (s *LabelsService) DeleteLabel(pid interface{}, labelID interface{}, options ...RequestOptionFunc) (*Response, error) { project, err := parseID(pid) if err != nil { return nil, err } - u := fmt.Sprintf("projects/%s/labels", PathEscape(project)) + label, err := parseID(labelID) + if err != nil { + return nil, err + } + u := fmt.Sprintf("projects/%s/labels/%s", PathEscape(project), PathEscape(label)) - req, err := s.client.NewRequest(http.MethodDelete, u, opt, options) + req, err := s.client.NewRequest(http.MethodDelete, u, nil, options) if err != nil { return nil, err } diff --git a/labels_test.go b/labels_test.go index 3abfe3aa4..8d1d51006 100644 --- a/labels_test.go +++ b/labels_test.go @@ -51,16 +51,12 @@ func TestCreateLabel(t *testing.T) { func TestDeleteLabel(t *testing.T) { mux, client := setup(t) - mux.HandleFunc("/api/v4/projects/1/labels", func(w http.ResponseWriter, r *http.Request) { + mux.HandleFunc("/api/v4/projects/1/labels/1", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, http.MethodDelete) }) // Delete label - label := &DeleteLabelOptions{ - Name: Ptr("My Label"), - } - - _, err := client.Labels.DeleteLabel("1", label) + _, err := client.Labels.DeleteLabel("1", "1") if err != nil { log.Fatal(err) } From a49cd36f2b5aa4eeabb66db7611bdec2326f4188 Mon Sep 17 00:00:00 2001 From: Sander van Harmelen Date: Sat, 9 Dec 2023 18:19:58 +0100 Subject: [PATCH 2/2] Make it backwards compatible for now --- group_labels.go | 27 +++++++++++++++++++-------- group_labels_test.go | 21 +++++++++++++++++++-- labels.go | 25 ++++++++++++++++++------- labels_test.go | 22 ++++++++++++++++++++-- 4 files changed, 76 insertions(+), 19 deletions(-) diff --git a/group_labels.go b/group_labels.go index 8f9872640..5a390269b 100644 --- a/group_labels.go +++ b/group_labels.go @@ -136,21 +136,32 @@ func (s *GroupLabelsService) CreateGroupLabel(gid interface{}, opt *CreateGroupL return l, resp, nil } -// DeleteGroupLabel deletes a group label given by its name. +// DeleteGroupLabelOptions represents the available DeleteGroupLabel() options. // -// GitLab API docs: https://docs.gitlab.com/ee/api/group_labels.html#delete-a-group-label -func (s *GroupLabelsService) DeleteGroupLabel(gid interface{}, labelID interface{}, options ...RequestOptionFunc) (*Response, error) { +// GitLab API docs: +// https://docs.gitlab.com/ee/api/group_labels.html#delete-a-group-label +type DeleteGroupLabelOptions DeleteLabelOptions + +// DeleteGroupLabel deletes a group label given by its name or ID. +// +// GitLab API docs: +// https://docs.gitlab.com/ee/api/group_labels.html#delete-a-group-label +func (s *GroupLabelsService) DeleteGroupLabel(gid interface{}, lid interface{}, opt *DeleteGroupLabelOptions, options ...RequestOptionFunc) (*Response, error) { group, err := parseID(gid) if err != nil { return nil, err } - label, err := parseID(labelID) - if err != nil { - return nil, err + u := fmt.Sprintf("groups/%s/labels", PathEscape(group)) + + if lid != nil { + label, err := parseID(lid) + if err != nil { + return nil, err + } + u = fmt.Sprintf("groups/%s/labels/%s", PathEscape(group), PathEscape(label)) } - u := fmt.Sprintf("groups/%s/labels/%s", PathEscape(group), PathEscape(label)) - req, err := s.client.NewRequest(http.MethodDelete, u, nil, options) + req, err := s.client.NewRequest(http.MethodDelete, u, opt, options) if err != nil { return nil, err } diff --git a/group_labels_test.go b/group_labels_test.go index 72e786526..42c2a326e 100644 --- a/group_labels_test.go +++ b/group_labels_test.go @@ -46,14 +46,31 @@ func TestCreateGroupGroupLabel(t *testing.T) { } } -func TestDeleteGroupLabel(t *testing.T) { +func TestDeleteGroupLabelByID(t *testing.T) { mux, client := setup(t) mux.HandleFunc("/api/v4/groups/1/labels/1", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, http.MethodDelete) }) - _, err := client.GroupLabels.DeleteGroupLabel("1", "1") + _, err := client.GroupLabels.DeleteGroupLabel("1", "1", nil) + if err != nil { + log.Fatal(err) + } +} + +func TestDeleteGroupLabelByName(t *testing.T) { + mux, client := setup(t) + + mux.HandleFunc("/api/v4/groups/1/labels", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, http.MethodDelete) + }) + + label := &DeleteGroupLabelOptions{ + Name: Ptr("My / GroupLabel"), + } + + _, err := client.GroupLabels.DeleteGroupLabel("1", nil, label) if err != nil { log.Fatal(err) } diff --git a/labels.go b/labels.go index f7c7fd9f5..d36e85b08 100644 --- a/labels.go +++ b/labels.go @@ -168,21 +168,32 @@ func (s *LabelsService) CreateLabel(pid interface{}, opt *CreateLabelOptions, op return l, resp, nil } -// DeleteLabel deletes a label given by its id. +// DeleteLabelOptions represents the available DeleteLabel() options. // // GitLab API docs: https://docs.gitlab.com/ee/api/labels.html#delete-a-label -func (s *LabelsService) DeleteLabel(pid interface{}, labelID interface{}, options ...RequestOptionFunc) (*Response, error) { +type DeleteLabelOptions struct { + Name *string `url:"name,omitempty" json:"name,omitempty"` +} + +// DeleteLabel deletes a label given by its name or ID. +// +// GitLab API docs: https://docs.gitlab.com/ee/api/labels.html#delete-a-label +func (s *LabelsService) DeleteLabel(pid interface{}, lid interface{}, opt *DeleteLabelOptions, options ...RequestOptionFunc) (*Response, error) { project, err := parseID(pid) if err != nil { return nil, err } - label, err := parseID(labelID) - if err != nil { - return nil, err + u := fmt.Sprintf("projects/%s/labels", PathEscape(project)) + + if lid != nil { + label, err := parseID(lid) + if err != nil { + return nil, err + } + u = fmt.Sprintf("projects/%s/labels/%s", PathEscape(project), PathEscape(label)) } - u := fmt.Sprintf("projects/%s/labels/%s", PathEscape(project), PathEscape(label)) - req, err := s.client.NewRequest(http.MethodDelete, u, nil, options) + req, err := s.client.NewRequest(http.MethodDelete, u, opt, options) if err != nil { return nil, err } diff --git a/labels_test.go b/labels_test.go index 8d1d51006..81260b3e0 100644 --- a/labels_test.go +++ b/labels_test.go @@ -48,7 +48,7 @@ func TestCreateLabel(t *testing.T) { } } -func TestDeleteLabel(t *testing.T) { +func TestDeleteLabelbyID(t *testing.T) { mux, client := setup(t) mux.HandleFunc("/api/v4/projects/1/labels/1", func(w http.ResponseWriter, r *http.Request) { @@ -56,7 +56,25 @@ func TestDeleteLabel(t *testing.T) { }) // Delete label - _, err := client.Labels.DeleteLabel("1", "1") + _, err := client.Labels.DeleteLabel("1", "1", nil) + if err != nil { + log.Fatal(err) + } +} + +func TestDeleteLabelbyName(t *testing.T) { + mux, client := setup(t) + + mux.HandleFunc("/api/v4/projects/1/labels", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, http.MethodDelete) + }) + + // Delete label + label := &DeleteLabelOptions{ + Name: Ptr("My Label"), + } + + _, err := client.Labels.DeleteLabel("1", nil, label) if err != nil { log.Fatal(err) }