Skip to content

Commit

Permalink
RHTAPSRE-467: Support the configuration of provider URL
Browse files Browse the repository at this point in the history
When onboarding self-hosted SCM pack can't determine the the base
url, so it should be provided explicitly.

Allow users to mention the provider (SCM) URL using an annotation
on the component.

Signed-off-by: gbenhaim <gbenhaim@redhat.com>
  • Loading branch information
gbenhaim committed Mar 12, 2024
1 parent ea473f6 commit d3fc6d1
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 20 deletions.
24 changes: 13 additions & 11 deletions gitops/generate_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ const (

PaCAnnotation = "pipelinesascode"
GitProviderAnnotationName = "git-provider"
GitProviderAnnotationURL = "git-provider-url"
PipelinesAsCodeWebhooksSecretName = "pipelines-as-code-webhooks-secret"
PipelinesAsCode_githubAppIdKey = "github-application-id"
PipelinesAsCode_githubPrivateKey = "github-private-key"
Expand Down Expand Up @@ -89,25 +90,26 @@ func GeneratePACRepository(component appstudiov1alpha1.Component, config map[str

isAppUsed := IsPaCApplicationConfigured(gitProvider, config)

var gitProviderConfig *pacv1alpha1.GitProvider = nil
var gitProviderConfig *pacv1alpha1.GitProvider = &pacv1alpha1.GitProvider{}
if !isAppUsed {
// Webhook is used
gitProviderConfig = &pacv1alpha1.GitProvider{
Secret: &pacv1alpha1.Secret{
Name: gitopsprepare.PipelinesAsCodeSecretName,
Key: GetProviderTokenKey(gitProvider),
},
WebhookSecret: &pacv1alpha1.Secret{
Name: PipelinesAsCodeWebhooksSecretName,
Key: GetWebhookSecretKeyForComponent(component),
},
gitProviderConfig.Secret = &pacv1alpha1.Secret{
Name: gitopsprepare.PipelinesAsCodeSecretName,
Key: GetProviderTokenKey(gitProvider),
}
gitProviderConfig.WebhookSecret = &pacv1alpha1.Secret{
Name: PipelinesAsCodeWebhooksSecretName,
Key: GetWebhookSecretKeyForComponent(component),
}

if gitProvider == "gitlab" {
gitProviderConfig.URL = "https://gitlab.com"
}
}

if url, ok := component.Annotations[GitProviderAnnotationURL]; ok {
gitProviderConfig.URL = url
}

repository := &pacv1alpha1.Repository{
TypeMeta: metav1.TypeMeta{
Kind: "Repository",
Expand Down
58 changes: 49 additions & 9 deletions gitops/generate_build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,12 @@ func TestGenerateBuild(t *testing.T) {
}

func TestGeneratePACRepository(t *testing.T) {
getComponent := func(repoUrl string) appstudiov1alpha1.Component {
getComponent := func(repoUrl string, annotations map[string]string) appstudiov1alpha1.Component {
return appstudiov1alpha1.Component{
ObjectMeta: metav1.ObjectMeta{
Name: "testcomponent",
Namespace: "workspace-name",
Name: "testcomponent",
Namespace: "workspace-name",
Annotations: annotations,
},
Spec: appstudiov1alpha1.ComponentSpec{
Source: appstudiov1alpha1.ComponentSource{
Expand All @@ -137,6 +138,7 @@ func TestGeneratePACRepository(t *testing.T) {
tests := []struct {
name string
repoUrl string
componentAnnotations map[string]string
pacConfig map[string][]byte
expectedGitProviderConfig *pacv1alpha1.GitProvider
}{
Expand All @@ -147,7 +149,7 @@ func TestGeneratePACRepository(t *testing.T) {
PipelinesAsCode_githubAppIdKey: []byte("12345"),
PipelinesAsCode_githubPrivateKey: []byte("private-key"),
},
expectedGitProviderConfig: nil,
expectedGitProviderConfig: &pacv1alpha1.GitProvider{},
},
{
name: "should create PaC repository for Github application even if Github webhook configured",
Expand All @@ -157,7 +159,7 @@ func TestGeneratePACRepository(t *testing.T) {
PipelinesAsCode_githubPrivateKey: []byte("private-key"),
"github.token": []byte("ghp_token"),
},
expectedGitProviderConfig: nil,
expectedGitProviderConfig: &pacv1alpha1.GitProvider{},
},
{
name: "should create PaC repository for Github webhook",
Expand All @@ -173,7 +175,7 @@ func TestGeneratePACRepository(t *testing.T) {
},
WebhookSecret: &pacv1alpha1.Secret{
Name: PipelinesAsCodeWebhooksSecretName,
Key: GetWebhookSecretKeyForComponent(getComponent("https://github.com/user/test-component-repository")),
Key: GetWebhookSecretKeyForComponent(getComponent("https://github.com/user/test-component-repository", nil)),
},
},
},
Expand All @@ -191,7 +193,7 @@ func TestGeneratePACRepository(t *testing.T) {
},
WebhookSecret: &pacv1alpha1.Secret{
Name: PipelinesAsCodeWebhooksSecretName,
Key: GetWebhookSecretKeyForComponent(getComponent("https://gitlab.com/user/test-component-repository/")),
Key: GetWebhookSecretKeyForComponent(getComponent("https://gitlab.com/user/test-component-repository/", nil)),
},
URL: "https://gitlab.com",
},
Expand All @@ -211,16 +213,54 @@ func TestGeneratePACRepository(t *testing.T) {
},
WebhookSecret: &pacv1alpha1.Secret{
Name: PipelinesAsCodeWebhooksSecretName,
Key: GetWebhookSecretKeyForComponent(getComponent("https://gitlab.com/user/test-component-repository")),
Key: GetWebhookSecretKeyForComponent(getComponent("https://gitlab.com/user/test-component-repository", nil)),
},
URL: "https://gitlab.com",
},
},
{
name: "should create PaC repository for self-hosted GitLab webhook",
repoUrl: "https://gitlab.self-hosted.com/user/test-component-repository/",
componentAnnotations: map[string]string{
GitProviderAnnotationName: "gitlab",
GitProviderAnnotationURL: "https://gitlab.self-hosted.com",
},
pacConfig: map[string][]byte{
"github.token": []byte("ghp_token"),
"gitlab.token": []byte("glpat-token"),
},
expectedGitProviderConfig: &pacv1alpha1.GitProvider{
Secret: &pacv1alpha1.Secret{
Name: gitopsprepare.PipelinesAsCodeSecretName,
Key: "gitlab.token",
},
WebhookSecret: &pacv1alpha1.Secret{
Name: PipelinesAsCodeWebhooksSecretName,
Key: GetWebhookSecretKeyForComponent(getComponent("https://gitlab.self-hosted.com/user/test-component-repository/", nil)),
},
URL: "https://gitlab.self-hosted.com",
},
},
{
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{
PipelinesAsCode_githubAppIdKey: []byte("12345"),
PipelinesAsCode_githubPrivateKey: []byte("private-key"),
},
expectedGitProviderConfig: &pacv1alpha1.GitProvider{
URL: "https://github.self-hosted.com",
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
component := getComponent(tt.repoUrl)
component := getComponent(tt.repoUrl, tt.componentAnnotations)

pacRepo, err := GeneratePACRepository(component, tt.pacConfig)

Expand Down

0 comments on commit d3fc6d1

Please sign in to comment.