Skip to content

Commit

Permalink
feat: support multiple files in WriteFileBlob (commit) (#256)
Browse files Browse the repository at this point in the history
  • Loading branch information
peter-svensson authored Oct 12, 2023
1 parent 2b7cff1 commit 4835597
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 15 deletions.
7 changes: 7 additions & 0 deletions bitbucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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 {
Expand Down
28 changes: 15 additions & 13 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
14 changes: 13 additions & 1 deletion downloads.go
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down
12 changes: 11 additions & 1 deletion repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down

0 comments on commit 4835597

Please sign in to comment.