Skip to content

Commit 1eeca88

Browse files
committed
fix: Branch repository provisioning with gitprovider v2 (#231)
1 parent baa09f3 commit 1eeca88

25 files changed

Lines changed: 759 additions & 423 deletions

controllers/codebase/service/chain/checkout_branch.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,25 +33,28 @@ func GetRepositoryCredentialsIfExists(cb *codebaseApi.Codebase, c client.Client)
3333

3434
func CheckoutBranch(
3535
ctx context.Context,
36-
repository, projectPath, branchName string,
37-
g gitproviderv2.Git,
36+
branchName string,
37+
repoContext *GitRepositoryContext,
3838
cb *codebaseApi.Codebase,
3939
c client.Client,
40-
createGitProviderWithConfig func(config gitproviderv2.Config) gitproviderv2.Git,
40+
gitProviderFactory func(config gitproviderv2.Config) gitproviderv2.Git,
4141
) error {
42-
currentBranchName, err := g.GetCurrentBranchName(ctx, projectPath)
42+
log := ctrl.LoggerFrom(ctx)
43+
gitProvider := gitProviderFactory(gitproviderv2.NewConfigFromGitServerAndSecret(repoContext.GitServer, repoContext.GitServerSecret))
44+
45+
currentBranchName, err := gitProvider.GetCurrentBranchName(ctx, repoContext.WorkDir)
4346
if err != nil {
4447
return fmt.Errorf("failed to get current branch name: %w", err)
4548
}
4649

4750
if currentBranchName == branchName {
48-
ctrl.Log.Info("default branch is already active", "name", branchName)
51+
log.Info("Default branch is already active", "name", branchName)
4952
return nil
5053
}
5154

5255
switch cb.Spec.Strategy {
5356
case "create":
54-
if err := g.Checkout(ctx, projectPath, branchName, false); err != nil {
57+
if err := gitProvider.Checkout(ctx, repoContext.WorkDir, branchName, false); err != nil {
5558
return fmt.Errorf("failed to checkout to default branch %s (create strategy): %w", branchName, err)
5659
}
5760

@@ -61,20 +64,17 @@ func CheckoutBranch(
6164
return err
6265
}
6366

64-
cloneRepoGitProvider := g
65-
67+
cfg := gitproviderv2.Config{}
6668
if user != nil && password != nil {
67-
cloneRepoGitProvider = createGitProviderWithConfig(gitproviderv2.Config{
68-
Username: *user,
69-
Token: *password,
70-
})
69+
cfg.Username = *user
70+
cfg.Token = *password
7171
}
7272

73-
if err := cloneRepoGitProvider.Checkout(ctx, projectPath, branchName, true); err != nil {
73+
if err := gitProviderFactory(cfg).Checkout(ctx, repoContext.WorkDir, branchName, true); err != nil {
7474
return fmt.Errorf("failed to checkout to default branch %s (clone strategy): %w", branchName, err)
7575
}
7676
case "import":
77-
if err := g.CheckoutRemoteBranch(ctx, projectPath, branchName); err != nil {
77+
if err := gitProvider.CheckoutRemoteBranch(ctx, repoContext.WorkDir, branchName); err != nil {
7878
return fmt.Errorf("failed to checkout to default branch %s (import strategy): %w", branchName, err)
7979
}
8080
default:

controllers/codebase/service/chain/checkout_branch_test.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ func TestCheckoutBranch_ShouldFailOnGetSecret(t *testing.T) {
102102
mGit := gitServerMocks.NewMockGit(t)
103103
mGit.On("GetCurrentBranchName", testify.Anything, "project-path").Return("some-other-branch", nil)
104104

105-
err := CheckoutBranch(context.Background(), "repo", "project-path", "branch", mGit, c, fakeCl, func(config gitproviderv2.Config) gitproviderv2.Git {
105+
err := CheckoutBranch(context.Background(), "branch", &GitRepositoryContext{}, c, fakeCl, func(config gitproviderv2.Config) gitproviderv2.Git {
106106
return mGit
107107
})
108108
assert.Error(t, err)
@@ -143,7 +143,7 @@ func TestCheckoutBranch_ShouldFailOnGetCurrentBranchName(t *testing.T) {
143143
mGit := gitServerMocks.NewMockGit(t)
144144
mGit.On("GetCurrentBranchName", testify.Anything, "project-path").Return("", errors.New("FATAL:FAILED"))
145145

146-
err := CheckoutBranch(context.Background(), "repo", "project-path", "branch", mGit, c, fakeCl, func(config gitproviderv2.Config) gitproviderv2.Git {
146+
err := CheckoutBranch(context.Background(), "branch", &GitRepositoryContext{}, c, fakeCl, func(config gitproviderv2.Config) gitproviderv2.Git {
147147
return mGit
148148
})
149149
assert.Error(t, err)
@@ -154,7 +154,6 @@ func TestCheckoutBranch_ShouldFailOnGetCurrentBranchName(t *testing.T) {
154154
}
155155

156156
func TestCheckoutBranch_ShouldFailOnCheckout(t *testing.T) {
157-
repo := "repo"
158157
c := &codebaseApi.Codebase{
159158
ObjectMeta: metaV1.ObjectMeta{
160159
Name: "fake-name",
@@ -186,7 +185,7 @@ func TestCheckoutBranch_ShouldFailOnCheckout(t *testing.T) {
186185
mGit.On("GetCurrentBranchName", testify.Anything, "project-path").Return("some-other-branch", nil)
187186
mGit.On("Checkout", testify.Anything, "project-path", "branch", true).Return(errors.New("FATAL:FAILED"))
188187

189-
err := CheckoutBranch(context.Background(), repo, "project-path", "branch", mGit, c, fakeCl, func(config gitproviderv2.Config) gitproviderv2.Git {
188+
err := CheckoutBranch(context.Background(), "branch", &GitRepositoryContext{}, c, fakeCl, func(config gitproviderv2.Config) gitproviderv2.Git {
190189
return mGit
191190
})
192191
assert.Error(t, err)
@@ -197,7 +196,6 @@ func TestCheckoutBranch_ShouldFailOnCheckout(t *testing.T) {
197196
}
198197

199198
func TestCheckoutBranch_ShouldPassForCloneStrategy(t *testing.T) {
200-
repo := "repo"
201199
c := &codebaseApi.Codebase{
202200
ObjectMeta: metaV1.ObjectMeta{
203201
Name: "fake-name",
@@ -251,7 +249,7 @@ func TestCheckoutBranch_ShouldPassForCloneStrategy(t *testing.T) {
251249
mGit.On("GetCurrentBranchName", testify.Anything, "project-path").Return("some-other-branch", nil)
252250
mGit.On("CheckoutRemoteBranch", testify.Anything, "project-path", "branch").Return(nil)
253251

254-
err := CheckoutBranch(context.Background(), repo, "project-path", "branch", mGit, c, fakeCl, func(config gitproviderv2.Config) gitproviderv2.Git {
252+
err := CheckoutBranch(context.Background(), "branch", &GitRepositoryContext{}, c, fakeCl, func(config gitproviderv2.Config) gitproviderv2.Git {
255253
return mGit
256254
})
257255
assert.NoError(t, err)

controllers/codebase/service/chain/common.go

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -105,47 +105,35 @@ func PrepareGitRepository(
105105
c client.Client,
106106
codebase *codebaseApi.Codebase,
107107
gitProviderFactory gitproviderv2.GitProviderFactory,
108-
createGitProviderWithConfig func(config gitproviderv2.Config) gitproviderv2.Git,
109108
) (*GitRepositoryContext, error) {
110109
log := ctrl.LoggerFrom(ctx)
111110

112-
// Step 1-2: Get git repository context (GitServer, Secret, and paths)
113111
gitRepoCtx, err := GetGitRepositoryContext(ctx, c, codebase)
114112
if err != nil {
115113
return nil, err
116114
}
117115

118-
// Step 3: Create git provider using factory
119-
g := gitProviderFactory(gitRepoCtx.GitServer, gitRepoCtx.GitServerSecret)
116+
gitProvider := gitProviderFactory(gitproviderv2.NewConfigFromGitServerAndSecret(gitRepoCtx.GitServer, gitRepoCtx.GitServerSecret))
120117

121-
// Step 4: Clone repository if needed
122118
if !util.DoesDirectoryExist(gitRepoCtx.WorkDir) || util.IsDirectoryEmpty(gitRepoCtx.WorkDir) {
123119
log.Info("Start cloning repository", "url", gitRepoCtx.RepoGitUrl)
124120

125-
if err := g.Clone(ctx, gitRepoCtx.RepoGitUrl, gitRepoCtx.WorkDir, 0); err != nil {
121+
if err := gitProvider.Clone(ctx, gitRepoCtx.RepoGitUrl, gitRepoCtx.WorkDir); err != nil {
126122
return nil, fmt.Errorf("failed to clone git repository: %w", err)
127123
}
128124

129125
log.Info("Repository has been cloned", "url", gitRepoCtx.RepoGitUrl)
130126
}
131127

132-
// Step 5: Get repo URL for checkout
133-
repoUrl, err := util.GetRepoUrl(codebase)
134-
if err != nil {
135-
return nil, fmt.Errorf("failed to build repo url: %w", err)
136-
}
137-
138-
// Step 6: Checkout default branch
139-
log.Info("Start checkout default branch", "branch", codebase.Spec.DefaultBranch, "repo", repoUrl)
128+
log.Info("Start checkout default branch", "branch", codebase.Spec.DefaultBranch)
140129

141-
err = CheckoutBranch(ctx, repoUrl, gitRepoCtx.WorkDir, codebase.Spec.DefaultBranch, g, codebase, c, createGitProviderWithConfig)
130+
err = CheckoutBranch(ctx, codebase.Spec.DefaultBranch, gitRepoCtx, codebase, c, gitProviderFactory)
142131
if err != nil {
143132
return nil, fmt.Errorf("failed to checkout default branch %v: %w", codebase.Spec.DefaultBranch, err)
144133
}
145134

146-
log.Info("Default branch has been checked out", "branch", codebase.Spec.DefaultBranch, "repo", repoUrl)
135+
log.Info("Default branch has been checked out", "branch", codebase.Spec.DefaultBranch)
147136

148-
// Return context for subsequent operations
149137
return gitRepoCtx, nil
150138
}
151139

controllers/codebase/service/chain/common_test.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -467,10 +467,7 @@ func TestPrepareGitRepository(t *testing.T) {
467467
context.Background(),
468468
k8sClient,
469469
tt.codebase,
470-
func(gitServer *codebaseApi.GitServer, secret *corev1.Secret) gitproviderv2.Git {
471-
return tt.gitClient(t)
472-
},
473-
func(config gitproviderv2.Config) gitproviderv2.Git {
470+
func(cfg gitproviderv2.Config) gitproviderv2.Git {
474471
return tt.gitClient(t)
475472
},
476473
)

controllers/codebase/service/chain/factory.go

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,17 @@ func MakeChain(ctx context.Context, c client.Client) handler.CodebaseHandler {
2222
ch := &chain{}
2323
gitlabCIManager := gitlabci.NewManager(c)
2424

25-
createGitProviderWithConfig := func(config gitproviderv2.Config) gitproviderv2.Git {
26-
return gitproviderv2.NewGitProvider(config)
27-
}
28-
2925
ch.Use(
3026
NewPutGitWebRepoUrl(c),
3127
NewPutProject(
3228
c,
3329
&gerrit.SSHGerritClient{},
3430
gitprovider.NewGitProjectProvider,
35-
gitproviderv2.DefaultGitProviderFactory,
36-
createGitProviderWithConfig,
31+
gitproviderv2.NewGitProviderFactory,
3732
),
3833
NewPutWebHook(c, resty.New()),
39-
NewPutGitLabCIConfig(c, gitlabCIManager, gitproviderv2.DefaultGitProviderFactory, createGitProviderWithConfig),
40-
NewPutDeployConfigs(c, gitproviderv2.DefaultGitProviderFactory, createGitProviderWithConfig),
34+
NewPutGitLabCIConfig(c, gitlabCIManager, gitproviderv2.NewGitProviderFactory),
35+
NewPutDeployConfigs(c, gitproviderv2.NewGitProviderFactory),
4136
NewPutDefaultCodeBaseBranch(c),
4237
NewCleaner(c),
4338
)

controllers/codebase/service/chain/put_deploy_configs.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,15 @@ import (
1414
)
1515

1616
type PutDeployConfigs struct {
17-
client client.Client
18-
gitProviderFactory gitproviderv2.GitProviderFactory
19-
createGitProviderWithConfig func(config gitproviderv2.Config) gitproviderv2.Git
17+
client client.Client
18+
gitProviderFactory gitproviderv2.GitProviderFactory
2019
}
2120

22-
func NewPutDeployConfigs(c client.Client, gitProviderFactory gitproviderv2.GitProviderFactory, createGitProviderWithConfig func(config gitproviderv2.Config) gitproviderv2.Git) *PutDeployConfigs {
23-
return &PutDeployConfigs{client: c, gitProviderFactory: gitProviderFactory, createGitProviderWithConfig: createGitProviderWithConfig}
21+
func NewPutDeployConfigs(
22+
c client.Client,
23+
gitProviderFactory gitproviderv2.GitProviderFactory,
24+
) *PutDeployConfigs {
25+
return &PutDeployConfigs{client: c, gitProviderFactory: gitProviderFactory}
2426
}
2527

2628
func (h *PutDeployConfigs) ServeRequest(ctx context.Context, c *codebaseApi.Codebase) error {
@@ -58,14 +60,14 @@ func (h *PutDeployConfigs) tryToPushConfigs(ctx context.Context, codebase *codeb
5860
}
5961

6062
// Prepare git repository (get server, clone, checkout)
61-
gitCtx, err := PrepareGitRepository(ctx, h.client, codebase, h.gitProviderFactory, h.createGitProviderWithConfig)
63+
gitCtx, err := PrepareGitRepository(ctx, h.client, codebase, h.gitProviderFactory)
6264
if err != nil {
6365
setFailedFields(codebase, codebaseApi.SetupDeploymentTemplates, err.Error())
6466
return fmt.Errorf("failed to prepare git repository: %w", err)
6567
}
6668

6769
// Create git provider using factory
68-
g := h.gitProviderFactory(gitCtx.GitServer, gitCtx.GitServerSecret)
70+
g := h.gitProviderFactory(gitproviderv2.NewConfigFromGitServerAndSecret(gitCtx.GitServer, gitCtx.GitServerSecret))
6971

7072
// Add Gerrit-specific commit hooks if needed
7173
if gitCtx.GitServer.Spec.GitProvider == codebaseApi.GitProviderGerrit {

controllers/codebase/service/chain/put_deploy_configs_test.go

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,12 @@ func TestPutDeployConfigs_ShouldPass(t *testing.T) {
108108
mGit.On("Push", testify.Anything, wd, gitproviderv2.RefSpecPushAllBranches).Return(nil)
109109
mGit.On("Clone", testify.Anything, testify.Anything, wd, testify.Anything).Return(nil)
110110

111-
pdc := NewPutDeployConfigs(fakeCl, func(gitServer *codebaseApi.GitServer, secret *coreV1.Secret) gitproviderv2.Git {
112-
return mGit
113-
}, func(config gitproviderv2.Config) gitproviderv2.Git {
114-
return mGit
115-
})
111+
pdc := NewPutDeployConfigs(
112+
fakeCl,
113+
func(cfg gitproviderv2.Config) gitproviderv2.Git {
114+
return mGit
115+
},
116+
)
116117

117118
err := pdc.ServeRequest(context.Background(), c)
118119
assert.NoError(t, err)
@@ -140,11 +141,12 @@ func TestPutDeployConfigs_ShouldPassWithNonApplication(t *testing.T) {
140141

141142
mGit := gitServerMocks.NewMockGit(t)
142143

143-
pdc := NewPutDeployConfigs(fakeCl, func(gitServer *codebaseApi.GitServer, secret *coreV1.Secret) gitproviderv2.Git {
144-
return mGit
145-
}, func(config gitproviderv2.Config) gitproviderv2.Git {
146-
return mGit
147-
})
144+
pdc := NewPutDeployConfigs(
145+
fakeCl,
146+
func(config gitproviderv2.Config) gitproviderv2.Git {
147+
return mGit
148+
},
149+
)
148150

149151
err := pdc.ServeRequest(context.Background(), c)
150152
assert.NoError(t, err)

controllers/codebase/service/chain/put_gitlab_ci_config.go

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,17 @@ import (
1616
)
1717

1818
type PutGitLabCIConfig struct {
19-
client client.Client
20-
gitlabCIManager gitlabci.Manager
21-
gitProviderFactory gitproviderv2.GitProviderFactory
22-
createGitProviderWithConfig func(config gitproviderv2.Config) gitproviderv2.Git
19+
client client.Client
20+
gitlabCIManager gitlabci.Manager
21+
gitProviderFactory gitproviderv2.GitProviderFactory
2322
}
2423

25-
func NewPutGitLabCIConfig(c client.Client, m gitlabci.Manager, gitProviderFactory gitproviderv2.GitProviderFactory, createGitProviderWithConfig func(config gitproviderv2.Config) gitproviderv2.Git) *PutGitLabCIConfig {
26-
return &PutGitLabCIConfig{client: c, gitlabCIManager: m, gitProviderFactory: gitProviderFactory, createGitProviderWithConfig: createGitProviderWithConfig}
24+
func NewPutGitLabCIConfig(
25+
c client.Client,
26+
m gitlabci.Manager,
27+
gitProviderFactory gitproviderv2.GitProviderFactory,
28+
) *PutGitLabCIConfig {
29+
return &PutGitLabCIConfig{client: c, gitlabCIManager: m, gitProviderFactory: gitProviderFactory}
2730
}
2831

2932
func (h *PutGitLabCIConfig) ServeRequest(ctx context.Context, codebase *codebaseApi.Codebase) error {
@@ -83,14 +86,14 @@ func (h *PutGitLabCIConfig) tryToPushGitLabCIConfig(ctx context.Context, codebas
8386
log := ctrl.LoggerFrom(ctx)
8487

8588
// Prepare git repository (get server, clone, checkout)
86-
gitCtx, err := PrepareGitRepository(ctx, h.client, codebase, h.gitProviderFactory, h.createGitProviderWithConfig)
89+
gitCtx, err := PrepareGitRepository(ctx, h.client, codebase, h.gitProviderFactory)
8790
if err != nil {
8891
setFailedFields(codebase, codebaseApi.RepositoryProvisioning, err.Error())
8992
return fmt.Errorf("failed to prepare git repository: %w", err)
9093
}
9194

9295
// Create git provider using factory
93-
g := h.gitProviderFactory(gitCtx.GitServer, gitCtx.GitServerSecret)
96+
g := h.gitProviderFactory(gitproviderv2.NewConfigFromGitServerAndSecret(gitCtx.GitServer, gitCtx.GitServerSecret))
9497

9598
// Inject GitLab CI configuration
9699
log.Info("Start injecting GitLab CI config")

controllers/codebase/service/chain/put_gitlab_ci_config_test.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -505,10 +505,7 @@ func TestPutGitLabCIConfig_ServeRequest(t *testing.T) {
505505
h := NewPutGitLabCIConfig(
506506
k8sClient,
507507
gitlabci.NewManager(k8sClient),
508-
func(gitServer *codebaseApi.GitServer, secret *corev1.Secret) gitproviderv2.Git {
509-
return gitClient
510-
},
511-
func(config gitproviderv2.Config) gitproviderv2.Git {
508+
func(gitproviderv2.Config) gitproviderv2.Git {
512509
return gitClient
513510
},
514511
)

0 commit comments

Comments
 (0)