]> source.dussan.org Git - gitea.git/commitdiff
Fix bug when migrate from API (#8631)
authorLunny Xiao <xiaolunwen@gmail.com>
Fri, 8 Nov 2019 22:21:00 +0000 (06:21 +0800)
committertechknowlogick <techknowlogick@gitea.io>
Fri, 8 Nov 2019 22:21:00 +0000 (17:21 -0500)
* fix bug when migrate from API

* fix test

* fix test

* improve

* fix error message

integrations/api_repo_test.go
models/repo.go
modules/task/migrate.go
routers/api/v1/repo/repo.go

index 60fe4a3649576b05471ebf22acba7bab20863ad1..a2683d4af47626b656e1fa2e34d57f0897047376 100644 (file)
@@ -334,7 +334,7 @@ func testAPIRepoMigrateConflict(t *testing.T, u *url.URL) {
                resp := httpContext.Session.MakeRequest(t, req, http.StatusConflict)
                respJSON := map[string]string{}
                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"])
        })
 }
 
index e0a93aaebfdeedd507849294ea8d1c5124a25995..812460e92ff17c63727a472eb1e1fa462f065b7f 100644 (file)
@@ -2840,3 +2840,9 @@ func (repo *Repository) GetTreePathLock(treePath string) (*LFSLock, error) {
        }
        return nil, nil
 }
+
+// UpdateRepositoryCols updates repository's columns
+func UpdateRepositoryCols(repo *Repository, cols ...string) error {
+       _, err := x.ID(repo.ID).Cols(cols...).Update(repo)
+       return err
+}
index 5d15a506d793279219908771e66ab83e9837b144..247403d7be35532987497605b1d6a5858edc3cda 100644 (file)
@@ -97,8 +97,6 @@ func runMigrateTask(t *models.Task) (err error) {
        opts.MigrateToRepoID = t.RepoID
        repo, err := migrations.MigrateRepository(t.Doer, t.Owner.Name, *opts)
        if err == nil {
-               notification.NotifyMigrateRepository(t.Doer, t.Owner, repo)
-
                log.Trace("Repository migrated [%d]: %s/%s", repo.ID, t.Owner.Name, repo.Name)
                return nil
        }
index d366435a2fe19bb7845af7024b414c69569d18be..7b752370d44e6e2df0bd67f4aa90db06301077dc 100644 (file)
@@ -6,6 +6,8 @@
 package repo
 
 import (
+       "bytes"
+       "errors"
        "fmt"
        "net/http"
        "net/url"
@@ -425,15 +427,54 @@ func Migrate(ctx *context.APIContext, form auth.MigrateRepoForm) {
                opts.Releases = false
        }
 
-       repo, err := migrations.MigrateRepository(ctx.User, ctxUser.Name, opts)
-       if err == nil {
-               notification.NotifyMigrateRepository(ctx.User, ctxUser, repo)
+       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
 
-               log.Trace("Repository migrated: %s/%s", ctxUser.Name, form.RepoName)
-               ctx.JSON(201, repo.APIFormat(models.AccessModeAdmin))
+       defer func() {
+               if e := recover(); e != nil {
+                       var buf bytes.Buffer
+                       fmt.Fprintf(&buf, "Handler crashed with error: %v", log.Stack(2))
+
+                       err = errors.New(buf.String())
+               }
+
+               if err == nil {
+                       repo.Status = models.RepositoryReady
+                       if err := models.UpdateRepositoryCols(repo, "status"); err == nil {
+                               notification.NotifyMigrateRepository(ctx.User, ctxUser, repo)
+                               return
+                       }
+               }
+
+               if repo != nil {
+                       if errDelete := models.DeleteRepository(ctx.User, ctxUser.ID, repo.ID); errDelete != nil {
+                               log.Error("DeleteRepository: %v", errDelete)
+                       }
+               }
+       }()
+
+       if _, err = migrations.MigrateRepository(ctx.User, ctxUser.Name, opts); err != nil {
+               handleMigrateError(ctx, ctxUser, remoteAddr, err)
                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 {
        case models.IsErrRepoAlreadyExist(err):
                ctx.Error(409, "", "The repository with the same name already exists.")
@@ -442,7 +483,7 @@ func Migrate(ctx *context.APIContext, form auth.MigrateRepoForm) {
        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()))
+               ctx.Error(422, "", fmt.Sprintf("You have already reached your limit of %d repositories.", repoOwner.MaxCreationLimit()))
        case models.IsErrNameReserved(err):
                ctx.Error(422, "", fmt.Sprintf("The username '%s' is reserved.", err.(models.ErrNameReserved).Name))
        case models.IsErrNamePatternNotAllowed(err):