From 216cf96cd4e085b38b5d7325885dccd98a9de4b2 Mon Sep 17 00:00:00 2001 From: Giteabot Date: Wed, 18 Feb 2026 05:57:43 +0800 Subject: [PATCH] Fix bug the protected branch rule name is conflicted with renamed branch name (#36650) (#36661) Backport #36650 by @lunny Fix #36464 Co-authored-by: Lunny Xiao --- models/git/branch.go | 10 +++++++-- models/git/branch_test.go | 47 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 2 deletions(-) diff --git a/models/git/branch.go b/models/git/branch.go index 7fef9f5ca3..d17d3a1182 100644 --- a/models/git/branch.go +++ b/models/git/branch.go @@ -397,10 +397,16 @@ func RenameBranch(ctx context.Context, repo *repo_model.Repository, from, to str if protectedBranch != nil { // there is a protect rule for this branch - protectedBranch.RuleName = to - if _, err = sess.ID(protectedBranch.ID).Cols("branch_name").Update(protectedBranch); err != nil { + existingRule, err := GetProtectedBranchRuleByName(ctx, repo.ID, to) + if err != nil { return err } + if existingRule == nil || existingRule.ID == protectedBranch.ID { + protectedBranch.RuleName = to + if _, err = sess.ID(protectedBranch.ID).Cols("branch_name").Update(protectedBranch); err != nil { + return err + } + } } else { // some glob protect rules may match this branch protected, err := IsBranchProtected(ctx, repo.ID, from) diff --git a/models/git/branch_test.go b/models/git/branch_test.go index 5be435172b..7b25d93e51 100644 --- a/models/git/branch_test.go +++ b/models/git/branch_test.go @@ -159,6 +159,53 @@ func TestRenameBranch(t *testing.T) { }) } +func TestRenameBranchProtectedRuleConflict(t *testing.T) { + assert.NoError(t, unittest.PrepareTestDatabase()) + repo1 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}) + master := unittest.AssertExistsAndLoadBean(t, &git_model.Branch{RepoID: repo1.ID, Name: "master"}) + + devBranch := &git_model.Branch{ + RepoID: repo1.ID, + Name: "dev", + CommitID: master.CommitID, + CommitMessage: master.CommitMessage, + CommitTime: master.CommitTime, + PusherID: master.PusherID, + } + assert.NoError(t, db.Insert(t.Context(), devBranch)) + + pbDev := git_model.ProtectedBranch{ + RepoID: repo1.ID, + RuleName: "dev", + CanPush: true, + } + assert.NoError(t, git_model.UpdateProtectBranch(t.Context(), repo1, &pbDev, git_model.WhitelistOptions{})) + + pbMain := git_model.ProtectedBranch{ + RepoID: repo1.ID, + RuleName: "main", + CanPush: true, + } + assert.NoError(t, git_model.UpdateProtectBranch(t.Context(), repo1, &pbMain, git_model.WhitelistOptions{})) + + assert.NoError(t, git_model.RenameBranch(t.Context(), repo1, "dev", "main", func(ctx context.Context, isDefault bool) error { + return nil + })) + + unittest.AssertNotExistsBean(t, &git_model.Branch{RepoID: repo1.ID, Name: "dev"}) + unittest.AssertExistsAndLoadBean(t, &git_model.Branch{RepoID: repo1.ID, Name: "main"}) + + protectedDev, err := git_model.GetProtectedBranchRuleByName(t.Context(), repo1.ID, "dev") + assert.NoError(t, err) + assert.NotNil(t, protectedDev) + assert.Equal(t, "dev", protectedDev.RuleName) + + protectedMainByID, err := git_model.GetProtectedBranchRuleByID(t.Context(), repo1.ID, pbMain.ID) + assert.NoError(t, err) + assert.NotNil(t, protectedMainByID) + assert.Equal(t, "main", protectedMainByID.RuleName) +} + func TestOnlyGetDeletedBranchOnCorrectRepo(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase())