From 23849d14893b5280b0fda8009bf78688a5c9aeed Mon Sep 17 00:00:00 2001 From: Ryan Currah Date: Wed, 26 Jun 2024 14:47:17 -0400 Subject: [PATCH] fix ShouldCreateWebhooks logic to ensure it is valid with only projects or repos and added unit tests. Signed-off-by: Ryan Currah --- pkg/apis/eventsource/v1alpha1/types.go | 3 +- pkg/apis/eventsource/v1alpha1/types_test.go | 91 +++++++++++++++++++++ 2 files changed, 92 insertions(+), 2 deletions(-) diff --git a/pkg/apis/eventsource/v1alpha1/types.go b/pkg/apis/eventsource/v1alpha1/types.go index 8c240dbcac..5f4b2ad952 100644 --- a/pkg/apis/eventsource/v1alpha1/types.go +++ b/pkg/apis/eventsource/v1alpha1/types.go @@ -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 { diff --git a/pkg/apis/eventsource/v1alpha1/types_test.go b/pkg/apis/eventsource/v1alpha1/types_test.go index e1af8ef86a..2c4cbbce7c 100644 --- a/pkg/apis/eventsource/v1alpha1/types_test.go +++ b/pkg/apis/eventsource/v1alpha1/types_test.go @@ -4,6 +4,7 @@ import ( "testing" "github.com/stretchr/testify/assert" + v1 "k8s.io/api/core/v1" ) func TestGetReplicas(t *testing.T) { @@ -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) + } + }) + } +}