fix(file_write_branch): support file creation by routing POST/PUT on sha #1
@@ -39,9 +39,9 @@ func TestFileWriteBranchCreatesBranchAndFile(t *testing.T) {
|
||||
_, _ = w.Write([]byte(createBranchResp))
|
||||
})
|
||||
|
||||
// Upsert file → 201
|
||||
// New file (no sha) → POST to /contents/{path}
|
||||
mux.HandleFunc("/api/v1/repos/owner/myrepo/contents/doc.md", func(w http.ResponseWriter, r *http.Request) {
|
||||
require.Equal(t, http.MethodPut, r.Method)
|
||||
require.Equal(t, http.MethodPost, r.Method)
|
||||
w.WriteHeader(http.StatusCreated)
|
||||
_, _ = w.Write([]byte(upsertFileResp))
|
||||
})
|
||||
@@ -64,6 +64,35 @@ func TestFileWriteBranchCreatesBranchAndFile(t *testing.T) {
|
||||
assert.Equal(t, "cmt1", result["commit_sha"])
|
||||
}
|
||||
|
||||
func TestFileWriteBranchUsesPutWhenShaProvided(t *testing.T) {
|
||||
mux := http.NewServeMux()
|
||||
|
||||
// Branch exists
|
||||
mux.HandleFunc("/api/v1/repos/owner/myrepo/branches/feat/existing", func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_, _ = w.Write([]byte(branchCheckExistsResp))
|
||||
})
|
||||
|
||||
// Existing file (sha provided) → PUT
|
||||
mux.HandleFunc("/api/v1/repos/owner/myrepo/contents/doc.md", func(w http.ResponseWriter, r *http.Request) {
|
||||
require.Equal(t, http.MethodPut, r.Method)
|
||||
w.WriteHeader(http.StatusOK)
|
||||
_, _ = w.Write([]byte(upsertFileResp))
|
||||
})
|
||||
|
||||
srv := httptest.NewServer(mux)
|
||||
defer srv.Close()
|
||||
|
||||
tool := tools.NewFileWriteBranch(gitea.NewClient(srv.URL, "tok"), allowlist.New([]string{"owner"}))
|
||||
out, err := tool.Call(context.Background(), json.RawMessage(`{
|
||||
"owner":"owner","name":"myrepo","path":"doc.md",
|
||||
"content":"hello","branch":"feat/existing",
|
||||
"sha":"oldsha","message":"update doc.md"
|
||||
}`))
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, out)
|
||||
}
|
||||
|
||||
func TestFileWriteBranchUsesDefaultBaseWhenBaseEmpty(t *testing.T) {
|
||||
var createBody []byte
|
||||
mux := http.NewServeMux()
|
||||
|
||||
Reference in New Issue
Block a user