@ -46,6 +46,7 @@ type GiteaLocalUploader struct {
issues map [ int64 ] * models . Issue
issues map [ int64 ] * models . Issue
gitRepo * git . Repository
gitRepo * git . Repository
prHeadCache map [ string ] struct { }
prHeadCache map [ string ] struct { }
sameApp bool
userMap map [ int64 ] int64 // external user id mapping to user id
userMap map [ int64 ] int64 // external user id mapping to user id
prCache map [ int64 ] * models . PullRequest
prCache map [ int64 ] * models . PullRequest
gitServiceType structs . GitServiceType
gitServiceType structs . GitServiceType
@ -128,6 +129,7 @@ func (g *GiteaLocalUploader) CreateRepo(repo *base.Repository, opts base.Migrate
MirrorInterval : opts . MirrorInterval ,
MirrorInterval : opts . MirrorInterval ,
} , NewMigrationHTTPTransport ( ) )
} , NewMigrationHTTPTransport ( ) )
g . sameApp = strings . HasPrefix ( repo . OriginalURL , setting . AppURL )
g . repo = r
g . repo = r
if err != nil {
if err != nil {
return err
return err
@ -256,7 +258,7 @@ func (g *GiteaLocalUploader) CreateReleases(releases ...*base.Release) error {
CreatedUnix : timeutil . TimeStamp ( release . Created . Unix ( ) ) ,
CreatedUnix : timeutil . TimeStamp ( release . Created . Unix ( ) ) ,
}
}
if err := g . remapExternal User ( release , & rel ) ; err != nil {
if err := g . remapUser ( release , & rel ) ; err != nil {
return err
return err
}
}
@ -373,7 +375,7 @@ func (g *GiteaLocalUploader) CreateIssues(issues ...*base.Issue) error {
UpdatedUnix : timeutil . TimeStamp ( issue . Updated . Unix ( ) ) ,
UpdatedUnix : timeutil . TimeStamp ( issue . Updated . Unix ( ) ) ,
}
}
if err := g . remapExternal User ( issue , & is ) ; err != nil {
if err := g . remapUser ( issue , & is ) ; err != nil {
return err
return err
}
}
@ -386,7 +388,7 @@ func (g *GiteaLocalUploader) CreateIssues(issues ...*base.Issue) error {
Type : reaction . Content ,
Type : reaction . Content ,
CreatedUnix : timeutil . TimeStampNow ( ) ,
CreatedUnix : timeutil . TimeStampNow ( ) ,
}
}
if err := g . remapExternal User ( reaction , & res ) ; err != nil {
if err := g . remapUser ( reaction , & res ) ; err != nil {
return err
return err
}
}
is . Reactions = append ( is . Reactions , & res )
is . Reactions = append ( is . Reactions , & res )
@ -437,7 +439,7 @@ func (g *GiteaLocalUploader) CreateComments(comments ...*base.Comment) error {
UpdatedUnix : timeutil . TimeStamp ( comment . Updated . Unix ( ) ) ,
UpdatedUnix : timeutil . TimeStamp ( comment . Updated . Unix ( ) ) ,
}
}
if err := g . remapExternal User ( comment , & cm ) ; err != nil {
if err := g . remapUser ( comment , & cm ) ; err != nil {
return err
return err
}
}
@ -447,7 +449,7 @@ func (g *GiteaLocalUploader) CreateComments(comments ...*base.Comment) error {
Type : reaction . Content ,
Type : reaction . Content ,
CreatedUnix : timeutil . TimeStampNow ( ) ,
CreatedUnix : timeutil . TimeStampNow ( ) ,
}
}
if err := g . remapExternal User ( reaction , & res ) ; err != nil {
if err := g . remapUser ( reaction , & res ) ; err != nil {
return err
return err
}
}
cm . Reactions = append ( cm . Reactions , & res )
cm . Reactions = append ( cm . Reactions , & res )
@ -471,7 +473,7 @@ func (g *GiteaLocalUploader) CreatePullRequests(prs ...*base.PullRequest) error
return err
return err
}
}
if err := g . remapExternal User ( pr , gpr . Issue ) ; err != nil {
if err := g . remapUser ( pr , gpr . Issue ) ; err != nil {
return err
return err
}
}
@ -626,7 +628,7 @@ func (g *GiteaLocalUploader) newPullRequest(pr *base.PullRequest) (*models.PullR
UpdatedUnix : timeutil . TimeStamp ( pr . Updated . Unix ( ) ) ,
UpdatedUnix : timeutil . TimeStamp ( pr . Updated . Unix ( ) ) ,
}
}
if err := g . remapExternal User ( pr , & issue ) ; err != nil {
if err := g . remapUser ( pr , & issue ) ; err != nil {
return nil , err
return nil , err
}
}
@ -636,7 +638,7 @@ func (g *GiteaLocalUploader) newPullRequest(pr *base.PullRequest) (*models.PullR
Type : reaction . Content ,
Type : reaction . Content ,
CreatedUnix : timeutil . TimeStampNow ( ) ,
CreatedUnix : timeutil . TimeStampNow ( ) ,
}
}
if err := g . remapExternal User ( reaction , & res ) ; err != nil {
if err := g . remapUser ( reaction , & res ) ; err != nil {
return nil , err
return nil , err
}
}
issue . Reactions = append ( issue . Reactions , & res )
issue . Reactions = append ( issue . Reactions , & res )
@ -710,7 +712,7 @@ func (g *GiteaLocalUploader) CreateReviews(reviews ...*base.Review) error {
UpdatedUnix : timeutil . TimeStamp ( review . CreatedAt . Unix ( ) ) ,
UpdatedUnix : timeutil . TimeStamp ( review . CreatedAt . Unix ( ) ) ,
}
}
if err := g . remapExternal User ( review , & cm ) ; err != nil {
if err := g . remapUser ( review , & cm ) ; err != nil {
return err
return err
}
}
@ -773,7 +775,7 @@ func (g *GiteaLocalUploader) CreateReviews(reviews ...*base.Review) error {
UpdatedUnix : timeutil . TimeStamp ( comment . UpdatedAt . Unix ( ) ) ,
UpdatedUnix : timeutil . TimeStamp ( comment . UpdatedAt . Unix ( ) ) ,
}
}
if err := g . remapExternal User ( review , & c ) ; err != nil {
if err := g . remapUser ( review , & c ) ; err != nil {
return err
return err
}
}
@ -816,23 +818,52 @@ func (g *GiteaLocalUploader) Finish() error {
return repo_model . UpdateRepositoryCols ( g . repo , "status" )
return repo_model . UpdateRepositoryCols ( g . repo , "status" )
}
}
func ( g * GiteaLocalUploader ) remapExternalUser ( source user_model . ExternalUserMigrated , target user_model . ExternalUserRemappable ) ( err error ) {
func ( g * GiteaLocalUploader ) remapUser ( source user_model . ExternalUserMigrated , target user_model . ExternalUserRemappable ) error {
userid , ok := g . userMap [ source . GetExternalID ( ) ]
var userid int64
tp := g . gitServiceType . Name ( )
var err error
if ! ok && tp != "" {
if g . sameApp {
userid , err = user_model . GetUserIDByExternalUserID ( tp , fmt . Sprintf ( "%d" , source . GetExternalID ( ) ) )
userid , err = g . remapLocalUser ( source , target )
} else {
userid , err = g . remapExternalUser ( source , target )
}
if err != nil {
if err != nil {
log . Error ( "GetUserIDByExternalUserID: %v" , err )
return err
}
}
if userid > 0 {
if userid > 0 {
g . userMap [ source . GetExternalID ( ) ] = userid
return target . RemapExternalUser ( "" , 0 , userid )
}
}
}
return target . RemapExternalUser ( source . GetExternalName ( ) , source . GetExternalID ( ) , g . doer . ID )
}
if userid > 0 {
func ( g * GiteaLocalUploader ) remapLocalUser ( source user_model . ExternalUserMigrated , target user_model . ExternalUserRemappable ) ( int64 , error ) {
err = target . RemapExternalUser ( "" , 0 , userid )
userid , ok := g . userMap [ source . GetExternalID ( ) ]
if ! ok {
name , err := user_model . GetUserNameByID ( g . ctx , source . GetExternalID ( ) )
if err != nil {
return 0 , err
}
// let's not reuse an ID when the user was deleted or has a different user name
if name != source . GetExternalName ( ) {
userid = 0
} else {
} else {
err = target . RemapExternalUser ( source . GetExternalName ( ) , source . GetExternalID ( ) , g . doer . ID )
userid = source . GetExternalID ( )
}
g . userMap [ source . GetExternalID ( ) ] = userid
}
return userid , nil
}
func ( g * GiteaLocalUploader ) remapExternalUser ( source user_model . ExternalUserMigrated , target user_model . ExternalUserRemappable ) ( userid int64 , err error ) {
userid , ok := g . userMap [ source . GetExternalID ( ) ]
if ! ok {
userid , err = user_model . GetUserIDByExternalUserID ( g . gitServiceType . Name ( ) , fmt . Sprintf ( "%d" , source . GetExternalID ( ) ) )
if err != nil {
log . Error ( "GetUserIDByExternalUserID: %v" , err )
return 0 , err
}
g . userMap [ source . GetExternalID ( ) ] = userid
}
}
return
return userid , nil
}
}