fix: Public API 500 error when creating file#8211
Open
fix: Public API 500 error when creating file#8211
Conversation
…ranches via API
When calling PUT /repos/{owner}/{repo}/contents/{path} with a branch that
already exists (but differs from the default branch), the handler was always
passing OldBranch=DefaultBranch to UpdateRepoFile. This caused UpdateRepoFile
to interpret the operation as "create a new branch from the default branch",
which failed with "branch already exists" when the target branch was already
present.
Fix by checking if the target branch exists before choosing OldBranch:
- If the target branch exists: OldBranch = target branch (commit on top of it)
- If the target branch doesn't exist: OldBranch = DefaultBranch (create it)
Also adds TestRepository_UpdateRepoFile_ExistingBranch to cover this case.
Co-authored-by: Meldiron <19310830+Meldiron@users.noreply.github.com>
Agent-Logs-Url: https://github.com/Meldiron/gogs/sessions/5f2d56f2-ffda-4c56-8ed1-07f2b443ba3e
Author
1 task
3 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Agent-Logs-Url: https://github.com/Meldiron/gogs/sessions/5f2d56f2-ffda-4c56-8ed1-07f2b443ba3e
Describe the pull request
Fixes bug when updating existing file only worked for default branch, not branch provided in payload
Link to the issue: #8210
Checklist
Test plan
New test added for scenario described in issue
## Additional info
This PR was made by AI, but I am available to address PR comments manually. I self-reviewed before submitting changes.