6
6
package repo
7
7
8
8
import (
9
+ "bytes"
10
+ "errors"
9
11
"fmt"
10
12
"net/http"
11
13
"net/url"
@@ -425,15 +427,54 @@ func Migrate(ctx *context.APIContext, form auth.MigrateRepoForm) {
425
427
opts .Releases = false
426
428
}
427
429
428
- repo , err := migrations .MigrateRepository (ctx .User , ctxUser .Name , opts )
429
- if err == nil {
430
- notification .NotifyMigrateRepository (ctx .User , ctxUser , repo )
430
+ repo , err := models .CreateRepository (ctx .User , ctxUser , models.CreateRepoOptions {
431
+ Name : opts .RepoName ,
432
+ Description : opts .Description ,
433
+ OriginalURL : opts .CloneAddr ,
434
+ IsPrivate : opts .Private ,
435
+ IsMirror : opts .Mirror ,
436
+ Status : models .RepositoryBeingMigrated ,
437
+ })
438
+ if err != nil {
439
+ handleMigrateError (ctx , ctxUser , remoteAddr , err )
440
+ return
441
+ }
442
+
443
+ opts .MigrateToRepoID = repo .ID
431
444
432
- log .Trace ("Repository migrated: %s/%s" , ctxUser .Name , form .RepoName )
433
- ctx .JSON (201 , repo .APIFormat (models .AccessModeAdmin ))
445
+ defer func () {
446
+ if e := recover (); e != nil {
447
+ var buf bytes.Buffer
448
+ fmt .Fprintf (& buf , "Handler crashed with error: %v" , log .Stack (2 ))
449
+
450
+ err = errors .New (buf .String ())
451
+ }
452
+
453
+ if err == nil {
454
+ repo .Status = models .RepositoryReady
455
+ if err := models .UpdateRepositoryCols (repo , "status" ); err == nil {
456
+ notification .NotifyMigrateRepository (ctx .User , ctxUser , repo )
457
+ return
458
+ }
459
+ }
460
+
461
+ if repo != nil {
462
+ if errDelete := models .DeleteRepository (ctx .User , ctxUser .ID , repo .ID ); errDelete != nil {
463
+ log .Error ("DeleteRepository: %v" , errDelete )
464
+ }
465
+ }
466
+ }()
467
+
468
+ if _ , err = migrations .MigrateRepository (ctx .User , ctxUser .Name , opts ); err != nil {
469
+ handleMigrateError (ctx , ctxUser , remoteAddr , err )
434
470
return
435
471
}
436
472
473
+ log .Trace ("Repository migrated: %s/%s" , ctxUser .Name , form .RepoName )
474
+ ctx .JSON (201 , repo .APIFormat (models .AccessModeAdmin ))
475
+ }
476
+
477
+ func handleMigrateError (ctx * context.APIContext , repoOwner * models.User , remoteAddr string , err error ) {
437
478
switch {
438
479
case models .IsErrRepoAlreadyExist (err ):
439
480
ctx .Error (409 , "" , "The repository with the same name already exists." )
@@ -442,7 +483,7 @@ func Migrate(ctx *context.APIContext, form auth.MigrateRepoForm) {
442
483
case migrations .IsTwoFactorAuthError (err ):
443
484
ctx .Error (422 , "" , "Remote visit required two factors authentication." )
444
485
case models .IsErrReachLimitOfRepo (err ):
445
- ctx .Error (422 , "" , fmt .Sprintf ("You have already reached your limit of %d repositories." , ctxUser .MaxCreationLimit ()))
486
+ ctx .Error (422 , "" , fmt .Sprintf ("You have already reached your limit of %d repositories." , repoOwner .MaxCreationLimit ()))
446
487
case models .IsErrNameReserved (err ):
447
488
ctx .Error (422 , "" , fmt .Sprintf ("The username '%s' is reserved." , err .(models.ErrNameReserved ).Name ))
448
489
case models .IsErrNamePatternNotAllowed (err ):
0 commit comments