feat(tools): file_delete
This commit is contained in:
@@ -181,6 +181,32 @@ func (c *Client) ListContents(ctx context.Context, owner, repo, path, ref string
|
||||
return entries, nil
|
||||
}
|
||||
|
||||
type DeleteFileArgs struct {
|
||||
Branch string `json:"branch"`
|
||||
Message string `json:"message"`
|
||||
Sha string `json:"sha"`
|
||||
}
|
||||
|
||||
func (c *Client) DeleteFile(ctx context.Context, owner, repo, path string, args DeleteFileArgs) (*FileWriteResult, error) {
|
||||
p := fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s", owner, repo, path)
|
||||
payload, err := json.Marshal(args)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
body, status, err := c.DeleteJSONBody(ctx, p, payload)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := MapStatus(status, body); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var out FileWriteResult
|
||||
if err := json.Unmarshal(body, &out); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &out, nil
|
||||
}
|
||||
|
||||
// UpsertFile creates a file when args.Sha is empty (POST) or updates an existing
|
||||
// file when args.Sha is set (PUT). Gitea routes both operations by HTTP method on
|
||||
// the same /contents/{path} URL, and rejects PUT without a sha.
|
||||
|
||||
@@ -233,3 +233,35 @@ func TestListContentsOnFileReturnsError(t *testing.T) {
|
||||
require.Error(t, err)
|
||||
assert.ErrorIs(t, err, gitea.ErrValidation)
|
||||
}
|
||||
|
||||
func TestDeleteFile(t *testing.T) {
|
||||
var captured []byte
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
assert.Equal(t, "/api/v1/repos/o/r/contents/src/old.go", r.URL.Path)
|
||||
assert.Equal(t, http.MethodDelete, r.Method)
|
||||
var err error
|
||||
captured, err = io.ReadAll(r.Body)
|
||||
require.NoError(t, err)
|
||||
w.WriteHeader(http.StatusOK)
|
||||
_, _ = w.Write([]byte(`{
|
||||
"content":null,
|
||||
"commit":{"sha":"cmt1","html_url":"http://example.com/commit/cmt1"}
|
||||
}`))
|
||||
}))
|
||||
defer srv.Close()
|
||||
|
||||
c := gitea.NewClient(srv.URL, "tok")
|
||||
result, err := c.DeleteFile(context.Background(), "o", "r", "src/old.go", gitea.DeleteFileArgs{
|
||||
Branch: "main",
|
||||
Message: "remove old.go",
|
||||
Sha: "blobsha",
|
||||
})
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "cmt1", result.Commit.Sha)
|
||||
|
||||
var payload map[string]string
|
||||
require.NoError(t, json.Unmarshal(captured, &payload))
|
||||
assert.Equal(t, "main", payload["branch"])
|
||||
assert.Equal(t, "remove old.go", payload["message"])
|
||||
assert.Equal(t, "blobsha", payload["sha"])
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user