Skip to content

Commit

Permalink
add ability to delete files (#270)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucademian committed Feb 6, 2024
1 parent 1849ed5 commit 6e555c5
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 11 deletions.
17 changes: 9 additions & 8 deletions bitbucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,14 +198,15 @@ type File struct {

// 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"`
Owner string `json:"owner"`
RepoSlug string `json:"repo_slug"`
FilePath string `json:"filepath"`
FileName string `json:"filename"`
Files []File `json:"files"`
FilesToDelete []string `json:"files_to_delete"`
Author string `json:"author"`
Message string `json:"message"`
Branch string `json:"branch"`
}

// RepositoryRefOptions represents the options for describing a repository's refs (i.e.
Expand Down
9 changes: 8 additions & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ func (c *Client) executePaginated(method string, urlStr string, text string, pag
return result, nil
}

func (c *Client) executeFileUpload(method string, urlStr string, files []File, params map[string]string) (interface{}, error) {
func (c *Client) executeFileUpload(method string, urlStr string, files []File, filesToDelete []string, params map[string]string) (interface{}, error) {
// Prepare a form that you will submit to that URL.
var b bytes.Buffer
w := multipart.NewWriter(&b)
Expand Down Expand Up @@ -308,6 +308,13 @@ func (c *Client) executeFileUpload(method string, urlStr string, files []File, p
}
}

for _, filename := range filesToDelete {
err := w.WriteField("files", filename)
if err != nil {
return nil, err
}
}

// Don't forget to close the multipart writer.
// If you don't close it, your request will be missing the terminating boundary.
w.Close()
Expand Down
2 changes: 1 addition & 1 deletion downloads.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func (dl *Downloads) Create(do *DownloadsOptions) (interface{}, error) {
Name: do.FileName,
}}
}
return dl.c.executeFileUpload("POST", urlStr, do.Files, make(map[string]string))
return dl.c.executeFileUpload("POST", urlStr, do.Files, []string{}, make(map[string]string))
}

func (dl *Downloads) List(do *DownloadsOptions) (interface{}, error) {
Expand Down
2 changes: 1 addition & 1 deletion repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ func (r *Repository) WriteFileBlob(ro *RepositoryBlobWriteOptions) error {

urlStr := r.c.requestUrl("/repositories/%s/%s/src", ro.Owner, ro.RepoSlug)

_, err := r.c.executeFileUpload("POST", urlStr, ro.Files, m)
_, err := r.c.executeFileUpload("POST", urlStr, ro.Files, ro.FilesToDelete, m)
return err
}

Expand Down

0 comments on commit 6e555c5

Please sign in to comment.