Skip to content

Commit 4835597

Browse files
feat: support multiple files in WriteFileBlob (commit) (#256)
1 parent 2b7cff1 commit 4835597

File tree

4 files changed

+46
-15
lines changed

4 files changed

+46
-15
lines changed

bitbucket.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,12 +191,18 @@ type RepositoryBlobOptions struct {
191191
Path string `json:"path"`
192192
}
193193

194+
type File struct {
195+
Path string
196+
Name string
197+
}
198+
194199
// Based on https://developer.atlassian.com/bitbucket/api/2/reference/resource/repositories/%7Bworkspace%7D/%7Brepo_slug%7D/src#post
195200
type RepositoryBlobWriteOptions struct {
196201
Owner string `json:"owner"`
197202
RepoSlug string `json:"repo_slug"`
198203
FilePath string `json:"filepath"`
199204
FileName string `json:"filename"`
205+
Files []File `json:"files"`
200206
Author string `json:"author"`
201207
Message string `json:"message"`
202208
Branch string `json:"branch"`
@@ -455,6 +461,7 @@ type DownloadsOptions struct {
455461
RepoSlug string `json:"repo_slug"`
456462
FilePath string `json:"filepath"`
457463
FileName string `json:"filename"`
464+
Files []File `json:"files"`
458465
}
459466

460467
type PageRes struct {

client.go

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -279,28 +279,30 @@ func (c *Client) executePaginated(method string, urlStr string, text string, pag
279279
return result, nil
280280
}
281281

282-
func (c *Client) executeFileUpload(method string, urlStr string, filePath string, fileName string, fieldname string, params map[string]string) (interface{}, error) {
283-
fileReader, err := os.Open(filePath)
284-
if err != nil {
285-
return nil, err
286-
}
287-
defer fileReader.Close()
288-
282+
func (c *Client) executeFileUpload(method string, urlStr string, files []File, params map[string]string) (interface{}, error) {
289283
// Prepare a form that you will submit to that URL.
290284
var b bytes.Buffer
291285
w := multipart.NewWriter(&b)
292286

293287
var fw io.Writer
294-
if fw, err = w.CreateFormFile(fieldname, fileName); err != nil {
295-
return nil, err
296-
}
288+
for _, file := range files {
289+
fileReader, err := os.Open(file.Path)
290+
if err != nil {
291+
return nil, err
292+
}
293+
defer fileReader.Close()
297294

298-
if _, err = io.Copy(fw, fileReader); err != nil {
299-
return nil, err
295+
if fw, err = w.CreateFormFile(file.Name, file.Name); err != nil {
296+
return nil, err
297+
}
298+
299+
if _, err = io.Copy(fw, fileReader); err != nil {
300+
return nil, err
301+
}
300302
}
301303

302304
for key, value := range params {
303-
err = w.WriteField(key, value)
305+
err := w.WriteField(key, value)
304306
if err != nil {
305307
return nil, err
306308
}

downloads.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,24 @@
11
package bitbucket
22

3+
import "fmt"
4+
35
type Downloads struct {
46
c *Client
57
}
68

79
func (dl *Downloads) Create(do *DownloadsOptions) (interface{}, error) {
810
urlStr := dl.c.requestUrl("/repositories/%s/%s/downloads", do.Owner, do.RepoSlug)
9-
return dl.c.executeFileUpload("POST", urlStr, do.FilePath, do.FileName, "files", make(map[string]string))
11+
12+
if do.FileName != "" {
13+
if len(do.Files) > 0 {
14+
return nil, fmt.Errorf("can't specify both files and filename")
15+
}
16+
do.Files = []File{{
17+
Path: do.FileName,
18+
Name: do.FileName,
19+
}}
20+
}
21+
return dl.c.executeFileUpload("POST", urlStr, do.Files, make(map[string]string))
1022
}
1123

1224
func (dl *Downloads) List(do *DownloadsOptions) (interface{}, error) {

repository.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -387,9 +387,19 @@ func (r *Repository) WriteFileBlob(ro *RepositoryBlobWriteOptions) error {
387387
m["branch"] = ro.Branch
388388
}
389389

390+
if ro.FileName != "" {
391+
if len(ro.Files) > 0 {
392+
return fmt.Errorf("can't specify both files and filename")
393+
}
394+
ro.Files = []File{{
395+
Path: ro.FileName,
396+
Name: ro.FileName,
397+
}}
398+
}
399+
390400
urlStr := r.c.requestUrl("/repositories/%s/%s/src", ro.Owner, ro.RepoSlug)
391401

392-
_, err := r.c.executeFileUpload("POST", urlStr, ro.FilePath, ro.FileName, ro.FileName, m)
402+
_, err := r.c.executeFileUpload("POST", urlStr, ro.Files, m)
393403
return err
394404
}
395405

0 commit comments

Comments
 (0)