Skip to content

Commit 9f51e41

Browse files
authored
Write/Commit a file (#126)
* feat: implement WriteFileBlob * chore: reformat structs to not mess up the diff
1 parent 80e71ed commit 9f51e41

File tree

4 files changed

+42
-3
lines changed

4 files changed

+42
-3
lines changed

bitbucket.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,17 @@ type RepositoryBlobOptions struct {
158158
Path string `json:"path"`
159159
}
160160

161+
// Based on https://developer.atlassian.com/bitbucket/api/2/reference/resource/repositories/%7Bworkspace%7D/%7Brepo_slug%7D/src#post
162+
type RepositoryBlobWriteOptions struct {
163+
Owner string `json:"owner"`
164+
RepoSlug string `json:"repo_slug"`
165+
FilePath string `json:"filepath"`
166+
FileName string `json:"filename"`
167+
Author string `json:"author"`
168+
Message string `json:"message"`
169+
Branch string `json:"branch"`
170+
}
171+
161172
type RepositoryBranchOptions struct {
162173
Owner string `json:"owner"`
163174
RepoSlug string `json:"repo_slug"`

client.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ func (c *Client) execute(method string, urlStr string, text string) (interface{}
262262
return result, nil
263263
}
264264

265-
func (c *Client) executeFileUpload(method string, urlStr string, filePath string, fileName string) (interface{}, error) {
265+
func (c *Client) executeFileUpload(method string, urlStr string, filePath string, fileName string, fieldname string, params map[string]string) (interface{}, error) {
266266
fileReader, err := os.Open(filePath)
267267
if err != nil {
268268
return nil, err
@@ -274,14 +274,21 @@ func (c *Client) executeFileUpload(method string, urlStr string, filePath string
274274
w := multipart.NewWriter(&b)
275275

276276
var fw io.Writer
277-
if fw, err = w.CreateFormFile("files", fileName); err != nil {
277+
if fw, err = w.CreateFormFile(fieldname, fileName); err != nil {
278278
return nil, err
279279
}
280280

281281
if _, err = io.Copy(fw, fileReader); err != nil {
282282
return nil, err
283283
}
284284

285+
for key, value := range params {
286+
err = w.WriteField(key, value)
287+
if err != nil {
288+
return nil, err
289+
}
290+
}
291+
285292
// Don't forget to close the multipart writer.
286293
// If you don't close it, your request will be missing the terminating boundary.
287294
w.Close()

downloads.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ type Downloads struct {
66

77
func (dl *Downloads) Create(do *DownloadsOptions) (interface{}, error) {
88
urlStr := dl.c.requestUrl("/repositories/%s/%s/downloads", do.Owner, do.RepoSlug)
9-
return dl.c.executeFileUpload("POST", urlStr, do.FilePath, do.FileName)
9+
return dl.c.executeFileUpload("POST", urlStr, do.FilePath, do.FileName, "files", make(map[string]string))
1010
}
1111

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

repository.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,27 @@ func (r *Repository) GetFileBlob(ro *RepositoryBlobOptions) (*RepositoryBlob, er
244244
return &blob, nil
245245
}
246246

247+
func (r *Repository) WriteFileBlob(ro *RepositoryBlobWriteOptions) error {
248+
m := make(map[string]string)
249+
250+
if ro.Author != "" {
251+
m["author"] = ro.Author
252+
}
253+
254+
if ro.Message != "" {
255+
m["message"] = ro.Message
256+
}
257+
258+
if ro.Branch != "" {
259+
m["branch"] = ro.Branch
260+
}
261+
262+
urlStr := r.c.requestUrl("/repositories/%s/%s/src", ro.Owner, ro.RepoSlug)
263+
264+
_, err := r.c.executeFileUpload("POST", urlStr, ro.FilePath, ro.FileName, ro.FileName, m)
265+
return err
266+
}
267+
247268
func (r *Repository) ListBranches(rbo *RepositoryBranchOptions) (*RepositoryBranches, error) {
248269

249270
params := url.Values{}

0 commit comments

Comments
 (0)