2021-11-15 03:11:10 +08:00
|
|
|
// Copyright 2021 The Gitea Authors. All rights reserved.
|
2022-11-28 02:20:29 +08:00
|
|
|
// SPDX-License-Identifier: MIT
|
2021-11-15 03:11:10 +08:00
|
|
|
|
|
|
|
package migrations
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-09-04 18:47:56 +08:00
|
|
|
"fmt"
|
2021-11-15 03:11:10 +08:00
|
|
|
"net/url"
|
|
|
|
"strings"
|
|
|
|
|
2022-09-04 18:47:56 +08:00
|
|
|
"code.gitea.io/gitea/modules/log"
|
2021-11-16 23:25:33 +08:00
|
|
|
base "code.gitea.io/gitea/modules/migration"
|
2021-11-15 03:11:10 +08:00
|
|
|
"code.gitea.io/gitea/modules/structs"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
_ base.Downloader = &GitBucketDownloader{}
|
|
|
|
_ base.DownloaderFactory = &GitBucketDownloaderFactory{}
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
RegisterDownloaderFactory(&GitBucketDownloaderFactory{})
|
|
|
|
}
|
|
|
|
|
|
|
|
// GitBucketDownloaderFactory defines a GitBucket downloader factory
|
2022-01-21 01:46:10 +08:00
|
|
|
type GitBucketDownloaderFactory struct{}
|
2021-11-15 03:11:10 +08:00
|
|
|
|
|
|
|
// New returns a Downloader related to this factory according MigrateOptions
|
|
|
|
func (f *GitBucketDownloaderFactory) New(ctx context.Context, opts base.MigrateOptions) (base.Downloader, error) {
|
|
|
|
u, err := url.Parse(opts.CloneAddr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
fields := strings.Split(u.Path, "/")
|
2023-01-17 15:22:00 +08:00
|
|
|
if len(fields) < 2 {
|
|
|
|
return nil, fmt.Errorf("invalid path: %s", u.Path)
|
|
|
|
}
|
|
|
|
baseURL := u.Scheme + "://" + u.Host + strings.TrimSuffix(strings.Join(fields[:len(fields)-2], "/"), "/git")
|
|
|
|
|
|
|
|
oldOwner := fields[len(fields)-2]
|
|
|
|
oldName := strings.TrimSuffix(fields[len(fields)-1], ".git")
|
2021-11-15 03:11:10 +08:00
|
|
|
|
2022-09-04 18:47:56 +08:00
|
|
|
log.Trace("Create GitBucket downloader. BaseURL: %s RepoOwner: %s RepoName: %s", baseURL, oldOwner, oldName)
|
2021-11-15 03:11:10 +08:00
|
|
|
return NewGitBucketDownloader(ctx, baseURL, opts.AuthUsername, opts.AuthPassword, opts.AuthToken, oldOwner, oldName), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GitServiceType returns the type of git service
|
|
|
|
func (f *GitBucketDownloaderFactory) GitServiceType() structs.GitServiceType {
|
|
|
|
return structs.GitBucketService
|
|
|
|
}
|
|
|
|
|
|
|
|
// GitBucketDownloader implements a Downloader interface to get repository information
|
|
|
|
// from GitBucket via GithubDownloader
|
|
|
|
type GitBucketDownloader struct {
|
|
|
|
*GithubDownloaderV3
|
|
|
|
}
|
|
|
|
|
2022-09-04 18:47:56 +08:00
|
|
|
// String implements Stringer
|
|
|
|
func (g *GitBucketDownloader) String() string {
|
|
|
|
return fmt.Sprintf("migration from gitbucket server %s %s/%s", g.baseURL, g.repoOwner, g.repoName)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ColorFormat provides a basic color format for a GitBucketDownloader
|
|
|
|
func (g *GitBucketDownloader) ColorFormat(s fmt.State) {
|
|
|
|
if g == nil {
|
|
|
|
log.ColorFprintf(s, "<nil: GitBucketDownloader>")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
log.ColorFprintf(s, "migration from gitbucket server %s %s/%s", g.baseURL, g.repoOwner, g.repoName)
|
|
|
|
}
|
|
|
|
|
2021-11-15 03:11:10 +08:00
|
|
|
// NewGitBucketDownloader creates a GitBucket downloader
|
|
|
|
func NewGitBucketDownloader(ctx context.Context, baseURL, userName, password, token, repoOwner, repoName string) *GitBucketDownloader {
|
|
|
|
githubDownloader := NewGithubDownloaderV3(ctx, baseURL, userName, password, token, repoOwner, repoName)
|
|
|
|
githubDownloader.SkipReactions = true
|
2023-01-17 15:22:00 +08:00
|
|
|
githubDownloader.SkipReviews = true
|
2021-11-15 03:11:10 +08:00
|
|
|
return &GitBucketDownloader{
|
|
|
|
githubDownloader,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SupportGetRepoComments return true if it supports get repo comments
|
|
|
|
func (g *GitBucketDownloader) SupportGetRepoComments() bool {
|
|
|
|
return false
|
|
|
|
}
|