Skip to content

Return 404 when deleting non-existent branch through api#36802

Closed
hramrach wants to merge 8 commits intogo-gitea:mainfrom
hramrach:delete-branch-fix
Closed

Return 404 when deleting non-existent branch through api#36802
hramrach wants to merge 8 commits intogo-gitea:mainfrom
hramrach:delete-branch-fix

Conversation

@hramrach
Copy link
Copy Markdown
Contributor

@hramrach hramrach commented Mar 2, 2026

Fixes: #36682
alternative to #36694

@GiteaBot GiteaBot added the lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. label Mar 2, 2026
@github-actions github-actions bot added the modifies/go Pull requests that update Go code label Mar 2, 2026
wxiaoguang
wxiaoguang previously approved these changes Mar 2, 2026
@GiteaBot GiteaBot added lgtm/need 1 This PR needs approval from one additional maintainer to be merged. and removed lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. labels Mar 2, 2026
@wxiaoguang wxiaoguang dismissed their stale review March 2, 2026 08:19

still not right

@GiteaBot GiteaBot added lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. and removed lgtm/need 1 This PR needs approval from one additional maintainer to be merged. labels Mar 2, 2026
@hramrach hramrach force-pushed the delete-branch-fix branch from 0f7e617 to 61993cc Compare March 2, 2026 08:57
@hramrach hramrach force-pushed the delete-branch-fix branch 6 times, most recently from b2e2e17 to d595378 Compare March 2, 2026 09:59
@wxiaoguang
Copy link
Copy Markdown
Contributor

I can help to edit this PR and complete it, if you don't mind.

@hramrach hramrach force-pushed the delete-branch-fix branch from d595378 to 6f416e5 Compare March 2, 2026 10:55
@hramrach
Copy link
Copy Markdown
Contributor Author

hramrach commented Mar 2, 2026

I tried to do nested branching instead of fallthrough. Maybe that makes the logic clearer?

@hramrach hramrach force-pushed the delete-branch-fix branch 2 times, most recently from 4823544 to 1b52526 Compare March 2, 2026 11:28
@hramrach
Copy link
Copy Markdown
Contributor Author

hramrach commented Mar 2, 2026

There is event git_model.IsBranchExist but it's kind of broken, it returns an error when the branch does not exist.

@hramrach hramrach force-pushed the delete-branch-fix branch from 1b52526 to 1fcb342 Compare March 2, 2026 11:39
@hramrach hramrach force-pushed the delete-branch-fix branch from 1fcb342 to 281bd56 Compare March 2, 2026 12:47
@hramrach
Copy link
Copy Markdown
Contributor Author

hramrach commented Mar 2, 2026

I can help to edit this PR and complete it, if you don't mind.

Feel free to edit the PR. it's enabled after all.

I have no idea how to improve it at this point.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 AddDeletedBranch to MarkBranchAsDeleted and 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

  • MarkBranchAsDeleted returns 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 treating util.ErrNotExist (or git_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

  • MarkBranchAsDeleted currently requires an existing branch row (GetBranch must 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 on ErrBranchNotExist.
// 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.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

wxiaoguang
wxiaoguang previously approved these changes Mar 2, 2026
@GiteaBot GiteaBot added lgtm/need 1 This PR needs approval from one additional maintainer to be merged. and removed lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. labels Mar 2, 2026
@github-actions github-actions bot removed the modifies/api This PR adds API routes or modifies them label Mar 2, 2026
@wxiaoguang wxiaoguang dismissed their stale review March 2, 2026 17:03

dismiss review

@GiteaBot GiteaBot added lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. and removed lgtm/need 1 This PR needs approval from one additional maintainer to be merged. labels Mar 2, 2026
@hramrach
Copy link
Copy Markdown
Contributor Author

hramrach commented Mar 2, 2026

I think moving AddDeletePRBranchComment to the one caller that needs that and renaming AddDeletedBranch to MarkBranchAsDeleted were good cleanups.

@github-actions github-actions bot added the modifies/api This PR adds API routes or modifies them label Mar 2, 2026
@hramrach hramrach force-pushed the delete-branch-fix branch from 2d61422 to f817f07 Compare March 2, 2026 18:01
@lunny
Copy link
Copy Markdown
Member

lunny commented Mar 2, 2026

#36694 merged.

@hramrach hramrach closed this Mar 2, 2026
@GiteaBot GiteaBot removed this from the 1.26.0 milestone Mar 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. modifies/api This PR adds API routes or modifies them modifies/go Pull requests that update Go code type/bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Deleting non-existent branch through API returns 204

5 participants