From f7cf231ae953c7804fd6be3b8bc07619e9abcf4d Mon Sep 17 00:00:00 2001 From: 6543 <6543@noreply.gitea.io> Date: Fri, 3 Apr 2020 12:55:16 +0000 Subject: [PATCH] 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 Reviewed-by: Andrew Thornton --- gitea/repo_file.go | 16 ++++++++++++++++ gitea/repo_file_test.go | 12 +++++++++++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/gitea/repo_file.go b/gitea/repo_file.go index b8919f9..f96cb00 100644 --- a/gitea/repo_file.go +++ b/gitea/repo_file.go @@ -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 +} diff --git a/gitea/repo_file_test.go b/gitea/repo_file_test.go index 73dd750..abd5255 100644 --- a/gitea/repo_file_test.go +++ b/gitea/repo_file_test.go @@ -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()) }