@@ -95,29 +95,36 @@ 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
- // Create test commits with file information
126
+ // Test payload optimization: should truncate to 1 file per field
127
+ notifier := & webhookNotifier {}
121
128
apiCommits := []* api.PayloadCommit {
122
129
{
123
130
ID : "abc123" ,
@@ -134,39 +141,94 @@ func TestWebhookPayloadOptimization(t *testing.T) {
134
141
Modified : []string {"file1.txt" },
135
142
},
136
143
}
137
-
138
144
apiHeadCommit := & api.PayloadCommit {
139
145
ID : "def456" ,
140
146
Message : "Another commit" ,
141
147
Added : []string {"file3.txt" },
142
148
Removed : []string {},
143
149
Modified : []string {"file1.txt" },
144
150
}
145
-
146
- // Test payload optimization
147
- 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
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
164
167
err = webhook_model .UpdateWebhook (db .DefaultContext , webhook )
165
168
assert .NoError (t , err )
169
+ apiCommits = []* api.PayloadCommit {
170
+ {
171
+ ID : "abc123" ,
172
+ Message : "Test commit" ,
173
+ Added : []string {"file1.txt" , "file2.txt" },
174
+ Removed : []string {"oldfile.txt" },
175
+ Modified : []string {"modified.txt" },
176
+ },
177
+ {
178
+ ID : "def456" ,
179
+ Message : "Another commit" ,
180
+ Added : []string {"file3.txt" },
181
+ Removed : []string {},
182
+ Modified : []string {"file1.txt" },
183
+ },
184
+ }
185
+ apiHeadCommit = & api.PayloadCommit {
186
+ ID : "def456" ,
187
+ Message : "Another commit" ,
188
+ Added : []string {"file3.txt" },
189
+ Removed : []string {},
190
+ Modified : []string {"file1.txt" },
191
+ }
192
+ optimizedCommits , _ = notifier .applyWebhookPayloadOptimizations (db .DefaultContext , repo , apiCommits , apiHeadCommit )
193
+ assert .Len (t , optimizedCommits , 1 )
194
+ assert .Equal (t , "abc123" , optimizedCommits [0 ].ID )
166
195
196
+ // Test with no limits (0 means unlimited)
197
+ webhook .ExcludeFilesLimit = 0
198
+ webhook .ExcludeCommitsLimit = 0
199
+ err = webhook_model .UpdateWebhook (db .DefaultContext , webhook )
200
+ assert .NoError (t , err )
201
+ apiCommits = []* api.PayloadCommit {
202
+ {
203
+ ID : "abc123" ,
204
+ Message : "Test commit" ,
205
+ Added : []string {"file1.txt" , "file2.txt" },
206
+ Removed : []string {"oldfile.txt" },
207
+ Modified : []string {"modified.txt" },
208
+ },
209
+ {
210
+ ID : "def456" ,
211
+ Message : "Another commit" ,
212
+ Added : []string {"file3.txt" },
213
+ Removed : []string {},
214
+ Modified : []string {"file1.txt" },
215
+ },
216
+ }
217
+ apiHeadCommit = & api.PayloadCommit {
218
+ ID : "def456" ,
219
+ Message : "Another commit" ,
220
+ Added : []string {"file3.txt" },
221
+ Removed : []string {},
222
+ Modified : []string {"file1.txt" },
223
+ }
167
224
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 )
225
+ assert .Equal (t , []string {"file1.txt" , "file2.txt" }, optimizedCommits [0 ].Added )
226
+ assert .Equal (t , []string {"oldfile.txt" }, optimizedCommits [0 ].Removed )
227
+ assert .Equal (t , []string {"modified.txt" }, optimizedCommits [0 ].Modified )
228
+ assert .Equal (t , []string {"file3.txt" }, optimizedCommits [1 ].Added )
229
+ assert .Equal (t , []string {}, optimizedCommits [1 ].Removed )
230
+ assert .Equal (t , []string {"file1.txt" }, optimizedCommits [1 ].Modified )
231
+ assert .Equal (t , []string {"file3.txt" }, optimizedHeadCommit .Added )
232
+ assert .Equal (t , []string {}, optimizedHeadCommit .Removed )
233
+ assert .Equal (t , []string {"file1.txt" }, optimizedHeadCommit .Modified )
172
234
}
0 commit comments