Skip to content

Commit 9682610

Browse files
committed
fix
1 parent 39718b2 commit 9682610

File tree

7 files changed

+110
-68
lines changed

7 files changed

+110
-68
lines changed

models/migrations/v1_24/v321.go renamed to models/migrations/v1_25/v322.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
// Copyright 2025 The Gitea Authors. All rights reserved.
22
// SPDX-License-Identifier: MIT
33

4-
package v1_24
4+
package v1_25
55

66
import (
77
"xorm.io/xorm"
88
)
99

1010
func AddWebhookPayloadOptimizationColumns(x *xorm.Engine) error {
1111
type Webhook struct {
12-
ExcludeFilesLimit int `xorm:"exclude_files_limit NOT NULL DEFAULT 0"`
13-
ExcludeCommitsLimit int `xorm:"exclude_commits_limit NOT NULL DEFAULT 0"`
12+
ExcludeFilesLimit int `xorm:"exclude_files_limit NOT NULL DEFAULT -1"`
13+
ExcludeCommitsLimit int `xorm:"exclude_commits_limit NOT NULL DEFAULT -1"`
1414
}
1515
_, err := x.SyncWithOptions(
1616
xorm.SyncOptions{

models/webhook/webhook.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,8 @@ type Webhook struct {
140140
HeaderAuthorizationEncrypted string `xorm:"TEXT"`
141141

142142
// Payload size optimization options
143-
ExcludeFilesLimit int `xorm:"exclude_files_limit"` // Limit number of file changes in commit payloads, 0 means unlimited
144-
ExcludeCommitsLimit int `xorm:"exclude_commits_limit"` // Limit number of commits in push payloads, 0 means unlimited
143+
ExcludeFilesLimit int `xorm:"exclude_files_limit"` // -1: do not trim, 0: trim all (none kept), >0: keep N file changes in commit payloads
144+
ExcludeCommitsLimit int `xorm:"exclude_commits_limit"` // -1: do not trim, 0: trim all (none kept), >0: keep N commits in push payloads
145145

146146
CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
147147
UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"`

options/locale/locale_en-US.ini

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2426,9 +2426,9 @@ settings.branch_filter = Branch filter
24262426
settings.branch_filter_desc = Branch whitelist for push, branch creation and branch deletion events, specified as glob pattern. If empty or <code>*</code>, events for all branches are reported. See <a href="%[1]s">%[2]s</a> documentation for syntax. Examples: <code>master</code>, <code>{master,release*}</code>.
24272427
settings.payload_optimization = Payload Size Optimization
24282428
settings.exclude_files_limit = Limit file changes
2429-
settings.exclude_files_limit_desc = Limit the number of file changes (added, removed, modified) included in each commit payload. 0 means unlimited.
2429+
settings.exclude_files_limit_desc = -1: do not trim, 0: trim all (none kept), >0: keep N file changes
24302430
settings.exclude_commits_limit = Limit commits
2431-
settings.exclude_commits_limit_desc = Limit the number of commits included in push payloads. 0 means unlimited.
2431+
settings.exclude_commits_limit_desc = -1: do not trim, 0: trim all (none kept), >0: keep N commits
24322432
settings.authorization_header = Authorization Header
24332433
settings.authorization_header_desc = Will be included as authorization header for requests when present. Examples: %s.
24342434
settings.active = Active
@@ -3287,7 +3287,7 @@ auths.tip.github = Register a new OAuth application on %s
32873287
auths.tip.gitlab_new = Register a new application on %s
32883288
auths.tip.google_plus = Obtain OAuth2 client credentials from the Google API console at %s
32893289
auths.tip.openid_connect = Use the OpenID Connect Discovery URL "https://{server}/.well-known/openid-configuration" to specify the endpoints
3290-
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
3290+
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
32913291
auths.tip.discord = Register a new application on %s
32923292
auths.tip.gitea = Register a new OAuth2 application. Guide can be found at %s
32933293
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"

services/forms/repo_form.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,8 +240,8 @@ type WebhookForm struct {
240240
AuthorizationHeader string
241241
Secret string
242242
// Payload size optimization options
243-
ExcludeFilesLimit int // Limit number of file changes in commit payloads, 0 means unlimited
244-
ExcludeCommitsLimit int // Limit number of commits in push payloads, 0 means unlimited
243+
ExcludeFilesLimit int // -1: do not trim, 0: trim all (none kept), >0: keep N file changes in commit payloads
244+
ExcludeCommitsLimit int // -1: do not trim, 0: trim all (none kept), >0: keep N commits in push payloads
245245
}
246246

247247
// PushOnly if the hook will be triggered when push

services/webhook/notifier.go

Lines changed: 48 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -657,47 +657,76 @@ func (m *webhookNotifier) applyWebhookPayloadOptimizations(ctx context.Context,
657657
}
658658

659659
// Check if any webhook has payload optimization options enabled
660-
hasFilesLimit := 0
661-
hasCommitsLimit := 0
660+
hasFilesLimit := -1
661+
hasCommitsLimit := -1
662662
for _, webhook := range webhooks {
663663
if webhook.HasEvent(webhook_module.HookEventPush) {
664-
if webhook.ExcludeFilesLimit > 0 && (hasFilesLimit == 0 || webhook.ExcludeFilesLimit < hasFilesLimit) {
664+
if webhook.ExcludeFilesLimit >= 0 && (hasFilesLimit == -1 || webhook.ExcludeFilesLimit < hasFilesLimit) {
665665
hasFilesLimit = webhook.ExcludeFilesLimit
666666
}
667-
if webhook.ExcludeCommitsLimit > 0 && (hasCommitsLimit == 0 || webhook.ExcludeCommitsLimit < hasCommitsLimit) {
667+
if webhook.ExcludeCommitsLimit >= 0 && (hasCommitsLimit == -1 || webhook.ExcludeCommitsLimit < hasCommitsLimit) {
668668
hasCommitsLimit = webhook.ExcludeCommitsLimit
669669
}
670670
}
671671
}
672672

673673
// Apply payload optimizations based on webhook configurations
674-
if hasFilesLimit > 0 {
674+
// -1 not trim, 0 trim all (none kept), >0 trim to N commits
675+
if hasFilesLimit != -1 {
675676
for _, commit := range apiCommits {
676-
if commit.Added != nil && len(commit.Added) > hasFilesLimit {
677-
commit.Added = commit.Added[:hasFilesLimit]
677+
if commit.Added != nil {
678+
if hasFilesLimit == 0 {
679+
commit.Added = nil
680+
} else if hasFilesLimit > 0 && len(commit.Added) > hasFilesLimit {
681+
commit.Added = commit.Added[:hasFilesLimit]
682+
}
678683
}
679-
if commit.Removed != nil && len(commit.Removed) > hasFilesLimit {
680-
commit.Removed = commit.Removed[:hasFilesLimit]
684+
if commit.Removed != nil {
685+
if hasFilesLimit == 0 {
686+
commit.Removed = nil
687+
} else if hasFilesLimit > 0 && len(commit.Removed) > hasFilesLimit {
688+
commit.Removed = commit.Removed[:hasFilesLimit]
689+
}
681690
}
682-
if commit.Modified != nil && len(commit.Modified) > hasFilesLimit {
683-
commit.Modified = commit.Modified[:hasFilesLimit]
691+
if commit.Modified != nil {
692+
if hasFilesLimit == 0 {
693+
commit.Modified = nil
694+
} else if hasFilesLimit > 0 && len(commit.Modified) > hasFilesLimit {
695+
commit.Modified = commit.Modified[:hasFilesLimit]
696+
}
684697
}
685698
}
686699
if apiHeadCommit != nil {
687-
if apiHeadCommit.Added != nil && len(apiHeadCommit.Added) > hasFilesLimit {
688-
apiHeadCommit.Added = apiHeadCommit.Added[:hasFilesLimit]
700+
if apiHeadCommit.Added != nil {
701+
if hasFilesLimit == 0 {
702+
apiHeadCommit.Added = nil
703+
} else if hasFilesLimit > 0 && len(apiHeadCommit.Added) > hasFilesLimit {
704+
apiHeadCommit.Added = apiHeadCommit.Added[:hasFilesLimit]
705+
}
689706
}
690-
if apiHeadCommit.Removed != nil && len(apiHeadCommit.Removed) > hasFilesLimit {
691-
apiHeadCommit.Removed = apiHeadCommit.Removed[:hasFilesLimit]
707+
if apiHeadCommit.Removed != nil {
708+
if hasFilesLimit == 0 {
709+
apiHeadCommit.Removed = nil
710+
} else if hasFilesLimit > 0 && len(apiHeadCommit.Removed) > hasFilesLimit {
711+
apiHeadCommit.Removed = apiHeadCommit.Removed[:hasFilesLimit]
712+
}
692713
}
693-
if apiHeadCommit.Modified != nil && len(apiHeadCommit.Modified) > hasFilesLimit {
694-
apiHeadCommit.Modified = apiHeadCommit.Modified[:hasFilesLimit]
714+
if apiHeadCommit.Modified != nil {
715+
if hasFilesLimit == 0 {
716+
apiHeadCommit.Modified = nil
717+
} else if hasFilesLimit > 0 && len(apiHeadCommit.Modified) > hasFilesLimit {
718+
apiHeadCommit.Modified = apiHeadCommit.Modified[:hasFilesLimit]
719+
}
695720
}
696721
}
697722
}
698723

699-
if hasCommitsLimit > 0 && len(apiCommits) > hasCommitsLimit {
700-
apiCommits = apiCommits[:hasCommitsLimit]
724+
if hasCommitsLimit != -1 {
725+
if hasCommitsLimit == 0 {
726+
apiCommits = nil
727+
} else if hasCommitsLimit > 0 && len(apiCommits) > hasCommitsLimit {
728+
apiCommits = apiCommits[:hasCommitsLimit]
729+
}
701730
}
702731

703732
return apiCommits, apiHeadCommit

services/webhook/webhook_test.go

Lines changed: 50 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,17 @@ func TestWebhookPayloadOptimization(t *testing.T) {
9898
var optimizedCommits []*api.PayloadCommit
9999
var optimizedHeadCommit *api.PayloadCommit
100100

101-
// Create a test repository
102101
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
103102

104-
// Create a webhook with file limit = 1
103+
// Clean up all webhooks for this repo to avoid interference
104+
webhooks, err := db.Find[webhook_model.Webhook](db.DefaultContext, webhook_model.ListWebhookOptions{RepoID: repo.ID})
105+
assert.NoError(t, err)
106+
for _, wh := range webhooks {
107+
err = webhook_model.DeleteWebhookByID(db.DefaultContext, wh.ID)
108+
assert.NoError(t, err)
109+
}
110+
111+
// Case: -1 (no trimming)
105112
webhook := &webhook_model.Webhook{
106113
RepoID: repo.ID,
107114
URL: "http://example.com/webhook",
@@ -110,21 +117,19 @@ func TestWebhookPayloadOptimization(t *testing.T) {
110117
Secret: "secret",
111118
IsActive: true,
112119
Type: webhook_module.GITEA,
113-
ExcludeFilesLimit: 1,
114-
ExcludeCommitsLimit: 0,
120+
ExcludeFilesLimit: -1,
121+
ExcludeCommitsLimit: -1,
115122
HookEvent: &webhook_module.HookEvent{
116123
PushOnly: true,
117124
},
118125
}
119126

120-
err := webhook.UpdateEvent()
127+
err = webhook.UpdateEvent()
121128
assert.NoError(t, err)
122129
err = webhook_model.CreateWebhook(db.DefaultContext, webhook)
123130
assert.NoError(t, err)
124131
assert.NotZero(t, webhook.ID)
125132

126-
// Test payload optimization: should truncate to 1 file per field
127-
notifier := &webhookNotifier{}
128133
apiCommits := []*api.PayloadCommit{
129134
{
130135
ID: "abc123",
@@ -148,22 +153,24 @@ func TestWebhookPayloadOptimization(t *testing.T) {
148153
Removed: []string{},
149154
Modified: []string{"file1.txt"},
150155
}
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)
156+
optimizedCommits, optimizedHeadCommit = (&webhookNotifier{}).applyWebhookPayloadOptimizations(db.DefaultContext, repo, apiCommits, apiHeadCommit)
157+
if assert.NotNil(t, optimizedCommits) && len(optimizedCommits) == 2 {
158+
assert.Equal(t, []string{"file1.txt", "file2.txt"}, optimizedCommits[0].Added)
159+
assert.Equal(t, []string{"oldfile.txt"}, optimizedCommits[0].Removed)
160+
assert.Equal(t, []string{"modified.txt"}, optimizedCommits[0].Modified)
161+
assert.Equal(t, []string{"file3.txt"}, optimizedCommits[1].Added)
162+
assert.Equal(t, []string{}, optimizedCommits[1].Removed)
163+
assert.Equal(t, []string{"file1.txt"}, optimizedCommits[1].Modified)
164+
}
165+
if assert.NotNil(t, optimizedHeadCommit) {
166+
assert.Equal(t, []string{"file3.txt"}, optimizedHeadCommit.Added)
167+
assert.Equal(t, []string{}, optimizedHeadCommit.Removed)
168+
assert.Equal(t, []string{"file1.txt"}, optimizedHeadCommit.Modified)
169+
}
163170

164-
// Test with commit limit = 1
171+
// Case: 0 (keep nothing)
165172
webhook.ExcludeFilesLimit = 0
166-
webhook.ExcludeCommitsLimit = 1
173+
webhook.ExcludeCommitsLimit = 0
167174
err = webhook_model.UpdateWebhook(db.DefaultContext, webhook)
168175
assert.NoError(t, err)
169176
apiCommits = []*api.PayloadCommit{
@@ -189,13 +196,17 @@ func TestWebhookPayloadOptimization(t *testing.T) {
189196
Removed: []string{},
190197
Modified: []string{"file1.txt"},
191198
}
192-
optimizedCommits, _ = notifier.applyWebhookPayloadOptimizations(db.DefaultContext, repo, apiCommits, apiHeadCommit)
193-
assert.Len(t, optimizedCommits, 1)
194-
assert.Equal(t, "abc123", optimizedCommits[0].ID)
199+
optimizedCommits, optimizedHeadCommit = (&webhookNotifier{}).applyWebhookPayloadOptimizations(db.DefaultContext, repo, apiCommits, apiHeadCommit)
200+
assert.Nil(t, optimizedCommits)
201+
if assert.NotNil(t, optimizedHeadCommit) {
202+
assert.Nil(t, optimizedHeadCommit.Added)
203+
assert.Nil(t, optimizedHeadCommit.Removed)
204+
assert.Nil(t, optimizedHeadCommit.Modified)
205+
}
195206

196-
// Test with no limits (0 means unlimited)
197-
webhook.ExcludeFilesLimit = 0
198-
webhook.ExcludeCommitsLimit = 0
207+
// Case: 1 (keep only 1)
208+
webhook.ExcludeFilesLimit = 1
209+
webhook.ExcludeCommitsLimit = 1
199210
err = webhook_model.UpdateWebhook(db.DefaultContext, webhook)
200211
assert.NoError(t, err)
201212
apiCommits = []*api.PayloadCommit{
@@ -221,14 +232,16 @@ func TestWebhookPayloadOptimization(t *testing.T) {
221232
Removed: []string{},
222233
Modified: []string{"file1.txt"},
223234
}
224-
optimizedCommits, optimizedHeadCommit = notifier.applyWebhookPayloadOptimizations(db.DefaultContext, repo, apiCommits, apiHeadCommit)
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)
235+
optimizedCommits, optimizedHeadCommit = (&webhookNotifier{}).applyWebhookPayloadOptimizations(db.DefaultContext, repo, apiCommits, apiHeadCommit)
236+
if assert.NotNil(t, optimizedCommits) && len(optimizedCommits) == 1 {
237+
assert.Equal(t, "abc123", optimizedCommits[0].ID)
238+
assert.Equal(t, []string{"file1.txt"}, optimizedCommits[0].Added)
239+
assert.Equal(t, []string{"oldfile.txt"}, optimizedCommits[0].Removed)
240+
assert.Equal(t, []string{"modified.txt"}, optimizedCommits[0].Modified)
241+
}
242+
if assert.NotNil(t, optimizedHeadCommit) {
243+
assert.Equal(t, []string{"file3.txt"}, optimizedHeadCommit.Added)
244+
assert.Equal(t, []string{}, optimizedHeadCommit.Removed)
245+
assert.Equal(t, []string{"file1.txt"}, optimizedHeadCommit.Modified)
246+
}
234247
}

templates/repo/settings/webhook/settings.tmpl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,12 @@
5252
<h4>{{ctx.Locale.Tr "repo.settings.payload_optimization"}}</h4>
5353
<div class="field">
5454
<label>{{ctx.Locale.Tr "repo.settings.exclude_files_limit"}}</label>
55-
<input name="exclude_files_limit" type="number" min="0" value="{{.Webhook.ExcludeFilesLimit}}" placeholder="0">
55+
<input name="exclude_files_limit" type="number" min="-1" value="{{.Webhook.ExcludeFilesLimit}}" placeholder="-1">
5656
<span class="help">{{ctx.Locale.Tr "repo.settings.exclude_files_limit_desc"}}</span>
5757
</div>
5858
<div class="field">
5959
<label>{{ctx.Locale.Tr "repo.settings.exclude_commits_limit"}}</label>
60-
<input name="exclude_commits_limit" type="number" min="0" value="{{.Webhook.ExcludeCommitsLimit}}" placeholder="0">
60+
<input name="exclude_commits_limit" type="number" min="-1" value="{{.Webhook.ExcludeCommitsLimit}}" placeholder="-1">
6161
<span class="help">{{ctx.Locale.Tr "repo.settings.exclude_commits_limit_desc"}}</span>
6262
</div>
6363
</div>

0 commit comments

Comments
 (0)