You've already forked sap-jenkins-library
							
							
				mirror of
				https://github.com/SAP/jenkins-library.git
				synced 2025-10-30 23:57:50 +02:00 
			
		
		
		
	fix(gitopsUpdateDeployment): take into account branch name when clonning (#4811)
* use branch when clonning a repo * fix unit test mocks
This commit is contained in:
		| @@ -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 | ||||
|  | ||||
| } | ||||
|   | ||||
| @@ -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 | ||||
| } | ||||
|  | ||||
| @@ -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) | ||||
| 	} | ||||
| @@ -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) | ||||
| 	if err != nil { | ||||
| 		return errors.Wrap(err, "failed to plain clone repository") | ||||
| 	} | ||||
|   | ||||
| @@ -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 | ||||
| 	} | ||||
|   | ||||
| @@ -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 { | ||||
|   | ||||
| @@ -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) | ||||
| @@ -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") | ||||
| 	}) | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user