Skip to content

Commit 1fcb342

Browse files
committed
Return 404 when deleting non-existent branch through api
1 parent 08254cf commit 1fcb342

3 files changed

Lines changed: 35 additions & 24 deletions

File tree

models/git/branch.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ func GetBranch(ctx context.Context, repoID int64, branchName string) (*Branch, e
177177
func IsBranchExist(ctx context.Context, repoID int64, branchName string) (bool, error) {
178178
var branch Branch
179179
has, err := db.GetEngine(ctx).Where("repo_id=?", repoID).And("name=?", branchName).Get(&branch)
180-
if err != nil {
180+
if err != nil && !IsErrBranchNotExist(err) {
181181
return false, err
182182
} else if !has {
183183
return false, nil

services/repository/branch.go

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,8 @@ func CanDeleteBranch(ctx context.Context, repo *repo_model.Repository, branchNam
575575

576576
// DeleteBranch delete branch
577577
func DeleteBranch(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, gitRepo *git.Repository, branchName string, pr *issues_model.PullRequest) error {
578+
var branchCommit *git.Commit
579+
branchExistsInGit := false
578580
err := repo.MustNotBeArchived()
579581
if err != nil {
580582
return err
@@ -584,41 +586,49 @@ func DeleteBranch(ctx context.Context, doer *user_model.User, repo *repo_model.R
584586
return err
585587
}
586588

587-
rawBranch, err := git_model.GetBranch(ctx, repo.ID, branchName)
588-
if err != nil && !git_model.IsErrBranchNotExist(err) {
589-
return fmt.Errorf("GetBranch: %vc", err)
590-
}
591-
592-
// database branch record not exist or it's a deleted branch
593-
notExist := git_model.IsErrBranchNotExist(err) || rawBranch.IsDeleted
589+
if err := db.WithTx(ctx, func(ctx context.Context) error {
590+
branchExistsInDB, err := git_model.IsBranchExist(ctx, repo.ID, branchName)
591+
if err != nil {
592+
return fmt.Errorf("GetBranch: %vc", err)
593+
}
594594

595-
branchCommit, err := gitRepo.GetBranchCommit(branchName)
596-
if err != nil && !errors.Is(err, util.ErrNotExist) {
597-
return err
598-
}
595+
branchCommit, err = gitRepo.GetBranchCommit(branchName)
596+
if err != nil && !errors.Is(err, util.ErrNotExist) {
597+
return err
598+
}
599+
branchExistsInGit = branchCommit != nil
599600

600-
if err := db.WithTx(ctx, func(ctx context.Context) error {
601-
if !notExist {
601+
if !branchExistsInDB {
602+
if branchExistsInGit {
603+
err := gitrepo.DeleteBranch(ctx, repo, branchName, true)
604+
return err
605+
} else {
606+
return git.ErrBranchNotExist{
607+
Name: branchName,
608+
}
609+
}
610+
} else {
602611
if err := git_model.AddDeletedBranch(ctx, repo.ID, branchName, doer.ID); err != nil {
603612
return err
604613
}
605-
}
606614

607-
if pr != nil {
608-
if err := issues_model.AddDeletePRBranchComment(ctx, doer, pr.BaseRepo, pr.Issue.ID, pr.HeadBranch); err != nil {
609-
return fmt.Errorf("DeleteBranch: %v", err)
615+
if pr != nil {
616+
if err := issues_model.AddDeletePRBranchComment(ctx, doer, pr.BaseRepo, pr.Issue.ID, pr.HeadBranch); err != nil {
617+
return fmt.Errorf("DeleteBranch: %v", err)
618+
}
610619
}
611-
}
612-
if branchCommit == nil {
613-
return nil
614-
}
615620

616-
return gitrepo.DeleteBranch(ctx, repo, branchName, true)
621+
if branchExistsInGit {
622+
return gitrepo.DeleteBranch(ctx, repo, branchName, true)
623+
} else {
624+
return nil
625+
}
626+
}
617627
}); err != nil {
618628
return err
619629
}
620630

621-
if branchCommit == nil {
631+
if !branchExistsInGit {
622632
return nil
623633
}
624634

tests/integration/api_branch_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,7 @@ func TestAPIBranchProtection(t *testing.T) {
402402
// Test branch deletion
403403
testAPIDeleteBranch(t, "master", http.StatusForbidden)
404404
testAPIDeleteBranch(t, "branch2", http.StatusNoContent)
405+
testAPIDeleteBranch(t, "branch2", http.StatusNotFound)
405406
}
406407

407408
func TestAPICreateBranchWithSyncBranches(t *testing.T) {

0 commit comments

Comments
 (0)