1
0
Fork 0
forked from forgejo/forgejo

preserve users if restoring a repository on the same Gitea instance (#18604)

When calling DumpRepository and RestoreRepository on the same Gitea
instance, the users are preserved: all labels, issues etc. belong to
the external user who is, in this particular case, the local user.

Dead code verifying g.gitServiceType.Name() == "" (i.e. plain git) is
removed. The function is never called because the plain git downloader
does not migrate anything that is associated to a user, by definition.

Errors returned by GetUserIDByExternalUserID are no longer ignored.

The userMap is used when the external user is not kown, which is the
most common case. It was only used when the external user exists
which happens less often and, as a result, every occurence of an
unknown external user required a SQL query.

Signed-off-by: Loïc Dachary <loic@dachary.org>

Co-authored-by: Loïc Dachary <loic@dachary.org>
Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
This commit is contained in:
singuliere 2022-02-06 10:05:29 +01:00 committed by GitHub
parent 9419dd2b62
commit 8bd89ca294
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 128 additions and 29 deletions

View file

@ -117,6 +117,56 @@ func TestGiteaUploadRepo(t *testing.T) {
assert.Len(t, pulls[0].Issue.Comments, 2)
}
func TestGiteaUploadRemapLocalUser(t *testing.T) {
unittest.PrepareTestEnv(t)
doer := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1}).(*user_model.User)
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}).(*user_model.User)
repoName := "migrated"
uploader := NewGiteaLocalUploader(context.Background(), doer, doer.Name, repoName)
// call remapLocalUser
uploader.sameApp = true
externalID := int64(1234567)
externalName := "username"
source := base.Release{
PublisherID: externalID,
PublisherName: externalName,
}
//
// The externalID does not match any existing user, everything
// belongs to the doer
//
target := models.Release{}
uploader.userMap = make(map[int64]int64)
err := uploader.remapUser(&source, &target)
assert.NoError(t, err)
assert.EqualValues(t, doer.ID, target.GetUserID())
//
// The externalID matches a known user but the name does not match,
// everything belongs to the doer
//
source.PublisherID = user.ID
target = models.Release{}
uploader.userMap = make(map[int64]int64)
err = uploader.remapUser(&source, &target)
assert.NoError(t, err)
assert.EqualValues(t, doer.ID, target.GetUserID())
//
// The externalID and externalName match an existing user, everything
// belongs to the existing user
//
source.PublisherName = user.Name
target = models.Release{}
uploader.userMap = make(map[int64]int64)
err = uploader.remapUser(&source, &target)
assert.NoError(t, err)
assert.EqualValues(t, user.ID, target.GetUserID())
}
func TestGiteaUploadRemapExternalUser(t *testing.T) {
unittest.PrepareTestEnv(t)
doer := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1}).(*user_model.User)
@ -124,9 +174,11 @@ func TestGiteaUploadRemapExternalUser(t *testing.T) {
repoName := "migrated"
uploader := NewGiteaLocalUploader(context.Background(), doer, doer.Name, repoName)
uploader.gitServiceType = structs.GiteaService
// call remapExternalUser
uploader.sameApp = false
externalID := int64(1234567)
externalName := "url.or.something"
externalName := "username"
source := base.Release{
PublisherID: externalID,
PublisherName: externalName,
@ -136,8 +188,9 @@ func TestGiteaUploadRemapExternalUser(t *testing.T) {
// When there is no user linked to the external ID, the migrated data is authored
// by the doer
//
uploader.userMap = make(map[int64]int64)
target := models.Release{}
err := uploader.remapExternalUser(&source, &target)
err := uploader.remapUser(&source, &target)
assert.NoError(t, err)
assert.EqualValues(t, doer.ID, target.GetUserID())
@ -158,8 +211,9 @@ func TestGiteaUploadRemapExternalUser(t *testing.T) {
// When a user is linked to the external ID, it becomes the author of
// the migrated data
//
uploader.userMap = make(map[int64]int64)
target = models.Release{}
err = uploader.remapExternalUser(&source, &target)
err = uploader.remapUser(&source, &target)
assert.NoError(t, err)
assert.EqualValues(t, target.GetUserID(), linkedUser.ID)
assert.EqualValues(t, linkedUser.ID, target.GetUserID())
}