@@ -166,23 +166,25 @@ func SettingsProtectedBranchPost(ctx *context.Context) {
166
166
}
167
167
168
168
// FIXME: If a new ProtectBranch has a duplicate RuleName, an error should be returned.
169
-
170
169
// Currently, if a new ProtectBranch with the same name is created,
171
170
// the existing ProtectBranch will be updated.
172
171
// But we cannot modify this logic now because many unit tests rely on it.
173
172
174
173
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.
181
176
protectBranch , err = git_model .GetProtectedBranchRuleByID (ctx , ctx .Repo .Repository .ID , f .RuleID )
182
177
if err != nil {
183
178
ctx .ServerError ("GetProtectBranchOfRepoByID" , err )
184
179
return
185
180
}
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
+ }
186
188
}
187
189
if protectBranch == nil {
188
190
// No options found, create defaults.
0 commit comments