Skip to content

Commit 544e445

Browse files
committed
refactor: swap webhook payload optimization logic (-1/0 values)
1 parent 9682610 commit 544e445

File tree

7 files changed

+30
-30
lines changed

7 files changed

+30
-30
lines changed

models/migrations/v1_25/v322.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import (
99

1010
func AddWebhookPayloadOptimizationColumns(x *xorm.Engine) error {
1111
type Webhook struct {
12-
ExcludeFilesLimit int `xorm:"exclude_files_limit NOT NULL DEFAULT -1"`
13-
ExcludeCommitsLimit int `xorm:"exclude_commits_limit NOT NULL DEFAULT -1"`
12+
ExcludeFilesLimit int `xorm:"exclude_files_limit NOT NULL DEFAULT 0"`
13+
ExcludeCommitsLimit int `xorm:"exclude_commits_limit NOT NULL DEFAULT 0"`
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"` // -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
143+
ExcludeFilesLimit int `xorm:"exclude_files_limit"` // -1: trim all (none kept), 0: do not trim, >0: keep N file changes in commit payloads
144+
ExcludeCommitsLimit int `xorm:"exclude_commits_limit"` // -1: trim all (none kept), 0: do not trim, >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: 2 additions & 2 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 = -1: do not trim, 0: trim all (none kept), >0: keep N file changes
2429+
settings.exclude_files_limit_desc = -1: trim all (none kept), 0: do not trim, >0: keep N file changes
24302430
settings.exclude_commits_limit = Limit commits
2431-
settings.exclude_commits_limit_desc = -1: do not trim, 0: trim all (none kept), >0: keep N commits
2431+
settings.exclude_commits_limit_desc = -1: trim all (none kept), 0: do not trim, >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

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 // -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
243+
ExcludeFilesLimit int // -1: trim all (none kept), 0: do not trim, >0: keep N file changes in commit payloads
244+
ExcludeCommitsLimit int // -1: trim all (none kept), 0: do not trim, >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: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -657,39 +657,39 @@ func (m *webhookNotifier) applyWebhookPayloadOptimizations(ctx context.Context,
657657
}
658658

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

673673
// Apply payload optimizations based on webhook configurations
674-
// -1 not trim, 0 trim all (none kept), >0 trim to N commits
675-
if hasFilesLimit != -1 {
674+
// -1 trim all (none kept), 0 do not trim, >0 trim to N commits
675+
if hasFilesLimit != 0 {
676676
for _, commit := range apiCommits {
677677
if commit.Added != nil {
678-
if hasFilesLimit == 0 {
678+
if hasFilesLimit == -1 {
679679
commit.Added = nil
680680
} else if hasFilesLimit > 0 && len(commit.Added) > hasFilesLimit {
681681
commit.Added = commit.Added[:hasFilesLimit]
682682
}
683683
}
684684
if commit.Removed != nil {
685-
if hasFilesLimit == 0 {
685+
if hasFilesLimit == -1 {
686686
commit.Removed = nil
687687
} else if hasFilesLimit > 0 && len(commit.Removed) > hasFilesLimit {
688688
commit.Removed = commit.Removed[:hasFilesLimit]
689689
}
690690
}
691691
if commit.Modified != nil {
692-
if hasFilesLimit == 0 {
692+
if hasFilesLimit == -1 {
693693
commit.Modified = nil
694694
} else if hasFilesLimit > 0 && len(commit.Modified) > hasFilesLimit {
695695
commit.Modified = commit.Modified[:hasFilesLimit]
@@ -698,21 +698,21 @@ func (m *webhookNotifier) applyWebhookPayloadOptimizations(ctx context.Context,
698698
}
699699
if apiHeadCommit != nil {
700700
if apiHeadCommit.Added != nil {
701-
if hasFilesLimit == 0 {
701+
if hasFilesLimit == -1 {
702702
apiHeadCommit.Added = nil
703703
} else if hasFilesLimit > 0 && len(apiHeadCommit.Added) > hasFilesLimit {
704704
apiHeadCommit.Added = apiHeadCommit.Added[:hasFilesLimit]
705705
}
706706
}
707707
if apiHeadCommit.Removed != nil {
708-
if hasFilesLimit == 0 {
708+
if hasFilesLimit == -1 {
709709
apiHeadCommit.Removed = nil
710710
} else if hasFilesLimit > 0 && len(apiHeadCommit.Removed) > hasFilesLimit {
711711
apiHeadCommit.Removed = apiHeadCommit.Removed[:hasFilesLimit]
712712
}
713713
}
714714
if apiHeadCommit.Modified != nil {
715-
if hasFilesLimit == 0 {
715+
if hasFilesLimit == -1 {
716716
apiHeadCommit.Modified = nil
717717
} else if hasFilesLimit > 0 && len(apiHeadCommit.Modified) > hasFilesLimit {
718718
apiHeadCommit.Modified = apiHeadCommit.Modified[:hasFilesLimit]
@@ -721,8 +721,8 @@ func (m *webhookNotifier) applyWebhookPayloadOptimizations(ctx context.Context,
721721
}
722722
}
723723

724-
if hasCommitsLimit != -1 {
725-
if hasCommitsLimit == 0 {
724+
if hasCommitsLimit != 0 {
725+
if hasCommitsLimit == -1 {
726726
apiCommits = nil
727727
} else if hasCommitsLimit > 0 && len(apiCommits) > hasCommitsLimit {
728728
apiCommits = apiCommits[:hasCommitsLimit]

services/webhook/webhook_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ func TestWebhookPayloadOptimization(t *testing.T) {
108108
assert.NoError(t, err)
109109
}
110110

111-
// Case: -1 (no trimming)
111+
// Case: 0 (no trimming)
112112
webhook := &webhook_model.Webhook{
113113
RepoID: repo.ID,
114114
URL: "http://example.com/webhook",
@@ -117,8 +117,8 @@ func TestWebhookPayloadOptimization(t *testing.T) {
117117
Secret: "secret",
118118
IsActive: true,
119119
Type: webhook_module.GITEA,
120-
ExcludeFilesLimit: -1,
121-
ExcludeCommitsLimit: -1,
120+
ExcludeFilesLimit: 0,
121+
ExcludeCommitsLimit: 0,
122122
HookEvent: &webhook_module.HookEvent{
123123
PushOnly: true,
124124
},
@@ -168,9 +168,9 @@ func TestWebhookPayloadOptimization(t *testing.T) {
168168
assert.Equal(t, []string{"file1.txt"}, optimizedHeadCommit.Modified)
169169
}
170170

171-
// Case: 0 (keep nothing)
172-
webhook.ExcludeFilesLimit = 0
173-
webhook.ExcludeCommitsLimit = 0
171+
// Case: -1 (trim all)
172+
webhook.ExcludeFilesLimit = -1
173+
webhook.ExcludeCommitsLimit = -1
174174
err = webhook_model.UpdateWebhook(db.DefaultContext, webhook)
175175
assert.NoError(t, err)
176176
apiCommits = []*api.PayloadCommit{

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="-1" value="{{.Webhook.ExcludeFilesLimit}}" placeholder="-1">
55+
<input name="exclude_files_limit" type="number" min="-1" value="{{.Webhook.ExcludeFilesLimit}}" placeholder="0">
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="-1" value="{{.Webhook.ExcludeCommitsLimit}}" placeholder="-1">
60+
<input name="exclude_commits_limit" type="number" min="-1" value="{{.Webhook.ExcludeCommitsLimit}}" placeholder="0">
6161
<span class="help">{{ctx.Locale.Tr "repo.settings.exclude_commits_limit_desc"}}</span>
6262
</div>
6363
</div>

0 commit comments

Comments
 (0)