Skip to content

Commit f03b608

Browse files
committed
get rule by id first
1 parent 59f2fb1 commit f03b608

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

routers/web/repo/setting_protected_branch.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -166,23 +166,25 @@ func SettingsProtectedBranchPost(ctx *context.Context) {
166166
}
167167

168168
// FIXME: If a new ProtectBranch has a duplicate RuleName, an error should be returned.
169-
170169
// Currently, if a new ProtectBranch with the same name is created,
171170
// the existing ProtectBranch will be updated.
172171
// But we cannot modify this logic now because many unit tests rely on it.
173172

174173
var err error
175-
protectBranch, err = git_model.GetProtectedBranchRuleByName(ctx, ctx.Repo.Repository.ID, f.RuleName)
176-
if err != nil {
177-
ctx.ServerError("GetProtectBranchOfRepoByName", err)
178-
return
179-
}
180-
if protectBranch == nil && f.RuleID > 0 {
174+
if f.RuleID > 0 {
175+
// If the RuleID isn't 0, it must be an edit operation. So we get rule by id.
181176
protectBranch, err = git_model.GetProtectedBranchRuleByID(ctx, ctx.Repo.Repository.ID, f.RuleID)
182177
if err != nil {
183178
ctx.ServerError("GetProtectBranchOfRepoByID", err)
184179
return
185180
}
181+
} else {
182+
// The RuleID is 0, it should be a create operation. We can only get rule by name.
183+
protectBranch, err = git_model.GetProtectedBranchRuleByName(ctx, ctx.Repo.Repository.ID, f.RuleName)
184+
if err != nil {
185+
ctx.ServerError("GetProtectBranchOfRepoByName", err)
186+
return
187+
}
186188
}
187189
if protectBranch == nil {
188190
// No options found, create defaults.

0 commit comments

Comments
 (0)