Return 404 when deleting non-existent branch through api#36802
Return 404 when deleting non-existent branch through api#36802hramrach wants to merge 8 commits intogo-gitea:mainfrom
Conversation
0f7e617 to
61993cc
Compare
b2e2e17 to
d595378
Compare
|
I can help to edit this PR and complete it, if you don't mind. |
d595378 to
6f416e5
Compare
|
I tried to do nested branching instead of fallthrough. Maybe that makes the logic clearer? |
4823544 to
1b52526
Compare
|
There is event git_model.IsBranchExist but it's kind of broken, it returns an error when the branch does not exist. |
1b52526 to
1fcb342
Compare
1fcb342 to
281bd56
Compare
Feel free to edit the PR. it's enabled after all. I have no idea how to improve it at this point. |
There was a problem hiding this comment.
Pull request overview
This PR fixes the branch deletion API behavior so that deleting a non-existent branch returns 404 Not Found instead of 204 No Content, aligning the endpoint with expected REST semantics.
Changes:
- Update repository branch deletion logic to detect non-existent branches and return a branch-not-exist error.
- Refactor deleted-branch DB updates by renaming
AddDeletedBranchtoMarkBranchAsDeletedand updating call sites. - Add/adjust integration and model tests to cover the new API behavior and updated service signature.
Reviewed changes
Copilot reviewed 8 out of 8 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
services/repository/branch.go |
Refactors DeleteBranch to return not-found when appropriate; adjusts post-merge deletion flow. |
routers/api/v1/repo/branch.go |
Uses updated DeleteBranch signature so API can surface 404 on missing branches. |
routers/web/repo/branch.go |
Updates web branch deletion path to use new DeleteBranch signature. |
routers/private/hook_post_receive.go |
Switches deleted-branch DB update to MarkBranchAsDeleted during git hook processing. |
models/git/branch.go |
Renames and redefines deleted-branch DB mutation helper as MarkBranchAsDeleted. |
models/git/branch_test.go |
Updates model test to use MarkBranchAsDeleted. |
tests/integration/api_branch_test.go |
Adds regression test asserting 404 when deleting a branch that’s already deleted. |
tests/integration/actions_trigger_test.go |
Updates service call to match new DeleteBranch signature. |
Comments suppressed due to low confidence (2)
routers/private/hook_post_receive.go:114
MarkBranchAsDeletedreturns an error when the branch record doesn't exist in the DB, and this handler treats that as a 500. Since branch rows may be missing (e.g., branches not yet synced, or cleaned up), deleting a ref could incorrectly fail the post-receive hook. Consider treatingutil.ErrNotExist(orgit_model.ErrBranchNotExist) as a no-op here, or changing the DB update helper to upsert a deleted-branch record.
if update.IsDelRef() {
if err := git_model.MarkBranchAsDeleted(ctx, repo.ID, update.RefFullName.BranchName(), update.PusherID); err != nil {
log.Error("Failed to add deleted branch: %s/%s Error: %v", ownerName, repoName, err)
ctx.JSON(http.StatusInternalServerError, private.HookPostReceiveResult{
Err: fmt.Sprintf("Failed to add deleted branch: %s/%s Error: %v", ownerName, repoName, err),
})
return
models/git/branch.go:276
MarkBranchAsDeletedcurrently requires an existing branch row (GetBranchmust succeed). Some callers (eg post-receive handling of delete refs) may need this operation to be idempotent even when the branch was never synced to DB. Consider making this function handle missing branches by inserting a deleted record (upsert), or provide a separate helper with that behavior so callers can avoid hard-failing onErrBranchNotExist.
// MarkBranchAsDeleted marks branch as deleted
func MarkBranchAsDeleted(ctx context.Context, repoID int64, branchName string, deletedByID int64) error {
branch, err := GetBranch(ctx, repoID, branchName)
if err != nil {
return err
}
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 8 out of 8 changed files in this pull request and generated 3 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
c213044 to
281bd56
Compare
|
I think moving AddDeletePRBranchComment to the one caller that needs that and renaming AddDeletedBranch to MarkBranchAsDeleted were good cleanups. |
2d61422 to
f817f07
Compare
|
#36694 merged. |
Fixes: #36682
alternative to #36694