You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

gitbucket.go 2.7KB

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