From 1915361b42257cb47c45e3f6b84df98d65c8f0a7 Mon Sep 17 00:00:00 2001 From: kerwin612 Date: Sat, 19 Jul 2025 22:11:11 +0800 Subject: [PATCH 1/3] Add webhook payload size optimization options --- models/migrations/migrations.go | 1 + models/migrations/v1_24/v321.go | 23 ++++++ models/webhook/webhook.go | 4 + models/webhook/webhook_test.go | 42 ++++++++++ options/locale/locale_en-US.ini | 5 ++ routers/web/repo/setting/webhook.go | 4 + services/forms/repo_form.go | 3 + services/webhook/notifier.go | 60 ++++++++++++++ services/webhook/webhook_test.go | 79 +++++++++++++++++++ templates/repo/settings/webhook/settings.tmpl | 19 +++++ 10 files changed, 240 insertions(+) create mode 100644 models/migrations/v1_24/v321.go diff --git a/models/migrations/migrations.go b/models/migrations/migrations.go index 176372486e8f6..525e667668923 100644 --- a/models/migrations/migrations.go +++ b/models/migrations/migrations.go @@ -382,6 +382,7 @@ func prepareMigrationTasks() []*migration { newMigration(318, "Add anonymous_access_mode for repo_unit", v1_24.AddRepoUnitAnonymousAccessMode), newMigration(319, "Add ExclusiveOrder to Label table", v1_24.AddExclusiveOrderColumnToLabelTable), newMigration(320, "Migrate two_factor_policy to login_source table", v1_24.MigrateSkipTwoFactor), + newMigration(321, "Add webhook payload optimization columns", v1_24.AddWebhookPayloadOptimizationColumns), } return preparedMigrations } diff --git a/models/migrations/v1_24/v321.go b/models/migrations/v1_24/v321.go new file mode 100644 index 0000000000000..e88146789e219 --- /dev/null +++ b/models/migrations/v1_24/v321.go @@ -0,0 +1,23 @@ +// Copyright 2025 The Gitea Authors. All rights reserved. +// SPDX-License-Identifier: MIT + +package v1_24 + +import ( + "xorm.io/xorm" +) + +func AddWebhookPayloadOptimizationColumns(x *xorm.Engine) error { + type Webhook struct { + ExcludeFiles bool `xorm:"exclude_files NOT NULL DEFAULT false"` + ExcludeCommits bool `xorm:"exclude_commits NOT NULL DEFAULT false"` + } + _, err := x.SyncWithOptions( + xorm.SyncOptions{ + IgnoreConstrains: true, + IgnoreIndices: true, + }, + new(Webhook), + ) + return err +} diff --git a/models/webhook/webhook.go b/models/webhook/webhook.go index b234d9ffee519..117447de90f52 100644 --- a/models/webhook/webhook.go +++ b/models/webhook/webhook.go @@ -139,6 +139,10 @@ type Webhook struct { // HeaderAuthorizationEncrypted should be accessed using HeaderAuthorization() and SetHeaderAuthorization() HeaderAuthorizationEncrypted string `xorm:"TEXT"` + // Payload size optimization options + ExcludeFiles bool `xorm:"exclude_files"` // Exclude file changes from commit payloads + ExcludeCommits bool `xorm:"exclude_commits"` // Exclude commits and head_commit from push payloads + CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"` UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"` } diff --git a/models/webhook/webhook_test.go b/models/webhook/webhook_test.go index edad8fc996c14..95ec4d6d95106 100644 --- a/models/webhook/webhook_test.go +++ b/models/webhook/webhook_test.go @@ -330,3 +330,45 @@ func TestCleanupHookTaskTable_OlderThan_LeavesTaskEarlierThanAgeToDelete(t *test assert.NoError(t, CleanupHookTaskTable(t.Context(), OlderThan, 168*time.Hour, 0)) unittest.AssertExistsAndLoadBean(t, hookTask) } + +func TestWebhookPayloadOptimization(t *testing.T) { + assert.NoError(t, unittest.PrepareTestDatabase()) + + webhook := &Webhook{ + RepoID: 1, + URL: "http://example.com/webhook", + HTTPMethod: "POST", + ContentType: ContentTypeJSON, + Secret: "secret", + IsActive: true, + Type: webhook_module.GITEA, + ExcludeFiles: true, + ExcludeCommits: false, + HookEvent: &webhook_module.HookEvent{ + PushOnly: true, + }, + } + + // Test creating webhook with payload optimization options + err := CreateWebhook(db.DefaultContext, webhook) + assert.NoError(t, err) + assert.NotZero(t, webhook.ID) + + // Test retrieving webhook and checking payload optimization options + retrievedWebhook, err := GetWebhookByID(db.DefaultContext, webhook.ID) + assert.NoError(t, err) + assert.True(t, retrievedWebhook.ExcludeFiles) + assert.False(t, retrievedWebhook.ExcludeCommits) + + // Test updating webhook with different payload optimization options + retrievedWebhook.ExcludeFiles = false + retrievedWebhook.ExcludeCommits = true + err = UpdateWebhook(db.DefaultContext, retrievedWebhook) + assert.NoError(t, err) + + // Verify the update + updatedWebhook, err := GetWebhookByID(db.DefaultContext, webhook.ID) + assert.NoError(t, err) + assert.False(t, updatedWebhook.ExcludeFiles) + assert.True(t, updatedWebhook.ExcludeCommits) +} diff --git a/options/locale/locale_en-US.ini b/options/locale/locale_en-US.ini index c1a3d37037fe3..b5063b03ceb8c 100644 --- a/options/locale/locale_en-US.ini +++ b/options/locale/locale_en-US.ini @@ -2424,6 +2424,11 @@ settings.event_package = Package settings.event_package_desc = Package created or deleted in a repository. settings.branch_filter = Branch filter settings.branch_filter_desc = Branch whitelist for push, branch creation and branch deletion events, specified as glob pattern. If empty or *, events for all branches are reported. See %[2]s documentation for syntax. Examples: master, {master,release*}. +settings.payload_optimization = Payload Size Optimization +settings.exclude_files = Exclude file changes +settings.exclude_files_desc = Remove file information (added, removed, modified files) from commit payloads to reduce webhook size. +settings.exclude_commits = Exclude commits +settings.exclude_commits_desc = Remove commits and head_commit from push payloads, keeping only before, after and total_commits fields. settings.authorization_header = Authorization Header settings.authorization_header_desc = Will be included as authorization header for requests when present. Examples: %s. settings.active = Active diff --git a/routers/web/repo/setting/webhook.go b/routers/web/repo/setting/webhook.go index f107449749364..ce2bce04db0cd 100644 --- a/routers/web/repo/setting/webhook.go +++ b/routers/web/repo/setting/webhook.go @@ -243,6 +243,8 @@ func createWebhook(ctx *context.Context, params webhookParams) { Meta: string(meta), OwnerID: orCtx.OwnerID, IsSystemWebhook: orCtx.IsSystemWebhook, + ExcludeFiles: params.WebhookForm.ExcludeFiles, + ExcludeCommits: params.WebhookForm.ExcludeCommits, } err = w.SetHeaderAuthorization(params.WebhookForm.AuthorizationHeader) if err != nil { @@ -294,6 +296,8 @@ func editWebhook(ctx *context.Context, params webhookParams) { w.IsActive = params.WebhookForm.Active w.HTTPMethod = params.HTTPMethod w.Meta = string(meta) + w.ExcludeFiles = params.WebhookForm.ExcludeFiles + w.ExcludeCommits = params.WebhookForm.ExcludeCommits err = w.SetHeaderAuthorization(params.WebhookForm.AuthorizationHeader) if err != nil { diff --git a/services/forms/repo_form.go b/services/forms/repo_form.go index cb267f891ccb7..cf933f67a9b0e 100644 --- a/services/forms/repo_form.go +++ b/services/forms/repo_form.go @@ -239,6 +239,9 @@ type WebhookForm struct { BranchFilter string `binding:"GlobPattern"` AuthorizationHeader string Secret string + // Payload size optimization options + ExcludeFiles bool // Exclude file changes from commit payloads + ExcludeCommits bool // Exclude commits and head_commit from push payloads } // PushOnly if the hook will be triggered when push diff --git a/services/webhook/notifier.go b/services/webhook/notifier.go index 672abd5c95d0e..5f54beba672e9 100644 --- a/services/webhook/notifier.go +++ b/services/webhook/notifier.go @@ -7,6 +7,7 @@ import ( "context" actions_model "code.gitea.io/gitea/models/actions" + "code.gitea.io/gitea/models/db" git_model "code.gitea.io/gitea/models/git" issues_model "code.gitea.io/gitea/models/issues" "code.gitea.io/gitea/models/organization" @@ -15,10 +16,12 @@ import ( access_model "code.gitea.io/gitea/models/perm/access" repo_model "code.gitea.io/gitea/models/repo" user_model "code.gitea.io/gitea/models/user" + webhook_model "code.gitea.io/gitea/models/webhook" "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/gitrepo" "code.gitea.io/gitea/modules/httplib" "code.gitea.io/gitea/modules/log" + "code.gitea.io/gitea/modules/optional" "code.gitea.io/gitea/modules/repository" "code.gitea.io/gitea/modules/setting" api "code.gitea.io/gitea/modules/structs" @@ -640,6 +643,57 @@ func (m *webhookNotifier) IssueChangeMilestone(ctx context.Context, doer *user_m } } +// applyWebhookPayloadOptimizations applies payload size optimizations based on webhook configurations +func (m *webhookNotifier) applyWebhookPayloadOptimizations(ctx context.Context, repo *repo_model.Repository, apiCommits []*api.PayloadCommit, apiHeadCommit *api.PayloadCommit) ([]*api.PayloadCommit, *api.PayloadCommit) { + // Get webhooks for this repository to check their configuration + webhooks, err := db.Find[webhook_model.Webhook](ctx, webhook_model.ListWebhookOptions{ + RepoID: repo.ID, + IsActive: optional.Some(true), + }) + if err != nil { + log.Error("Failed to get webhooks for repository %d: %v", repo.ID, err) + // Continue with default behavior if we can't get webhooks + return apiCommits, apiHeadCommit + } + + // Check if any webhook has payload optimization options enabled + hasExcludeFiles := false + hasExcludeCommits := false + for _, webhook := range webhooks { + if webhook.HasEvent(webhook_module.HookEventPush) { + if webhook.ExcludeFiles { + hasExcludeFiles = true + } + if webhook.ExcludeCommits { + hasExcludeCommits = true + } + } + } + + // Apply payload optimizations based on webhook configurations + if hasExcludeFiles { + // Remove file information from commits + for _, commit := range apiCommits { + commit.Added = nil + commit.Removed = nil + commit.Modified = nil + } + if apiHeadCommit != nil { + apiHeadCommit.Added = nil + apiHeadCommit.Removed = nil + apiHeadCommit.Modified = nil + } + } + + if hasExcludeCommits { + // Exclude commits and head_commit from payload + apiCommits = nil + apiHeadCommit = nil + } + + return apiCommits, apiHeadCommit +} + func (m *webhookNotifier) PushCommits(ctx context.Context, pusher *user_model.User, repo *repo_model.Repository, opts *repository.PushUpdateOptions, commits *repository.PushCommits) { apiPusher := convert.ToUser(ctx, pusher, nil) apiCommits, apiHeadCommit, err := commits.ToAPIPayloadCommits(ctx, repo) @@ -648,6 +702,9 @@ func (m *webhookNotifier) PushCommits(ctx context.Context, pusher *user_model.Us return } + // Apply payload optimizations + apiCommits, apiHeadCommit = m.applyWebhookPayloadOptimizations(ctx, repo, apiCommits, apiHeadCommit) + if err := PrepareWebhooks(ctx, EventSource{Repository: repo}, webhook_module.HookEventPush, &api.PushPayload{ Ref: opts.RefFullName.String(), Before: opts.OldCommitID, @@ -887,6 +944,9 @@ func (m *webhookNotifier) SyncPushCommits(ctx context.Context, pusher *user_mode return } + // Apply payload optimizations + apiCommits, apiHeadCommit = m.applyWebhookPayloadOptimizations(ctx, repo, apiCommits, apiHeadCommit) + if err := PrepareWebhooks(ctx, EventSource{Repository: repo}, webhook_module.HookEventPush, &api.PushPayload{ Ref: opts.RefFullName.String(), Before: opts.OldCommitID, diff --git a/services/webhook/webhook_test.go b/services/webhook/webhook_test.go index 5a805347e38a7..00dfe540df168 100644 --- a/services/webhook/webhook_test.go +++ b/services/webhook/webhook_test.go @@ -91,3 +91,82 @@ func TestWebhookUserMail(t *testing.T) { assert.Equal(t, user.GetPlaceholderEmail(), convert.ToUser(db.DefaultContext, user, nil).Email) assert.Equal(t, user.Email, convert.ToUser(db.DefaultContext, user, user).Email) } + +func TestWebhookPayloadOptimization(t *testing.T) { + assert.NoError(t, unittest.PrepareTestDatabase()) + + // Create a test repository + repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}) + + // Create a webhook with payload optimization enabled + webhook := &webhook_model.Webhook{ + RepoID: repo.ID, + URL: "http://example.com/webhook", + HTTPMethod: "POST", + ContentType: webhook_model.ContentTypeJSON, + Secret: "secret", + IsActive: true, + Type: webhook_module.GITEA, + ExcludeFiles: true, + ExcludeCommits: false, + HookEvent: &webhook_module.HookEvent{ + PushOnly: true, + }, + } + + err := webhook_model.CreateWebhook(db.DefaultContext, webhook) + assert.NoError(t, err) + + // Create test commits with file information + apiCommits := []*api.PayloadCommit{ + { + ID: "abc123", + Message: "Test commit", + Added: []string{"file1.txt", "file2.txt"}, + Removed: []string{"oldfile.txt"}, + Modified: []string{"modified.txt"}, + }, + { + ID: "def456", + Message: "Another commit", + Added: []string{"file3.txt"}, + Removed: []string{}, + Modified: []string{"file1.txt"}, + }, + } + + apiHeadCommit := &api.PayloadCommit{ + ID: "def456", + Message: "Another commit", + Added: []string{"file3.txt"}, + Removed: []string{}, + Modified: []string{"file1.txt"}, + } + + // Test payload optimization + notifier := &webhookNotifier{} + optimizedCommits, optimizedHeadCommit := notifier.applyWebhookPayloadOptimizations(db.DefaultContext, repo, apiCommits, apiHeadCommit) + + // Verify that file information was removed when ExcludeFiles is true + assert.Nil(t, optimizedCommits[0].Added) + assert.Nil(t, optimizedCommits[0].Removed) + assert.Nil(t, optimizedCommits[0].Modified) + assert.Nil(t, optimizedCommits[1].Added) + assert.Nil(t, optimizedCommits[1].Removed) + assert.Nil(t, optimizedCommits[1].Modified) + assert.Nil(t, optimizedHeadCommit.Added) + assert.Nil(t, optimizedHeadCommit.Removed) + assert.Nil(t, optimizedHeadCommit.Modified) + + // Test with ExcludeCommits enabled + webhook.ExcludeFiles = false + webhook.ExcludeCommits = true + err = webhook_model.UpdateWebhook(db.DefaultContext, webhook) + assert.NoError(t, err) + + optimizedCommits, optimizedHeadCommit = notifier.applyWebhookPayloadOptimizations(db.DefaultContext, repo, apiCommits, apiHeadCommit) + + // Verify that commits and head_commit were excluded + assert.Nil(t, optimizedCommits) + assert.Nil(t, optimizedHeadCommit) +} diff --git a/templates/repo/settings/webhook/settings.tmpl b/templates/repo/settings/webhook/settings.tmpl index a8ad1d6c9e5cf..c390246a71095 100644 --- a/templates/repo/settings/webhook/settings.tmpl +++ b/templates/repo/settings/webhook/settings.tmpl @@ -47,6 +47,25 @@ {{ctx.Locale.Tr "repo.settings.branch_filter_desc" "https://pkg.go.dev/github.com/gobwas/glob#Compile" "github.com/gobwas/glob"}} + +
+

{{ctx.Locale.Tr "repo.settings.payload_optimization"}}

+
+
+ + + {{ctx.Locale.Tr "repo.settings.exclude_files_desc"}} +
+
+
+
+ + + {{ctx.Locale.Tr "repo.settings.exclude_commits_desc"}} +
+
+
+

{{ctx.Locale.Tr "repo.settings.event_desc"}}

From 5472d66d16c6f1cdbfbe02cb56858d83c2867352 Mon Sep 17 00:00:00 2001 From: kerwin612 Date: Wed, 23 Jul 2025 14:41:59 +0800 Subject: [PATCH 2/3] refactor webhook payload optimization: switch from bool to limit number --- models/migrations/v1_24/v321.go | 4 +- models/webhook/webhook.go | 4 +- models/webhook/webhook_test.go | 30 ++-- options/locale/locale_en-US.ini | 8 +- routers/web/repo/setting/webhook.go | 30 ++-- services/forms/repo_form.go | 6 +- services/webhook/notifier.go | 45 +++--- services/webhook/webhook_test.go | 134 +++++++++++++----- templates/repo/settings/webhook/settings.tmpl | 16 +-- 9 files changed, 172 insertions(+), 105 deletions(-) diff --git a/models/migrations/v1_24/v321.go b/models/migrations/v1_24/v321.go index e88146789e219..748ee6b0149d0 100644 --- a/models/migrations/v1_24/v321.go +++ b/models/migrations/v1_24/v321.go @@ -9,8 +9,8 @@ import ( func AddWebhookPayloadOptimizationColumns(x *xorm.Engine) error { type Webhook struct { - ExcludeFiles bool `xorm:"exclude_files NOT NULL DEFAULT false"` - ExcludeCommits bool `xorm:"exclude_commits NOT NULL DEFAULT false"` + ExcludeFilesLimit int `xorm:"exclude_files_limit NOT NULL DEFAULT 0"` + ExcludeCommitsLimit int `xorm:"exclude_commits_limit NOT NULL DEFAULT 0"` } _, err := x.SyncWithOptions( xorm.SyncOptions{ diff --git a/models/webhook/webhook.go b/models/webhook/webhook.go index 117447de90f52..989e140dc42a0 100644 --- a/models/webhook/webhook.go +++ b/models/webhook/webhook.go @@ -140,8 +140,8 @@ type Webhook struct { HeaderAuthorizationEncrypted string `xorm:"TEXT"` // Payload size optimization options - ExcludeFiles bool `xorm:"exclude_files"` // Exclude file changes from commit payloads - ExcludeCommits bool `xorm:"exclude_commits"` // Exclude commits and head_commit from push payloads + ExcludeFilesLimit int `xorm:"exclude_files_limit"` // Limit number of file changes in commit payloads, 0 means unlimited + ExcludeCommitsLimit int `xorm:"exclude_commits_limit"` // Limit number of commits in push payloads, 0 means unlimited CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"` UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"` diff --git a/models/webhook/webhook_test.go b/models/webhook/webhook_test.go index 95ec4d6d95106..db1150772e5cd 100644 --- a/models/webhook/webhook_test.go +++ b/models/webhook/webhook_test.go @@ -335,15 +335,15 @@ func TestWebhookPayloadOptimization(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) webhook := &Webhook{ - RepoID: 1, - URL: "http://example.com/webhook", - HTTPMethod: "POST", - ContentType: ContentTypeJSON, - Secret: "secret", - IsActive: true, - Type: webhook_module.GITEA, - ExcludeFiles: true, - ExcludeCommits: false, + RepoID: 1, + URL: "http://example.com/webhook", + HTTPMethod: "POST", + ContentType: ContentTypeJSON, + Secret: "secret", + IsActive: true, + Type: webhook_module.GITEA, + ExcludeFilesLimit: 1, + ExcludeCommitsLimit: 0, HookEvent: &webhook_module.HookEvent{ PushOnly: true, }, @@ -357,18 +357,18 @@ func TestWebhookPayloadOptimization(t *testing.T) { // Test retrieving webhook and checking payload optimization options retrievedWebhook, err := GetWebhookByID(db.DefaultContext, webhook.ID) assert.NoError(t, err) - assert.True(t, retrievedWebhook.ExcludeFiles) - assert.False(t, retrievedWebhook.ExcludeCommits) + assert.Equal(t, 1, retrievedWebhook.ExcludeFilesLimit) + assert.Equal(t, 0, retrievedWebhook.ExcludeCommitsLimit) // Test updating webhook with different payload optimization options - retrievedWebhook.ExcludeFiles = false - retrievedWebhook.ExcludeCommits = true + retrievedWebhook.ExcludeFilesLimit = 0 + retrievedWebhook.ExcludeCommitsLimit = 2 err = UpdateWebhook(db.DefaultContext, retrievedWebhook) assert.NoError(t, err) // Verify the update updatedWebhook, err := GetWebhookByID(db.DefaultContext, webhook.ID) assert.NoError(t, err) - assert.False(t, updatedWebhook.ExcludeFiles) - assert.True(t, updatedWebhook.ExcludeCommits) + assert.Equal(t, 0, updatedWebhook.ExcludeFilesLimit) + assert.Equal(t, 2, updatedWebhook.ExcludeCommitsLimit) } diff --git a/options/locale/locale_en-US.ini b/options/locale/locale_en-US.ini index b5063b03ceb8c..8199126c9a647 100644 --- a/options/locale/locale_en-US.ini +++ b/options/locale/locale_en-US.ini @@ -2425,10 +2425,10 @@ settings.event_package_desc = Package created or deleted in a repository. settings.branch_filter = Branch filter settings.branch_filter_desc = Branch whitelist for push, branch creation and branch deletion events, specified as glob pattern. If empty or *, events for all branches are reported. See %[2]s documentation for syntax. Examples: master, {master,release*}. settings.payload_optimization = Payload Size Optimization -settings.exclude_files = Exclude file changes -settings.exclude_files_desc = Remove file information (added, removed, modified files) from commit payloads to reduce webhook size. -settings.exclude_commits = Exclude commits -settings.exclude_commits_desc = Remove commits and head_commit from push payloads, keeping only before, after and total_commits fields. +settings.exclude_files_limit = Limit file changes +settings.exclude_files_limit_desc = Limit the number of file changes (added, removed, modified) included in each commit payload. 0 means unlimited. +settings.exclude_commits_limit = Limit commits +settings.exclude_commits_limit_desc = Limit the number of commits included in push payloads. 0 means unlimited. settings.authorization_header = Authorization Header settings.authorization_header_desc = Will be included as authorization header for requests when present. Examples: %s. settings.active = Active diff --git a/routers/web/repo/setting/webhook.go b/routers/web/repo/setting/webhook.go index ce2bce04db0cd..7b6d69674d9c7 100644 --- a/routers/web/repo/setting/webhook.go +++ b/routers/web/repo/setting/webhook.go @@ -232,19 +232,19 @@ func createWebhook(ctx *context.Context, params webhookParams) { } w := &webhook.Webhook{ - RepoID: orCtx.RepoID, - URL: params.URL, - HTTPMethod: params.HTTPMethod, - ContentType: params.ContentType, - Secret: params.WebhookForm.Secret, - HookEvent: ParseHookEvent(params.WebhookForm), - IsActive: params.WebhookForm.Active, - Type: params.Type, - Meta: string(meta), - OwnerID: orCtx.OwnerID, - IsSystemWebhook: orCtx.IsSystemWebhook, - ExcludeFiles: params.WebhookForm.ExcludeFiles, - ExcludeCommits: params.WebhookForm.ExcludeCommits, + RepoID: orCtx.RepoID, + URL: params.URL, + HTTPMethod: params.HTTPMethod, + ContentType: params.ContentType, + Secret: params.WebhookForm.Secret, + HookEvent: ParseHookEvent(params.WebhookForm), + IsActive: params.WebhookForm.Active, + Type: params.Type, + Meta: string(meta), + OwnerID: orCtx.OwnerID, + IsSystemWebhook: orCtx.IsSystemWebhook, + ExcludeFilesLimit: params.WebhookForm.ExcludeFilesLimit, + ExcludeCommitsLimit: params.WebhookForm.ExcludeCommitsLimit, } err = w.SetHeaderAuthorization(params.WebhookForm.AuthorizationHeader) if err != nil { @@ -296,8 +296,8 @@ func editWebhook(ctx *context.Context, params webhookParams) { w.IsActive = params.WebhookForm.Active w.HTTPMethod = params.HTTPMethod w.Meta = string(meta) - w.ExcludeFiles = params.WebhookForm.ExcludeFiles - w.ExcludeCommits = params.WebhookForm.ExcludeCommits + w.ExcludeFilesLimit = params.WebhookForm.ExcludeFilesLimit + w.ExcludeCommitsLimit = params.WebhookForm.ExcludeCommitsLimit err = w.SetHeaderAuthorization(params.WebhookForm.AuthorizationHeader) if err != nil { diff --git a/services/forms/repo_form.go b/services/forms/repo_form.go index cf933f67a9b0e..e9c58bae6c7aa 100644 --- a/services/forms/repo_form.go +++ b/services/forms/repo_form.go @@ -240,8 +240,8 @@ type WebhookForm struct { AuthorizationHeader string Secret string // Payload size optimization options - ExcludeFiles bool // Exclude file changes from commit payloads - ExcludeCommits bool // Exclude commits and head_commit from push payloads + ExcludeFilesLimit int // Limit number of file changes in commit payloads, 0 means unlimited + ExcludeCommitsLimit int // Limit number of commits in push payloads, 0 means unlimited } // PushOnly if the hook will be triggered when push @@ -625,7 +625,7 @@ type UpdateAllowEditsForm struct { // | _// __ \| | _/ __ \\__ \ / ___// __ \ // | | \ ___/| |_\ ___/ / __ \_\___ \\ ___/ // |____|_ /\___ >____/\___ >____ /____ >\___ > -// \/ \/ \/ \/ \/ \/ +// \/ \/ \/ \/ \/ \/ // NewReleaseForm form for creating release type NewReleaseForm struct { diff --git a/services/webhook/notifier.go b/services/webhook/notifier.go index 5f54beba672e9..5c3fc4996b1b4 100644 --- a/services/webhook/notifier.go +++ b/services/webhook/notifier.go @@ -657,38 +657,47 @@ func (m *webhookNotifier) applyWebhookPayloadOptimizations(ctx context.Context, } // Check if any webhook has payload optimization options enabled - hasExcludeFiles := false - hasExcludeCommits := false + hasFilesLimit := 0 + hasCommitsLimit := 0 for _, webhook := range webhooks { if webhook.HasEvent(webhook_module.HookEventPush) { - if webhook.ExcludeFiles { - hasExcludeFiles = true + if webhook.ExcludeFilesLimit > 0 && (hasFilesLimit == 0 || webhook.ExcludeFilesLimit < hasFilesLimit) { + hasFilesLimit = webhook.ExcludeFilesLimit } - if webhook.ExcludeCommits { - hasExcludeCommits = true + if webhook.ExcludeCommitsLimit > 0 && (hasCommitsLimit == 0 || webhook.ExcludeCommitsLimit < hasCommitsLimit) { + hasCommitsLimit = webhook.ExcludeCommitsLimit } } } // Apply payload optimizations based on webhook configurations - if hasExcludeFiles { - // Remove file information from commits + if hasFilesLimit > 0 { for _, commit := range apiCommits { - commit.Added = nil - commit.Removed = nil - commit.Modified = nil + if commit.Added != nil && len(commit.Added) > hasFilesLimit { + commit.Added = commit.Added[:hasFilesLimit] + } + if commit.Removed != nil && len(commit.Removed) > hasFilesLimit { + commit.Removed = commit.Removed[:hasFilesLimit] + } + if commit.Modified != nil && len(commit.Modified) > hasFilesLimit { + commit.Modified = commit.Modified[:hasFilesLimit] + } } if apiHeadCommit != nil { - apiHeadCommit.Added = nil - apiHeadCommit.Removed = nil - apiHeadCommit.Modified = nil + if apiHeadCommit.Added != nil && len(apiHeadCommit.Added) > hasFilesLimit { + apiHeadCommit.Added = apiHeadCommit.Added[:hasFilesLimit] + } + if apiHeadCommit.Removed != nil && len(apiHeadCommit.Removed) > hasFilesLimit { + apiHeadCommit.Removed = apiHeadCommit.Removed[:hasFilesLimit] + } + if apiHeadCommit.Modified != nil && len(apiHeadCommit.Modified) > hasFilesLimit { + apiHeadCommit.Modified = apiHeadCommit.Modified[:hasFilesLimit] + } } } - if hasExcludeCommits { - // Exclude commits and head_commit from payload - apiCommits = nil - apiHeadCommit = nil + if hasCommitsLimit > 0 && len(apiCommits) > hasCommitsLimit { + apiCommits = apiCommits[:hasCommitsLimit] } return apiCommits, apiHeadCommit diff --git a/services/webhook/webhook_test.go b/services/webhook/webhook_test.go index 00dfe540df168..bc9501cbcfa8d 100644 --- a/services/webhook/webhook_test.go +++ b/services/webhook/webhook_test.go @@ -95,29 +95,36 @@ func TestWebhookUserMail(t *testing.T) { func TestWebhookPayloadOptimization(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) + var optimizedCommits []*api.PayloadCommit + var optimizedHeadCommit *api.PayloadCommit + // Create a test repository repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}) - // Create a webhook with payload optimization enabled + // Create a webhook with file limit = 1 webhook := &webhook_model.Webhook{ - RepoID: repo.ID, - URL: "http://example.com/webhook", - HTTPMethod: "POST", - ContentType: webhook_model.ContentTypeJSON, - Secret: "secret", - IsActive: true, - Type: webhook_module.GITEA, - ExcludeFiles: true, - ExcludeCommits: false, + RepoID: repo.ID, + URL: "http://example.com/webhook", + HTTPMethod: "POST", + ContentType: webhook_model.ContentTypeJSON, + Secret: "secret", + IsActive: true, + Type: webhook_module.GITEA, + ExcludeFilesLimit: 1, + ExcludeCommitsLimit: 0, HookEvent: &webhook_module.HookEvent{ PushOnly: true, }, } - err := webhook_model.CreateWebhook(db.DefaultContext, webhook) + err := webhook.UpdateEvent() + assert.NoError(t, err) + err = webhook_model.CreateWebhook(db.DefaultContext, webhook) assert.NoError(t, err) + assert.NotZero(t, webhook.ID) - // Create test commits with file information + // Test payload optimization: should truncate to 1 file per field + notifier := &webhookNotifier{} apiCommits := []*api.PayloadCommit{ { ID: "abc123", @@ -134,7 +141,6 @@ func TestWebhookPayloadOptimization(t *testing.T) { Modified: []string{"file1.txt"}, }, } - apiHeadCommit := &api.PayloadCommit{ ID: "def456", Message: "Another commit", @@ -142,31 +148,87 @@ func TestWebhookPayloadOptimization(t *testing.T) { Removed: []string{}, Modified: []string{"file1.txt"}, } - - // Test payload optimization - notifier := &webhookNotifier{} - optimizedCommits, optimizedHeadCommit := notifier.applyWebhookPayloadOptimizations(db.DefaultContext, repo, apiCommits, apiHeadCommit) - - // Verify that file information was removed when ExcludeFiles is true - assert.Nil(t, optimizedCommits[0].Added) - assert.Nil(t, optimizedCommits[0].Removed) - assert.Nil(t, optimizedCommits[0].Modified) - assert.Nil(t, optimizedCommits[1].Added) - assert.Nil(t, optimizedCommits[1].Removed) - assert.Nil(t, optimizedCommits[1].Modified) - assert.Nil(t, optimizedHeadCommit.Added) - assert.Nil(t, optimizedHeadCommit.Removed) - assert.Nil(t, optimizedHeadCommit.Modified) - - // Test with ExcludeCommits enabled - webhook.ExcludeFiles = false - webhook.ExcludeCommits = true + optimizedCommits, _ = notifier.applyWebhookPayloadOptimizations(db.DefaultContext, repo, apiCommits, apiHeadCommit) + assert.Equal(t, []string{"file1.txt"}, optimizedCommits[0].Added) + assert.Equal(t, []string{"oldfile.txt"}, optimizedCommits[0].Removed) + assert.Equal(t, []string{"modified.txt"}, optimizedCommits[0].Modified) + assert.Equal(t, []string{"file3.txt"}, optimizedCommits[1].Added) + assert.Equal(t, []string{}, optimizedCommits[1].Removed) + assert.Equal(t, []string{"file1.txt"}, optimizedCommits[1].Modified) + + _, optimizedHeadCommit = notifier.applyWebhookPayloadOptimizations(db.DefaultContext, repo, apiCommits, apiHeadCommit) + assert.Equal(t, []string{"file3.txt"}, optimizedHeadCommit.Added) + assert.Equal(t, []string{}, optimizedHeadCommit.Removed) + assert.Equal(t, []string{"file1.txt"}, optimizedHeadCommit.Modified) + + // Test with commit limit = 1 + webhook.ExcludeFilesLimit = 0 + webhook.ExcludeCommitsLimit = 1 err = webhook_model.UpdateWebhook(db.DefaultContext, webhook) assert.NoError(t, err) + apiCommits = []*api.PayloadCommit{ + { + ID: "abc123", + Message: "Test commit", + Added: []string{"file1.txt", "file2.txt"}, + Removed: []string{"oldfile.txt"}, + Modified: []string{"modified.txt"}, + }, + { + ID: "def456", + Message: "Another commit", + Added: []string{"file3.txt"}, + Removed: []string{}, + Modified: []string{"file1.txt"}, + }, + } + apiHeadCommit = &api.PayloadCommit{ + ID: "def456", + Message: "Another commit", + Added: []string{"file3.txt"}, + Removed: []string{}, + Modified: []string{"file1.txt"}, + } + optimizedCommits, _ = notifier.applyWebhookPayloadOptimizations(db.DefaultContext, repo, apiCommits, apiHeadCommit) + assert.Len(t, optimizedCommits, 1) + assert.Equal(t, "abc123", optimizedCommits[0].ID) + // Test with no limits (0 means unlimited) + webhook.ExcludeFilesLimit = 0 + webhook.ExcludeCommitsLimit = 0 + err = webhook_model.UpdateWebhook(db.DefaultContext, webhook) + assert.NoError(t, err) + apiCommits = []*api.PayloadCommit{ + { + ID: "abc123", + Message: "Test commit", + Added: []string{"file1.txt", "file2.txt"}, + Removed: []string{"oldfile.txt"}, + Modified: []string{"modified.txt"}, + }, + { + ID: "def456", + Message: "Another commit", + Added: []string{"file3.txt"}, + Removed: []string{}, + Modified: []string{"file1.txt"}, + }, + } + apiHeadCommit = &api.PayloadCommit{ + ID: "def456", + Message: "Another commit", + Added: []string{"file3.txt"}, + Removed: []string{}, + Modified: []string{"file1.txt"}, + } optimizedCommits, optimizedHeadCommit = notifier.applyWebhookPayloadOptimizations(db.DefaultContext, repo, apiCommits, apiHeadCommit) - - // Verify that commits and head_commit were excluded - assert.Nil(t, optimizedCommits) - assert.Nil(t, optimizedHeadCommit) + assert.Equal(t, []string{"file1.txt", "file2.txt"}, optimizedCommits[0].Added) + assert.Equal(t, []string{"oldfile.txt"}, optimizedCommits[0].Removed) + assert.Equal(t, []string{"modified.txt"}, optimizedCommits[0].Modified) + assert.Equal(t, []string{"file3.txt"}, optimizedCommits[1].Added) + assert.Equal(t, []string{}, optimizedCommits[1].Removed) + assert.Equal(t, []string{"file1.txt"}, optimizedCommits[1].Modified) + assert.Equal(t, []string{"file3.txt"}, optimizedHeadCommit.Added) + assert.Equal(t, []string{}, optimizedHeadCommit.Removed) + assert.Equal(t, []string{"file1.txt"}, optimizedHeadCommit.Modified) } diff --git a/templates/repo/settings/webhook/settings.tmpl b/templates/repo/settings/webhook/settings.tmpl index c390246a71095..ef93f34f1b1a2 100644 --- a/templates/repo/settings/webhook/settings.tmpl +++ b/templates/repo/settings/webhook/settings.tmpl @@ -51,18 +51,14 @@

{{ctx.Locale.Tr "repo.settings.payload_optimization"}}

-
- - - {{ctx.Locale.Tr "repo.settings.exclude_files_desc"}} -
+ + + {{ctx.Locale.Tr "repo.settings.exclude_files_limit_desc"}}
-
- - - {{ctx.Locale.Tr "repo.settings.exclude_commits_desc"}} -
+ + + {{ctx.Locale.Tr "repo.settings.exclude_commits_limit_desc"}}
From 9682610d9adbe6a9d4d4951e24cfd7c0c6af16ec Mon Sep 17 00:00:00 2001 From: kerwin612 Date: Thu, 24 Jul 2025 21:46:10 +0800 Subject: [PATCH 3/3] fix --- .../{v1_24/v321.go => v1_25/v322.go} | 6 +- models/webhook/webhook.go | 4 +- options/locale/locale_en-US.ini | 6 +- services/forms/repo_form.go | 4 +- services/webhook/notifier.go | 67 ++++++++++---- services/webhook/webhook_test.go | 87 +++++++++++-------- templates/repo/settings/webhook/settings.tmpl | 4 +- 7 files changed, 110 insertions(+), 68 deletions(-) rename models/migrations/{v1_24/v321.go => v1_25/v322.go} (92%) diff --git a/models/migrations/v1_24/v321.go b/models/migrations/v1_25/v322.go similarity index 92% rename from models/migrations/v1_24/v321.go rename to models/migrations/v1_25/v322.go index 748ee6b0149d0..b1888e7c681fa 100644 --- a/models/migrations/v1_24/v321.go +++ b/models/migrations/v1_25/v322.go @@ -1,7 +1,7 @@ // Copyright 2025 The Gitea Authors. All rights reserved. // SPDX-License-Identifier: MIT -package v1_24 +package v1_25 import ( "xorm.io/xorm" @@ -9,8 +9,8 @@ import ( func AddWebhookPayloadOptimizationColumns(x *xorm.Engine) error { type Webhook struct { - ExcludeFilesLimit int `xorm:"exclude_files_limit NOT NULL DEFAULT 0"` - ExcludeCommitsLimit int `xorm:"exclude_commits_limit NOT NULL DEFAULT 0"` + ExcludeFilesLimit int `xorm:"exclude_files_limit NOT NULL DEFAULT -1"` + ExcludeCommitsLimit int `xorm:"exclude_commits_limit NOT NULL DEFAULT -1"` } _, err := x.SyncWithOptions( xorm.SyncOptions{ diff --git a/models/webhook/webhook.go b/models/webhook/webhook.go index 03bd411f1e39a..e78a81b7dfd6a 100644 --- a/models/webhook/webhook.go +++ b/models/webhook/webhook.go @@ -140,8 +140,8 @@ type Webhook struct { HeaderAuthorizationEncrypted string `xorm:"TEXT"` // Payload size optimization options - ExcludeFilesLimit int `xorm:"exclude_files_limit"` // Limit number of file changes in commit payloads, 0 means unlimited - ExcludeCommitsLimit int `xorm:"exclude_commits_limit"` // Limit number of commits in push payloads, 0 means unlimited + ExcludeFilesLimit int `xorm:"exclude_files_limit"` // -1: do not trim, 0: trim all (none kept), >0: keep N file changes in commit payloads + ExcludeCommitsLimit int `xorm:"exclude_commits_limit"` // -1: do not trim, 0: trim all (none kept), >0: keep N commits in push payloads CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"` UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"` diff --git a/options/locale/locale_en-US.ini b/options/locale/locale_en-US.ini index 3c296c7a8b459..7fb88791be09b 100644 --- a/options/locale/locale_en-US.ini +++ b/options/locale/locale_en-US.ini @@ -2426,9 +2426,9 @@ settings.branch_filter = Branch filter settings.branch_filter_desc = Branch whitelist for push, branch creation and branch deletion events, specified as glob pattern. If empty or *, events for all branches are reported. See %[2]s documentation for syntax. Examples: master, {master,release*}. settings.payload_optimization = Payload Size Optimization settings.exclude_files_limit = Limit file changes -settings.exclude_files_limit_desc = Limit the number of file changes (added, removed, modified) included in each commit payload. 0 means unlimited. +settings.exclude_files_limit_desc = -1: do not trim, 0: trim all (none kept), >0: keep N file changes settings.exclude_commits_limit = Limit commits -settings.exclude_commits_limit_desc = Limit the number of commits included in push payloads. 0 means unlimited. +settings.exclude_commits_limit_desc = -1: do not trim, 0: trim all (none kept), >0: keep N commits settings.authorization_header = Authorization Header settings.authorization_header_desc = Will be included as authorization header for requests when present. Examples: %s. settings.active = Active @@ -3287,7 +3287,7 @@ auths.tip.github = Register a new OAuth application on %s auths.tip.gitlab_new = Register a new application on %s auths.tip.google_plus = Obtain OAuth2 client credentials from the Google API console at %s auths.tip.openid_connect = Use the OpenID Connect Discovery URL "https://{server}/.well-known/openid-configuration" to specify the endpoints -auths.tip.twitter = Go to %s, create an application and ensure that the “Allow this application to be used to Sign in with Twitter” option is enabled +auths.tip.twitter = Go to %s, create an application and ensure that the "Allow this application to be used to Sign in with Twitter" option is enabled auths.tip.discord = Register a new application on %s auths.tip.gitea = Register a new OAuth2 application. Guide can be found at %s auths.tip.yandex = Create a new application at %s. Select following permissions from the "Yandex.Passport API" section: "Access to email address", "Access to user avatar" and "Access to username, first name and surname, gender" diff --git a/services/forms/repo_form.go b/services/forms/repo_form.go index e9c58bae6c7aa..7d2cc58300ce3 100644 --- a/services/forms/repo_form.go +++ b/services/forms/repo_form.go @@ -240,8 +240,8 @@ type WebhookForm struct { AuthorizationHeader string Secret string // Payload size optimization options - ExcludeFilesLimit int // Limit number of file changes in commit payloads, 0 means unlimited - ExcludeCommitsLimit int // Limit number of commits in push payloads, 0 means unlimited + ExcludeFilesLimit int // -1: do not trim, 0: trim all (none kept), >0: keep N file changes in commit payloads + ExcludeCommitsLimit int // -1: do not trim, 0: trim all (none kept), >0: keep N commits in push payloads } // PushOnly if the hook will be triggered when push diff --git a/services/webhook/notifier.go b/services/webhook/notifier.go index 5c3fc4996b1b4..16f9844be5808 100644 --- a/services/webhook/notifier.go +++ b/services/webhook/notifier.go @@ -657,47 +657,76 @@ func (m *webhookNotifier) applyWebhookPayloadOptimizations(ctx context.Context, } // Check if any webhook has payload optimization options enabled - hasFilesLimit := 0 - hasCommitsLimit := 0 + hasFilesLimit := -1 + hasCommitsLimit := -1 for _, webhook := range webhooks { if webhook.HasEvent(webhook_module.HookEventPush) { - if webhook.ExcludeFilesLimit > 0 && (hasFilesLimit == 0 || webhook.ExcludeFilesLimit < hasFilesLimit) { + if webhook.ExcludeFilesLimit >= 0 && (hasFilesLimit == -1 || webhook.ExcludeFilesLimit < hasFilesLimit) { hasFilesLimit = webhook.ExcludeFilesLimit } - if webhook.ExcludeCommitsLimit > 0 && (hasCommitsLimit == 0 || webhook.ExcludeCommitsLimit < hasCommitsLimit) { + if webhook.ExcludeCommitsLimit >= 0 && (hasCommitsLimit == -1 || webhook.ExcludeCommitsLimit < hasCommitsLimit) { hasCommitsLimit = webhook.ExcludeCommitsLimit } } } // Apply payload optimizations based on webhook configurations - if hasFilesLimit > 0 { + // -1 not trim, 0 trim all (none kept), >0 trim to N commits + if hasFilesLimit != -1 { for _, commit := range apiCommits { - if commit.Added != nil && len(commit.Added) > hasFilesLimit { - commit.Added = commit.Added[:hasFilesLimit] + if commit.Added != nil { + if hasFilesLimit == 0 { + commit.Added = nil + } else if hasFilesLimit > 0 && len(commit.Added) > hasFilesLimit { + commit.Added = commit.Added[:hasFilesLimit] + } } - if commit.Removed != nil && len(commit.Removed) > hasFilesLimit { - commit.Removed = commit.Removed[:hasFilesLimit] + if commit.Removed != nil { + if hasFilesLimit == 0 { + commit.Removed = nil + } else if hasFilesLimit > 0 && len(commit.Removed) > hasFilesLimit { + commit.Removed = commit.Removed[:hasFilesLimit] + } } - if commit.Modified != nil && len(commit.Modified) > hasFilesLimit { - commit.Modified = commit.Modified[:hasFilesLimit] + if commit.Modified != nil { + if hasFilesLimit == 0 { + commit.Modified = nil + } else if hasFilesLimit > 0 && len(commit.Modified) > hasFilesLimit { + commit.Modified = commit.Modified[:hasFilesLimit] + } } } if apiHeadCommit != nil { - if apiHeadCommit.Added != nil && len(apiHeadCommit.Added) > hasFilesLimit { - apiHeadCommit.Added = apiHeadCommit.Added[:hasFilesLimit] + if apiHeadCommit.Added != nil { + if hasFilesLimit == 0 { + apiHeadCommit.Added = nil + } else if hasFilesLimit > 0 && len(apiHeadCommit.Added) > hasFilesLimit { + apiHeadCommit.Added = apiHeadCommit.Added[:hasFilesLimit] + } } - if apiHeadCommit.Removed != nil && len(apiHeadCommit.Removed) > hasFilesLimit { - apiHeadCommit.Removed = apiHeadCommit.Removed[:hasFilesLimit] + if apiHeadCommit.Removed != nil { + if hasFilesLimit == 0 { + apiHeadCommit.Removed = nil + } else if hasFilesLimit > 0 && len(apiHeadCommit.Removed) > hasFilesLimit { + apiHeadCommit.Removed = apiHeadCommit.Removed[:hasFilesLimit] + } } - if apiHeadCommit.Modified != nil && len(apiHeadCommit.Modified) > hasFilesLimit { - apiHeadCommit.Modified = apiHeadCommit.Modified[:hasFilesLimit] + if apiHeadCommit.Modified != nil { + if hasFilesLimit == 0 { + apiHeadCommit.Modified = nil + } else if hasFilesLimit > 0 && len(apiHeadCommit.Modified) > hasFilesLimit { + apiHeadCommit.Modified = apiHeadCommit.Modified[:hasFilesLimit] + } } } } - if hasCommitsLimit > 0 && len(apiCommits) > hasCommitsLimit { - apiCommits = apiCommits[:hasCommitsLimit] + if hasCommitsLimit != -1 { + if hasCommitsLimit == 0 { + apiCommits = nil + } else if hasCommitsLimit > 0 && len(apiCommits) > hasCommitsLimit { + apiCommits = apiCommits[:hasCommitsLimit] + } } return apiCommits, apiHeadCommit diff --git a/services/webhook/webhook_test.go b/services/webhook/webhook_test.go index bc9501cbcfa8d..dd5497c62c7d4 100644 --- a/services/webhook/webhook_test.go +++ b/services/webhook/webhook_test.go @@ -98,10 +98,17 @@ func TestWebhookPayloadOptimization(t *testing.T) { var optimizedCommits []*api.PayloadCommit var optimizedHeadCommit *api.PayloadCommit - // Create a test repository repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}) - // Create a webhook with file limit = 1 + // Clean up all webhooks for this repo to avoid interference + webhooks, err := db.Find[webhook_model.Webhook](db.DefaultContext, webhook_model.ListWebhookOptions{RepoID: repo.ID}) + assert.NoError(t, err) + for _, wh := range webhooks { + err = webhook_model.DeleteWebhookByID(db.DefaultContext, wh.ID) + assert.NoError(t, err) + } + + // Case: -1 (no trimming) webhook := &webhook_model.Webhook{ RepoID: repo.ID, URL: "http://example.com/webhook", @@ -110,21 +117,19 @@ func TestWebhookPayloadOptimization(t *testing.T) { Secret: "secret", IsActive: true, Type: webhook_module.GITEA, - ExcludeFilesLimit: 1, - ExcludeCommitsLimit: 0, + ExcludeFilesLimit: -1, + ExcludeCommitsLimit: -1, HookEvent: &webhook_module.HookEvent{ PushOnly: true, }, } - err := webhook.UpdateEvent() + err = webhook.UpdateEvent() assert.NoError(t, err) err = webhook_model.CreateWebhook(db.DefaultContext, webhook) assert.NoError(t, err) assert.NotZero(t, webhook.ID) - // Test payload optimization: should truncate to 1 file per field - notifier := &webhookNotifier{} apiCommits := []*api.PayloadCommit{ { ID: "abc123", @@ -148,22 +153,24 @@ func TestWebhookPayloadOptimization(t *testing.T) { Removed: []string{}, Modified: []string{"file1.txt"}, } - optimizedCommits, _ = notifier.applyWebhookPayloadOptimizations(db.DefaultContext, repo, apiCommits, apiHeadCommit) - assert.Equal(t, []string{"file1.txt"}, optimizedCommits[0].Added) - assert.Equal(t, []string{"oldfile.txt"}, optimizedCommits[0].Removed) - assert.Equal(t, []string{"modified.txt"}, optimizedCommits[0].Modified) - assert.Equal(t, []string{"file3.txt"}, optimizedCommits[1].Added) - assert.Equal(t, []string{}, optimizedCommits[1].Removed) - assert.Equal(t, []string{"file1.txt"}, optimizedCommits[1].Modified) - - _, optimizedHeadCommit = notifier.applyWebhookPayloadOptimizations(db.DefaultContext, repo, apiCommits, apiHeadCommit) - assert.Equal(t, []string{"file3.txt"}, optimizedHeadCommit.Added) - assert.Equal(t, []string{}, optimizedHeadCommit.Removed) - assert.Equal(t, []string{"file1.txt"}, optimizedHeadCommit.Modified) + optimizedCommits, optimizedHeadCommit = (&webhookNotifier{}).applyWebhookPayloadOptimizations(db.DefaultContext, repo, apiCommits, apiHeadCommit) + if assert.NotNil(t, optimizedCommits) && len(optimizedCommits) == 2 { + assert.Equal(t, []string{"file1.txt", "file2.txt"}, optimizedCommits[0].Added) + assert.Equal(t, []string{"oldfile.txt"}, optimizedCommits[0].Removed) + assert.Equal(t, []string{"modified.txt"}, optimizedCommits[0].Modified) + assert.Equal(t, []string{"file3.txt"}, optimizedCommits[1].Added) + assert.Equal(t, []string{}, optimizedCommits[1].Removed) + assert.Equal(t, []string{"file1.txt"}, optimizedCommits[1].Modified) + } + if assert.NotNil(t, optimizedHeadCommit) { + assert.Equal(t, []string{"file3.txt"}, optimizedHeadCommit.Added) + assert.Equal(t, []string{}, optimizedHeadCommit.Removed) + assert.Equal(t, []string{"file1.txt"}, optimizedHeadCommit.Modified) + } - // Test with commit limit = 1 + // Case: 0 (keep nothing) webhook.ExcludeFilesLimit = 0 - webhook.ExcludeCommitsLimit = 1 + webhook.ExcludeCommitsLimit = 0 err = webhook_model.UpdateWebhook(db.DefaultContext, webhook) assert.NoError(t, err) apiCommits = []*api.PayloadCommit{ @@ -189,13 +196,17 @@ func TestWebhookPayloadOptimization(t *testing.T) { Removed: []string{}, Modified: []string{"file1.txt"}, } - optimizedCommits, _ = notifier.applyWebhookPayloadOptimizations(db.DefaultContext, repo, apiCommits, apiHeadCommit) - assert.Len(t, optimizedCommits, 1) - assert.Equal(t, "abc123", optimizedCommits[0].ID) + optimizedCommits, optimizedHeadCommit = (&webhookNotifier{}).applyWebhookPayloadOptimizations(db.DefaultContext, repo, apiCommits, apiHeadCommit) + assert.Nil(t, optimizedCommits) + if assert.NotNil(t, optimizedHeadCommit) { + assert.Nil(t, optimizedHeadCommit.Added) + assert.Nil(t, optimizedHeadCommit.Removed) + assert.Nil(t, optimizedHeadCommit.Modified) + } - // Test with no limits (0 means unlimited) - webhook.ExcludeFilesLimit = 0 - webhook.ExcludeCommitsLimit = 0 + // Case: 1 (keep only 1) + webhook.ExcludeFilesLimit = 1 + webhook.ExcludeCommitsLimit = 1 err = webhook_model.UpdateWebhook(db.DefaultContext, webhook) assert.NoError(t, err) apiCommits = []*api.PayloadCommit{ @@ -221,14 +232,16 @@ func TestWebhookPayloadOptimization(t *testing.T) { Removed: []string{}, Modified: []string{"file1.txt"}, } - optimizedCommits, optimizedHeadCommit = notifier.applyWebhookPayloadOptimizations(db.DefaultContext, repo, apiCommits, apiHeadCommit) - assert.Equal(t, []string{"file1.txt", "file2.txt"}, optimizedCommits[0].Added) - assert.Equal(t, []string{"oldfile.txt"}, optimizedCommits[0].Removed) - assert.Equal(t, []string{"modified.txt"}, optimizedCommits[0].Modified) - assert.Equal(t, []string{"file3.txt"}, optimizedCommits[1].Added) - assert.Equal(t, []string{}, optimizedCommits[1].Removed) - assert.Equal(t, []string{"file1.txt"}, optimizedCommits[1].Modified) - assert.Equal(t, []string{"file3.txt"}, optimizedHeadCommit.Added) - assert.Equal(t, []string{}, optimizedHeadCommit.Removed) - assert.Equal(t, []string{"file1.txt"}, optimizedHeadCommit.Modified) + optimizedCommits, optimizedHeadCommit = (&webhookNotifier{}).applyWebhookPayloadOptimizations(db.DefaultContext, repo, apiCommits, apiHeadCommit) + if assert.NotNil(t, optimizedCommits) && len(optimizedCommits) == 1 { + assert.Equal(t, "abc123", optimizedCommits[0].ID) + assert.Equal(t, []string{"file1.txt"}, optimizedCommits[0].Added) + assert.Equal(t, []string{"oldfile.txt"}, optimizedCommits[0].Removed) + assert.Equal(t, []string{"modified.txt"}, optimizedCommits[0].Modified) + } + if assert.NotNil(t, optimizedHeadCommit) { + assert.Equal(t, []string{"file3.txt"}, optimizedHeadCommit.Added) + assert.Equal(t, []string{}, optimizedHeadCommit.Removed) + assert.Equal(t, []string{"file1.txt"}, optimizedHeadCommit.Modified) + } } diff --git a/templates/repo/settings/webhook/settings.tmpl b/templates/repo/settings/webhook/settings.tmpl index ef93f34f1b1a2..0f423c7963e1a 100644 --- a/templates/repo/settings/webhook/settings.tmpl +++ b/templates/repo/settings/webhook/settings.tmpl @@ -52,12 +52,12 @@

{{ctx.Locale.Tr "repo.settings.payload_optimization"}}

- + {{ctx.Locale.Tr "repo.settings.exclude_files_limit_desc"}}
- + {{ctx.Locale.Tr "repo.settings.exclude_commits_limit_desc"}}