2019-05-07 06:42:51 +05:30
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
|
|
|
// Copyright 2018 Jonas Franz. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package migrations
|
|
|
|
|
|
|
|
import (
|
2019-12-17 09:46:54 +05:30
|
|
|
"context"
|
2019-10-13 18:53:14 +05:30
|
|
|
"fmt"
|
2020-11-29 06:07:58 +05:30
|
|
|
"net"
|
|
|
|
"net/url"
|
2021-03-16 03:22:11 +05:30
|
|
|
"path/filepath"
|
2020-11-29 06:07:58 +05:30
|
|
|
"strings"
|
2019-10-13 18:53:14 +05:30
|
|
|
|
2019-05-07 06:42:51 +05:30
|
|
|
"code.gitea.io/gitea/models"
|
2021-11-18 11:28:42 +05:30
|
|
|
admin_model "code.gitea.io/gitea/models/admin"
|
2019-05-07 06:42:51 +05:30
|
|
|
"code.gitea.io/gitea/modules/log"
|
2020-11-29 06:07:58 +05:30
|
|
|
"code.gitea.io/gitea/modules/matchlist"
|
2021-11-16 20:55:33 +05:30
|
|
|
base "code.gitea.io/gitea/modules/migration"
|
2019-11-16 14:00:06 +05:30
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2021-03-16 03:22:11 +05:30
|
|
|
"code.gitea.io/gitea/modules/util"
|
2019-05-07 06:42:51 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
// MigrateOptions is equal to base.MigrateOptions
|
|
|
|
type MigrateOptions = base.MigrateOptions
|
|
|
|
|
|
|
|
var (
|
|
|
|
factories []base.DownloaderFactory
|
2020-11-29 06:07:58 +05:30
|
|
|
|
|
|
|
allowList *matchlist.Matchlist
|
|
|
|
blockList *matchlist.Matchlist
|
2019-05-07 06:42:51 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
// RegisterDownloaderFactory registers a downloader factory
|
|
|
|
func RegisterDownloaderFactory(factory base.DownloaderFactory) {
|
|
|
|
factories = append(factories, factory)
|
|
|
|
}
|
|
|
|
|
2021-03-16 03:22:11 +05:30
|
|
|
// IsMigrateURLAllowed checks if an URL is allowed to be migrated from
|
|
|
|
func IsMigrateURLAllowed(remoteURL string, doer *models.User) error {
|
|
|
|
// Remote address can be HTTP/HTTPS/Git URL or local path.
|
2021-03-18 19:28:47 +05:30
|
|
|
u, err := url.Parse(remoteURL)
|
2020-11-29 06:07:58 +05:30
|
|
|
if err != nil {
|
2021-03-16 03:22:11 +05:30
|
|
|
return &models.ErrInvalidCloneAddr{IsURLError: true}
|
2020-11-29 06:07:58 +05:30
|
|
|
}
|
|
|
|
|
2021-03-16 03:22:11 +05:30
|
|
|
if u.Scheme == "file" || u.Scheme == "" {
|
|
|
|
if !doer.CanImportLocal() {
|
|
|
|
return &models.ErrInvalidCloneAddr{Host: "<LOCAL_FILESYSTEM>", IsPermissionDenied: true, LocalPath: true}
|
2020-11-29 06:07:58 +05:30
|
|
|
}
|
2021-03-16 03:22:11 +05:30
|
|
|
isAbs := filepath.IsAbs(u.Host + u.Path)
|
|
|
|
if !isAbs {
|
|
|
|
return &models.ErrInvalidCloneAddr{Host: "<LOCAL_FILESYSTEM>", IsInvalidPath: true, LocalPath: true}
|
|
|
|
}
|
|
|
|
isDir, err := util.IsDir(u.Host + u.Path)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Unable to check if %s is a directory: %v", u.Host+u.Path, err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !isDir {
|
|
|
|
return &models.ErrInvalidCloneAddr{Host: "<LOCAL_FILESYSTEM>", IsInvalidPath: true, LocalPath: true}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if u.Scheme == "git" && u.Port() != "" && (strings.Contains(remoteURL, "%0d") || strings.Contains(remoteURL, "%0a")) {
|
|
|
|
return &models.ErrInvalidCloneAddr{Host: u.Host, IsURLError: true}
|
2020-11-29 06:07:58 +05:30
|
|
|
}
|
|
|
|
|
2021-03-16 03:22:11 +05:30
|
|
|
if u.Opaque != "" || u.Scheme != "" && u.Scheme != "http" && u.Scheme != "https" && u.Scheme != "git" {
|
|
|
|
return &models.ErrInvalidCloneAddr{Host: u.Host, IsProtocolInvalid: true, IsPermissionDenied: true, IsURLError: true}
|
|
|
|
}
|
|
|
|
|
2021-03-18 19:28:47 +05:30
|
|
|
host := strings.ToLower(u.Host)
|
2021-03-16 03:22:11 +05:30
|
|
|
if len(setting.Migrations.AllowedDomains) > 0 {
|
2021-03-18 19:28:47 +05:30
|
|
|
if !allowList.Match(host) {
|
2021-03-16 03:22:11 +05:30
|
|
|
return &models.ErrInvalidCloneAddr{Host: u.Host, IsPermissionDenied: true}
|
|
|
|
}
|
|
|
|
} else {
|
2021-03-18 19:28:47 +05:30
|
|
|
if blockList.Match(host) {
|
2021-03-16 03:22:11 +05:30
|
|
|
return &models.ErrInvalidCloneAddr{Host: u.Host, IsPermissionDenied: true}
|
2021-03-08 18:40:17 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-29 06:07:58 +05:30
|
|
|
if !setting.Migrations.AllowLocalNetworks {
|
|
|
|
addrList, err := net.LookupIP(strings.Split(u.Host, ":")[0])
|
|
|
|
if err != nil {
|
2021-03-16 03:22:11 +05:30
|
|
|
return &models.ErrInvalidCloneAddr{Host: u.Host, NotResolvedIP: true}
|
2020-11-29 06:07:58 +05:30
|
|
|
}
|
|
|
|
for _, addr := range addrList {
|
2021-11-01 14:09:52 +05:30
|
|
|
if util.IsIPPrivate(addr) || !addr.IsGlobalUnicast() {
|
2021-03-16 03:22:11 +05:30
|
|
|
return &models.ErrInvalidCloneAddr{Host: u.Host, PrivateNet: addr.String(), IsPermissionDenied: true}
|
2020-11-29 06:07:58 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-05-07 06:42:51 +05:30
|
|
|
// MigrateRepository migrate repository according MigrateOptions
|
2021-06-17 03:32:24 +05:30
|
|
|
func MigrateRepository(ctx context.Context, doer *models.User, ownerName string, opts base.MigrateOptions, messenger base.Messenger) (*models.Repository, error) {
|
2021-03-16 03:22:11 +05:30
|
|
|
err := IsMigrateURLAllowed(opts.CloneAddr, doer)
|
2020-11-29 06:07:58 +05:30
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-04-09 03:55:57 +05:30
|
|
|
if opts.LFS && len(opts.LFSEndpoint) > 0 {
|
|
|
|
err := IsMigrateURLAllowed(opts.LFSEndpoint, doer)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2020-12-27 09:04:19 +05:30
|
|
|
downloader, err := newDownloader(ctx, ownerName, opts)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var uploader = NewGiteaLocalUploader(ctx, doer, ownerName, opts.RepoName)
|
|
|
|
uploader.gitServiceType = opts.GitServiceType
|
|
|
|
|
2021-06-17 03:32:24 +05:30
|
|
|
if err := migrateRepository(downloader, uploader, opts, messenger); err != nil {
|
2020-12-27 09:04:19 +05:30
|
|
|
if err1 := uploader.Rollback(); err1 != nil {
|
|
|
|
log.Error("rollback failed: %v", err1)
|
|
|
|
}
|
2021-11-18 11:28:42 +05:30
|
|
|
if err2 := admin_model.CreateRepositoryNotice(fmt.Sprintf("Migrate repository from %s failed: %v", opts.OriginalURL, err)); err2 != nil {
|
2020-12-27 09:04:19 +05:30
|
|
|
log.Error("create respotiry notice failed: ", err2)
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return uploader.repo, nil
|
|
|
|
}
|
2020-11-29 06:07:58 +05:30
|
|
|
|
2020-12-27 09:04:19 +05:30
|
|
|
func newDownloader(ctx context.Context, ownerName string, opts base.MigrateOptions) (base.Downloader, error) {
|
2019-05-07 06:42:51 +05:30
|
|
|
var (
|
|
|
|
downloader base.Downloader
|
2020-12-27 09:04:19 +05:30
|
|
|
err error
|
2019-05-07 06:42:51 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
for _, factory := range factories {
|
2020-08-28 07:06:37 +05:30
|
|
|
if factory.GitServiceType() == opts.GitServiceType {
|
2020-09-02 23:19:25 +05:30
|
|
|
downloader, err = factory.New(ctx, opts)
|
2019-05-07 06:42:51 +05:30
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if downloader == nil {
|
|
|
|
opts.Wiki = true
|
|
|
|
opts.Milestones = false
|
|
|
|
opts.Labels = false
|
|
|
|
opts.Releases = false
|
|
|
|
opts.Comments = false
|
|
|
|
opts.Issues = false
|
|
|
|
opts.PullRequests = false
|
2019-10-13 18:53:14 +05:30
|
|
|
downloader = NewPlainGitDownloader(ownerName, opts.RepoName, opts.CloneAddr)
|
2019-12-19 03:19:56 +05:30
|
|
|
log.Trace("Will migrate from git: %s", opts.OriginalURL)
|
2019-05-07 06:42:51 +05:30
|
|
|
}
|
|
|
|
|
2019-11-16 14:00:06 +05:30
|
|
|
if setting.Migrations.MaxAttempts > 1 {
|
2020-09-02 23:19:25 +05:30
|
|
|
downloader = base.NewRetryDownloader(ctx, downloader, setting.Migrations.MaxAttempts, setting.Migrations.RetryBackoff)
|
2019-11-16 14:00:06 +05:30
|
|
|
}
|
2020-12-27 09:04:19 +05:30
|
|
|
return downloader, nil
|
2019-05-07 06:42:51 +05:30
|
|
|
}
|
|
|
|
|
2020-04-20 18:00:46 +05:30
|
|
|
// migrateRepository will download information and then upload it to Uploader, this is a simple
|
2019-05-07 06:42:51 +05:30
|
|
|
// process for small repository. For a big repository, save all the data to disk
|
|
|
|
// before upload is better
|
2021-06-17 03:32:24 +05:30
|
|
|
func migrateRepository(downloader base.Downloader, uploader base.Uploader, opts base.MigrateOptions, messenger base.Messenger) error {
|
|
|
|
if messenger == nil {
|
|
|
|
messenger = base.NilMessenger
|
|
|
|
}
|
|
|
|
|
2019-05-07 06:42:51 +05:30
|
|
|
repo, err := downloader.GetRepoInfo()
|
|
|
|
if err != nil {
|
2021-01-22 01:03:58 +05:30
|
|
|
if !base.IsErrNotSupported(err) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
log.Info("migrating repo infos is not supported, ignored")
|
2019-05-07 06:42:51 +05:30
|
|
|
}
|
|
|
|
repo.IsPrivate = opts.Private
|
|
|
|
repo.IsMirror = opts.Mirror
|
2019-05-20 18:13:43 +05:30
|
|
|
if opts.Description != "" {
|
|
|
|
repo.Description = opts.Description
|
|
|
|
}
|
2021-01-22 01:03:58 +05:30
|
|
|
if repo.CloneURL, err = downloader.FormatCloneURL(opts, repo.CloneURL); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-06-04 18:44:20 +05:30
|
|
|
log.Trace("migrating git data from %s", repo.CloneURL)
|
2021-06-17 03:32:24 +05:30
|
|
|
messenger("repo.migrate.migrating_git")
|
2021-01-22 01:03:58 +05:30
|
|
|
if err = uploader.CreateRepo(repo, opts); err != nil {
|
2019-05-07 06:42:51 +05:30
|
|
|
return err
|
|
|
|
}
|
2019-11-13 12:31:19 +05:30
|
|
|
defer uploader.Close()
|
2019-05-07 06:42:51 +05:30
|
|
|
|
2019-08-14 11:46:12 +05:30
|
|
|
log.Trace("migrating topics")
|
2021-06-17 03:32:24 +05:30
|
|
|
messenger("repo.migrate.migrating_topics")
|
2019-08-14 11:46:12 +05:30
|
|
|
topics, err := downloader.GetTopics()
|
|
|
|
if err != nil {
|
2021-01-22 01:03:58 +05:30
|
|
|
if !base.IsErrNotSupported(err) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
log.Warn("migrating topics is not supported, ignored")
|
2019-08-14 11:46:12 +05:30
|
|
|
}
|
2021-01-22 01:03:58 +05:30
|
|
|
if len(topics) != 0 {
|
|
|
|
if err = uploader.CreateTopics(topics...); err != nil {
|
2019-08-14 11:46:12 +05:30
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-07 06:42:51 +05:30
|
|
|
if opts.Milestones {
|
|
|
|
log.Trace("migrating milestones")
|
2021-06-17 03:32:24 +05:30
|
|
|
messenger("repo.migrate.migrating_milestones")
|
2019-05-07 06:42:51 +05:30
|
|
|
milestones, err := downloader.GetMilestones()
|
|
|
|
if err != nil {
|
2021-01-22 01:03:58 +05:30
|
|
|
if !base.IsErrNotSupported(err) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
log.Warn("migrating milestones is not supported, ignored")
|
2019-05-07 06:42:51 +05:30
|
|
|
}
|
|
|
|
|
2019-07-07 00:54:50 +05:30
|
|
|
msBatchSize := uploader.MaxBatchInsertSize("milestone")
|
|
|
|
for len(milestones) > 0 {
|
|
|
|
if len(milestones) < msBatchSize {
|
|
|
|
msBatchSize = len(milestones)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := uploader.CreateMilestones(milestones...); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
milestones = milestones[msBatchSize:]
|
2019-05-07 06:42:51 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if opts.Labels {
|
|
|
|
log.Trace("migrating labels")
|
2021-06-17 03:32:24 +05:30
|
|
|
messenger("repo.migrate.migrating_labels")
|
2019-05-07 06:42:51 +05:30
|
|
|
labels, err := downloader.GetLabels()
|
|
|
|
if err != nil {
|
2021-01-22 01:03:58 +05:30
|
|
|
if !base.IsErrNotSupported(err) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
log.Warn("migrating labels is not supported, ignored")
|
2019-05-07 06:42:51 +05:30
|
|
|
}
|
|
|
|
|
2019-07-07 00:54:50 +05:30
|
|
|
lbBatchSize := uploader.MaxBatchInsertSize("label")
|
|
|
|
for len(labels) > 0 {
|
|
|
|
if len(labels) < lbBatchSize {
|
|
|
|
lbBatchSize = len(labels)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := uploader.CreateLabels(labels...); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
labels = labels[lbBatchSize:]
|
2019-05-07 06:42:51 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if opts.Releases {
|
|
|
|
log.Trace("migrating releases")
|
2021-06-17 03:32:24 +05:30
|
|
|
messenger("repo.migrate.migrating_releases")
|
2019-05-07 06:42:51 +05:30
|
|
|
releases, err := downloader.GetReleases()
|
|
|
|
if err != nil {
|
2021-01-22 01:03:58 +05:30
|
|
|
if !base.IsErrNotSupported(err) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
log.Warn("migrating releases is not supported, ignored")
|
2019-05-07 06:42:51 +05:30
|
|
|
}
|
|
|
|
|
2019-07-07 00:54:50 +05:30
|
|
|
relBatchSize := uploader.MaxBatchInsertSize("release")
|
|
|
|
for len(releases) > 0 {
|
2019-12-12 05:50:11 +05:30
|
|
|
if len(releases) < relBatchSize {
|
|
|
|
relBatchSize = len(releases)
|
2019-07-07 00:54:50 +05:30
|
|
|
}
|
|
|
|
|
2021-01-22 01:03:58 +05:30
|
|
|
if err = uploader.CreateReleases(releases[:relBatchSize]...); err != nil {
|
2019-07-07 00:54:50 +05:30
|
|
|
return err
|
|
|
|
}
|
|
|
|
releases = releases[relBatchSize:]
|
2019-05-07 06:42:51 +05:30
|
|
|
}
|
2019-12-12 05:50:11 +05:30
|
|
|
|
|
|
|
// Once all releases (if any) are inserted, sync any remaining non-release tags
|
2021-01-22 01:03:58 +05:30
|
|
|
if err = uploader.SyncTags(); err != nil {
|
2019-12-12 05:50:11 +05:30
|
|
|
return err
|
|
|
|
}
|
2019-05-07 06:42:51 +05:30
|
|
|
}
|
|
|
|
|
2020-01-23 22:58:15 +05:30
|
|
|
var (
|
|
|
|
commentBatchSize = uploader.MaxBatchInsertSize("comment")
|
|
|
|
reviewBatchSize = uploader.MaxBatchInsertSize("review")
|
|
|
|
)
|
2019-07-07 00:54:50 +05:30
|
|
|
|
2021-06-30 12:53:49 +05:30
|
|
|
supportAllComments := downloader.SupportGetRepoComments()
|
|
|
|
|
2019-05-07 06:42:51 +05:30
|
|
|
if opts.Issues {
|
|
|
|
log.Trace("migrating issues and comments")
|
2021-06-17 03:32:24 +05:30
|
|
|
messenger("repo.migrate.migrating_issues")
|
2019-07-07 00:54:50 +05:30
|
|
|
var issueBatchSize = uploader.MaxBatchInsertSize("issue")
|
|
|
|
|
2019-05-31 01:56:57 +05:30
|
|
|
for i := 1; ; i++ {
|
2019-07-07 00:54:50 +05:30
|
|
|
issues, isEnd, err := downloader.GetIssues(i, issueBatchSize)
|
2019-05-07 06:42:51 +05:30
|
|
|
if err != nil {
|
2021-01-22 01:03:58 +05:30
|
|
|
if !base.IsErrNotSupported(err) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
log.Warn("migrating issues is not supported, ignored")
|
|
|
|
break
|
2019-05-07 06:42:51 +05:30
|
|
|
}
|
|
|
|
|
2019-06-29 19:08:22 +05:30
|
|
|
if err := uploader.CreateIssues(issues...); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-05-07 06:42:51 +05:30
|
|
|
|
2021-06-30 12:53:49 +05:30
|
|
|
if opts.Comments && !supportAllComments {
|
2020-12-27 09:04:19 +05:30
|
|
|
var allComments = make([]*base.Comment, 0, commentBatchSize)
|
|
|
|
for _, issue := range issues {
|
|
|
|
log.Trace("migrating issue %d's comments", issue.Number)
|
2021-06-30 12:53:49 +05:30
|
|
|
comments, _, err := downloader.GetComments(base.GetCommentOptions{
|
2021-08-22 04:17:45 +05:30
|
|
|
Context: issue.Context,
|
2021-06-30 12:53:49 +05:30
|
|
|
})
|
2020-12-27 09:04:19 +05:30
|
|
|
if err != nil {
|
2021-01-22 01:03:58 +05:30
|
|
|
if !base.IsErrNotSupported(err) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
log.Warn("migrating comments is not supported, ignored")
|
2020-12-27 09:04:19 +05:30
|
|
|
}
|
2019-05-07 06:42:51 +05:30
|
|
|
|
2020-12-27 09:04:19 +05:30
|
|
|
allComments = append(allComments, comments...)
|
2019-07-08 07:44:12 +05:30
|
|
|
|
2020-12-27 09:04:19 +05:30
|
|
|
if len(allComments) >= commentBatchSize {
|
2021-01-22 01:03:58 +05:30
|
|
|
if err = uploader.CreateComments(allComments[:commentBatchSize]...); err != nil {
|
2020-12-27 09:04:19 +05:30
|
|
|
return err
|
|
|
|
}
|
2019-06-29 19:08:22 +05:30
|
|
|
|
2020-12-27 09:04:19 +05:30
|
|
|
allComments = allComments[commentBatchSize:]
|
2019-05-07 06:42:51 +05:30
|
|
|
}
|
2019-06-29 19:08:22 +05:30
|
|
|
}
|
|
|
|
|
2020-12-27 09:04:19 +05:30
|
|
|
if len(allComments) > 0 {
|
2021-01-22 01:03:58 +05:30
|
|
|
if err = uploader.CreateComments(allComments...); err != nil {
|
2020-12-27 09:04:19 +05:30
|
|
|
return err
|
|
|
|
}
|
2019-05-07 06:42:51 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-31 01:56:57 +05:30
|
|
|
if isEnd {
|
2019-05-07 06:42:51 +05:30
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if opts.PullRequests {
|
|
|
|
log.Trace("migrating pull requests and comments")
|
2021-06-17 03:32:24 +05:30
|
|
|
messenger("repo.migrate.migrating_pulls")
|
2019-07-07 02:02:15 +05:30
|
|
|
var prBatchSize = uploader.MaxBatchInsertSize("pullrequest")
|
2019-05-31 01:56:57 +05:30
|
|
|
for i := 1; ; i++ {
|
2020-10-14 09:36:00 +05:30
|
|
|
prs, isEnd, err := downloader.GetPullRequests(i, prBatchSize)
|
2019-05-07 06:42:51 +05:30
|
|
|
if err != nil {
|
2021-01-22 01:03:58 +05:30
|
|
|
if !base.IsErrNotSupported(err) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
log.Warn("migrating pull requests is not supported, ignored")
|
|
|
|
break
|
2019-05-07 06:42:51 +05:30
|
|
|
}
|
|
|
|
|
2019-06-29 19:08:22 +05:30
|
|
|
if err := uploader.CreatePullRequests(prs...); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-05-07 06:42:51 +05:30
|
|
|
|
2020-12-27 09:04:19 +05:30
|
|
|
if opts.Comments {
|
2021-06-30 12:53:49 +05:30
|
|
|
if !supportAllComments {
|
|
|
|
// plain comments
|
|
|
|
var allComments = make([]*base.Comment, 0, commentBatchSize)
|
|
|
|
for _, pr := range prs {
|
|
|
|
log.Trace("migrating pull request %d's comments", pr.Number)
|
|
|
|
comments, _, err := downloader.GetComments(base.GetCommentOptions{
|
2021-08-22 04:17:45 +05:30
|
|
|
Context: pr.Context,
|
2021-06-30 12:53:49 +05:30
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
if !base.IsErrNotSupported(err) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
log.Warn("migrating comments is not supported, ignored")
|
2021-01-22 01:03:58 +05:30
|
|
|
}
|
2019-06-29 19:08:22 +05:30
|
|
|
|
2021-06-30 12:53:49 +05:30
|
|
|
allComments = append(allComments, comments...)
|
2019-06-29 19:08:22 +05:30
|
|
|
|
2021-06-30 12:53:49 +05:30
|
|
|
if len(allComments) >= commentBatchSize {
|
|
|
|
if err = uploader.CreateComments(allComments[:commentBatchSize]...); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
allComments = allComments[commentBatchSize:]
|
2020-12-27 09:04:19 +05:30
|
|
|
}
|
2019-05-07 06:42:51 +05:30
|
|
|
}
|
2021-06-30 12:53:49 +05:30
|
|
|
if len(allComments) > 0 {
|
|
|
|
if err = uploader.CreateComments(allComments...); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-12-27 09:04:19 +05:30
|
|
|
}
|
2019-06-29 19:08:22 +05:30
|
|
|
}
|
|
|
|
|
2020-12-27 09:04:19 +05:30
|
|
|
// migrate reviews
|
|
|
|
var allReviews = make([]*base.Review, 0, reviewBatchSize)
|
|
|
|
for _, pr := range prs {
|
2021-08-22 04:17:45 +05:30
|
|
|
reviews, err := downloader.GetReviews(pr.Context)
|
2021-01-22 01:03:58 +05:30
|
|
|
if err != nil {
|
|
|
|
if !base.IsErrNotSupported(err) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
log.Warn("migrating reviews is not supported, ignored")
|
|
|
|
break
|
|
|
|
}
|
2020-01-23 22:58:15 +05:30
|
|
|
|
2020-12-27 09:04:19 +05:30
|
|
|
allReviews = append(allReviews, reviews...)
|
2020-01-23 22:58:15 +05:30
|
|
|
|
2020-12-27 09:04:19 +05:30
|
|
|
if len(allReviews) >= reviewBatchSize {
|
2021-01-22 01:03:58 +05:30
|
|
|
if err = uploader.CreateReviews(allReviews[:reviewBatchSize]...); err != nil {
|
2020-12-27 09:04:19 +05:30
|
|
|
return err
|
|
|
|
}
|
|
|
|
allReviews = allReviews[reviewBatchSize:]
|
2020-01-23 22:58:15 +05:30
|
|
|
}
|
|
|
|
}
|
2020-12-27 09:04:19 +05:30
|
|
|
if len(allReviews) > 0 {
|
2021-01-22 01:03:58 +05:30
|
|
|
if err = uploader.CreateReviews(allReviews...); err != nil {
|
2020-12-27 09:04:19 +05:30
|
|
|
return err
|
|
|
|
}
|
2020-01-23 22:58:15 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-14 09:36:00 +05:30
|
|
|
if isEnd {
|
2019-05-07 06:42:51 +05:30
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-30 12:53:49 +05:30
|
|
|
if opts.Comments && supportAllComments {
|
|
|
|
log.Trace("migrating comments")
|
|
|
|
for i := 1; ; i++ {
|
|
|
|
comments, isEnd, err := downloader.GetComments(base.GetCommentOptions{
|
|
|
|
Page: i,
|
|
|
|
PageSize: commentBatchSize,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := uploader.CreateComments(comments...); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if isEnd {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-27 09:04:19 +05:30
|
|
|
return uploader.Finish()
|
2019-05-07 06:42:51 +05:30
|
|
|
}
|
2020-11-29 06:07:58 +05:30
|
|
|
|
|
|
|
// Init migrations service
|
|
|
|
func Init() error {
|
|
|
|
var err error
|
|
|
|
allowList, err = matchlist.NewMatchlist(setting.Migrations.AllowedDomains...)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("init migration allowList domains failed: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
blockList, err = matchlist.NewMatchlist(setting.Migrations.BlockedDomains...)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("init migration blockList domains failed: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|