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

Commit

Permalink
Fix the update labels method
Browse files Browse the repository at this point in the history
  • Loading branch information
svanharmelen committed Oct 28, 2024
1 parent 098437b commit d20355a
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 43 deletions.
41 changes: 31 additions & 10 deletions group_labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,12 @@ func (s *GroupLabelsService) ListGroupLabels(gid interface{}, opt *ListGroupLabe
//
// GitLab API docs:
// https://docs.gitlab.com/ee/api/group_labels.html#get-a-single-group-label
func (s *GroupLabelsService) GetGroupLabel(gid interface{}, labelID interface{}, options ...RequestOptionFunc) (*GroupLabel, *Response, error) {
func (s *GroupLabelsService) GetGroupLabel(gid interface{}, lid interface{}, options ...RequestOptionFunc) (*GroupLabel, *Response, error) {
group, err := parseID(gid)
if err != nil {
return nil, nil, err
}
label, err := parseID(labelID)
label, err := parseID(lid)
if err != nil {
return nil, nil, err
}
Expand All @@ -108,7 +108,12 @@ func (s *GroupLabelsService) GetGroupLabel(gid interface{}, labelID interface{},
//
// GitLab API docs:
// https://docs.gitlab.com/ee/api/group_labels.html#create-a-new-group-label
type CreateGroupLabelOptions CreateLabelOptions
type CreateGroupLabelOptions struct {
Name *string `url:"name,omitempty" json:"name,omitempty"`
Color *string `url:"color,omitempty" json:"color,omitempty"`
Description *string `url:"description,omitempty" json:"description,omitempty"`
Priority *int `url:"priority,omitempty" json:"priority,omitempty"`
}

// CreateGroupLabel creates a new label for given group with given name and
// color.
Expand Down Expand Up @@ -140,7 +145,9 @@ func (s *GroupLabelsService) CreateGroupLabel(gid interface{}, opt *CreateGroupL
//
// GitLab API docs:
// https://docs.gitlab.com/ee/api/group_labels.html#delete-a-group-label
type DeleteGroupLabelOptions DeleteLabelOptions
type DeleteGroupLabelOptions struct {
Name *string `url:"name,omitempty" json:"name,omitempty"`
}

// DeleteGroupLabel deletes a group label given by its name or ID.
//
Expand Down Expand Up @@ -173,20 +180,34 @@ func (s *GroupLabelsService) DeleteGroupLabel(gid interface{}, lid interface{},
//
// GitLab API docs:
// https://docs.gitlab.com/ee/api/group_labels.html#update-a-group-label
type UpdateGroupLabelOptions UpdateLabelOptions
type UpdateGroupLabelOptions struct {
Name *string `url:"name,omitempty" json:"name,omitempty"`
NewName *string `url:"new_name,omitempty" json:"new_name,omitempty"`
Color *string `url:"color,omitempty" json:"color,omitempty"`
Description *string `url:"description,omitempty" json:"description,omitempty"`
Priority *int `url:"priority,omitempty" json:"priority,omitempty"`
}

// UpdateGroupLabel updates an existing label with new name or now color. At least
// one parameter is required, to update the label.
//
// GitLab API docs:
// https://docs.gitlab.com/ee/api/group_labels.html#update-a-group-label
func (s *GroupLabelsService) UpdateGroupLabel(gid interface{}, opt *UpdateGroupLabelOptions, options ...RequestOptionFunc) (*GroupLabel, *Response, error) {
func (s *GroupLabelsService) UpdateGroupLabel(gid interface{}, lid interface{}, opt *UpdateGroupLabelOptions, options ...RequestOptionFunc) (*GroupLabel, *Response, error) {
group, err := parseID(gid)
if err != nil {
return nil, nil, err
}
u := fmt.Sprintf("groups/%s/labels", PathEscape(group))

if lid != nil {
label, err := parseID(lid)
if err != nil {
return nil, nil, err
}
u = fmt.Sprintf("groups/%s/labels/%s", PathEscape(group), PathEscape(label))
}

req, err := s.client.NewRequest(http.MethodPut, u, opt, options)
if err != nil {
return nil, nil, err
Expand All @@ -207,12 +228,12 @@ func (s *GroupLabelsService) UpdateGroupLabel(gid interface{}, opt *UpdateGroupL
//
// GitLab API docs:
// https://docs.gitlab.com/ee/api/group_labels.html#subscribe-to-a-group-label
func (s *GroupLabelsService) SubscribeToGroupLabel(gid interface{}, labelID interface{}, options ...RequestOptionFunc) (*GroupLabel, *Response, error) {
func (s *GroupLabelsService) SubscribeToGroupLabel(gid interface{}, lid interface{}, options ...RequestOptionFunc) (*GroupLabel, *Response, error) {
group, err := parseID(gid)
if err != nil {
return nil, nil, err
}
label, err := parseID(labelID)
label, err := parseID(lid)
if err != nil {
return nil, nil, err
}
Expand All @@ -238,12 +259,12 @@ func (s *GroupLabelsService) SubscribeToGroupLabel(gid interface{}, labelID inte
//
// GitLab API docs:
// https://docs.gitlab.com/ee/api/group_labels.html#unsubscribe-from-a-group-label
func (s *GroupLabelsService) UnsubscribeFromGroupLabel(gid interface{}, labelID interface{}, options ...RequestOptionFunc) (*Response, error) {
func (s *GroupLabelsService) UnsubscribeFromGroupLabel(gid interface{}, lid interface{}, options ...RequestOptionFunc) (*Response, error) {
group, err := parseID(gid)
if err != nil {
return nil, err
}
label, err := parseID(labelID)
label, err := parseID(lid)
if err != nil {
return nil, err
}
Expand Down
25 changes: 10 additions & 15 deletions group_labels_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,18 @@ func TestCreateGroupGroupLabel(t *testing.T) {

mux.HandleFunc("/api/v4/groups/1/labels", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodPost)
fmt.Fprint(w, `{"id":1, "name": "My / GroupLabel", "color" : "#11FF22"}`)
fmt.Fprint(w, `{"id":1, "name": "MyGroupLabel", "color" : "#11FF22"}`)
})

l := &CreateGroupLabelOptions{
Name: Ptr("My / GroupLabel"),
Name: Ptr("MyGroupLabel"),
Color: Ptr("#11FF22"),
}
label, _, err := client.GroupLabels.CreateGroupLabel("1", l)
if err != nil {
log.Fatal(err)
}
want := &GroupLabel{ID: 1, Name: "My / GroupLabel", Color: "#11FF22"}
want := &GroupLabel{ID: 1, Name: "MyGroupLabel", Color: "#11FF22"}
if !reflect.DeepEqual(want, label) {
t.Errorf("GroupLabels.CreateGroupLabel returned %+v, want %+v", label, want)
}
Expand All @@ -62,15 +62,11 @@ func TestDeleteGroupLabelByID(t *testing.T) {
func TestDeleteGroupLabelByName(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/MyGroupLabel", 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)
_, err := client.GroupLabels.DeleteGroupLabel("1", "MyGroupLabel", nil)
if err != nil {
log.Fatal(err)
}
Expand All @@ -79,19 +75,18 @@ func TestDeleteGroupLabelByName(t *testing.T) {
func TestUpdateGroupLabel(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/MyGroupLabel", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodPut)
fmt.Fprint(w, `{"id":1, "name": "New / GroupLabel", "color" : "#11FF23" , "description":"This is updated label"}`)
fmt.Fprint(w, `{"id":1, "name": "NewLabel", "color" : "#11FF23" , "description":"This is updated label"}`)
})

l := &UpdateGroupLabelOptions{
Name: Ptr("My / GroupLabel"),
NewName: Ptr("New / GroupLabel"),
NewName: Ptr("NewLabel"),
Color: Ptr("#11FF23"),
Description: Ptr("This is updated label"),
}

label, resp, err := client.GroupLabels.UpdateGroupLabel("1", l)
label, resp, err := client.GroupLabels.UpdateGroupLabel("1", "MyGroupLabel", l)

if resp == nil {
log.Fatal(err)
Expand All @@ -100,7 +95,7 @@ func TestUpdateGroupLabel(t *testing.T) {
log.Fatal(err)
}

want := &GroupLabel{ID: 1, Name: "New / GroupLabel", Color: "#11FF23", Description: "This is updated label"}
want := &GroupLabel{ID: 1, Name: "NewLabel", Color: "#11FF23", Description: "This is updated label"}

if !reflect.DeepEqual(want, label) {
t.Errorf("GroupLabels.UpdateGroupLabel returned %+v, want %+v", label, want)
Expand Down
26 changes: 17 additions & 9 deletions labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,12 @@ func (s *LabelsService) ListLabels(pid interface{}, opt *ListLabelsOptions, opti
// GetLabel get a single label for a given project.
//
// GitLab API docs: https://docs.gitlab.com/ee/api/labels.html#get-a-single-project-label
func (s *LabelsService) GetLabel(pid interface{}, labelID interface{}, options ...RequestOptionFunc) (*Label, *Response, error) {
func (s *LabelsService) GetLabel(pid interface{}, lid interface{}, options ...RequestOptionFunc) (*Label, *Response, error) {
project, err := parseID(pid)
if err != nil {
return nil, nil, err
}
label, err := parseID(labelID)
label, err := parseID(lid)
if err != nil {
return nil, nil, err
}
Expand Down Expand Up @@ -216,13 +216,21 @@ type UpdateLabelOptions struct {
// one parameter is required, to update the label.
//
// GitLab API docs: https://docs.gitlab.com/ee/api/labels.html#edit-an-existing-label
func (s *LabelsService) UpdateLabel(pid interface{}, opt *UpdateLabelOptions, options ...RequestOptionFunc) (*Label, *Response, error) {
func (s *LabelsService) UpdateLabel(pid interface{}, lid interface{}, opt *UpdateLabelOptions, options ...RequestOptionFunc) (*Label, *Response, error) {
project, err := parseID(pid)
if err != nil {
return nil, nil, err
}
u := fmt.Sprintf("projects/%s/labels", PathEscape(project))

if lid != nil {
label, err := parseID(lid)
if err != nil {
return nil, nil, err
}
u = fmt.Sprintf("projects/%s/labels/%s", PathEscape(project), PathEscape(label))
}

req, err := s.client.NewRequest(http.MethodPut, u, opt, options)
if err != nil {
return nil, nil, err
Expand All @@ -243,12 +251,12 @@ func (s *LabelsService) UpdateLabel(pid interface{}, opt *UpdateLabelOptions, op
//
// GitLab API docs:
// https://docs.gitlab.com/ee/api/labels.html#subscribe-to-a-label
func (s *LabelsService) SubscribeToLabel(pid interface{}, labelID interface{}, options ...RequestOptionFunc) (*Label, *Response, error) {
func (s *LabelsService) SubscribeToLabel(pid interface{}, lid interface{}, options ...RequestOptionFunc) (*Label, *Response, error) {
project, err := parseID(pid)
if err != nil {
return nil, nil, err
}
label, err := parseID(labelID)
label, err := parseID(lid)
if err != nil {
return nil, nil, err
}
Expand All @@ -274,12 +282,12 @@ func (s *LabelsService) SubscribeToLabel(pid interface{}, labelID interface{}, o
//
// GitLab API docs:
// https://docs.gitlab.com/ee/api/labels.html#unsubscribe-from-a-label
func (s *LabelsService) UnsubscribeFromLabel(pid interface{}, labelID interface{}, options ...RequestOptionFunc) (*Response, error) {
func (s *LabelsService) UnsubscribeFromLabel(pid interface{}, lid interface{}, options ...RequestOptionFunc) (*Response, error) {
project, err := parseID(pid)
if err != nil {
return nil, err
}
label, err := parseID(labelID)
label, err := parseID(lid)
if err != nil {
return nil, err
}
Expand All @@ -297,12 +305,12 @@ func (s *LabelsService) UnsubscribeFromLabel(pid interface{}, labelID interface{
//
// GitLab API docs:
// https://docs.gitlab.com/ee/api/labels.html#promote-a-project-label-to-a-group-label
func (s *LabelsService) PromoteLabel(pid interface{}, labelID interface{}, options ...RequestOptionFunc) (*Response, error) {
func (s *LabelsService) PromoteLabel(pid interface{}, lid interface{}, options ...RequestOptionFunc) (*Response, error) {
project, err := parseID(pid)
if err != nil {
return nil, err
}
label, err := parseID(labelID)
label, err := parseID(lid)
if err != nil {
return nil, err
}
Expand Down
17 changes: 8 additions & 9 deletions labels_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,20 @@ func TestCreateLabel(t *testing.T) {

mux.HandleFunc("/api/v4/projects/1/labels", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodPost)
fmt.Fprint(w, `{"id":1, "name": "My Label", "color" : "#11FF22", "priority": 2}`)
fmt.Fprint(w, `{"id":1, "name": "MyLabel", "color" : "#11FF22", "priority": 2}`)
})

// Create new label
l := &CreateLabelOptions{
Name: Ptr("My Label"),
Name: Ptr("MyLabel"),
Color: Ptr("#11FF22"),
Priority: Ptr(2),
}
label, _, err := client.Labels.CreateLabel("1", l)
if err != nil {
log.Fatal(err)
}
want := &Label{ID: 1, Name: "My Label", Color: "#11FF22", Priority: 2}
want := &Label{ID: 1, Name: "MyLabel", Color: "#11FF22", Priority: 2}
if !reflect.DeepEqual(want, label) {
t.Errorf("Labels.CreateLabel returned %+v, want %+v", label, want)
}
Expand All @@ -65,16 +65,16 @@ func TestDeleteLabelbyID(t *testing.T) {
func TestDeleteLabelbyName(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/MyLabel", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodDelete)
})

// Delete label
label := &DeleteLabelOptions{
Name: Ptr("My Label"),
Name: Ptr("MyLabel"),
}

_, err := client.Labels.DeleteLabel("1", nil, label)
_, err := client.Labels.DeleteLabel("1", "MyLabel", label)
if err != nil {
log.Fatal(err)
}
Expand All @@ -83,21 +83,20 @@ func TestDeleteLabelbyName(t *testing.T) {
func TestUpdateLabel(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/MyLabel", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodPut)
fmt.Fprint(w, `{"id":1, "name": "New Label", "color" : "#11FF23" , "description":"This is updated label", "priority": 42}`)
})

// Update label
l := &UpdateLabelOptions{
Name: Ptr("My Label"),
NewName: Ptr("New Label"),
Color: Ptr("#11FF23"),
Description: Ptr("This is updated label"),
Priority: Ptr(42),
}

label, resp, err := client.Labels.UpdateLabel("1", l)
label, resp, err := client.Labels.UpdateLabel("1", "MyLabel", l)

if resp == nil {
log.Fatal(err)
Expand Down

0 comments on commit d20355a

Please sign in to comment.