@@ -95,27 +95,33 @@ func TestWebhookUserMail(t *testing.T) {
95
95
func TestWebhookPayloadOptimization (t * testing.T ) {
96
96
assert .NoError (t , unittest .PrepareTestDatabase ())
97
97
98
+ var optimizedCommits []* api.PayloadCommit
99
+ var optimizedHeadCommit * api.PayloadCommit
100
+
98
101
// Create a test repository
99
102
repo := unittest .AssertExistsAndLoadBean (t , & repo_model.Repository {ID : 1 })
100
103
101
- // Create a webhook with payload optimization enabled
104
+ // Create a webhook with file limit = 1
102
105
webhook := & 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 ,
112
115
HookEvent : & webhook_module.HookEvent {
113
116
PushOnly : true ,
114
117
},
115
118
}
116
119
117
- err := webhook_model .CreateWebhook (db .DefaultContext , webhook )
120
+ err := webhook .UpdateEvent ()
121
+ assert .NoError (t , err )
122
+ err = webhook_model .CreateWebhook (db .DefaultContext , webhook )
118
123
assert .NoError (t , err )
124
+ assert .NotZero (t , webhook .ID )
119
125
120
126
// Create test commits with file information
121
127
apiCommits := []* api.PayloadCommit {
@@ -143,30 +149,43 @@ func TestWebhookPayloadOptimization(t *testing.T) {
143
149
Modified : []string {"file1.txt" },
144
150
}
145
151
146
- // Test payload optimization
152
+ // Test payload optimization: should truncate to 1 file per field
147
153
notifier := & 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
154
+ optimizedCommits , _ = notifier .applyWebhookPayloadOptimizations (db .DefaultContext , repo , apiCommits , apiHeadCommit )
155
+ assert . Equal ( t , [] string { "file1.txt" }, optimizedCommits [ 0 ]. Added )
156
+ assert . Equal ( t , [] string { "oldfile.txt" }, optimizedCommits [ 0 ]. Removed )
157
+ assert .Equal (t , [] string { "modified.txt" }, optimizedCommits [0 ].Modified )
158
+ assert .Equal (t , [] string { "file3.txt" }, optimizedCommits [1 ]. Added )
159
+ assert .Equal (t , [] string {}, optimizedCommits [1 ]. Removed )
160
+ assert .Equal (t , [] string { "file1.txt" }, optimizedCommits [1 ].Modified )
161
+
162
+ _ , optimizedHeadCommit = notifier . applyWebhookPayloadOptimizations ( db . DefaultContext , repo , apiCommits , apiHeadCommit )
163
+ assert .Equal ( t , [] string { "file3.txt" } , optimizedHeadCommit .Added )
164
+ assert .Equal ( t , [] string {} , optimizedHeadCommit .Removed )
165
+ assert .Equal ( t , [] string { "file1.txt" } , optimizedHeadCommit .Modified )
166
+
167
+ // Test with commit limit = 1
168
+ webhook .ExcludeFilesLimit = 0
169
+ webhook .ExcludeCommitsLimit = 1
164
170
err = webhook_model .UpdateWebhook (db .DefaultContext , webhook )
165
171
assert .NoError (t , err )
172
+ optimizedCommits , _ = notifier .applyWebhookPayloadOptimizations (db .DefaultContext , repo , apiCommits , apiHeadCommit )
173
+ assert .Len (t , optimizedCommits , 1 )
174
+ assert .Equal (t , "abc123" , optimizedCommits [0 ].ID )
166
175
176
+ // Test with no limits (0 means unlimited)
177
+ webhook .ExcludeFilesLimit = 0
178
+ webhook .ExcludeCommitsLimit = 0
179
+ err = webhook_model .UpdateWebhook (db .DefaultContext , webhook )
180
+ assert .NoError (t , err )
167
181
optimizedCommits , 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 )
182
+ assert .Equal (t , []string {"file1.txt" , "file2.txt" }, optimizedCommits [0 ].Added )
183
+ assert .Equal (t , []string {"oldfile.txt" }, optimizedCommits [0 ].Removed )
184
+ assert .Equal (t , []string {"modified.txt" }, optimizedCommits [0 ].Modified )
185
+ assert .Equal (t , []string {"file3.txt" }, optimizedCommits [1 ].Added )
186
+ assert .Equal (t , []string {}, optimizedCommits [1 ].Removed )
187
+ assert .Equal (t , []string {"file1.txt" }, optimizedCommits [1 ].Modified )
188
+ assert .Equal (t , []string {"file3.txt" }, optimizedHeadCommit .Added )
189
+ assert .Equal (t , []string {}, optimizedHeadCommit .Removed )
190
+ assert .Equal (t , []string {"file1.txt" }, optimizedHeadCommit .Modified )
172
191
}
0 commit comments