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
Fully support project and group avatars #1506
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -18,10 +18,13 @@ package gitlab | |||||
|
||||||
import ( | ||||||
"bytes" | ||||||
"encoding/json" | ||||||
"fmt" | ||||||
"io" | ||||||
"net/http" | ||||||
"time" | ||||||
|
||||||
retryablehttp "github.com/hashicorp/go-retryablehttp" | ||||||
) | ||||||
|
||||||
// GroupsService handles communication with the group related methods of | ||||||
|
@@ -90,6 +93,15 @@ type GroupAvatar struct { | |||||
Image io.Reader | ||||||
} | ||||||
|
||||||
// MarshalJSON implements the json.Marshaler interface. | ||||||
func (a *GroupAvatar) MarshalJSON() ([]byte, error) { | ||||||
if a.Filename == "" && a.Image == nil { | ||||||
return []byte(`""`), nil | ||||||
} | ||||||
type alias GroupAvatar | ||||||
return json.Marshal((*alias)(a)) | ||||||
} | ||||||
|
||||||
// LDAPGroupLink represents a GitLab LDAP group link. | ||||||
// | ||||||
// GitLab API docs: https://docs.gitlab.com/ce/api/groups.html#ldap-group-links | ||||||
|
@@ -312,6 +324,7 @@ func (s *GroupsService) DownloadAvatar(gid interface{}, options ...RequestOption | |||||
type CreateGroupOptions struct { | ||||||
Name *string `url:"name,omitempty" json:"name,omitempty"` | ||||||
Path *string `url:"path,omitempty" json:"path,omitempty"` | ||||||
Avatar *GroupAvatar `url:"-" json:"-"` | ||||||
Description *string `url:"description,omitempty" json:"description,omitempty"` | ||||||
MembershipLock *bool `url:"membership_lock,omitempty" json:"membership_lock,omitempty"` | ||||||
Visibility *VisibilityValue `url:"visibility,omitempty" json:"visibility,omitempty"` | ||||||
|
@@ -336,7 +349,22 @@ type CreateGroupOptions struct { | |||||
// | ||||||
// GitLab API docs: https://docs.gitlab.com/ce/api/groups.html#new-group | ||||||
func (s *GroupsService) CreateGroup(opt *CreateGroupOptions, options ...RequestOptionFunc) (*Group, *Response, error) { | ||||||
req, err := s.client.NewRequest(http.MethodPost, "groups", opt, options) | ||||||
var err error | ||||||
var req *retryablehttp.Request | ||||||
|
||||||
if opt.Avatar == nil { | ||||||
req, err = s.client.NewRequest(http.MethodPost, "groups", opt, options) | ||||||
} else { | ||||||
req, err = s.client.UploadRequest( | ||||||
http.MethodPost, | ||||||
"groups", | ||||||
opt.Avatar.Image, | ||||||
opt.Avatar.Filename, | ||||||
UploadAvatar, | ||||||
opt, | ||||||
options, | ||||||
) | ||||||
} | ||||||
if err != nil { | ||||||
return nil, nil, err | ||||||
} | ||||||
|
@@ -420,6 +448,7 @@ func (s *GroupsService) TransferSubGroup(gid interface{}, opt *TransferSubGroupO | |||||
type UpdateGroupOptions struct { | ||||||
Name *string `url:"name,omitempty" json:"name,omitempty"` | ||||||
Path *string `url:"path,omitempty" json:"path,omitempty"` | ||||||
Avatar *GroupAvatar `url:"-" json:"avatar,omitempty"` | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't this be:
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hmmm, good question. I think in case of |
||||||
Description *string `url:"description,omitempty" json:"description,omitempty"` | ||||||
MembershipLock *bool `url:"membership_lock,omitempty" json:"membership_lock,omitempty"` | ||||||
Visibility *VisibilityValue `url:"visibility,omitempty" json:"visibility,omitempty"` | ||||||
|
@@ -453,7 +482,21 @@ func (s *GroupsService) UpdateGroup(gid interface{}, opt *UpdateGroupOptions, op | |||||
} | ||||||
u := fmt.Sprintf("groups/%s", PathEscape(group)) | ||||||
|
||||||
req, err := s.client.NewRequest(http.MethodPut, u, opt, options) | ||||||
var req *retryablehttp.Request | ||||||
|
||||||
if opt.Avatar == nil || (opt.Avatar.Filename == "" && opt.Avatar.Image == nil) { | ||||||
req, err = s.client.NewRequest(http.MethodPut, u, opt, options) | ||||||
} else { | ||||||
req, err = s.client.UploadRequest( | ||||||
http.MethodPut, | ||||||
u, | ||||||
opt.Avatar.Image, | ||||||
opt.Avatar.Filename, | ||||||
UploadAvatar, | ||||||
opt, | ||||||
options, | ||||||
) | ||||||
} | ||||||
if err != nil { | ||||||
return nil, nil, err | ||||||
} | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you explain why this is needed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we wouldn't have this the field wouldn't be sent. In the current implementation it will send an empty string which effectively means to remove the avatar, e.g.: