@@ -18,6 +18,7 @@ import (
18
18
user_model "code.gitea.io/gitea/models/user"
19
19
"code.gitea.io/gitea/modules/setting"
20
20
"code.gitea.io/gitea/modules/structs"
21
+ "code.gitea.io/gitea/modules/util"
21
22
22
23
"xorm.io/builder"
23
24
)
@@ -130,6 +131,50 @@ func DeleteUser(ctx context.Context, u *user_model.User) (err error) {
130
131
}
131
132
}
132
133
134
+ // ***** START: Branch Protections *****
135
+ {
136
+ const batchSize = 50
137
+ for start := 0 ; ; start += batchSize {
138
+ protections := make ([]* ProtectedBranch , 0 , batchSize )
139
+ // @perf: We can't filter on DB side by u.ID, as those IDs are serialized as JSON strings.
140
+ // We could filter down with `WHERE repo_id IN (reposWithPushPermission(u))`,
141
+ // though that query will be quite complex and tricky to maintain (compare `getRepoAssignees()`).
142
+ // Also, as we didn't update branch protections when removing entries from `access` table,
143
+ // it's safer to iterate all protected branches.
144
+ if err = e .Limit (batchSize , start ).Find (& protections ); err != nil {
145
+ return fmt .Errorf ("findProtectedBranches: %v" , err )
146
+ }
147
+ if len (protections ) == 0 {
148
+ break
149
+ }
150
+ for _ , p := range protections {
151
+ var matched1 , matched2 , matched3 bool
152
+ if len (p .WhitelistUserIDs ) != 0 {
153
+ p .WhitelistUserIDs , matched1 = util .RemoveIDFromList (
154
+ p .WhitelistUserIDs , u .ID )
155
+ }
156
+ if len (p .ApprovalsWhitelistUserIDs ) != 0 {
157
+ p .ApprovalsWhitelistUserIDs , matched2 = util .RemoveIDFromList (
158
+ p .ApprovalsWhitelistUserIDs , u .ID )
159
+ }
160
+ if len (p .MergeWhitelistUserIDs ) != 0 {
161
+ p .MergeWhitelistUserIDs , matched3 = util .RemoveIDFromList (
162
+ p .MergeWhitelistUserIDs , u .ID )
163
+ }
164
+ if matched1 || matched2 || matched3 {
165
+ if _ , err = e .ID (p .ID ).Cols (
166
+ "whitelist_user_i_ds" ,
167
+ "merge_whitelist_user_i_ds" ,
168
+ "approvals_whitelist_user_i_ds" ,
169
+ ).Update (p ); err != nil {
170
+ return fmt .Errorf ("updateProtectedBranches: %v" , err )
171
+ }
172
+ }
173
+ }
174
+ }
175
+ }
176
+ // ***** END: Branch Protections *****
177
+
133
178
// ***** START: PublicKey *****
134
179
if _ , err = e .Delete (& asymkey_model.PublicKey {OwnerID : u .ID }); err != nil {
135
180
return fmt .Errorf ("deletePublicKeys: %v" , err )
0 commit comments