Skip to content

Commit 318cb85

Browse files
authored
Fix bug the protected branch rule name is conflicted with renamed branch name (#36650)
Fix #36464
1 parent ddacefa commit 318cb85

2 files changed

Lines changed: 55 additions & 2 deletions

File tree

models/git/branch.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -397,10 +397,16 @@ func RenameBranch(ctx context.Context, repo *repo_model.Repository, from, to str
397397

398398
if protectedBranch != nil {
399399
// there is a protect rule for this branch
400-
protectedBranch.RuleName = to
401-
if _, err = sess.ID(protectedBranch.ID).Cols("branch_name").Update(protectedBranch); err != nil {
400+
existingRule, err := GetProtectedBranchRuleByName(ctx, repo.ID, to)
401+
if err != nil {
402402
return err
403403
}
404+
if existingRule == nil || existingRule.ID == protectedBranch.ID {
405+
protectedBranch.RuleName = to
406+
if _, err = sess.ID(protectedBranch.ID).Cols("branch_name").Update(protectedBranch); err != nil {
407+
return err
408+
}
409+
}
404410
} else {
405411
// some glob protect rules may match this branch
406412
protected, err := IsBranchProtected(ctx, repo.ID, from)

models/git/branch_test.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,53 @@ func TestRenameBranch(t *testing.T) {
159159
})
160160
}
161161

162+
func TestRenameBranchProtectedRuleConflict(t *testing.T) {
163+
assert.NoError(t, unittest.PrepareTestDatabase())
164+
repo1 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
165+
master := unittest.AssertExistsAndLoadBean(t, &git_model.Branch{RepoID: repo1.ID, Name: "master"})
166+
167+
devBranch := &git_model.Branch{
168+
RepoID: repo1.ID,
169+
Name: "dev",
170+
CommitID: master.CommitID,
171+
CommitMessage: master.CommitMessage,
172+
CommitTime: master.CommitTime,
173+
PusherID: master.PusherID,
174+
}
175+
assert.NoError(t, db.Insert(t.Context(), devBranch))
176+
177+
pbDev := git_model.ProtectedBranch{
178+
RepoID: repo1.ID,
179+
RuleName: "dev",
180+
CanPush: true,
181+
}
182+
assert.NoError(t, git_model.UpdateProtectBranch(t.Context(), repo1, &pbDev, git_model.WhitelistOptions{}))
183+
184+
pbMain := git_model.ProtectedBranch{
185+
RepoID: repo1.ID,
186+
RuleName: "main",
187+
CanPush: true,
188+
}
189+
assert.NoError(t, git_model.UpdateProtectBranch(t.Context(), repo1, &pbMain, git_model.WhitelistOptions{}))
190+
191+
assert.NoError(t, git_model.RenameBranch(t.Context(), repo1, "dev", "main", func(ctx context.Context, isDefault bool) error {
192+
return nil
193+
}))
194+
195+
unittest.AssertNotExistsBean(t, &git_model.Branch{RepoID: repo1.ID, Name: "dev"})
196+
unittest.AssertExistsAndLoadBean(t, &git_model.Branch{RepoID: repo1.ID, Name: "main"})
197+
198+
protectedDev, err := git_model.GetProtectedBranchRuleByName(t.Context(), repo1.ID, "dev")
199+
assert.NoError(t, err)
200+
assert.NotNil(t, protectedDev)
201+
assert.Equal(t, "dev", protectedDev.RuleName)
202+
203+
protectedMainByID, err := git_model.GetProtectedBranchRuleByID(t.Context(), repo1.ID, pbMain.ID)
204+
assert.NoError(t, err)
205+
assert.NotNil(t, protectedMainByID)
206+
assert.Equal(t, "main", protectedMainByID.RuleName)
207+
}
208+
162209
func TestOnlyGetDeletedBranchOnCorrectRepo(t *testing.T) {
163210
assert.NoError(t, unittest.PrepareTestDatabase())
164211

0 commit comments

Comments
 (0)