0
0
mirror of https://github.com/go-gitea/gitea.git synced 2026-06-21 21:12:26 +02:00

ssh only for git migrations

This commit is contained in:
pomidorry 2026-06-15 21:47:03 +03:00
parent 1cc0df3648
commit ea8af2ac82
9 changed files with 60 additions and 78 deletions

View File

@ -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

View File

@ -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

View File

@ -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...)

View File

@ -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 "<host>/<sub-path>" where <sub-path> is the URL
// minus the trailing "/git/<owner>/<repo>.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)

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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

View File

@ -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