Skip to content

Commit 7ccba58

Browse files
Merge branch 'nephio-project:main' into disaster-recovery-setup
2 parents d8f5d4b + 5f9b783 commit 7ccba58

2 files changed

Lines changed: 45 additions & 19 deletions

File tree

pkg/externalrepo/git/gitrepofactory.go

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,13 @@ func (f *GitRepoFactory) NewRepositoryImpl(ctx context.Context, repositorySpec *
6666
}
6767

6868
func (f *GitRepoFactory) CheckRepositoryConnection(ctx context.Context, repositorySpec *configapi.Repository, options externalrepotypes.ExternalRepoOptions) error {
69-
// Nil checks
70-
if repositorySpec == nil || repositorySpec.Spec.Git == nil {
71-
return fmt.Errorf("repositorySpec is nil or missing Git configuration")
72-
}
73-
if repositorySpec.Spec.Git.Repo == "" {
74-
return fmt.Errorf("repository URL is empty")
69+
if err := f.validateRepositorySpec(repositorySpec); err != nil {
70+
return err
7571
}
76-
if repositorySpec.Spec.Git.Branch == "" {
77-
return fmt.Errorf("target branch is empty")
72+
73+
branch := repositorySpec.Spec.Git.Branch
74+
if branch == "" {
75+
branch = "main"
7876
}
7977

8078
// Fetch credentials from secret
@@ -101,7 +99,7 @@ func (f *GitRepoFactory) CheckRepositoryConnection(ctx context.Context, reposito
10199
})
102100

103101
refs, err := remote.List(&gogit.ListOptions{
104-
Auth: auth,
102+
Auth: auth,
105103
Timeout: 20,
106104
})
107105
if err != nil {
@@ -110,15 +108,25 @@ func (f *GitRepoFactory) CheckRepositoryConnection(ctx context.Context, reposito
110108

111109
branchExists := false
112110
for _, ref := range refs {
113-
if ref.Name().IsBranch() && ref.Name().Short() == repositorySpec.Spec.Git.Branch {
111+
if ref.Name().IsBranch() && ref.Name().Short() == branch {
114112
branchExists = true
115113
break
116114
}
117115
}
118116

119-
if !branchExists {
120-
return fmt.Errorf("branch %q not found in repository", repositorySpec.Spec.Git.Branch)
117+
if !branchExists && !repositorySpec.Spec.Git.CreateBranch {
118+
return fmt.Errorf("branch %q not found in repository", branch)
121119
}
122120

123121
return nil
124122
}
123+
124+
func (f *GitRepoFactory) validateRepositorySpec(repositorySpec *configapi.Repository) error {
125+
if repositorySpec == nil || repositorySpec.Spec.Git == nil {
126+
return fmt.Errorf("repositorySpec is nil or missing Git configuration")
127+
}
128+
if repositorySpec.Spec.Git.Repo == "" {
129+
return fmt.Errorf("repository URL is empty")
130+
}
131+
return nil
132+
}

pkg/externalrepo/git/gitrepofactory_test.go

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,6 @@ func TestCheckRepositoryConnection(t *testing.T) {
8080
assert.True(t, err != nil)
8181
assert.Contains(t, err.Error(), "repository URL is empty")
8282

83-
// empty branch name
84-
repoSpec.Spec.Git.Repo = "https://example.com/repo.git"
85-
repoSpec.Spec.Git.Branch = ""
86-
err = gf.CheckRepositoryConnection(context.TODO(), repoSpec, externalrepotypes.ExternalRepoOptions{})
87-
assert.True(t, err != nil)
88-
assert.Contains(t, err.Error(), "target branch is empty")
89-
9083
tempDir := t.TempDir()
9184
tarfile := filepath.Join("testdata", "trivial-repository.tar")
9285
branch := "main"
@@ -105,6 +98,31 @@ func TestCheckRepositoryConnection(t *testing.T) {
10598
err = gf.CheckRepositoryConnection(context.TODO(), repoSpec, externalrepotypes.ExternalRepoOptions{})
10699
assert.NoError(t, err)
107100

101+
// empty branch defaults to main
102+
repoSpec = &configapi.Repository{
103+
Spec: configapi.RepositorySpec{
104+
Git: &configapi.GitRepository{
105+
Repo: address,
106+
Branch: "", // empty branch should default to main
107+
},
108+
},
109+
}
110+
err = gf.CheckRepositoryConnection(context.TODO(), repoSpec, externalrepotypes.ExternalRepoOptions{})
111+
assert.NoError(t, err)
112+
113+
// branch not found but createBranch is set to true
114+
repoSpec = &configapi.Repository{
115+
Spec: configapi.RepositorySpec{
116+
Git: &configapi.GitRepository{
117+
Repo: address,
118+
Branch: "nonexistent-branch",
119+
CreateBranch: true,
120+
},
121+
},
122+
}
123+
err = gf.CheckRepositoryConnection(context.TODO(), repoSpec, externalrepotypes.ExternalRepoOptions{})
124+
assert.NoError(t, err)
125+
108126
// branch not found
109127
repoSpec = &configapi.Repository{
110128
Spec: configapi.RepositorySpec{

0 commit comments

Comments
 (0)