* Fix bug when migrate from API (#8631) * fix bug when migrate from API * fix test * fix test * improve * fix error message * Update routers/api/v1/repo/repo.go * remove unrelated Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
This commit is contained in:
parent
c63a80138a
commit
cb3fe4cbf1
4 changed files with 31 additions and 10 deletions
|
@ -334,7 +334,7 @@ func testAPIRepoMigrateConflict(t *testing.T, u *url.URL) {
|
||||||
resp := httpContext.Session.MakeRequest(t, req, http.StatusConflict)
|
resp := httpContext.Session.MakeRequest(t, req, http.StatusConflict)
|
||||||
respJSON := map[string]string{}
|
respJSON := map[string]string{}
|
||||||
DecodeJSON(t, resp, &respJSON)
|
DecodeJSON(t, resp, &respJSON)
|
||||||
assert.Equal(t, respJSON["message"], "The repository with the same name already exists.")
|
assert.Equal(t, "The repository with the same name already exists.", respJSON["message"])
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2811,3 +2811,9 @@ func (repo *Repository) GetOriginalURLHostname() string {
|
||||||
|
|
||||||
return u.Host
|
return u.Host
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// UpdateRepositoryCols updates repository's columns
|
||||||
|
func UpdateRepositoryCols(repo *Repository, cols ...string) error {
|
||||||
|
_, err := x.ID(repo.ID).Cols(cols...).Update(repo)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
|
@ -97,8 +97,6 @@ func runMigrateTask(t *models.Task) (err error) {
|
||||||
opts.MigrateToRepoID = t.RepoID
|
opts.MigrateToRepoID = t.RepoID
|
||||||
repo, err := migrations.MigrateRepository(t.Doer, t.Owner.Name, *opts)
|
repo, err := migrations.MigrateRepository(t.Doer, t.Owner.Name, *opts)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
notification.NotifyMigrateRepository(t.Doer, t.Owner, repo)
|
|
||||||
|
|
||||||
log.Trace("Repository migrated [%d]: %s/%s", repo.ID, t.Owner.Name, repo.Name)
|
log.Trace("Repository migrated [%d]: %s/%s", repo.ID, t.Owner.Name, repo.Name)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -433,7 +433,21 @@ func Migrate(ctx *context.APIContext, form auth.MigrateRepoForm) {
|
||||||
opts.Releases = false
|
opts.Releases = false
|
||||||
}
|
}
|
||||||
|
|
||||||
var repo *models.Repository
|
repo, err := models.CreateRepository(ctx.User, ctxUser, models.CreateRepoOptions{
|
||||||
|
Name: opts.RepoName,
|
||||||
|
Description: opts.Description,
|
||||||
|
OriginalURL: opts.CloneAddr,
|
||||||
|
IsPrivate: opts.Private,
|
||||||
|
IsMirror: opts.Mirror,
|
||||||
|
Status: models.RepositoryBeingMigrated,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
handleMigrateError(ctx, ctxUser, remoteAddr, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
opts.MigrateToRepoID = repo.ID
|
||||||
|
|
||||||
defer func() {
|
defer func() {
|
||||||
if e := recover(); e != nil {
|
if e := recover(); e != nil {
|
||||||
var buf bytes.Buffer
|
var buf bytes.Buffer
|
||||||
|
@ -443,7 +457,7 @@ func Migrate(ctx *context.APIContext, form auth.MigrateRepoForm) {
|
||||||
|
|
||||||
if err == nil {
|
if err == nil {
|
||||||
repo.Status = models.RepositoryReady
|
repo.Status = models.RepositoryReady
|
||||||
if err := models.UpdateRepositoryStatus(repo.ID, repo.Status); err == nil {
|
if err := models.UpdateRepositoryCols(repo, "status"); err == nil {
|
||||||
notification.NotifyMigrateRepository(ctx.User, ctxUser, repo)
|
notification.NotifyMigrateRepository(ctx.User, ctxUser, repo)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -456,13 +470,16 @@ func Migrate(ctx *context.APIContext, form auth.MigrateRepoForm) {
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
repo, err = migrations.MigrateRepository(ctx.User, ctxUser.Name, opts)
|
if _, err = migrations.MigrateRepository(ctx.User, ctxUser.Name, opts); err != nil {
|
||||||
if err == nil {
|
handleMigrateError(ctx, ctxUser, remoteAddr, err)
|
||||||
log.Trace("Repository migrated: %s/%s", ctxUser.Name, form.RepoName)
|
|
||||||
ctx.JSON(201, repo.APIFormat(models.AccessModeAdmin))
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
log.Trace("Repository migrated: %s/%s", ctxUser.Name, form.RepoName)
|
||||||
|
ctx.JSON(201, repo.APIFormat(models.AccessModeAdmin))
|
||||||
|
}
|
||||||
|
|
||||||
|
func handleMigrateError(ctx *context.APIContext, repoOwner *models.User, remoteAddr string, err error) {
|
||||||
switch {
|
switch {
|
||||||
case models.IsErrRepoAlreadyExist(err):
|
case models.IsErrRepoAlreadyExist(err):
|
||||||
ctx.Error(409, "", "The repository with the same name already exists.")
|
ctx.Error(409, "", "The repository with the same name already exists.")
|
||||||
|
@ -471,7 +488,7 @@ func Migrate(ctx *context.APIContext, form auth.MigrateRepoForm) {
|
||||||
case migrations.IsTwoFactorAuthError(err):
|
case migrations.IsTwoFactorAuthError(err):
|
||||||
ctx.Error(422, "", "Remote visit required two factors authentication.")
|
ctx.Error(422, "", "Remote visit required two factors authentication.")
|
||||||
case models.IsErrReachLimitOfRepo(err):
|
case models.IsErrReachLimitOfRepo(err):
|
||||||
ctx.Error(422, "", fmt.Sprintf("You have already reached your limit of %d repositories.", ctxUser.MaxCreationLimit()))
|
ctx.Error(422, "", fmt.Sprintf("You have already reached your limit of %d repositories.", repoOwner.MaxCreationLimit()))
|
||||||
case models.IsErrNameReserved(err):
|
case models.IsErrNameReserved(err):
|
||||||
ctx.Error(422, "", fmt.Sprintf("The username '%s' is reserved.", err.(models.ErrNameReserved).Name))
|
ctx.Error(422, "", fmt.Sprintf("The username '%s' is reserved.", err.(models.ErrNameReserved).Name))
|
||||||
case models.IsErrNamePatternNotAllowed(err):
|
case models.IsErrNamePatternNotAllowed(err):
|
||||||
|
|
Reference in a new issue