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

Commit

Permalink
Added UploadAvatar.
Browse files Browse the repository at this point in the history
Fixes #229.
  • Loading branch information
mitar committed Dec 30, 2021
1 parent 5e9db4a commit d12864e
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 0 deletions.
47 changes: 47 additions & 0 deletions projects.go
Original file line number Diff line number Diff line change
Expand Up @@ -1808,3 +1808,50 @@ func (s *ProjectsService) TransferProject(pid interface{}, opt *TransferProjectO

return p, resp, err
}

// UploadAvatar uploads an avatar for the project
//
// GitLab API docs: https://docs.gitlab.com/ee/api/projects.html#upload-a-project-avatar
func (s *ProjectsService) UploadAvatar(pid interface{}, avatar io.Reader, filename string, options ...RequestOptionFunc) (*Project, *Response, error) {
project, err := parseID(pid)
if err != nil {
return nil, nil, err
}
u := fmt.Sprintf("projects/%s", PathEscape(project))

b := &bytes.Buffer{}
w := multipart.NewWriter(b)

_, filename = filepath.Split(filename)
fw, err := w.CreateFormFile("avatar", filename)
if err != nil {
return nil, nil, err
}

_, err = io.Copy(fw, avatar)
if err != nil {
return nil, nil, err
}
w.Close()

req, err := s.client.NewRequest(http.MethodPut, u, nil, options)
if err != nil {
return nil, nil, err
}

// Set the buffer as the request body.
if err = req.SetBody(b); err != nil {
return nil, nil, err
}

// Overwrite the default content type.
req.Header.Set("Content-Type", w.FormDataContentType())

p := new(Project)
resp, err := s.client.Do(req, p)
if err != nil {
return nil, resp, err
}

return p, resp, err
}
54 changes: 54 additions & 0 deletions projects_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package gitlab

import (
"bytes"
"fmt"
"io/ioutil"
"net/http"
Expand Down Expand Up @@ -463,6 +464,59 @@ func TestUploadFile_Retry(t *testing.T) {
}
}

func TestUploadAvatar(t *testing.T) {
mux, server, client := setup(t)
defer teardown(server)

mux.HandleFunc("/api/v4/projects/1", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodPut)
if false == strings.Contains(r.Header.Get("Content-Type"), "multipart/form-data;") {
t.Fatalf("Projects.UploadAvatar request content-type %+v want multipart/form-data;", r.Header.Get("Content-Type"))
}
if r.ContentLength == -1 {
t.Fatalf("Projects.UploadAvatar request content-length is -1")
}
fmt.Fprint(w, `{}`)
})

b := bytes.Buffer{}

_, _, err := client.Projects.UploadAvatar(1, &b, "image.png")

if err != nil {
t.Fatalf("Projects.UploadAvatar returns an error: %v", err)
}
}

func TestUploadAvatar_Retry(t *testing.T) {
mux, server, client := setup(t)
defer teardown(server)

isFirstRequest := true
mux.HandleFunc("/api/v4/projects/1", func(w http.ResponseWriter, r *http.Request) {
if isFirstRequest {
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
isFirstRequest = false
return
}
if false == strings.Contains(r.Header.Get("Content-Type"), "multipart/form-data;") {
t.Fatalf("Projects.UploadAvatar request content-type %+v want multipart/form-data;", r.Header.Get("Content-Type"))
}
if r.ContentLength == -1 {
t.Fatalf("Projects.UploadAvatar request content-length is -1")
}
fmt.Fprint(w, `{}`)
})

b := bytes.Buffer{}

_, _, err := client.Projects.UploadAvatar(1, &b, "image.png")

if err != nil {
t.Fatalf("Projects.UploadAvatar returns an error: %v", err)
}
}

func TestListProjectForks(t *testing.T) {
mux, server, client := setup(t)
defer teardown(server)
Expand Down

0 comments on commit d12864e

Please sign in to comment.