Skip to content

Commit 6e07c6f

Browse files
committed
add webhook payload optimization API support
1 parent 42a880f commit 6e07c6f

File tree

4 files changed

+80
-3
lines changed

4 files changed

+80
-3
lines changed

modules/structs/hook.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ type Hook struct {
2525
Events []string `json:"events"`
2626
AuthorizationHeader string `json:"authorization_header"`
2727
Active bool `json:"active"`
28+
// Payload size optimization options
29+
ExcludeFilesLimit int `json:"exclude_files_limit"` // -1: trim all (none kept), 0: do not trim, >0: keep N file changes
30+
ExcludeCommitsLimit int `json:"exclude_commits_limit"` // -1: trim all (none kept), 0: do not trim, >0: keep N commits
2831
// swagger:strfmt date-time
2932
Updated time.Time `json:"updated_at"`
3033
// swagger:strfmt date-time
@@ -48,6 +51,9 @@ type CreateHookOption struct {
4851
Events []string `json:"events"`
4952
BranchFilter string `json:"branch_filter" binding:"GlobPattern"`
5053
AuthorizationHeader string `json:"authorization_header"`
54+
// Payload size optimization options
55+
ExcludeFilesLimit int `json:"exclude_files_limit"` // -1: trim all (none kept), 0: do not trim, >0: keep N file changes
56+
ExcludeCommitsLimit int `json:"exclude_commits_limit"` // -1: trim all (none kept), 0: do not trim, >0: keep N commits
5157
// default: false
5258
Active bool `json:"active"`
5359
}
@@ -58,7 +64,10 @@ type EditHookOption struct {
5864
Events []string `json:"events"`
5965
BranchFilter string `json:"branch_filter" binding:"GlobPattern"`
6066
AuthorizationHeader string `json:"authorization_header"`
61-
Active *bool `json:"active"`
67+
// Payload size optimization options
68+
ExcludeFilesLimit *int `json:"exclude_files_limit"` // -1: trim all (none kept), 0: do not trim, >0: keep N file changes
69+
ExcludeCommitsLimit *int `json:"exclude_commits_limit"` // -1: trim all (none kept), 0: do not trim, >0: keep N commits
70+
Active *bool `json:"active"`
6271
}
6372

6473
// Payloader payload is some part of one hook

routers/api/v1/utils/hook.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,8 +224,10 @@ func addHook(ctx *context.APIContext, form *api.CreateHookOption, ownerID, repoI
224224
HookEvents: updateHookEvents(form.Events),
225225
BranchFilter: form.BranchFilter,
226226
},
227-
IsActive: form.Active,
228-
Type: form.Type,
227+
IsActive: form.Active,
228+
Type: form.Type,
229+
ExcludeFilesLimit: form.ExcludeFilesLimit,
230+
ExcludeCommitsLimit: form.ExcludeCommitsLimit,
229231
}
230232
err := w.SetHeaderAuthorization(form.AuthorizationHeader)
231233
if err != nil {
@@ -391,6 +393,14 @@ func editHook(ctx *context.APIContext, form *api.EditHookOption, w *webhook.Webh
391393
w.IsActive = *form.Active
392394
}
393395

396+
if form.ExcludeFilesLimit != nil {
397+
w.ExcludeFilesLimit = *form.ExcludeFilesLimit
398+
}
399+
400+
if form.ExcludeCommitsLimit != nil {
401+
w.ExcludeCommitsLimit = *form.ExcludeCommitsLimit
402+
}
403+
394404
if err := webhook.UpdateWebhook(ctx, w); err != nil {
395405
ctx.APIErrorInternal(err)
396406
return false

services/webhook/general.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,8 @@ func ToHook(repoLink string, w *webhook_model.Webhook) (*api.Hook, error) {
417417
Config: config,
418418
Events: w.EventsArray(),
419419
AuthorizationHeader: authorizationHeader,
420+
ExcludeFilesLimit: w.ExcludeFilesLimit,
421+
ExcludeCommitsLimit: w.ExcludeCommitsLimit,
420422
Updated: w.UpdatedUnix.AsTime(),
421423
Created: w.CreatedUnix.AsTime(),
422424
BranchFilter: w.BranchFilter,

tests/integration/repo_webhook_test.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1222,3 +1222,59 @@ jobs:
12221222
assert.Equal(t, "user2/repo1", webhookData.payloads[i].Repo.FullName)
12231223
}
12241224
}
1225+
1226+
func Test_WebhookPayloadOptimizationAPI(t *testing.T) {
1227+
onGiteaRun(t, func(t *testing.T, giteaURL *url.URL) {
1228+
session := loginUser(t, "user2")
1229+
1230+
// Test creating webhook with payload optimization options via API
1231+
createHookOption := map[string]any{
1232+
"type": "gitea",
1233+
"config": map[string]string{
1234+
"url": "http://example.com/webhook",
1235+
"content_type": "json",
1236+
},
1237+
"events": []string{"push"},
1238+
"exclude_files_limit": 5,
1239+
"exclude_commits_limit": 10,
1240+
"active": true,
1241+
}
1242+
1243+
req := NewRequestWithJSON(t, "POST", "/api/v1/repos/user2/repo1/hooks", createHookOption)
1244+
resp := session.MakeRequest(t, req, http.StatusCreated)
1245+
1246+
var hook api.Hook
1247+
DecodeJSON(t, resp, &hook)
1248+
1249+
// Verify the webhook was created with correct payload optimization settings
1250+
assert.Equal(t, 5, hook.ExcludeFilesLimit)
1251+
assert.Equal(t, 10, hook.ExcludeCommitsLimit)
1252+
1253+
// Test updating webhook with different payload optimization options
1254+
editHookOption := map[string]any{
1255+
"exclude_files_limit": -1,
1256+
"exclude_commits_limit": 0,
1257+
}
1258+
1259+
req = NewRequestWithJSON(t, "PATCH", fmt.Sprintf("/api/v1/repos/user2/repo1/hooks/%d", hook.ID), editHookOption)
1260+
resp = session.MakeRequest(t, req, http.StatusOK)
1261+
1262+
var updatedHook api.Hook
1263+
DecodeJSON(t, resp, &updatedHook)
1264+
1265+
// Verify the webhook was updated with correct payload optimization settings
1266+
assert.Equal(t, -1, updatedHook.ExcludeFilesLimit)
1267+
assert.Equal(t, 0, updatedHook.ExcludeCommitsLimit)
1268+
1269+
// Test getting webhook to verify the settings are persisted
1270+
req = NewRequest(t, "GET", fmt.Sprintf("/api/v1/repos/user2/repo1/hooks/%d", hook.ID))
1271+
resp = session.MakeRequest(t, req, http.StatusOK)
1272+
1273+
var retrievedHook api.Hook
1274+
DecodeJSON(t, resp, &retrievedHook)
1275+
1276+
// Verify the webhook settings are correctly retrieved
1277+
assert.Equal(t, -1, retrievedHook.ExcludeFilesLimit)
1278+
assert.Equal(t, 0, retrievedHook.ExcludeCommitsLimit)
1279+
})
1280+
}

0 commit comments

Comments
 (0)