Add DeleteFile() (#302)

add DeleteFile + Tests

Co-authored-by: 6543 <6543@obermui.de>
Reviewed-on: https://gitea.com/gitea/go-sdk/pulls/302
Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com>
Reviewed-by: Andrew Thornton <art27@cantab.net>
This commit is contained in:
6543 2020-04-03 12:55:16 +00:00 committed by Andrew Thornton
parent 8821d1f9ab
commit f7cf231ae9
2 changed files with 27 additions and 1 deletions

View file

@ -142,3 +142,19 @@ func (c *Client) UpdateFile(owner, repo, filepath string, opt UpdateFileOptions)
fr := new(FileResponse)
return fr, c.getParsedResponse("PUT", fmt.Sprintf("/repos/%s/%s/contents/%s", owner, repo, filepath), jsonHeader, bytes.NewReader(body), fr)
}
// DeleteFile delete a file from repository
func (c *Client) DeleteFile(owner, repo, filepath string, opt DeleteFileOptions) error {
body, err := json.Marshal(&opt)
if err != nil {
return err
}
status, err := c.getStatusCode("DELETE", fmt.Sprintf("/repos/%s/%s/contents/%s", owner, repo, filepath), jsonHeader, bytes.NewReader(body))
if err != nil {
return err
}
if status != 200 && status != 204 {
return fmt.Errorf("unexpected Status: %d", status)
}
return nil
}

View file

@ -13,7 +13,7 @@ import (
)
func TestFileCreateUpdateGet(t *testing.T) {
log.Println("== TestFileCreateUpdateGet ==")
log.Println("== TestFileCRUD ==")
c := newTestClient()
repo, err := createTestRepo(t, "ChangeFiles", c)
@ -50,4 +50,14 @@ func TestFileCreateUpdateGet(t *testing.T) {
assert.NoError(t, err)
assert.EqualValues(t, updatedFile.Content.SHA, file.SHA)
assert.EqualValues(t, &updatedFile.Content.Content, &file.Content)
err = c.DeleteFile(repo.Owner.UserName, repo.Name, "A", DeleteFileOptions{
FileOptions: FileOptions{
Message: "Delete File A",
},
SHA: updatedFile.Content.SHA,
})
assert.NoError(t, err)
_, err = c.GetFile(repo.Owner.UserName, repo.Name, "master", "A")
assert.EqualValues(t, "404 Not Found", err.Error())
}