Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow PR secrets to be used on close #3084

Merged
merged 7 commits into from
Dec 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions server/model/secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package model
import (
"errors"
"fmt"
"path/filepath"
"regexp"
"sort"
)
Expand Down Expand Up @@ -104,15 +103,23 @@ func (s Secret) IsRepository() bool {
}

// Match returns true if an image and event match the restricted list.
6543 marked this conversation as resolved.
Show resolved Hide resolved
// Note that EventPullClosed are treated as EventPull.
func (s *Secret) Match(event WebhookEvent) bool {
// if there is no filter set secret matches all webhook events
if len(s.Events) == 0 {
return true
}
for _, pattern := range s.Events {
if match, _ := filepath.Match(string(pattern), string(event)); match {
6543 marked this conversation as resolved.
Show resolved Hide resolved
// tread all pull events the same way
if event == EventPullClosed {
event = EventPull
}
// one match is enough
for _, e := range s.Events {
if e == event {
return true
}
}
// a filter is set but the webhook did not match it
return false
}

Expand Down
55 changes: 42 additions & 13 deletions server/model/secret_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,52 @@ import (
"testing"

"github.com/franela/goblin"
"github.com/stretchr/testify/assert"
)

func TestSecret(t *testing.T) {
func TestSecretMatch(t *testing.T) {
tcl := []*struct {
name string
secret Secret
event WebhookEvent
match bool
}{
{
name: "should match event",
secret: Secret{Events: []WebhookEvent{"pull_request"}},
event: EventPull,
match: true,
},
{
name: "should not match event",
secret: Secret{Events: []WebhookEvent{"pull_request"}},
event: EventPush,
match: false,
},
{
name: "should match when no event filters defined",
secret: Secret{},
event: EventPull,
match: true,
},
{
name: "pull close should match pull",
secret: Secret{Events: []WebhookEvent{"pull_request"}},
event: EventPullClosed,
match: true,
},
}

for _, tc := range tcl {
t.Run(tc.name, func(t *testing.T) {
assert.Equal(t, tc.match, tc.secret.Match(tc.event))
})
}
}

func TestSecretValidate(t *testing.T) {
g := goblin.Goblin(t)
g.Describe("Secret", func() {
g.It("should match event", func() {
secret := Secret{Events: []WebhookEvent{"pull_request"}}
g.Assert(secret.Match("pull_request")).IsTrue()
})
g.It("should not match event", func() {
secret := Secret{Events: []WebhookEvent{"pull_request"}}
g.Assert(secret.Match("push")).IsFalse()
})
g.It("should match when no event filters defined", func() {
secret := Secret{}
g.Assert(secret.Match("pull_request")).IsTrue()
})
g.It("should pass validation", func() {
secret := Secret{
Name: "secretname",
Expand Down