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

fix(gitopsUpdateDeployment): take into account branch name when clonning #4811

Merged
merged 4 commits into from
Feb 6, 2024
Merged
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
2 changes: 1 addition & 1 deletion cmd/batsExecuteTests.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func runBatsExecuteTests(config *batsExecuteTestsOptions, telemetryData *telemet
func (b *batsExecuteTestsUtilsBundle) CloneRepo(URL string) error {
// ToDo: BatsExecute test needs to check if the repo can come from a
// enterprise github instance and needs ca-cert handelling seperately
_, err := pipergit.PlainClone("", "", URL, "bats-core", []byte{})
_, err := pipergit.PlainClone("", "", URL, "", "bats-core", []byte{})
return err

}
8 changes: 4 additions & 4 deletions cmd/gitopsUpdateDeployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const toolKustomize = "kustomize"
type iGitopsUpdateDeploymentGitUtils interface {
CommitFiles(filePaths []string, commitMessage, author string) (plumbing.Hash, error)
PushChangesToRepository(username, password string, force *bool, caCerts []byte) error
PlainClone(username, password, serverURL, directory string, caCerts []byte) error
PlainClone(username, password, serverURL, branchName, directory string, caCerts []byte) error
ChangeBranch(branchName string) error
}

Expand Down Expand Up @@ -99,9 +99,9 @@ func (g *gitopsUpdateDeploymentGitUtils) PushChangesToRepository(username, passw
return gitUtil.PushChangesToRepository(username, password, force, g.repository, caCerts)
}

func (g *gitopsUpdateDeploymentGitUtils) PlainClone(username, password, serverURL, directory string, caCerts []byte) error {
func (g *gitopsUpdateDeploymentGitUtils) PlainClone(username, password, serverURL, branchName, directory string, caCerts []byte) error {
var err error
g.repository, err = gitUtil.PlainClone(username, password, serverURL, directory, caCerts)
g.repository, err = gitUtil.PlainClone(username, password, serverURL, branchName, directory, caCerts)
if err != nil {
return errors.Wrapf(err, "plain clone failed '%s'", serverURL)
}
Expand Down Expand Up @@ -323,7 +323,7 @@ func logNotRequiredButFilledFieldForKustomize(config *gitopsUpdateDeploymentOpti

func cloneRepositoryAndChangeBranch(config *gitopsUpdateDeploymentOptions, gitUtils iGitopsUpdateDeploymentGitUtils, fileUtils gitopsUpdateDeploymentFileUtils, temporaryFolder string, certs []byte) error {

err := gitUtils.PlainClone(config.Username, config.Password, config.ServerURL, temporaryFolder, certs)
err := gitUtils.PlainClone(config.Username, config.Password, config.ServerURL, config.BranchName, temporaryFolder, certs)
CCFenner marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return errors.Wrap(err, "failed to plain clone repository")
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/gitopsUpdateDeployment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -866,7 +866,7 @@ func (v gitUtilsMock) PushChangesToRepository(_ string, _ string, force *bool, c
return nil
}

func (v *gitUtilsMock) PlainClone(_, _, _, directory string, caCerts []byte) error {
func (v *gitUtilsMock) PlainClone(_, _, _, _, directory string, caCerts []byte) error {
if v.skipClone {
return nil
}
Expand Down
12 changes: 7 additions & 5 deletions pkg/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,17 @@ func pushChangesToRepository(username, password string, force *bool, repository
}

// PlainClone Clones a non-bare repository to the provided directory
func PlainClone(username, password, serverURL, directory string, caCerts []byte) (*git.Repository, error) {
func PlainClone(username, password, serverURL, branchName, directory string, caCerts []byte) (*git.Repository, error) {
abstractedGit := &abstractionGit{}
return plainClone(username, password, serverURL, directory, abstractedGit, caCerts)
return plainClone(username, password, serverURL, branchName, directory, abstractedGit, caCerts)
}

func plainClone(username, password, serverURL, directory string, abstractionGit utilsGit, caCerts []byte) (*git.Repository, error) {
func plainClone(username, password, serverURL, branchName, directory string, abstractionGit utilsGit, caCerts []byte) (*git.Repository, error) {
gitCloneOptions := git.CloneOptions{
Auth: &http.BasicAuth{Username: username, Password: password},
URL: serverURL,
Auth: &http.BasicAuth{Username: username, Password: password},
URL: serverURL,
ReferenceName: plumbing.NewBranchReferenceName(branchName),
SingleBranch: true, // we don't need other branches, clone only branchName
}

if len(caCerts) > 0 {
Expand Down
4 changes: 2 additions & 2 deletions pkg/git/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func TestPlainClone(t *testing.T) {
t.Run("successful clone", func(t *testing.T) {
t.Parallel()
abstractedGit := &UtilsGitMock{}
_, err := plainClone("user", "password", "URL", "directory", abstractedGit, []byte{})
_, err := plainClone("user", "password", "URL", "", "directory", abstractedGit, []byte{})
assert.NoError(t, err)
assert.Equal(t, "directory", abstractedGit.path)
assert.False(t, abstractedGit.isBare)
Expand All @@ -78,7 +78,7 @@ func TestPlainClone(t *testing.T) {
t.Run("error on cloning", func(t *testing.T) {
t.Parallel()
abstractedGit := UtilsGitMockError{}
_, err := plainClone("user", "password", "URL", "directory", abstractedGit, []byte{})
_, err := plainClone("user", "password", "URL", "", "directory", abstractedGit, []byte{})
assert.EqualError(t, err, "failed to clone git: error during clone")
})
}
Expand Down
Loading