@@ -21,6 +21,33 @@ import (
21
21
webhook_service "code.gitea.io/gitea/services/webhook"
22
22
)
23
23
24
+ // getBoolFromMap extracts a boolean value from a map with a default fallback
25
+ func getBoolFromMap (m map [string ]interface {}, defaultValue bool ) bool { //nolint:unparam
26
+ if val , ok := m ["enable" ]; ok {
27
+ if boolVal , ok := val .(bool ); ok {
28
+ return boolVal
29
+ }
30
+ }
31
+ return defaultValue
32
+ }
33
+
34
+ // getIntFromMap extracts an integer value from a map with a default fallback
35
+ func getIntFromMap (m map [string ]interface {}, defaultValue int ) int { //nolint:unparam
36
+ if val , ok := m ["limit" ]; ok {
37
+ switch v := val .(type ) {
38
+ case int :
39
+ return v
40
+ case float64 :
41
+ return int (v )
42
+ case string :
43
+ if intVal , err := strconv .Atoi (v ); err == nil {
44
+ return intVal
45
+ }
46
+ }
47
+ }
48
+ return defaultValue
49
+ }
50
+
24
51
// ListOwnerHooks lists the webhooks of the provided owner
25
52
func ListOwnerHooks (ctx * context.APIContext , owner * user_model.User ) {
26
53
opts := & webhook.ListWebhookOptions {
@@ -224,11 +251,40 @@ func addHook(ctx *context.APIContext, form *api.CreateHookOption, ownerID, repoI
224
251
HookEvents : updateHookEvents (form .Events ),
225
252
BranchFilter : form .BranchFilter ,
226
253
},
227
- IsActive : form .Active ,
228
- Type : form .Type ,
229
- ExcludeFilesLimit : form .ExcludeFilesLimit ,
230
- ExcludeCommitsLimit : form .ExcludeCommitsLimit ,
254
+ IsActive : form .Active ,
255
+ Type : form .Type ,
256
+ }
257
+
258
+ // Set payload optimization config
259
+ if form .PayloadOptimization != nil {
260
+ payloadOptConfig := & webhook.PayloadOptimizationConfig {}
261
+
262
+ // Parse files config
263
+ if filesConfig , ok := form .PayloadOptimization ["files" ].(map [string ]interface {}); ok {
264
+ payloadOptConfig .Files = & webhook.PayloadOptimizationItem {
265
+ Enable : getBoolFromMap (filesConfig , false ),
266
+ Limit : getIntFromMap (filesConfig , 0 ),
267
+ }
268
+ } else {
269
+ payloadOptConfig .Files = & webhook.PayloadOptimizationItem {Enable : false , Limit : 0 }
270
+ }
271
+
272
+ // Parse commits config
273
+ if commitsConfig , ok := form .PayloadOptimization ["commits" ].(map [string ]interface {}); ok {
274
+ payloadOptConfig .Commits = & webhook.PayloadOptimizationItem {
275
+ Enable : getBoolFromMap (commitsConfig , false ),
276
+ Limit : getIntFromMap (commitsConfig , 0 ),
277
+ }
278
+ } else {
279
+ payloadOptConfig .Commits = & webhook.PayloadOptimizationItem {Enable : false , Limit : 0 }
280
+ }
281
+
282
+ if err := w .SetPayloadOptimizationConfig (payloadOptConfig ); err != nil {
283
+ ctx .APIErrorInternal (err )
284
+ return nil , false
285
+ }
231
286
}
287
+
232
288
err := w .SetHeaderAuthorization (form .AuthorizationHeader )
233
289
if err != nil {
234
290
ctx .APIErrorInternal (err )
@@ -393,12 +449,34 @@ func editHook(ctx *context.APIContext, form *api.EditHookOption, w *webhook.Webh
393
449
w .IsActive = * form .Active
394
450
}
395
451
396
- if form .ExcludeFilesLimit != nil {
397
- w .ExcludeFilesLimit = * form .ExcludeFilesLimit
398
- }
452
+ // Update payload optimization config
453
+ if form .PayloadOptimization != nil {
454
+ payloadOptConfig := & webhook.PayloadOptimizationConfig {}
455
+
456
+ // Parse files config
457
+ if filesConfig , ok := (* form .PayloadOptimization )["files" ].(map [string ]interface {}); ok {
458
+ payloadOptConfig .Files = & webhook.PayloadOptimizationItem {
459
+ Enable : getBoolFromMap (filesConfig , false ),
460
+ Limit : getIntFromMap (filesConfig , 0 ),
461
+ }
462
+ } else {
463
+ payloadOptConfig .Files = & webhook.PayloadOptimizationItem {Enable : false , Limit : 0 }
464
+ }
399
465
400
- if form .ExcludeCommitsLimit != nil {
401
- w .ExcludeCommitsLimit = * form .ExcludeCommitsLimit
466
+ // Parse commits config
467
+ if commitsConfig , ok := (* form .PayloadOptimization )["commits" ].(map [string ]interface {}); ok {
468
+ payloadOptConfig .Commits = & webhook.PayloadOptimizationItem {
469
+ Enable : getBoolFromMap (commitsConfig , false ),
470
+ Limit : getIntFromMap (commitsConfig , 0 ),
471
+ }
472
+ } else {
473
+ payloadOptConfig .Commits = & webhook.PayloadOptimizationItem {Enable : false , Limit : 0 }
474
+ }
475
+
476
+ if err := w .SetPayloadOptimizationConfig (payloadOptConfig ); err != nil {
477
+ ctx .APIErrorInternal (err )
478
+ return false
479
+ }
402
480
}
403
481
404
482
if err := webhook .UpdateWebhook (ctx , w ); err != nil {
0 commit comments