Skip to content

Commit da3afdf

Browse files
authored
Merge pull request #2330 from pratikjagrut/dependency.path
feat: fallback to ssh if https doesn't work and vice versa
2 parents 391d49c + 672462f commit da3afdf

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

pkg/devspace/dependency/util/util.go

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,20 @@ func GetDependencyPath(workingDirectory string, source *latest.SourceConfig) (co
6464
return getDependencyConfigPath(localPath, source)
6565
}
6666

67+
// switch https <-> ssh urls
68+
func switchURLType(gitPath string) string {
69+
var newGitURL string
70+
if strings.HasPrefix(gitPath, "https") {
71+
splitURL := strings.Split(gitPath, "/")
72+
newGitURL = fmt.Sprintf("git@%s:%s", splitURL[2], strings.Join(splitURL[3:], "/"))
73+
} else {
74+
splitURL := strings.Split(gitPath, "@")
75+
replacedURL := strings.ReplaceAll(splitURL[1], ":", "/")
76+
newGitURL = fmt.Sprintf("https://%s", replacedURL)
77+
}
78+
return newGitURL
79+
}
80+
6781
func DownloadDependency(ctx context.Context, workingDirectory string, source *latest.SourceConfig, log log.Logger) (configPath string, err error) {
6882
downloadMutex.Lock()
6983
defer downloadMutex.Unlock()
@@ -110,7 +124,17 @@ func DownloadDependency(ctx context.Context, workingDirectory string, source *la
110124
return getDependencyConfigPath(localPath, source)
111125
}
112126

113-
return "", errors.Wrap(err, "clone repository")
127+
err = repo.Clone(ctx, git.CloneOptions{
128+
URL: switchURLType(gitPath),
129+
Tag: source.Tag,
130+
Branch: source.Branch,
131+
Commit: source.Revision,
132+
Args: source.CloneArgs,
133+
DisableShallow: source.DisableShallow,
134+
})
135+
if err != nil {
136+
return "", errors.Wrap(err, "clone repository")
137+
}
114138
}
115139

116140
log.Debugf("Pulled %s", gitPath)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package util
2+
3+
import (
4+
"testing"
5+
6+
"gotest.tools/assert"
7+
)
8+
9+
func TestSwitchURLType(t *testing.T) {
10+
httpURL := "https://github.com/loft-sh/devspace.git"
11+
sshURL := "[email protected]:loft-sh/devspace.git"
12+
13+
assert.Equal(t, sshURL, switchURLType(httpURL))
14+
assert.Equal(t, httpURL, switchURLType(sshURL))
15+
}

0 commit comments

Comments
 (0)