Skip to content

Commit

Permalink
Merge pull request #1506 from timofurrer/feature/avatars
Browse files Browse the repository at this point in the history
Fully support project and group avatars
  • Loading branch information
svanharmelen committed Nov 18, 2022
2 parents 3ef2120 + 02292ff commit fce9772
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 5 deletions.
47 changes: 45 additions & 2 deletions groups.go
Expand Up @@ -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
Expand Down Expand Up @@ -91,6 +94,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
Expand Down Expand Up @@ -321,6 +333,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"`
Expand All @@ -345,7 +358,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
}
Expand Down Expand Up @@ -429,6 +457,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"`
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"`
Expand Down Expand Up @@ -462,7 +491,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
}
Expand Down
16 changes: 13 additions & 3 deletions projects.go
Expand Up @@ -17,6 +17,7 @@
package gitlab

import (
"encoding/json"
"fmt"
"io"
"net/http"
Expand Down Expand Up @@ -711,6 +712,15 @@ type ProjectAvatar struct {
Image io.Reader
}

// MarshalJSON implements the json.Marshaler interface.
func (a *ProjectAvatar) MarshalJSON() ([]byte, error) {
if a.Filename == "" && a.Image == nil {
return []byte(`""`), nil
}
type alias ProjectAvatar
return json.Marshal((*alias)(a))
}

// CreateProject creates a new project owned by the authenticated user.
//
// GitLab API docs: https://docs.gitlab.com/ce/api/projects.html#create-project
Expand Down Expand Up @@ -812,7 +822,7 @@ type EditProjectOptions struct {
AutoDevopsDeployStrategy *string `url:"auto_devops_deploy_strategy,omitempty" json:"auto_devops_deploy_strategy,omitempty"`
AutoDevopsEnabled *bool `url:"auto_devops_enabled,omitempty" json:"auto_devops_enabled,omitempty"`
AutocloseReferencedIssues *bool `url:"autoclose_referenced_issues,omitempty" json:"autoclose_referenced_issues,omitempty"`
Avatar *ProjectAvatar `url:"-" json:"-"`
Avatar *ProjectAvatar `url:"-" json:"avatar,omitempty"`
BuildCoverageRegex *string `url:"build_coverage_regex,omitempty" json:"build_coverage_regex,omitempty"`
BuildGitStrategy *string `url:"build_git_strategy,omitempty" json:"build_git_strategy,omitempty"`
BuildTimeout *int `url:"build_timeout,omitempty" json:"build_timeout,omitempty"`
Expand Down Expand Up @@ -902,11 +912,11 @@ func (s *ProjectsService) EditProject(pid interface{}, opt *EditProjectOptions,

var req *retryablehttp.Request

if opt.Avatar == nil {
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.MethodPost,
http.MethodPut,
u,
opt.Avatar.Image,
opt.Avatar.Filename,
Expand Down

0 comments on commit fce9772

Please sign in to comment.