code.gitea.io/gitea@v1.21.7/services/migrations/gitbucket.go (about) 1 // Copyright 2021 The Gitea Authors. All rights reserved. 2 // SPDX-License-Identifier: MIT 3 4 package migrations 5 6 import ( 7 "context" 8 "fmt" 9 "net/url" 10 "strings" 11 12 "code.gitea.io/gitea/modules/log" 13 base "code.gitea.io/gitea/modules/migration" 14 "code.gitea.io/gitea/modules/structs" 15 ) 16 17 var ( 18 _ base.Downloader = &GitBucketDownloader{} 19 _ base.DownloaderFactory = &GitBucketDownloaderFactory{} 20 ) 21 22 func init() { 23 RegisterDownloaderFactory(&GitBucketDownloaderFactory{}) 24 } 25 26 // GitBucketDownloaderFactory defines a GitBucket downloader factory 27 type GitBucketDownloaderFactory struct{} 28 29 // New returns a Downloader related to this factory according MigrateOptions 30 func (f *GitBucketDownloaderFactory) New(ctx context.Context, opts base.MigrateOptions) (base.Downloader, error) { 31 u, err := url.Parse(opts.CloneAddr) 32 if err != nil { 33 return nil, err 34 } 35 36 fields := strings.Split(u.Path, "/") 37 if len(fields) < 2 { 38 return nil, fmt.Errorf("invalid path: %s", u.Path) 39 } 40 baseURL := u.Scheme + "://" + u.Host + strings.TrimSuffix(strings.Join(fields[:len(fields)-2], "/"), "/git") 41 42 oldOwner := fields[len(fields)-2] 43 oldName := strings.TrimSuffix(fields[len(fields)-1], ".git") 44 45 log.Trace("Create GitBucket downloader. BaseURL: %s RepoOwner: %s RepoName: %s", baseURL, oldOwner, oldName) 46 return NewGitBucketDownloader(ctx, baseURL, opts.AuthUsername, opts.AuthPassword, opts.AuthToken, oldOwner, oldName), nil 47 } 48 49 // GitServiceType returns the type of git service 50 func (f *GitBucketDownloaderFactory) GitServiceType() structs.GitServiceType { 51 return structs.GitBucketService 52 } 53 54 // GitBucketDownloader implements a Downloader interface to get repository information 55 // from GitBucket via GithubDownloader 56 type GitBucketDownloader struct { 57 *GithubDownloaderV3 58 } 59 60 // String implements Stringer 61 func (g *GitBucketDownloader) String() string { 62 return fmt.Sprintf("migration from gitbucket server %s %s/%s", g.baseURL, g.repoOwner, g.repoName) 63 } 64 65 func (g *GitBucketDownloader) LogString() string { 66 if g == nil { 67 return "<GitBucketDownloader nil>" 68 } 69 return fmt.Sprintf("<GitBucketDownloader %s %s/%s>", g.baseURL, g.repoOwner, g.repoName) 70 } 71 72 // NewGitBucketDownloader creates a GitBucket downloader 73 func NewGitBucketDownloader(ctx context.Context, baseURL, userName, password, token, repoOwner, repoName string) *GitBucketDownloader { 74 githubDownloader := NewGithubDownloaderV3(ctx, baseURL, userName, password, token, repoOwner, repoName) 75 githubDownloader.SkipReactions = true 76 githubDownloader.SkipReviews = true 77 return &GitBucketDownloader{ 78 githubDownloader, 79 } 80 } 81 82 // SupportGetRepoComments return true if it supports get repo comments 83 func (g *GitBucketDownloader) SupportGetRepoComments() bool { 84 return false 85 }