Skip to content

Commit

Permalink
fix ShouldCreateWebhooks logic to ensure it is valid with only projec…
Browse files Browse the repository at this point in the history
…ts or repos and added unit tests.

Signed-off-by: Ryan Currah <ryan@currah.ca>
  • Loading branch information
ryancurrah committed Jun 26, 2024
1 parent ccb7c1b commit 14674ae
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 2 deletions.
3 changes: 1 addition & 2 deletions pkg/apis/eventsource/v1alpha1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -1131,8 +1131,7 @@ func (b BitbucketServerEventSource) ShouldCreateWebhooks() bool {
return b.AccessToken != nil &&
b.Webhook != nil &&
b.Webhook.URL != "" &&
b.GetBitbucketServerRepositories() != nil &&
len(b.Projects) > 0
((len(b.GetBitbucketServerRepositories()) > 0 && b.GetBitbucketServerRepositories() != nil) || len(b.Projects) > 0)
}

func (b BitbucketServerEventSource) GetBitbucketServerRepositories() []BitbucketServerRepository {
Expand Down
91 changes: 91 additions & 0 deletions pkg/apis/eventsource/v1alpha1/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"testing"

"github.com/stretchr/testify/assert"
v1 "k8s.io/api/core/v1"
)

func TestGetReplicas(t *testing.T) {
Expand Down Expand Up @@ -41,3 +42,93 @@ func TestGithubEventSourceGetRepositories(t *testing.T) {
}
assert.Equal(t, len(es.GetOwnedRepositories()), 2)
}

// TestShouldCreateWebhooks tests the ShouldCreateWebhooks function on the BitbucketServerEventSource type.
func TestShouldCreateWebhooks(t *testing.T) {
// Create a dummy SecretKeySelector for testing
dummySecretKeySelector := &v1.SecretKeySelector{
LocalObjectReference: v1.LocalObjectReference{
Name: "dummy-secret",
},
Key: "token",
}

tests := []struct {
name string
b BitbucketServerEventSource
want bool
}{
{
name: "Valid Webhooks - Both Projects and Repositories",
b: BitbucketServerEventSource{
AccessToken: dummySecretKeySelector,
Webhook: &WebhookContext{URL: "http://example.com"},
Projects: []string{"Project1"},
Repositories: []BitbucketServerRepository{{ProjectKey: "Proj1", RepositorySlug: "Repo1"}},
},
want: true,
},
{
name: "No AccessToken",
b: BitbucketServerEventSource{
Webhook: &WebhookContext{URL: "http://example.com"},
Projects: []string{"Project1"},
Repositories: []BitbucketServerRepository{{ProjectKey: "Proj1", RepositorySlug: "Repo1"}},
},
want: false,
},
{
name: "No Webhook",
b: BitbucketServerEventSource{
AccessToken: dummySecretKeySelector,
Projects: []string{"Project1"},
Repositories: []BitbucketServerRepository{{ProjectKey: "Proj1", RepositorySlug: "Repo1"}},
},
want: false,
},
{
name: "No URL",
b: BitbucketServerEventSource{
AccessToken: dummySecretKeySelector,
Webhook: &WebhookContext{},
Projects: []string{"Project1"},
Repositories: []BitbucketServerRepository{{ProjectKey: "Proj1", RepositorySlug: "Repo1"}},
},
want: false,
},
{
name: "No Projects or Repositories",
b: BitbucketServerEventSource{
AccessToken: dummySecretKeySelector,
Webhook: &WebhookContext{URL: "http://example.com"},
},
want: false,
},
{
name: "Valid with Only Repositories",
b: BitbucketServerEventSource{
AccessToken: dummySecretKeySelector,
Webhook: &WebhookContext{URL: "http://example.com"},
Repositories: []BitbucketServerRepository{{ProjectKey: "Proj1", RepositorySlug: "Repo1"}},
},
want: true,
},
{
name: "Valid with Only Projects",
b: BitbucketServerEventSource{
AccessToken: dummySecretKeySelector,
Webhook: &WebhookContext{URL: "http://example.com"},
Projects: []string{"Project1"},
},
want: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.b.ShouldCreateWebhooks(); got != tt.want {
t.Errorf("%s: ShouldCreateWebhooks() = %v, want %v", tt.name, got, tt.want)
}
})
}
}

0 comments on commit 14674ae

Please sign in to comment.