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