Skip to content

Commit

Permalink
Add the support for delete of the bitbucket file (drone#139)
Browse files Browse the repository at this point in the history
* Add the support for delete of the bitbucket

* Removed the print statements

* Fix the delete file in bitbucket test
  • Loading branch information
DeepakPatankar authored Jan 4, 2022
1 parent 6f375cd commit abe308c
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 5 deletions.
30 changes: 30 additions & 0 deletions scm/driver/bitbucket/bitbucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
20 changes: 19 additions & 1 deletion scm/driver/bitbucket/content.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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 {
Expand Down
34 changes: 30 additions & 4 deletions scm/driver/bitbucket/content_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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: "[email protected]",
},
}

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")
}
}

Expand Down

0 comments on commit abe308c

Please sign in to comment.