From 3b15d507ff9ef2b5777719245ea2dcd3fd277d0b Mon Sep 17 00:00:00 2001 From: zhzhang Date: Mon, 18 Nov 2024 16:45:52 +0800 Subject: [PATCH] Fix ssh clone url when ssh domain has port --- common/utils/common/repo.go | 7 ++++++- common/utils/common/repo_test.go | 24 ++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/common/utils/common/repo.go b/common/utils/common/repo.go index c9831deb..fe3a0b3d 100644 --- a/common/utils/common/repo.go +++ b/common/utils/common/repo.go @@ -68,5 +68,10 @@ func buildHTTPCloneURL(domain string, repoType types.RepositoryType, path string func buildSSHCloneURL(domain string, repoType types.RepositoryType, path string) string { sshDomainWithoutPrefix := strings.TrimPrefix(domain, "ssh://") - return fmt.Sprintf("%s:%ss/%s.git", strings.TrimSuffix(sshDomainWithoutPrefix, "/"), repoType, path) + hasPort := strings.Contains(sshDomainWithoutPrefix, ":") + if hasPort { + return fmt.Sprintf("ssh://%s/%ss/%s.git", strings.TrimSuffix(sshDomainWithoutPrefix, "/"), repoType, path) + } else { + return fmt.Sprintf("%s:%ss/%s.git", strings.TrimSuffix(sshDomainWithoutPrefix, "/"), repoType, path) + } } diff --git a/common/utils/common/repo_test.go b/common/utils/common/repo_test.go index d0826807..2370176c 100644 --- a/common/utils/common/repo_test.go +++ b/common/utils/common/repo_test.go @@ -173,6 +173,30 @@ func TestBuildCloneInfo(t *testing.T) { SSHCloneURL: "git@opencsg.com:models/abc/def.git", }, }, + { + name: "Test BuildCloneInfo when SSHDomain without ssh:// prefix and port", + args: args{ + config: &config.Config{ + APIServer: struct { + Port int `env:"STARHUB_SERVER_SERVER_PORT, default=8080"` + PublicDomain string `env:"STARHUB_SERVER_PUBLIC_DOMAIN, default=http://localhost:8080"` + SSHDomain string `env:"STARHUB_SERVER_SSH_DOMAIN, default=git@localhost:2222"` + }{ + Port: 8080, + PublicDomain: "https://opencsg.com", + SSHDomain: "ssh://git@opencsg.com:2222", + }, + }, + repository: &database.Repository{ + RepositoryType: types.ModelRepo, + Path: "abc/def", + }, + }, + want: types.Repository{ + HTTPCloneURL: "https://opencsg.com/models/abc/def.git", + SSHCloneURL: "ssh://git@opencsg.com:2222/models/abc/def.git", + }, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) {