fix(file_write_branch): support file creation by routing POST/PUT on sha #1
Reference in New Issue
Block a user
Delete Branch "fix/file-write-branch-create"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Problem
file_write_branchis documented as "Create or update a file on a feature branch", but creates fail.internal/gitea/files.go:UpsertFileunconditionally usesPUT /repos/{owner}/{repo}/contents/{path}, which Gitea routes to its update handler — and that handler requiressha. New files have no sha, so every create returns:This blocks adding any new tool to the connector via the connector itself, since every new tool needs a new
*.gofile ininternal/tools/.Fix
Route by HTTP method based on whether
args.Shais provided:sha == ""→POST(create)sha != ""→PUT(update)Same URL, same payload shape (
Shaalready hasomitempty), so no other call sites need to change. Keeps the publicUpsertFilesignature stable.Tests
TestFileWriteBranchCreatesBranchAndFile— flipped to assertMethodPoston the contents endpoint (the test was previously baking in the buggy behavior).TestFileWriteBranchUsesPutWhenShaProvided— new test exercising the update path with a sha.Follow-up
Once this lands and the connector is redeployed, a follow-up PR can add
issue_get(currently impossible to ship through the connector because it requires creatinginternal/tools/issue_get.go).Review
Verdict: ✅ Approve — one minor note
internal/gitea/files.go— solidThe
UpsertFilerouting logic is exactly right:POSTwhenshais empty (new file),PUTwhenshais set (update). The comment on the function is accurate and useful.sha,omitemptyonUpsertFileArgsis correct — it meansshais omitted from the JSON body on POST, which is what Gitea expects.internal/tools/file_write_branch_test.go— thoroughThe test suite covers all the meaningful cases:
baseis empty/branchesmessagevalidationUse of
atomic.Int32to assertPOST /branchesis never called inTestFileWriteBranchSkipsCreateWhenBranchExistsis a nice touch.Minor note
TestFileWriteBranchUsesPutWhenShaProvidedonly checksrequire.NotNil(t, out). For consistency withTestFileWriteBranchCreatesBranchAndFile, it would be slightly better to assertcommit_shaandbranchare present in the output. Not a blocker.Addressed in
d35ff97— TestFileWriteBranchUsesPutWhenShaProvided now parses out into a map and asserts branch == "feat/existing" and commit_sha == "cmt1", matching the create-test pattern.