@@ -95,20 +95,23 @@ func TestWebhookUserMail(t *testing.T) {
9595func TestWebhookPayloadOptimization (t * testing.T ) {
9696assert .NoError (t , unittest .PrepareTestDatabase ())
9797
98+ var optimizedCommits []* api.PayloadCommit
99+ var optimizedHeadCommit * api.PayloadCommit
100+
98101// Create a test repository
99102repo := unittest .AssertExistsAndLoadBean (t , & repo_model.Repository {ID : 1 })
100103
101- // Create a webhook with payload optimization enabled
104+ // Create a webhook with file limit = 1
102105webhook := & webhook_model.Webhook {
103- RepoID : repo .ID ,
104- URL : "http://example.com/webhook" ,
105- HTTPMethod : "POST" ,
106- ContentType : webhook_model .ContentTypeJSON ,
107- Secret : "secret" ,
108- IsActive : true ,
109- Type : webhook_module .GITEA ,
110- ExcludeFiles : true ,
111- ExcludeCommits : false ,
106+ RepoID : repo .ID ,
107+ URL : "http://example.com/webhook" ,
108+ HTTPMethod : "POST" ,
109+ ContentType : webhook_model .ContentTypeJSON ,
110+ Secret : "secret" ,
111+ IsActive : true ,
112+ Type : webhook_module .GITEA ,
113+ ExcludeFilesLimit : 1 ,
114+ ExcludeCommitsLimit : 0 ,
112115HookEvent : & webhook_module.HookEvent {
113116PushOnly : true ,
114117},
@@ -143,30 +146,43 @@ func TestWebhookPayloadOptimization(t *testing.T) {
143146Modified : []string {"file1.txt" },
144147}
145148
146- // Test payload optimization
149+ // Test payload optimization: should truncate to 1 file per field
147150notifier := & webhookNotifier {}
148- optimizedCommits , optimizedHeadCommit : = notifier .applyWebhookPayloadOptimizations (db .DefaultContext , repo , apiCommits , apiHeadCommit )
149-
150- // Verify that file information was removed when ExcludeFiles is true
151- assert .Nil (t , optimizedCommits [0 ].Added )
152- assert .Nil (t , optimizedCommits [0 ]. Removed )
153- assert .Nil (t , optimizedCommits [0 ]. Modified )
154- assert .Nil (t , optimizedCommits [1 ].Added )
155- assert . Nil ( t , optimizedCommits [ 1 ]. Removed )
156- assert . Nil ( t , optimizedCommits [ 1 ]. Modified )
157- assert .Nil ( t , optimizedHeadCommit .Added )
158- assert .Nil ( t , optimizedHeadCommit .Removed )
159- assert .Nil ( t , optimizedHeadCommit .Modified )
160-
161- // Test with ExcludeCommits enabled
162- webhook .ExcludeFiles = false
163- webhook .ExcludeCommits = true
151+ optimizedCommits , _ = notifier .applyWebhookPayloadOptimizations (db .DefaultContext , repo , apiCommits , apiHeadCommit )
152+ assert . Equal ( t , [] string { "file1.txt" }, optimizedCommits [ 0 ]. Added )
153+ assert . Equal ( t , [] string { "oldfile.txt" }, optimizedCommits [ 0 ]. Removed )
154+ assert .Equal (t , [] string { "modified.txt" }, optimizedCommits [0 ].Modified )
155+ assert .Equal (t , [] string { "file3.txt" }, optimizedCommits [1 ]. Added )
156+ assert .Equal (t , [] string {}, optimizedCommits [1 ]. Removed )
157+ assert .Equal (t , [] string { "file1.txt" }, optimizedCommits [1 ].Modified )
158+
159+ _ , optimizedHeadCommit = notifier . applyWebhookPayloadOptimizations ( db . DefaultContext , repo , apiCommits , apiHeadCommit )
160+ assert .Equal ( t , [] string { "file3.txt" } , optimizedHeadCommit .Added )
161+ assert .Equal ( t , [] string {} , optimizedHeadCommit .Removed )
162+ assert .Equal ( t , [] string { "file1.txt" } , optimizedHeadCommit .Modified )
163+
164+ // Test with commit limit = 1
165+ webhook .ExcludeFilesLimit = 0
166+ webhook .ExcludeCommitsLimit = 1
164167err = webhook_model .UpdateWebhook (db .DefaultContext , webhook )
165168assert .NoError (t , err )
169+ optimizedCommits , _ = notifier .applyWebhookPayloadOptimizations (db .DefaultContext , repo , apiCommits , apiHeadCommit )
170+ assert .Len (t , optimizedCommits , 1 )
171+ assert .Equal (t , "abc123" , optimizedCommits [0 ].ID )
166172
173+ // Test with no limits (0 means unlimited)
174+ webhook .ExcludeFilesLimit = 0
175+ webhook .ExcludeCommitsLimit = 0
176+ err = webhook_model .UpdateWebhook (db .DefaultContext , webhook )
177+ assert .NoError (t , err )
167178optimizedCommits , optimizedHeadCommit = notifier .applyWebhookPayloadOptimizations (db .DefaultContext , repo , apiCommits , apiHeadCommit )
168-
169- // Verify that commits and head_commit were excluded
170- assert .Nil (t , optimizedCommits )
171- assert .Nil (t , optimizedHeadCommit )
179+ assert .Equal (t , []string {"file1.txt" , "file2.txt" }, optimizedCommits [0 ].Added )
180+ assert .Equal (t , []string {"oldfile.txt" }, optimizedCommits [0 ].Removed )
181+ assert .Equal (t , []string {"modified.txt" }, optimizedCommits [0 ].Modified )
182+ assert .Equal (t , []string {"file3.txt" }, optimizedCommits [1 ].Added )
183+ assert .Equal (t , []string {}, optimizedCommits [1 ].Removed )
184+ assert .Equal (t , []string {"file1.txt" }, optimizedCommits [1 ].Modified )
185+ assert .Equal (t , []string {"file3.txt" }, optimizedHeadCommit .Added )
186+ assert .Equal (t , []string {}, optimizedHeadCommit .Removed )
187+ assert .Equal (t , []string {"file1.txt" }, optimizedHeadCommit .Modified )
172188}
0 commit comments