From f4529224992946cf6b9ecc2db8fcb353b86c1e8a Mon Sep 17 00:00:00 2001 From: Ivan Dagelic Date: Mon, 21 Oct 2024 13:04:23 +0200 Subject: [PATCH] fix: support http protocol for git urls Signed-off-by: Ivan Dagelic --- pkg/git/service.go | 17 +++---- pkg/git/service_test.go | 98 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 107 insertions(+), 8 deletions(-) create mode 100644 pkg/git/service_test.go diff --git a/pkg/git/service.go b/pkg/git/service.go index 0b4aa29345..1c66db81ab 100644 --- a/pkg/git/service.go +++ b/pkg/git/service.go @@ -100,18 +100,19 @@ func (s *Service) CloneRepository(repo *gitprovider.GitRepository, auth *http.Ba } func (s *Service) CloneRepositoryCmd(repo *gitprovider.GitRepository, auth *http.BasicAuth) []string { - branch := fmt.Sprintf("\"%s\"", repo.Branch) - cloneCmd := []string{"git", "clone", "--single-branch", "--branch", branch} + cloneCmd := []string{"git", "clone", "--single-branch", "--branch", fmt.Sprintf("\"%s\"", repo.Branch)} + cloneUrl := repo.Url + + // Default to https protocol if not specified + if !strings.Contains(cloneUrl, "://") { + cloneUrl = fmt.Sprintf("https://%s", cloneUrl) + } if auth != nil { - repoUrl := strings.TrimPrefix(repo.Url, "https://") - repoUrl = strings.TrimPrefix(repoUrl, "http://") - cloneCmd = append(cloneCmd, fmt.Sprintf("https://%s:%s@%s", auth.Username, auth.Password, repoUrl)) - } else { - cloneCmd = append(cloneCmd, repo.Url) + cloneUrl = fmt.Sprintf("%s://%s:%s@%s", strings.Split(cloneUrl, "://")[0], auth.Username, auth.Password, strings.SplitN(cloneUrl, "://", 2)[1]) } - cloneCmd = append(cloneCmd, s.ProjectDir) + cloneCmd = append(cloneCmd, cloneUrl, s.ProjectDir) if repo.Target == gitprovider.CloneTargetCommit { cloneCmd = append(cloneCmd, "&&", "cd", s.ProjectDir) diff --git a/pkg/git/service_test.go b/pkg/git/service_test.go new file mode 100644 index 0000000000..f1d7ad4b26 --- /dev/null +++ b/pkg/git/service_test.go @@ -0,0 +1,98 @@ +// Copyright 2024 Daytona Platforms Inc. +// SPDX-License-Identifier: Apache-2.0 + +package git_test + +import ( + "testing" + + "github.com/daytonaio/daytona/pkg/git" + "github.com/daytonaio/daytona/pkg/gitprovider" + "github.com/go-git/go-git/v5/plumbing/transport/http" + "github.com/stretchr/testify/suite" +) + +var repoHttp = &gitprovider.GitRepository{ + Id: "123", + Url: "http://localhost:3000/daytonaio/daytona", + Name: "daytona", + Branch: "main", + Target: gitprovider.CloneTargetBranch, +} + +var repoHttps = &gitprovider.GitRepository{ + Id: "123", + Url: "https://github.com/daytonaio/daytona", + Name: "daytona", + Branch: "main", + Target: gitprovider.CloneTargetBranch, +} + +var repoWithoutProtocol = &gitprovider.GitRepository{ + Id: "123", + Url: "github.com/daytonaio/daytona", + Name: "daytona", + Branch: "main", + Target: gitprovider.CloneTargetBranch, +} + +var repoWithCloneTargetCommit = &gitprovider.GitRepository{ + Id: "123", + Url: "https://github.com/daytonaio/daytona", + Name: "daytona", + Branch: "main", + Sha: "1234567890", + Target: gitprovider.CloneTargetCommit, +} + +var creds = &http.BasicAuth{ + Username: "daytonaio", + Password: "Daytona123", +} + +type GitServiceTestSuite struct { + suite.Suite + gitService git.IGitService +} + +func NewGitServiceTestSuite() *GitServiceTestSuite { + return &GitServiceTestSuite{} +} + +func (s *GitServiceTestSuite) SetupTest() { + s.gitService = &git.Service{ + ProjectDir: "/workdir", + } +} + +func TestGitService(t *testing.T) { + suite.Run(t, NewGitServiceTestSuite()) +} + +func (s *GitServiceTestSuite) TestCloneRepositoryCmd_WithCreds() { + cloneCmd := s.gitService.CloneRepositoryCmd(repoHttps, creds) + s.Require().Equal([]string{"git", "clone", "--single-branch", "--branch", "\"main\"", "https://daytonaio:Daytona123@github.com/daytonaio/daytona", "/workdir"}, cloneCmd) + + cloneCmd = s.gitService.CloneRepositoryCmd(repoHttp, creds) + s.Require().Equal([]string{"git", "clone", "--single-branch", "--branch", "\"main\"", "http://daytonaio:Daytona123@localhost:3000/daytonaio/daytona", "/workdir"}, cloneCmd) + + cloneCmd = s.gitService.CloneRepositoryCmd(repoWithoutProtocol, creds) + s.Require().Equal([]string{"git", "clone", "--single-branch", "--branch", "\"main\"", "https://daytonaio:Daytona123@github.com/daytonaio/daytona", "/workdir"}, cloneCmd) + + cloneCmd = s.gitService.CloneRepositoryCmd(repoWithCloneTargetCommit, creds) + s.Require().Equal([]string{"git", "clone", "--single-branch", "--branch", "\"main\"", "https://daytonaio:Daytona123@github.com/daytonaio/daytona", "/workdir", "&&", "cd", "/workdir", "&&", "git", "checkout", "1234567890"}, cloneCmd) +} + +func (s *GitServiceTestSuite) TestCloneRepositoryCmd_WithoutCreds() { + cloneCmd := s.gitService.CloneRepositoryCmd(repoHttps, nil) + s.Require().Equal([]string{"git", "clone", "--single-branch", "--branch", "\"main\"", "https://github.com/daytonaio/daytona", "/workdir"}, cloneCmd) + + cloneCmd = s.gitService.CloneRepositoryCmd(repoHttp, nil) + s.Require().Equal([]string{"git", "clone", "--single-branch", "--branch", "\"main\"", "http://localhost:3000/daytonaio/daytona", "/workdir"}, cloneCmd) + + cloneCmd = s.gitService.CloneRepositoryCmd(repoWithoutProtocol, nil) + s.Require().Equal([]string{"git", "clone", "--single-branch", "--branch", "\"main\"", "https://github.com/daytonaio/daytona", "/workdir"}, cloneCmd) + + cloneCmd = s.gitService.CloneRepositoryCmd(repoWithCloneTargetCommit, nil) + s.Require().Equal([]string{"git", "clone", "--single-branch", "--branch", "\"main\"", "https://github.com/daytonaio/daytona", "/workdir", "&&", "cd", "/workdir", "&&", "git", "checkout", "1234567890"}, cloneCmd) +}