44package repo
55
66import (
7- "bytes "
7+ gocontext "context "
88 "errors"
99 "fmt"
1010 "net/http"
@@ -173,7 +173,7 @@ func Migrate(ctx *context.APIContext) {
173173 opts .AWSSecretAccessKey = form .AWSSecretAccessKey
174174 }
175175
176- repo , err := repo_service .CreateRepositoryDirectly (ctx , ctx .Doer , repoOwner , repo_service.CreateRepoOptions {
176+ createdRepo , err := repo_service .CreateRepositoryDirectly (ctx , ctx .Doer , repoOwner , repo_service.CreateRepoOptions {
177177 Name : opts .RepoName ,
178178 Description : opts .Description ,
179179 OriginalURL : form .CloneAddr ,
@@ -187,35 +187,37 @@ func Migrate(ctx *context.APIContext) {
187187 return
188188 }
189189
190- opts .MigrateToRepoID = repo .ID
190+ opts .MigrateToRepoID = createdRepo .ID
191191
192- defer func () {
193- if e := recover (); e != nil {
194- var buf bytes.Buffer
195- fmt .Fprintf (& buf , "Handler crashed with error: %v" , log .Stack (2 ))
196-
197- err = errors .New (buf .String ())
198- }
199-
200- if err == nil {
201- notify_service .MigrateRepository (ctx , ctx .Doer , repoOwner , repo )
202- return
203- }
204-
205- if repo != nil {
206- if errDelete := repo_service .DeleteRepositoryDirectly (ctx , repo .ID ); errDelete != nil {
207- log .Error ("DeleteRepository: %v" , errDelete )
192+ doLongTimeMigrate := func (ctx gocontext.Context , doer * user_model.User ) (migratedRepo * repo_model.Repository , retErr error ) {
193+ defer func () {
194+ if e := recover (); e != nil {
195+ log .Error ("MigrateRepository panic: %v\n %s" , e , log .Stack (2 ))
196+ if errDelete := repo_service .DeleteRepositoryDirectly (ctx , createdRepo .ID ); errDelete != nil {
197+ log .Error ("Unable to delete repo after MigrateRepository panic: %v" , errDelete )
198+ }
199+ retErr = errors .New ("MigrateRepository panic" ) // no idea why it would happen, just legacy code
208200 }
201+ }()
202+
203+ migratedRepo , err := migrations .MigrateRepository (ctx , doer , repoOwner .Name , opts , nil )
204+ if err != nil {
205+ return nil , err
209206 }
210- }()
207+ notify_service .MigrateRepository (ctx , doer , repoOwner , migratedRepo )
208+ return migratedRepo , nil
209+ }
211210
212- if repo , err = migrations .MigrateRepository (graceful .GetManager ().HammerContext (), ctx .Doer , repoOwner .Name , opts , nil ); err != nil {
211+ // use a background context, don't cancel the migration even if the client goes away
212+ // HammerContext doesn't seem right (from https://github.com/go-gitea/gitea/pull/9335/files)
213+ // There are other abuses, maybe most HammerContext abuses should be fixed together in the future.
214+ migratedRepo , err := doLongTimeMigrate (graceful .GetManager ().HammerContext (), ctx .Doer )
215+ if err != nil {
213216 handleMigrateError (ctx , repoOwner , err )
214217 return
215218 }
216219
217- log .Trace ("Repository migrated: %s/%s" , repoOwner .Name , form .RepoName )
218- ctx .JSON (http .StatusCreated , convert .ToRepo (ctx , repo , access_model.Permission {AccessMode : perm .AccessModeAdmin }))
220+ ctx .JSON (http .StatusCreated , convert .ToRepo (ctx , migratedRepo , access_model.Permission {AccessMode : perm .AccessModeAdmin }))
219221}
220222
221223func handleMigrateError (ctx * context.APIContext , repoOwner * user_model.User , err error ) {
0 commit comments