Skip to content
This repository has been archived by the owner on Dec 10, 2024. It is now read-only.

Commit

Permalink
Merge pull request #1831 from ysmilda/main
Browse files Browse the repository at this point in the history
Support deleting labels by label-id
  • Loading branch information
svanharmelen authored Dec 9, 2023
2 parents 6e2f48e + a49cd36 commit 0cb04be
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 9 deletions.
15 changes: 12 additions & 3 deletions group_labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,16 +142,25 @@ func (s *GroupLabelsService) CreateGroupLabel(gid interface{}, opt *CreateGroupL
// 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.
// 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{}, opt *DeleteGroupLabelOptions, options ...RequestOptionFunc) (*Response, error) {
// 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
}
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))
}

req, err := s.client.NewRequest(http.MethodDelete, u, opt, options)
if err != nil {
return nil, err
Expand Down
17 changes: 15 additions & 2 deletions group_labels_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,20 @@ 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", 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) {
Expand All @@ -57,7 +70,7 @@ func TestDeleteGroupLabel(t *testing.T) {
Name: Ptr("My / GroupLabel"),
}

_, err := client.GroupLabels.DeleteGroupLabel("1", label)
_, err := client.GroupLabels.DeleteGroupLabel("1", nil, label)
if err != nil {
log.Fatal(err)
}
Expand Down
12 changes: 10 additions & 2 deletions labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,16 +175,24 @@ type DeleteLabelOptions struct {
Name *string `url:"name,omitempty" json:"name,omitempty"`
}

// DeleteLabel deletes a label given by its name.
// 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{}, opt *DeleteLabelOptions, options ...RequestOptionFunc) (*Response, error) {
func (s *LabelsService) DeleteLabel(pid interface{}, lid interface{}, opt *DeleteLabelOptions, options ...RequestOptionFunc) (*Response, error) {
project, err := parseID(pid)
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))
}

req, err := s.client.NewRequest(http.MethodDelete, u, opt, options)
if err != nil {
return nil, err
Expand Down
18 changes: 16 additions & 2 deletions labels_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,21 @@ 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) {
testMethod(t, r, http.MethodDelete)
})

// Delete label
_, 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) {
Expand All @@ -60,7 +74,7 @@ func TestDeleteLabel(t *testing.T) {
Name: Ptr("My Label"),
}

_, err := client.Labels.DeleteLabel("1", label)
_, err := client.Labels.DeleteLabel("1", nil, label)
if err != nil {
log.Fatal(err)
}
Expand Down

0 comments on commit 0cb04be

Please sign in to comment.