@@ -21,6 +21,37 @@ 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+ // 
26+ //nolint:unparam // defaultValue is needed for generic helper function 
27+ func  getBoolFromMap (m  map [string ]any , defaultValue  bool ) bool  {
28+ 	if  val , ok  :=  m ["enable" ]; ok  {
29+ 		if  boolVal , ok  :=  val .(bool ); ok  {
30+ 			return  boolVal 
31+ 		}
32+ 	}
33+ 	return  defaultValue 
34+ }
35+ 
36+ // getIntFromMap extracts an integer value from a map with a default fallback 
37+ // 
38+ //nolint:unparam // defaultValue is needed for generic helper function 
39+ func  getIntFromMap (m  map [string ]any , defaultValue  int ) int  {
40+ 	if  val , ok  :=  m ["limit" ]; ok  {
41+ 		switch  v  :=  val .(type ) {
42+ 		case  int :
43+ 			return  v 
44+ 		case  float64 :
45+ 			return  int (v )
46+ 		case  string :
47+ 			if  intVal , err  :=  strconv .Atoi (v ); err  ==  nil  {
48+ 				return  intVal 
49+ 			}
50+ 		}
51+ 	}
52+ 	return  defaultValue 
53+ }
54+ 
2455// ListOwnerHooks lists the webhooks of the provided owner 
2556func  ListOwnerHooks (ctx  * context.APIContext , owner  * user_model.User ) {
2657	opts  :=  & webhook.ListWebhookOptions {
@@ -224,11 +255,40 @@ func addHook(ctx *context.APIContext, form *api.CreateHookOption, ownerID, repoI
224255			HookEvents :   updateHookEvents (form .Events ),
225256			BranchFilter : form .BranchFilter ,
226257		},
227- 		IsActive :            form .Active ,
228- 		Type :                form .Type ,
229- 		ExcludeFilesLimit :   form .ExcludeFilesLimit ,
230- 		ExcludeCommitsLimit : form .ExcludeCommitsLimit ,
258+ 		IsActive : form .Active ,
259+ 		Type :     form .Type ,
260+ 	}
261+ 
262+ 	// Set payload optimization config 
263+ 	if  form .PayloadOptimization  !=  nil  {
264+ 		payloadOptConfig  :=  & webhook.PayloadOptimizationConfig {}
265+ 
266+ 		// Parse files config 
267+ 		if  filesConfig , ok  :=  form .PayloadOptimization ["files" ].(map [string ]any ); ok  {
268+ 			payloadOptConfig .Files  =  & webhook.PayloadOptimizationItem {
269+ 				Enable : getBoolFromMap (filesConfig , false ),
270+ 				Limit :  getIntFromMap (filesConfig , 0 ),
271+ 			}
272+ 		} else  {
273+ 			payloadOptConfig .Files  =  & webhook.PayloadOptimizationItem {Enable : false , Limit : 0 }
274+ 		}
275+ 
276+ 		// Parse commits config 
277+ 		if  commitsConfig , ok  :=  form .PayloadOptimization ["commits" ].(map [string ]any ); ok  {
278+ 			payloadOptConfig .Commits  =  & webhook.PayloadOptimizationItem {
279+ 				Enable : getBoolFromMap (commitsConfig , false ),
280+ 				Limit :  getIntFromMap (commitsConfig , 0 ),
281+ 			}
282+ 		} else  {
283+ 			payloadOptConfig .Commits  =  & webhook.PayloadOptimizationItem {Enable : false , Limit : 0 }
284+ 		}
285+ 
286+ 		if  err  :=  w .SetPayloadOptimizationConfig (payloadOptConfig ); err  !=  nil  {
287+ 			ctx .APIErrorInternal (err )
288+ 			return  nil , false 
289+ 		}
231290	}
291+ 
232292	err  :=  w .SetHeaderAuthorization (form .AuthorizationHeader )
233293	if  err  !=  nil  {
234294		ctx .APIErrorInternal (err )
@@ -393,12 +453,34 @@ func editHook(ctx *context.APIContext, form *api.EditHookOption, w *webhook.Webh
393453		w .IsActive  =  * form .Active 
394454	}
395455
396- 	if  form .ExcludeFilesLimit  !=  nil  {
397- 		w .ExcludeFilesLimit  =  * form .ExcludeFilesLimit 
398- 	}
456+ 	// Update payload optimization config 
457+ 	if  form .PayloadOptimization  !=  nil  {
458+ 		payloadOptConfig  :=  & webhook.PayloadOptimizationConfig {}
459+ 
460+ 		// Parse files config 
461+ 		if  filesConfig , ok  :=  (* form .PayloadOptimization )["files" ].(map [string ]any ); ok  {
462+ 			payloadOptConfig .Files  =  & webhook.PayloadOptimizationItem {
463+ 				Enable : getBoolFromMap (filesConfig , false ),
464+ 				Limit :  getIntFromMap (filesConfig , 0 ),
465+ 			}
466+ 		} else  {
467+ 			payloadOptConfig .Files  =  & webhook.PayloadOptimizationItem {Enable : false , Limit : 0 }
468+ 		}
399469
400- 	if  form .ExcludeCommitsLimit  !=  nil  {
401- 		w .ExcludeCommitsLimit  =  * form .ExcludeCommitsLimit 
470+ 		// Parse commits config 
471+ 		if  commitsConfig , ok  :=  (* form .PayloadOptimization )["commits" ].(map [string ]any ); ok  {
472+ 			payloadOptConfig .Commits  =  & webhook.PayloadOptimizationItem {
473+ 				Enable : getBoolFromMap (commitsConfig , false ),
474+ 				Limit :  getIntFromMap (commitsConfig , 0 ),
475+ 			}
476+ 		} else  {
477+ 			payloadOptConfig .Commits  =  & webhook.PayloadOptimizationItem {Enable : false , Limit : 0 }
478+ 		}
479+ 
480+ 		if  err  :=  w .SetPayloadOptimizationConfig (payloadOptConfig ); err  !=  nil  {
481+ 			ctx .APIErrorInternal (err )
482+ 			return  false 
483+ 		}
402484	}
403485
404486	if  err  :=  webhook .UpdateWebhook (ctx , w ); err  !=  nil  {
0 commit comments