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

Ensure protocol in git provider URL in Repository CR #384

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
48 changes: 23 additions & 25 deletions controllers/component_build_controller_pac_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
88 changes: 77 additions & 11 deletions controllers/component_build_controller_unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand All @@ -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"),
Expand All @@ -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"),
Expand All @@ -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",
mmorhun marked this conversation as resolved.
Show resolved Hide resolved
},
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",
},
},
Expand Down Expand Up @@ -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",
},
},
}
Expand Down
Loading