diff --git a/services/migrations/codebase.go b/services/migrations/codebase.go index e85e64bb06..97c6cfe7aa 100644 --- a/services/migrations/codebase.go +++ b/services/migrations/codebase.go @@ -33,19 +33,22 @@ type CodebaseDownloaderFactory struct{} // New returns a downloader related to this factory according MigrateOptions func (f *CodebaseDownloaderFactory) New(ctx context.Context, opts base.MigrateOptions) (base.Downloader, error) { - info, err := parseServiceCloneURL(opts.CloneAddr) + u, err := url.Parse(opts.CloneAddr) if err != nil { return nil, err } - if len(info.segments) != 2 { - return nil, fmt.Errorf("invalid path: %s", info.repoPath) + u.User = nil + + fields := strings.Split(strings.Trim(u.Path, "/"), "/") + if len(fields) != 2 { + return nil, fmt.Errorf("invalid path: %s", u.Path) } - project := info.segments[0] - repoName := strings.TrimSuffix(info.segments[1], ".git") + project := fields[0] + repoName := strings.TrimSuffix(fields[1], ".git") - log.Trace("Create Codebase downloader. BaseURL: %v RepoName: %s", info.apiURL, repoName) + log.Trace("Create Codebase downloader. BaseURL: %v RepoName: %s", u, repoName) - return NewCodebaseDownloader(ctx, info.apiURL, project, repoName, opts.AuthUsername, opts.AuthPassword), nil + return NewCodebaseDownloader(ctx, u, project, repoName, opts.AuthUsername, opts.AuthPassword), nil } // GitServiceType returns the type of git service diff --git a/services/migrations/codecommit.go b/services/migrations/codecommit.go index 05745ae5a2..2c2019470a 100644 --- a/services/migrations/codecommit.go +++ b/services/migrations/codecommit.go @@ -34,23 +34,26 @@ type CodeCommitDownloaderFactory struct{} // New returns a Downloader related to this factory according MigrateOptions func (c *CodeCommitDownloaderFactory) New(ctx context.Context, opts base.MigrateOptions) (base.Downloader, error) { - info, err := parseServiceCloneURL(opts.CloneAddr) + u, err := url.Parse(opts.CloneAddr) if err != nil { return nil, err } - hostElems := strings.Split(info.apiURL.Host, ".") + hostElems := strings.Split(u.Host, ".") if len(hostElems) != 4 { return nil, errors.New("cannot get the region from clone URL") } region := hostElems[1] - if len(info.segments) == 0 || info.segments[len(info.segments)-1] == "" { + pathElems := strings.Split(u.Path, "/") + if len(pathElems) == 0 { return nil, errors.New("cannot get the repo name from clone URL") } - repoName := info.segments[len(info.segments)-1] + repoName := pathElems[len(pathElems)-1] - return NewCodeCommitDownloader(ctx, repoName, info.apiURL.String(), opts.AWSAccessKeyID, opts.AWSSecretAccessKey, region), nil + baseURL := u.Scheme + "://" + u.Host + + return NewCodeCommitDownloader(ctx, repoName, baseURL, opts.AWSAccessKeyID, opts.AWSSecretAccessKey, region), nil } // GitServiceType returns the type of git service diff --git a/services/migrations/common.go b/services/migrations/common.go index fce6567a34..e9895d00c2 100644 --- a/services/migrations/common.go +++ b/services/migrations/common.go @@ -5,7 +5,6 @@ package migrations import ( "fmt" - "net/url" "strings" system_model "gitea.dev/models/system" @@ -14,36 +13,6 @@ import ( base "gitea.dev/modules/migration" ) -// serviceCloneURLInfo bundles the API base URL and parsed repo path of a clone -// address, hiding scheme conversion (ssh→https) needed for forge API calls. -type serviceCloneURLInfo struct { - apiURL *url.URL - repoPath string - segments []string -} - -// parseServiceCloneURL parses a clone address and returns its API base URL -// (always http/https — ssh/git are promoted to https for API calls) together -// with the repo path and its segments. -func parseServiceCloneURL(cloneAddr string) (*serviceCloneURLInfo, error) { - u, err := url.Parse(cloneAddr) - if err != nil { - return nil, err - } - apiURL := *u - apiURL.User = nil - apiURL.Path = "" - apiURL.RawQuery = "" - apiURL.Fragment = "" - // Forge APIs are HTTP(S) only; promote ssh/git clone schemes to https. - if apiURL.Scheme == "ssh" || apiURL.Scheme == "git" { - apiURL.Scheme = "https" - } - repoPath := strings.TrimPrefix(u.Path, "/") - segments := strings.Split(repoPath, "/") - return &serviceCloneURLInfo{apiURL: &apiURL, repoPath: repoPath, segments: segments}, nil -} - // WarnAndNotice will log the provided message and send a repository notice func WarnAndNotice(fmtStr string, args ...any) { log.Warn(fmtStr, args...) diff --git a/services/migrations/gitbucket.go b/services/migrations/gitbucket.go index 69f5f7ead8..026fa3c12b 100644 --- a/services/migrations/gitbucket.go +++ b/services/migrations/gitbucket.go @@ -6,6 +6,7 @@ package migrations import ( "context" "fmt" + "net/url" "strings" "gitea.dev/modules/log" @@ -27,24 +28,19 @@ type GitBucketDownloaderFactory struct{} // New returns a Downloader related to this factory according MigrateOptions func (f *GitBucketDownloaderFactory) New(ctx context.Context, opts base.MigrateOptions) (base.Downloader, error) { - info, err := parseServiceCloneURL(opts.CloneAddr) + u, err := url.Parse(opts.CloneAddr) if err != nil { return nil, err } - if len(info.segments) < 2 { - return nil, fmt.Errorf("invalid path: %s", info.repoPath) - } - // GitBucket exposes its API at "/" where is the URL - // minus the trailing "/git//.git" used for the git clone endpoint. - subPath := strings.Join(info.segments[:len(info.segments)-2], "/") - if subPath != "" { - subPath = "/" + subPath + fields := strings.Split(u.Path, "/") + if len(fields) < 2 { + return nil, fmt.Errorf("invalid path: %s", u.Path) } - baseURL := info.apiURL.String() + strings.TrimSuffix(subPath, "/git") + baseURL := u.Scheme + "://" + u.Host + strings.TrimSuffix(strings.Join(fields[:len(fields)-2], "/"), "/git") - oldOwner := info.segments[len(info.segments)-2] - oldName := strings.TrimSuffix(info.segments[len(info.segments)-1], ".git") + oldOwner := fields[len(fields)-2] + oldName := strings.TrimSuffix(fields[len(fields)-1], ".git") log.Trace("Create GitBucket downloader. BaseURL: %s RepoOwner: %s RepoName: %s", baseURL, oldOwner, oldName) return NewGitBucketDownloader(ctx, baseURL, opts.AuthUsername, opts.AuthPassword, opts.AuthToken, oldOwner, oldName) diff --git a/services/migrations/gitea_downloader.go b/services/migrations/gitea_downloader.go index 2fbadff3c6..c754d70090 100644 --- a/services/migrations/gitea_downloader.go +++ b/services/migrations/gitea_downloader.go @@ -9,6 +9,7 @@ import ( "fmt" "io" "net/http" + "net/url" "strings" "time" @@ -32,21 +33,24 @@ type GiteaDownloaderFactory struct{} // New returns a Downloader related to this factory according MigrateOptions func (f *GiteaDownloaderFactory) New(ctx context.Context, opts base.MigrateOptions) (base.Downloader, error) { - info, err := parseServiceCloneURL(opts.CloneAddr) + u, err := url.Parse(opts.CloneAddr) if err != nil { return nil, err } - repoNameSpace := strings.TrimSuffix(info.repoPath, ".git") + baseURL := u.Scheme + "://" + u.Host + repoNameSpace := strings.TrimPrefix(u.Path, "/") + repoNameSpace = strings.TrimSuffix(repoNameSpace, ".git") + path := strings.Split(repoNameSpace, "/") if len(path) < 2 { return nil, fmt.Errorf("invalid path: %s", repoNameSpace) } - baseURL := info.apiURL.String() repoPath := strings.Join(path[len(path)-2:], "/") if len(path) > 2 { - baseURL += "/" + strings.Join(path[:len(path)-2], "/") + subPath := strings.Join(path[:len(path)-2], "/") + baseURL += "/" + subPath } log.Trace("Create gitea downloader. BaseURL: %s RepoName: %s", baseURL, repoNameSpace) diff --git a/services/migrations/github.go b/services/migrations/github.go index f45f09191d..b8bf6aee45 100644 --- a/services/migrations/github.go +++ b/services/migrations/github.go @@ -40,17 +40,15 @@ type GithubDownloaderV3Factory struct{} // New returns a Downloader related to this factory according MigrateOptions func (f *GithubDownloaderV3Factory) New(ctx context.Context, opts base.MigrateOptions) (base.Downloader, error) { - info, err := parseServiceCloneURL(opts.CloneAddr) + u, err := url.Parse(opts.CloneAddr) if err != nil { return nil, err } - if len(info.segments) < 2 { - return nil, fmt.Errorf("invalid path: %s", info.repoPath) - } - baseURL := info.apiURL.String() - oldOwner := info.segments[0] - oldName := strings.TrimSuffix(info.segments[1], ".git") + baseURL := u.Scheme + "://" + u.Host + fields := strings.Split(u.Path, "/") + oldOwner := fields[1] + oldName := strings.TrimSuffix(fields[2], ".git") log.Trace("Create github downloader BaseURL: %s %s/%s", baseURL, oldOwner, oldName) diff --git a/services/migrations/gitlab.go b/services/migrations/gitlab.go index 9a77bf1d83..cf4c2f24e9 100644 --- a/services/migrations/gitlab.go +++ b/services/migrations/gitlab.go @@ -39,13 +39,14 @@ type GitlabDownloaderFactory struct{} // New returns a Downloader related to this factory according MigrateOptions func (f *GitlabDownloaderFactory) New(ctx context.Context, opts base.MigrateOptions) (base.Downloader, error) { - info, err := parseServiceCloneURL(opts.CloneAddr) + u, err := url.Parse(opts.CloneAddr) if err != nil { return nil, err } - baseURL := info.apiURL.String() - repoNameSpace := strings.TrimSuffix(info.repoPath, ".git") + baseURL := u.Scheme + "://" + u.Host + repoNameSpace := strings.TrimPrefix(u.Path, "/") + repoNameSpace = strings.TrimSuffix(repoNameSpace, ".git") log.Trace("Create gitlab downloader. BaseURL: %s RepoName: %s", baseURL, repoNameSpace) diff --git a/services/migrations/gogs.go b/services/migrations/gogs.go index 73ea856bc9..7a41851e14 100644 --- a/services/migrations/gogs.go +++ b/services/migrations/gogs.go @@ -32,19 +32,22 @@ type GogsDownloaderFactory struct{} // New returns a Downloader related to this factory according MigrateOptions func (f *GogsDownloaderFactory) New(ctx context.Context, opts base.MigrateOptions) (base.Downloader, error) { - info, err := parseServiceCloneURL(opts.CloneAddr) + u, err := url.Parse(opts.CloneAddr) if err != nil { return nil, err } - repoNameSpace := strings.TrimSuffix(info.repoPath, ".git") - if len(info.segments) < 2 || info.segments[0] == "" { + baseURL := u.Scheme + "://" + u.Host + repoNameSpace := strings.TrimSuffix(u.Path, ".git") + repoNameSpace = strings.Trim(repoNameSpace, "/") + + fields := strings.Split(repoNameSpace, "/") + if len(fields) < 2 { return nil, fmt.Errorf("invalid path: %s", repoNameSpace) } - baseURL := info.apiURL.String() - log.Trace("Create gogs downloader. BaseURL: %s RepoOwner: %s RepoName: %s", baseURL, info.segments[0], info.segments[1]) - return NewGogsDownloader(ctx, baseURL, opts.AuthUsername, opts.AuthPassword, opts.AuthToken, info.segments[0], strings.TrimSuffix(info.segments[1], ".git")), nil + log.Trace("Create gogs downloader. BaseURL: %s RepoOwner: %s RepoName: %s", baseURL, fields[0], fields[1]) + return NewGogsDownloader(ctx, baseURL, opts.AuthUsername, opts.AuthPassword, opts.AuthToken, fields[0], fields[1]), nil } // GitServiceType returns the type of git service diff --git a/services/migrations/onedev.go b/services/migrations/onedev.go index af1ca26d66..262b12cf1c 100644 --- a/services/migrations/onedev.go +++ b/services/migrations/onedev.go @@ -37,14 +37,19 @@ type OneDevDownloaderFactory struct{} // New returns a downloader related to this factory according MigrateOptions func (f *OneDevDownloaderFactory) New(ctx context.Context, opts base.MigrateOptions) (base.Downloader, error) { - info, err := parseServiceCloneURL(opts.CloneAddr) + u, err := url.Parse(opts.CloneAddr) if err != nil { return nil, err } - log.Trace("Create onedev downloader. BaseURL: %v RepoPath: %s", info.apiURL, info.repoPath) + repoPath := strings.Trim(u.Path, "/") - return NewOneDevDownloader(ctx, info.apiURL, opts.AuthUsername, opts.AuthPassword, info.repoPath), nil + u.Path = "" + u.Fragment = "" + + log.Trace("Create onedev downloader. BaseURL: %v RepoPath: %s", u, repoPath) + + return NewOneDevDownloader(ctx, u, opts.AuthUsername, opts.AuthPassword, repoPath), nil } // GitServiceType returns the type of git service