Skip to content

Commit b9484cf

Browse files
committed
fix
1 parent 4d0c02b commit b9484cf

File tree

6 files changed

+62
-16
lines changed

6 files changed

+62
-16
lines changed

options/locale/locale_en-US.ini

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2433,7 +2433,9 @@ settings.event_workflow_job_desc = Gitea Actions Workflow job queued, waiting, i
24332433
settings.event_package = Package
24342434
settings.event_package_desc = Package created or deleted in a repository.
24352435
settings.branch_filter = Branch filter
2436-
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>.
2436+
settings.branch_filter_desc_1 = Branch (and ref name) allowlist for push, branch creation and branch deletion events, specified as glob pattern. If empty or <code>*</code>, events for all branches and tags are reported.
2437+
settings.branch_filter_desc_2 = Use <code>refs/heads/</code> or <code>refs/tags/</code> prefix to match full ref names.
2438+
settings.branch_filter_desc_doc = See <a href="%[1]s">%[2]s</a> documentation for syntax.
24372439
settings.authorization_header = Authorization Header
24382440
settings.authorization_header_desc = Will be included as authorization header for requests when present. Examples: %s.
24392441
settings.active = Active

services/webhook/webhook.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -111,19 +111,22 @@ func enqueueHookTask(taskID int64) error {
111111
return nil
112112
}
113113

114-
func checkBranch(w *webhook_model.Webhook, branch string) bool {
115-
if w.BranchFilter == "" || w.BranchFilter == "*" {
114+
func checkBranchFilter(branchFilter string, ref git.RefName) bool {
115+
if branchFilter == "" || branchFilter == "*" || branchFilter == "**" {
116116
return true
117117
}
118118

119-
g, err := glob.Compile(w.BranchFilter)
119+
g, err := glob.Compile(branchFilter)
120120
if err != nil {
121121
// should not really happen as BranchFilter is validated
122122
log.Error("CheckBranch failed: %s", err)
123123
return false
124124
}
125125

126-
return g.Match(branch)
126+
if ref.IsBranch() && g.Match(ref.BranchName()) {
127+
return true
128+
}
129+
return g.Match(ref.String())
127130
}
128131

129132
// PrepareWebhook creates a hook task and enqueues it for processing.
@@ -147,12 +150,9 @@ func PrepareWebhook(ctx context.Context, w *webhook_model.Webhook, event webhook
147150
return nil
148151
}
149152

150-
// Apply the filter directly to the ref name
153+
// Check the payload's git ref against the webhook's branch filter, if any.
151154
if ref := getPayloadRef(p); ref != "" {
152-
// FIXME: here comes the problem, "ref" is the full ref name, but the filter
153-
// But, "checkBranch" check it against "w.BranchFilter", does it make sense?
154-
if !checkBranch(w, ref) {
155-
log.Info("Ref %q doesn't match branch filter %q, skipping", ref, w.BranchFilter)
155+
if !checkBranchFilter(w.BranchFilter, ref) {
156156
return nil
157157
}
158158
}

services/webhook/webhook_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"code.gitea.io/gitea/models/unittest"
1111
user_model "code.gitea.io/gitea/models/user"
1212
webhook_model "code.gitea.io/gitea/models/webhook"
13+
"code.gitea.io/gitea/modules/git"
1314
"code.gitea.io/gitea/modules/setting"
1415
api "code.gitea.io/gitea/modules/structs"
1516
"code.gitea.io/gitea/modules/test"
@@ -90,3 +91,30 @@ func TestWebhookUserMail(t *testing.T) {
9091
assert.Equal(t, user.GetPlaceholderEmail(), convert.ToUser(t.Context(), user, nil).Email)
9192
assert.Equal(t, user.Email, convert.ToUser(t.Context(), user, user).Email)
9293
}
94+
95+
func TestCheckBranchFilter(t *testing.T) {
96+
cases := []struct {
97+
filter string
98+
ref git.RefName
99+
match bool
100+
}{
101+
{"", "any-ref", true},
102+
{"*", "any-ref", true},
103+
{"**", "any-ref", true},
104+
105+
{"main", git.RefNameFromBranch("main"), true},
106+
{"main", git.RefNameFromTag("main"), false},
107+
108+
{"feature/*", git.RefNameFromBranch("feature"), false},
109+
{"feature/*", git.RefNameFromBranch("feature/foo"), true},
110+
{"feature/*", git.RefNameFromTag("feature/foo"), false},
111+
112+
{"{refs/heads/feature/*,refs/tags/release/*}", git.RefNameFromBranch("feature/foo"), true},
113+
{"{refs/heads/feature/*,refs/tags/release/*}", git.RefNameFromBranch("main"), false},
114+
{"{refs/heads/feature/*,refs/tags/release/*}", git.RefNameFromTag("release/bar"), true},
115+
{"{refs/heads/feature/*,refs/tags/release/*}", git.RefNameFromTag("dev"), false},
116+
}
117+
for _, v := range cases {
118+
assert.Equal(t, v.match, checkBranchFilter(v.filter, v.ref), "filter: %q ref: %q", v.filter, v.ref)
119+
}
120+
}

templates/repo/settings/webhook/settings.tmpl

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,16 @@
4444
<div class="field">
4545
<label>{{ctx.Locale.Tr "repo.settings.branch_filter"}}</label>
4646
<input name="branch_filter" type="text" value="{{or .Webhook.BranchFilter "*"}}">
47-
<span class="help">{{ctx.Locale.Tr "repo.settings.branch_filter_desc" "https://pkg.go.dev/github.com/gobwas/glob#Compile" "github.com/gobwas/glob"}}</span>
47+
<span class="help">
48+
{{ctx.Locale.Tr "repo.settings.branch_filter_desc_1"}}
49+
{{ctx.Locale.Tr "repo.settings.branch_filter_desc_2"}}
50+
{{ctx.Locale.Tr "repo.settings.branch_filter_desc_doc" "https://pkg.go.dev/github.com/gobwas/glob#Compile" "github.com/gobwas/glob"}}
51+
<ul class="tw-m-0">
52+
<li><code>main</code></li>
53+
<li><code>{main,feature/*}</code></li>
54+
<li><code>{refs/heads/feature/*,refs/tags/release/*}</code></li>
55+
</ul>
56+
</span>
4857
</div>
4958

5059
<div class="field">

web_src/css/base.css

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,12 @@ samp,
101101
font-size: 0.95em; /* compensate for monospace fonts being usually slightly larger */
102102
}
103103

104+
code {
105+
padding: 2px 4px;
106+
border-radius: .24em;
107+
background-color: var(--color-label-bg);
108+
}
109+
104110
b,
105111
strong,
106112
h1,
@@ -177,6 +183,11 @@ table {
177183
border-collapse: collapse;
178184
}
179185

186+
ul {
187+
margin: 0.5em 0;
188+
padding: 0 1.5em;
189+
}
190+
180191
button {
181192
cursor: pointer;
182193
}
@@ -603,11 +614,6 @@ img.ui.avatar,
603614
text-align: center;
604615
}
605616

606-
.ui .message > ul {
607-
margin: 0;
608-
padding: 0 1em;
609-
}
610-
611617
.ui .header > i + .content {
612618
padding-left: 0.75rem;
613619
vertical-align: middle;

web_src/css/form.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,7 @@ textarea:focus,
218218

219219
.form .help {
220220
color: var(--color-secondary-dark-5);
221+
margin-top: 0.25em;
221222
padding-bottom: 0.6em;
222223
display: inline-block;
223224
text-wrap: balance;

0 commit comments

Comments
 (0)