diff options
author | singuliere <35190819+singuliere@users.noreply.github.com> | 2022-02-06 10:05:29 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-02-06 17:05:29 +0800 |
commit | 8bd89ca2941bdb4f41be82d0ae66fdf5ceca0fe5 (patch) | |
tree | 461a43fabe026918f33ca12411c23ea65e54cbd3 /services/migrations/gitea_uploader_test.go | |
parent | 9419dd2b62ab4399e4e7711a3ec4b81858883923 (diff) | |
download | gitea-8bd89ca2941bdb4f41be82d0ae66fdf5ceca0fe5.tar.gz gitea-8bd89ca2941bdb4f41be82d0ae66fdf5ceca0fe5.zip |
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>
Diffstat (limited to 'services/migrations/gitea_uploader_test.go')
-rw-r--r-- | services/migrations/gitea_uploader_test.go | 62 |
1 files changed, 58 insertions, 4 deletions
diff --git a/services/migrations/gitea_uploader_test.go b/services/migrations/gitea_uploader_test.go index 7552245d74..0a35b5a632 100644 --- a/services/migrations/gitea_uploader_test.go +++ b/services/migrations/gitea_uploader_test.go @@ -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()) } |