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"
|
|
|
|
|
2019-05-07 06:42:51 +05:30
|
|
|
"code.gitea.io/gitea/models"
|
|
|
|
"code.gitea.io/gitea/modules/log"
|
|
|
|
"code.gitea.io/gitea/modules/migrations/base"
|
2019-11-16 14:00:06 +05:30
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2019-10-14 11:40:42 +05:30
|
|
|
"code.gitea.io/gitea/modules/structs"
|
2019-05-07 06:42:51 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
// MigrateOptions is equal to base.MigrateOptions
|
|
|
|
type MigrateOptions = base.MigrateOptions
|
|
|
|
|
|
|
|
var (
|
|
|
|
factories []base.DownloaderFactory
|
|
|
|
)
|
|
|
|
|
|
|
|
// RegisterDownloaderFactory registers a downloader factory
|
|
|
|
func RegisterDownloaderFactory(factory base.DownloaderFactory) {
|
|
|
|
factories = append(factories, factory)
|
|
|
|
}
|
|
|
|
|
|
|
|
// MigrateRepository migrate repository according MigrateOptions
|
2019-12-17 09:46:54 +05:30
|
|
|
func MigrateRepository(ctx context.Context, doer *models.User, ownerName string, opts base.MigrateOptions) (*models.Repository, error) {
|
2019-05-07 06:42:51 +05:30
|
|
|
var (
|
|
|
|
downloader base.Downloader
|
2019-12-17 09:46:54 +05:30
|
|
|
uploader = NewGiteaLocalUploader(ctx, doer, ownerName, opts.RepoName)
|
2019-10-14 11:40:42 +05:30
|
|
|
theFactory base.DownloaderFactory
|
2019-05-07 06:42:51 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
for _, factory := range factories {
|
|
|
|
if match, err := factory.Match(opts); err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else if match {
|
|
|
|
downloader, err = factory.New(opts)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-10-14 11:40:42 +05:30
|
|
|
theFactory = factory
|
2019-05-07 06:42:51 +05:30
|
|
|
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-14 11:40:42 +05:30
|
|
|
opts.GitServiceType = structs.PlainGitService
|
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-10-14 11:40:42 +05:30
|
|
|
} else if opts.GitServiceType == structs.NotMigrated {
|
|
|
|
opts.GitServiceType = theFactory.GitServiceType()
|
2019-05-07 06:42:51 +05:30
|
|
|
}
|
|
|
|
|
2019-10-14 11:40:42 +05:30
|
|
|
uploader.gitServiceType = opts.GitServiceType
|
2019-11-16 14:00:06 +05:30
|
|
|
|
|
|
|
if setting.Migrations.MaxAttempts > 1 {
|
|
|
|
downloader = base.NewRetryDownloader(downloader, setting.Migrations.MaxAttempts, setting.Migrations.RetryBackoff)
|
|
|
|
}
|
|
|
|
|
2019-12-17 09:46:54 +05:30
|
|
|
downloader.SetContext(ctx)
|
|
|
|
|
2019-05-07 06:42:51 +05:30
|
|
|
if err := migrateRepository(downloader, uploader, opts); err != nil {
|
|
|
|
if err1 := uploader.Rollback(); err1 != nil {
|
|
|
|
log.Error("rollback failed: %v", err1)
|
|
|
|
}
|
2019-10-13 18:53:14 +05:30
|
|
|
|
2019-12-19 03:19:56 +05:30
|
|
|
if err2 := models.CreateRepositoryNotice(fmt.Sprintf("Migrate repository from %s failed: %v", opts.OriginalURL, err)); err2 != nil {
|
2019-10-13 18:53:14 +05:30
|
|
|
log.Error("create respotiry notice failed: ", err2)
|
|
|
|
}
|
2019-05-07 06:42:51 +05:30
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return uploader.repo, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// migrateRepository will download informations and upload to Uploader, this is a simple
|
|
|
|
// process for small repository. For a big repository, save all the data to disk
|
|
|
|
// before upload is better
|
|
|
|
func migrateRepository(downloader base.Downloader, uploader base.Uploader, opts base.MigrateOptions) error {
|
|
|
|
repo, err := downloader.GetRepoInfo()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
repo.IsPrivate = opts.Private
|
|
|
|
repo.IsMirror = opts.Mirror
|
2019-05-20 18:13:43 +05:30
|
|
|
if opts.Description != "" {
|
|
|
|
repo.Description = opts.Description
|
|
|
|
}
|
2019-05-07 06:42:51 +05:30
|
|
|
log.Trace("migrating git data")
|
2019-07-02 02:47:16 +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")
|
|
|
|
topics, err := downloader.GetTopics()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(topics) > 0 {
|
|
|
|
if err := uploader.CreateTopics(topics...); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-07 06:42:51 +05:30
|
|
|
if opts.Milestones {
|
|
|
|
log.Trace("migrating milestones")
|
|
|
|
milestones, err := downloader.GetMilestones()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
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")
|
|
|
|
labels, err := downloader.GetLabels()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
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")
|
|
|
|
releases, err := downloader.GetReleases()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-12-12 05:50:11 +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
|
|
|
|
if err := uploader.SyncTags(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-05-07 06:42:51 +05:30
|
|
|
}
|
|
|
|
|
2019-07-07 00:54:50 +05:30
|
|
|
var commentBatchSize = uploader.MaxBatchInsertSize("comment")
|
|
|
|
|
2019-05-07 06:42:51 +05:30
|
|
|
if opts.Issues {
|
|
|
|
log.Trace("migrating issues and comments")
|
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 {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2019-06-29 19:08:22 +05:30
|
|
|
if !opts.Comments {
|
|
|
|
continue
|
|
|
|
}
|
2019-05-07 06:42:51 +05:30
|
|
|
|
2019-07-07 00:54:50 +05:30
|
|
|
var allComments = make([]*base.Comment, 0, commentBatchSize)
|
2019-06-29 19:08:22 +05:30
|
|
|
for _, issue := range issues {
|
2019-05-07 06:42:51 +05:30
|
|
|
comments, err := downloader.GetComments(issue.Number)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-07-08 07:44:12 +05:30
|
|
|
|
2019-06-29 19:08:22 +05:30
|
|
|
allComments = append(allComments, comments...)
|
|
|
|
|
2019-07-07 00:54:50 +05:30
|
|
|
if len(allComments) >= commentBatchSize {
|
|
|
|
if err := uploader.CreateComments(allComments[:commentBatchSize]...); err != nil {
|
2019-05-07 06:42:51 +05:30
|
|
|
return err
|
|
|
|
}
|
2019-07-07 00:54:50 +05:30
|
|
|
|
|
|
|
allComments = allComments[commentBatchSize:]
|
2019-06-29 19:08:22 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(allComments) > 0 {
|
|
|
|
if err := uploader.CreateComments(allComments...); err != nil {
|
|
|
|
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")
|
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++ {
|
2019-07-07 00:54:50 +05:30
|
|
|
prs, err := downloader.GetPullRequests(i, prBatchSize)
|
2019-05-07 06:42:51 +05:30
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2019-06-29 19:08:22 +05:30
|
|
|
if !opts.Comments {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-07-07 00:54:50 +05:30
|
|
|
var allComments = make([]*base.Comment, 0, commentBatchSize)
|
2019-06-29 19:08:22 +05:30
|
|
|
for _, pr := range prs {
|
2019-05-07 06:42:51 +05:30
|
|
|
comments, err := downloader.GetComments(pr.Number)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-06-29 19:08:22 +05:30
|
|
|
|
|
|
|
allComments = append(allComments, comments...)
|
|
|
|
|
2019-07-07 00:54:50 +05:30
|
|
|
if len(allComments) >= commentBatchSize {
|
|
|
|
if err := uploader.CreateComments(allComments[:commentBatchSize]...); err != nil {
|
2019-05-07 06:42:51 +05:30
|
|
|
return err
|
|
|
|
}
|
2019-07-07 00:54:50 +05:30
|
|
|
allComments = allComments[commentBatchSize:]
|
2019-05-07 06:42:51 +05:30
|
|
|
}
|
|
|
|
}
|
2019-06-29 19:08:22 +05:30
|
|
|
if len(allComments) > 0 {
|
|
|
|
if err := uploader.CreateComments(allComments...); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-07 00:54:50 +05:30
|
|
|
if len(prs) < prBatchSize {
|
2019-05-07 06:42:51 +05:30
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|