diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2019-05-07 09:12:51 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-05-07 09:12:51 +0800 |
commit | 08069dc4656fa53ee5dd25189e15012cb4f8acb2 (patch) | |
tree | 2e08cb239fef3221e55da75f106dfcb0140e08a1 /routers/api/v1/repo/repo.go | |
parent | 1c7c739eb9ea1d2ffdaed3c776c84d42858c0851 (diff) | |
download | gitea-08069dc4656fa53ee5dd25189e15012cb4f8acb2.tar.gz gitea-08069dc4656fa53ee5dd25189e15012cb4f8acb2.zip |
Improve migrations to support migrating milestones/labels/issues/comments/pullrequests (#6290)
* add migrations
* fix package dependency
* fix lints
* implements migrations except pull requests
* add releases
* migrating releases
* fix bug
* fix lint
* fix migrate releases
* fix tests
* add rollback
* pull request migtations
* fix import
* fix go module vendor
* add tests for upload to gitea
* more migrate options
* fix swagger-check
* fix misspell
* add options on migration UI
* fix log error
* improve UI options on migrating
* add support for username password when migrating from github
* fix tests
* remove comments and fix migrate limitation
* improve error handles
* migrate API will also support migrate milestones/labels/issues/pulls/releases
* fix tests and remove unused codes
* add DownloaderFactory and docs about how to create a new Downloader
* fix misspell
* fix migration docs
* Add hints about migrate options on migration page
* fix tests
Diffstat (limited to 'routers/api/v1/repo/repo.go')
-rw-r--r-- | routers/api/v1/repo/repo.go | 75 |
1 files changed, 54 insertions, 21 deletions
diff --git a/routers/api/v1/repo/repo.go b/routers/api/v1/repo/repo.go index 1991ed5968..35fea20d49 100644 --- a/routers/api/v1/repo/repo.go +++ b/routers/api/v1/repo/repo.go @@ -14,6 +14,7 @@ import ( "code.gitea.io/gitea/modules/auth" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/log" + "code.gitea.io/gitea/modules/migrations" "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/util" "code.gitea.io/gitea/routers/api/v1/convert" @@ -401,31 +402,63 @@ func Migrate(ctx *context.APIContext, form auth.MigrateRepoForm) { return } - repo, err := models.MigrateRepository(ctx.User, ctxUser, models.MigrateRepoOptions{ - Name: form.RepoName, - Description: form.Description, - IsPrivate: form.Private || setting.Repository.ForcePrivate, - IsMirror: form.Mirror, - RemoteAddr: remoteAddr, - }) - if err != nil { - if models.IsErrRepoAlreadyExist(err) { - ctx.Error(409, "", "The repository with the same name already exists.") - return - } + var opts = migrations.MigrateOptions{ + RemoteURL: remoteAddr, + Name: form.RepoName, + Description: form.Description, + Private: form.Private || setting.Repository.ForcePrivate, + Mirror: form.Mirror, + AuthUsername: form.AuthUsername, + AuthPassword: form.AuthPassword, + Wiki: form.Wiki, + Issues: form.Issues, + Milestones: form.Milestones, + Labels: form.Labels, + Comments: true, + PullRequests: form.PullRequests, + Releases: form.Releases, + } + if opts.Mirror { + opts.Issues = false + opts.Milestones = false + opts.Labels = false + opts.Comments = false + opts.PullRequests = false + opts.Releases = false + } - err = util.URLSanitizedError(err, remoteAddr) - if repo != nil { - if errDelete := models.DeleteRepository(ctx.User, ctxUser.ID, repo.ID); errDelete != nil { - log.Error("DeleteRepository: %v", errDelete) - } - } - ctx.Error(500, "MigrateRepository", err) + repo, err := migrations.MigrateRepository(ctx.User, ctxUser.Name, opts) + if err == nil { + log.Trace("Repository migrated: %s/%s", ctxUser.Name, form.RepoName) + ctx.JSON(201, repo.APIFormat(models.AccessModeAdmin)) return } - log.Trace("Repository migrated: %s/%s", ctxUser.Name, form.RepoName) - ctx.JSON(201, repo.APIFormat(models.AccessModeAdmin)) + switch { + case models.IsErrRepoAlreadyExist(err): + ctx.Error(409, "", "The repository with the same name already exists.") + case migrations.IsRateLimitError(err): + ctx.Error(422, "", "Remote visit addressed rate limitation.") + case migrations.IsTwoFactorAuthError(err): + ctx.Error(422, "", "Remote visit required two factors authentication.") + case models.IsErrReachLimitOfRepo(err): + ctx.Error(422, "", fmt.Sprintf("You have already reached your limit of %d repositories.", ctxUser.MaxCreationLimit())) + case models.IsErrNameReserved(err): + ctx.Error(422, "", fmt.Sprintf("The username '%s' is reserved.", err.(models.ErrNameReserved).Name)) + case models.IsErrNamePatternNotAllowed(err): + ctx.Error(422, "", fmt.Sprintf("The pattern '%s' is not allowed in a username.", err.(models.ErrNamePatternNotAllowed).Pattern)) + default: + err = util.URLSanitizedError(err, remoteAddr) + if strings.Contains(err.Error(), "Authentication failed") || + strings.Contains(err.Error(), "Bad credentials") || + strings.Contains(err.Error(), "could not read Username") { + ctx.Error(422, "", fmt.Sprintf("Authentication failed: %v.", err)) + } else if strings.Contains(err.Error(), "fatal:") { + ctx.Error(422, "", fmt.Sprintf("Migration failed: %v.", err)) + } else { + ctx.Error(500, "MigrateRepository", err) + } + } } // Get one repository |