diff --git a/controllers/component_build_controller_pac_repository.go b/controllers/component_build_controller_pac_repository.go index f2ff6348..2aad079e 100644 --- a/controllers/component_build_controller_pac_repository.go +++ b/controllers/component_build_controller_pac_repository.go @@ -213,34 +213,32 @@ func generatePACRepository(component appstudiov1alpha1.Component, config *corev1 }, } - if gitProvider == "gitlab" { - if providerUrl, configured := component.Annotations[GitProviderAnnotationURL]; configured { - gitProviderConfig.URL = providerUrl + // gitProviderType is needed for incoming webhook handling + gitProviderType := gitProvider + if gitProvider == "bitbucket" { + // https://pipelinesascode.com/docs/guide/incoming_webhook/#incoming-webhook-url + gitProviderType = "bitbucket-cloud" + } + gitProviderConfig.Type = gitProviderType + + var gitProviderUrl string + if providerUrlFromAnnotation, configured := component.Annotations[GitProviderAnnotationURL]; configured { + // Use git provider URL provided via the annotation. + // Make sure that the url has protocol as it's required. + if !strings.Contains(providerUrlFromAnnotation, "://") { + gitProviderUrl = "https://" + providerUrlFromAnnotation } else { - // Get git provider URL from source URL. - u, err := url.Parse(component.Spec.Source.GitSource.URL) - if err != nil { - return nil, err - } - gitProviderConfig.URL = u.Scheme + "://" + u.Host + gitProviderUrl = providerUrlFromAnnotation } + } else { + // Get git provider URL from source URL. + u, err := url.Parse(component.Spec.Source.GitSource.URL) + if err != nil { + return nil, err + } + gitProviderUrl = u.Scheme + "://" + u.Host } - } - - if url, ok := component.Annotations[GitProviderAnnotationURL]; ok { - if gitProviderConfig == nil { - gitProviderConfig = &pacv1alpha1.GitProvider{} - } - gitProviderConfig.URL = url - } - - if gitProviderConfig != nil { - repositoryGitProvider := gitProvider - // https://pipelinesascode.com/docs/guide/incoming_webhook/#incoming-webhook-url - if repositoryGitProvider == "bitbucket" { - repositoryGitProvider = "bitbucket-cloud" - } - gitProviderConfig.Type = repositoryGitProvider + gitProviderConfig.URL = gitProviderUrl } repository := &pacv1alpha1.Repository{ diff --git a/controllers/component_build_controller_unit_test.go b/controllers/component_build_controller_unit_test.go index c2970f11..95788c14 100644 --- a/controllers/component_build_controller_unit_test.go +++ b/controllers/component_build_controller_unit_test.go @@ -986,7 +986,7 @@ func TestGeneratePACRepository(t *testing.T) { expectedGitProviderConfig *pacv1alpha1.GitProvider }{ { - name: "should create PaC repository for Github application", + name: "should create PaC repository for GitHub application", repoUrl: "https://github.com/user/test-component-repository", pacConfig: map[string][]byte{ PipelinesAsCodeGithubAppIdKey: []byte("12345"), @@ -995,7 +995,7 @@ func TestGeneratePACRepository(t *testing.T) { expectedGitProviderConfig: nil, }, { - name: "should create PaC repository for Github application even if Github webhook configured", + name: "should create PaC repository for GitHub application even if Github webhook configured", repoUrl: "https://github.com/user/test-component-repository", pacConfig: map[string][]byte{ PipelinesAsCodeGithubAppIdKey: []byte("12345"), @@ -1005,7 +1005,7 @@ func TestGeneratePACRepository(t *testing.T) { expectedGitProviderConfig: nil, }, { - name: "should create PaC repository for Github webhook", + name: "should create PaC repository for GitHub webhook", repoUrl: "https://github.com/user/test-component-repository", pacConfig: map[string][]byte{ "password": []byte("ghp_token"), @@ -1019,6 +1019,65 @@ func TestGeneratePACRepository(t *testing.T) { Name: pipelinesAsCodeWebhooksSecretName, Key: getWebhookSecretKeyForComponent(getComponent("https://github.com/user/test-component-repository", nil)), }, + URL: "https://github.com", + Type: "github", + }, + }, + { + name: "should create PaC repository for GitHub application on self-hosted GitHub", + repoUrl: "https://github.self-hosted.com/user/test-component-repository", + componentAnnotations: map[string]string{ + GitProviderAnnotationName: "github", + GitProviderAnnotationURL: "https://github.self-hosted.com", + }, + pacConfig: map[string][]byte{ + PipelinesAsCodeGithubAppIdKey: []byte("12345"), + PipelinesAsCodeGithubPrivateKey: []byte("private-key"), + }, + expectedGitProviderConfig: nil, + }, + { + name: "should create PaC repository for self-hosted GitHub webhook and figure out provider URL from source URL", + repoUrl: "https://github.self-hosted.com/user/test-component-repository/", + componentAnnotations: map[string]string{ + GitProviderAnnotationName: "github", + }, + pacConfig: map[string][]byte{ + "password": []byte("ghp_token"), + }, + expectedGitProviderConfig: &pacv1alpha1.GitProvider{ + Secret: &pacv1alpha1.Secret{ + Name: PipelinesAsCodeGitHubAppSecretName, + Key: "password", + }, + WebhookSecret: &pacv1alpha1.Secret{ + Name: pipelinesAsCodeWebhooksSecretName, + Key: getWebhookSecretKeyForComponent(getComponent("https://github.self-hosted.com/user/test-component-repository/", nil)), + }, + URL: "https://github.self-hosted.com", + Type: "github", + }, + }, + { + name: "should create PaC repository for self-hosted GitHub webhook and use provider URL from annotation", + repoUrl: "https://github.self-hosted.com/user/test-component-repository/", + componentAnnotations: map[string]string{ + GitProviderAnnotationName: "github", + GitProviderAnnotationURL: "https://github.self-hosted-proxy.com", + }, + pacConfig: map[string][]byte{ + "password": []byte("ghp_token"), + }, + expectedGitProviderConfig: &pacv1alpha1.GitProvider{ + Secret: &pacv1alpha1.Secret{ + Name: PipelinesAsCodeGitHubAppSecretName, + Key: "password", + }, + WebhookSecret: &pacv1alpha1.Secret{ + Name: pipelinesAsCodeWebhooksSecretName, + Key: getWebhookSecretKeyForComponent(getComponent("https://github.self-hosted.com/user/test-component-repository/", nil)), + }, + URL: "https://github.self-hosted-proxy.com", Type: "github", }, }, @@ -1108,19 +1167,26 @@ func TestGeneratePACRepository(t *testing.T) { }, }, { - name: "should create PaC repository for Github application on self-hosted Github", - repoUrl: "https://github.self-hosted.com/user/test-component-repository", + name: "should create PaC repository for self-hosted GitLab webhook and use provider URL from annotation that has no protocol", + repoUrl: "https://gitlab.self-hosted.com/user/test-component-repository/", componentAnnotations: map[string]string{ - GitProviderAnnotationName: "github", - GitProviderAnnotationURL: "https://github.self-hosted.com", + GitProviderAnnotationName: "gitlab", + GitProviderAnnotationURL: "gitlab.self-hosted-proxy.com", }, pacConfig: map[string][]byte{ - PipelinesAsCodeGithubAppIdKey: []byte("12345"), - PipelinesAsCodeGithubPrivateKey: []byte("private-key"), + "password": []byte("glpat-token"), }, expectedGitProviderConfig: &pacv1alpha1.GitProvider{ - URL: "https://github.self-hosted.com", - Type: "github", + Secret: &pacv1alpha1.Secret{ + Name: PipelinesAsCodeGitHubAppSecretName, + Key: "password", + }, + WebhookSecret: &pacv1alpha1.Secret{ + Name: pipelinesAsCodeWebhooksSecretName, + Key: getWebhookSecretKeyForComponent(getComponent("https://gitlab.self-hosted.com/user/test-component-repository/", nil)), + }, + URL: "https://gitlab.self-hosted-proxy.com", + Type: "gitlab", }, }, }