Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support deleting labels by label-id #1831

Merged
merged 2 commits into from
Dec 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading