From 6e555c55f9f2981175ebeb8c30f3cebd60160293 Mon Sep 17 00:00:00 2001 From: Luca Demian Date: Tue, 6 Feb 2024 00:53:13 -0500 Subject: [PATCH] add ability to delete files (#270) --- bitbucket.go | 17 +++++++++-------- client.go | 9 ++++++++- downloads.go | 2 +- repository.go | 2 +- 4 files changed, 19 insertions(+), 11 deletions(-) diff --git a/bitbucket.go b/bitbucket.go index 89d12e8..d3c3366 100644 --- a/bitbucket.go +++ b/bitbucket.go @@ -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. diff --git a/client.go b/client.go index 2d8f6dd..3ae6262 100644 --- a/client.go +++ b/client.go @@ -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) @@ -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() diff --git a/downloads.go b/downloads.go index 468fc7c..c16b08f 100644 --- a/downloads.go +++ b/downloads.go @@ -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) { diff --git a/repository.go b/repository.go index 757e5b0..0725d8e 100644 --- a/repository.go +++ b/repository.go @@ -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 }