diff --git a/bitbucket.go b/bitbucket.go index 8f13b11..74fb07c 100644 --- a/bitbucket.go +++ b/bitbucket.go @@ -191,12 +191,18 @@ type RepositoryBlobOptions struct { Path string `json:"path"` } +type File struct { + Path string + Name string +} + // Based on https://developer.atlassian.com/bitbucket/api/2/reference/resource/repositories/%7Bworkspace%7D/%7Brepo_slug%7D/src#post type RepositoryBlobWriteOptions struct { Owner string `json:"owner"` RepoSlug string `json:"repo_slug"` FilePath string `json:"filepath"` FileName string `json:"filename"` + Files []File `json:"files"` Author string `json:"author"` Message string `json:"message"` Branch string `json:"branch"` @@ -455,6 +461,7 @@ type DownloadsOptions struct { RepoSlug string `json:"repo_slug"` FilePath string `json:"filepath"` FileName string `json:"filename"` + Files []File `json:"files"` } type PageRes struct { diff --git a/client.go b/client.go index 211341e..2d8f6dd 100644 --- a/client.go +++ b/client.go @@ -279,28 +279,30 @@ func (c *Client) executePaginated(method string, urlStr string, text string, pag return result, nil } -func (c *Client) executeFileUpload(method string, urlStr string, filePath string, fileName string, fieldname string, params map[string]string) (interface{}, error) { - fileReader, err := os.Open(filePath) - if err != nil { - return nil, err - } - defer fileReader.Close() - +func (c *Client) executeFileUpload(method string, urlStr string, files []File, params map[string]string) (interface{}, error) { // Prepare a form that you will submit to that URL. var b bytes.Buffer w := multipart.NewWriter(&b) var fw io.Writer - if fw, err = w.CreateFormFile(fieldname, fileName); err != nil { - return nil, err - } + for _, file := range files { + fileReader, err := os.Open(file.Path) + if err != nil { + return nil, err + } + defer fileReader.Close() - if _, err = io.Copy(fw, fileReader); err != nil { - return nil, err + if fw, err = w.CreateFormFile(file.Name, file.Name); err != nil { + return nil, err + } + + if _, err = io.Copy(fw, fileReader); err != nil { + return nil, err + } } for key, value := range params { - err = w.WriteField(key, value) + err := w.WriteField(key, value) if err != nil { return nil, err } diff --git a/downloads.go b/downloads.go index b8b9939..468fc7c 100644 --- a/downloads.go +++ b/downloads.go @@ -1,12 +1,24 @@ package bitbucket +import "fmt" + type Downloads struct { c *Client } func (dl *Downloads) Create(do *DownloadsOptions) (interface{}, error) { urlStr := dl.c.requestUrl("/repositories/%s/%s/downloads", do.Owner, do.RepoSlug) - return dl.c.executeFileUpload("POST", urlStr, do.FilePath, do.FileName, "files", make(map[string]string)) + + if do.FileName != "" { + if len(do.Files) > 0 { + return nil, fmt.Errorf("can't specify both files and filename") + } + do.Files = []File{{ + Path: do.FileName, + Name: do.FileName, + }} + } + return dl.c.executeFileUpload("POST", urlStr, do.Files, make(map[string]string)) } func (dl *Downloads) List(do *DownloadsOptions) (interface{}, error) { diff --git a/repository.go b/repository.go index 44a85d9..757e5b0 100644 --- a/repository.go +++ b/repository.go @@ -387,9 +387,19 @@ func (r *Repository) WriteFileBlob(ro *RepositoryBlobWriteOptions) error { m["branch"] = ro.Branch } + if ro.FileName != "" { + if len(ro.Files) > 0 { + return fmt.Errorf("can't specify both files and filename") + } + ro.Files = []File{{ + Path: ro.FileName, + Name: ro.FileName, + }} + } + urlStr := r.c.requestUrl("/repositories/%s/%s/src", ro.Owner, ro.RepoSlug) - _, err := r.c.executeFileUpload("POST", urlStr, ro.FilePath, ro.FileName, ro.FileName, m) + _, err := r.c.executeFileUpload("POST", urlStr, ro.Files, m) return err }