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