From abe308c453f9a76896ea94e60818b6726c9435a7 Mon Sep 17 00:00:00 2001 From: Deepak Patankar Date: Tue, 4 Jan 2022 16:33:09 +0530 Subject: [PATCH] Add the support for delete of the bitbucket file (#139) * Add the support for delete of the bitbucket * Removed the print statements * Fix the delete file in bitbucket test --- scm/driver/bitbucket/bitbucket.go | 30 ++++++++++++++++++++++++ scm/driver/bitbucket/content.go | 20 +++++++++++++++- scm/driver/bitbucket/content_test.go | 34 ++++++++++++++++++++++++---- 3 files changed, 79 insertions(+), 5 deletions(-) diff --git a/scm/driver/bitbucket/bitbucket.go b/scm/driver/bitbucket/bitbucket.go index 1b8a1af77..d5afda370 100644 --- a/scm/driver/bitbucket/bitbucket.go +++ b/scm/driver/bitbucket/bitbucket.go @@ -98,6 +98,36 @@ func (c *wrapper) do(ctx context.Context, method, path string, in, out interface req.Header = map[string][]string{ "Content-Type": {w.FormDataContentType()}, } + case *contentDelete: + body := &bytes.Buffer{} + writer := multipart.NewWriter(body) + fw, err := writer.CreateFormField("files") + _, err = io.Copy(fw, strings.NewReader(content.File)) + if err != nil { + return nil, err + } + fw, err = writer.CreateFormField("message") + _, err = io.Copy(fw, strings.NewReader(content.Message)) + if err != nil { + return nil, err + } + fw, err = writer.CreateFormField("author") + _, err = io.Copy(fw, strings.NewReader(content.Author)) + if err != nil { + return nil, err + } + if content.Branch != "" { + fw, err = writer.CreateFormField("branch") + _, err = io.Copy(fw, strings.NewReader(content.Branch)) + if err != nil { + return nil, err + } + } + writer.Close() + req.Body = bytes.NewReader(body.Bytes()) + req.Header = map[string][]string{ + "Content-Type": {writer.FormDataContentType()}, + } default: buf := new(bytes.Buffer) json.NewEncoder(buf).Encode(in) diff --git a/scm/driver/bitbucket/content.go b/scm/driver/bitbucket/content.go index 73f91d79e..eda337ad7 100644 --- a/scm/driver/bitbucket/content.go +++ b/scm/driver/bitbucket/content.go @@ -68,7 +68,17 @@ func (s *contentService) Update(ctx context.Context, repo, path string, params * } func (s *contentService) Delete(ctx context.Context, repo, path string, params *scm.ContentParams) (*scm.Response, error) { - return nil, scm.ErrNotSupported + author := fmt.Sprintf("%s <%s>", params.Signature.Name, params.Signature.Email) + endpoint := fmt.Sprintf("/2.0/repositories/%s/src", repo) + in := &contentDelete{ + File: path, + Branch: params.Branch, + Message: params.Message, + Sha: params.Sha, + Author: author, + } + res, err := s.client.do(ctx, "POST", endpoint, in, nil) + return res, err } func (s *contentService) List(ctx context.Context, repo, path, ref string, opts scm.ListOptions) ([]*scm.ContentInfo, *scm.Response, error) { @@ -109,6 +119,14 @@ type contentCreateUpdate struct { Author string `json:"author"` } +type contentDelete struct { + File string `json:"file"` + Branch string `json:"branch"` + Message string `json:"message"` + Sha string `json:"sha"` + Author string `json:"author"` +} + func convertContentInfoList(from *contents) []*scm.ContentInfo { to := []*scm.ContentInfo{} for _, v := range from.Values { diff --git a/scm/driver/bitbucket/content_test.go b/scm/driver/bitbucket/content_test.go index 3336f8cd2..3ec8083cd 100644 --- a/scm/driver/bitbucket/content_test.go +++ b/scm/driver/bitbucket/content_test.go @@ -183,10 +183,36 @@ func TestContentUpdateBadCommitID(t *testing.T) { } func TestContentDelete(t *testing.T) { - content := new(contentService) - _, err := content.Delete(context.Background(), "atlassian/atlaskit", "README", &scm.ContentParams{}) - if err != scm.ErrNotSupported { - t.Errorf("Expect Not Supported error") + defer gock.Off() + + gock.New("https://api.bitbucket.org"). + Post("/2.0/repositories/atlassian/atlaskit/src"). + Reply(201). + Type("application/json") + + params := &scm.ContentParams{ + Message: "my commit message", + Signature: scm.Signature{ + Name: "Monalisa Octocat", + Email: "octocat@github.com", + }, + } + + client := NewDefault() + res, err := client.Contents.Update( + context.Background(), + "atlassian/atlaskit", + "test/hello", + params, + ) + + if err != nil { + t.Error(err) + return + } + + if res.Status != 201 { + t.Errorf("Unexpected Results") } }