From e54904ddc3cac795c0afd2650f164425387bf309 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Dachary?= Date: Mon, 31 Jan 2022 17:29:54 +0100 Subject: [PATCH 1/3] add test coverage for original author conversion during migrations MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit And create a function to factorize a code snippet that is repeated five times and would otherwise be more difficult to test and maintain consistently. Signed-off-by: Loïc Dachary --- models/fixtures/external_login_user.yml | 1 + models/issue.go | 17 +++ models/issue_comment.go | 17 +++ models/issue_reaction.go | 17 +++ models/release.go | 17 +++ models/review.go | 17 +++ models/user/external_login_user.go | 11 ++ modules/migration/comment.go | 6 + modules/migration/issue.go | 6 + modules/migration/reaction.go | 6 + modules/migration/release.go | 6 + modules/migration/review.go | 6 + services/migrations/gitea_uploader.go | 153 +++++---------------- services/migrations/gitea_uploader_test.go | 50 +++++++ 14 files changed, 213 insertions(+), 117 deletions(-) create mode 100644 models/fixtures/external_login_user.yml diff --git a/models/fixtures/external_login_user.yml b/models/fixtures/external_login_user.yml new file mode 100644 index 0000000000000..ca780a73aa0c1 --- /dev/null +++ b/models/fixtures/external_login_user.yml @@ -0,0 +1 @@ +[] # empty diff --git a/models/issue.go b/models/issue.go index 3a61b085dc3b2..01082e5d25079 100644 --- a/models/issue.go +++ b/models/issue.go @@ -2323,3 +2323,20 @@ func deleteIssuesByRepoID(sess db.Engine, repoID int64) (attachmentPaths []strin return } + +// RemapExternalUser ExternalUserRemappable interface +func (issue *Issue) RemapExternalUser(externalName string, externalID, userID int64) error { + issue.OriginalAuthor = externalName + issue.OriginalAuthorID = externalID + issue.PosterID = userID + return nil +} + +// GetUserID ExternalUserRemappable interface +func (issue *Issue) GetUserID() int64 { return issue.PosterID } + +// GetExternalName ExternalUserRemappable interface +func (issue *Issue) GetExternalName() string { return issue.OriginalAuthor } + +// GetExternalID ExternalUserRemappable interface +func (issue *Issue) GetExternalID() int64 { return issue.OriginalAuthorID } diff --git a/models/issue_comment.go b/models/issue_comment.go index 34541dc3cdf1c..31bd041ca759d 100644 --- a/models/issue_comment.go +++ b/models/issue_comment.go @@ -1464,3 +1464,20 @@ func commitBranchCheck(gitRepo *git.Repository, startCommit *git.Commit, endComm } return nil } + +// RemapExternalUser ExternalUserRemappable interface +func (c *Comment) RemapExternalUser(externalName string, externalID, userID int64) error { + c.OriginalAuthor = externalName + c.OriginalAuthorID = externalID + c.PosterID = userID + return nil +} + +// GetUserID ExternalUserRemappable interface +func (c *Comment) GetUserID() int64 { return c.PosterID } + +// GetExternalName ExternalUserRemappable interface +func (c *Comment) GetExternalName() string { return c.OriginalAuthor } + +// GetExternalID ExternalUserRemappable interface +func (c *Comment) GetExternalID() int64 { return c.OriginalAuthorID } diff --git a/models/issue_reaction.go b/models/issue_reaction.go index 2e0ab07db77e9..45b1d64fe113e 100644 --- a/models/issue_reaction.go +++ b/models/issue_reaction.go @@ -343,3 +343,20 @@ func (list ReactionList) GetMoreUserCount() int { } return len(list) - setting.UI.ReactionMaxUserNum } + +// RemapExternalUser ExternalUserRemappable interface +func (r *Reaction) RemapExternalUser(externalName string, externalID, userID int64) error { + r.OriginalAuthor = externalName + r.OriginalAuthorID = externalID + r.UserID = userID + return nil +} + +// GetUserID ExternalUserRemappable interface +func (r *Reaction) GetUserID() int64 { return r.UserID } + +// GetExternalName ExternalUserRemappable interface +func (r *Reaction) GetExternalName() string { return r.OriginalAuthor } + +// GetExternalID ExternalUserRemappable interface +func (r *Reaction) GetExternalID() int64 { return r.OriginalAuthorID } diff --git a/models/release.go b/models/release.go index 51ac0426ac028..0285f6bd5e8d1 100644 --- a/models/release.go +++ b/models/release.go @@ -456,3 +456,20 @@ func SaveOrUpdateTag(repo *repo_model.Repository, newRel *Release) error { } return nil } + +// RemapExternalUser ExternalUserRemappable interface +func (r *Release) RemapExternalUser(externalName string, externalID, userID int64) error { + r.OriginalAuthor = externalName + r.OriginalAuthorID = externalID + r.PublisherID = userID + return nil +} + +// UserID ExternalUserRemappable interface +func (r *Release) GetUserID() int64 { return r.PublisherID } + +// ExternalName ExternalUserRemappable interface +func (r *Release) GetExternalName() string { return r.OriginalAuthor } + +// ExternalID ExternalUserRemappable interface +func (r *Release) GetExternalID() int64 { return r.OriginalAuthorID } diff --git a/models/review.go b/models/review.go index 8b0092b1ed870..22c47486a14b6 100644 --- a/models/review.go +++ b/models/review.go @@ -987,3 +987,20 @@ func (r *Review) HTMLURL() string { } return comment.HTMLURL() } + +// RemapExternalUser ExternalUserRemappable interface +func (r *Review) RemapExternalUser(externalName string, externalID, userID int64) error { + r.OriginalAuthor = externalName + r.OriginalAuthorID = externalID + r.ReviewerID = userID + return nil +} + +// GetUserID ExternalUserRemappable interface +func (r *Review) GetUserID() int64 { return r.ReviewerID } + +// GetExternalName ExternalUserRemappable interface +func (r *Review) GetExternalName() string { return r.OriginalAuthor } + +// GetExternalID ExternalUserRemappable interface +func (r *Review) GetExternalID() int64 { return r.OriginalAuthorID } diff --git a/models/user/external_login_user.go b/models/user/external_login_user.go index 8cf7c652b11d4..435fbbedeeabb 100644 --- a/models/user/external_login_user.go +++ b/models/user/external_login_user.go @@ -68,6 +68,17 @@ type ExternalLoginUser struct { ExpiresAt time.Time } +type ExternalUserMigrated interface { + GetExternalName() string + GetExternalID() int64 +} + +type ExternalUserRemappable interface { + GetUserID() int64 + RemapExternalUser(externalName string, externalID, userID int64) error + ExternalUserMigrated +} + func init() { db.RegisterModel(new(ExternalLoginUser)) } diff --git a/modules/migration/comment.go b/modules/migration/comment.go index 234fea3e82a72..36277129d9785 100644 --- a/modules/migration/comment.go +++ b/modules/migration/comment.go @@ -18,3 +18,9 @@ type Comment struct { Content string Reactions []*Reaction } + +// GetExternalName ExternalUserMigrated interface +func (c *Comment) GetExternalName() string { return c.PosterName } + +// ExternalID ExternalUserMigrated interface +func (c *Comment) GetExternalID() int64 { return c.PosterID } diff --git a/modules/migration/issue.go b/modules/migration/issue.go index 19781ad9840a1..984f07d8c9eed 100644 --- a/modules/migration/issue.go +++ b/modules/migration/issue.go @@ -46,3 +46,9 @@ type Issue struct { Assignees []string `json:"assignees"` Context IssueContext `yaml:"-"` } + +// GetExternalName ExternalUserMigrated interface +func (i *Issue) GetExternalName() string { return i.PosterName } + +// GetExternalID ExternalUserMigrated interface +func (i *Issue) GetExternalID() int64 { return i.PosterID } diff --git a/modules/migration/reaction.go b/modules/migration/reaction.go index 2ba44a15a24ee..0946bdd40b35f 100644 --- a/modules/migration/reaction.go +++ b/modules/migration/reaction.go @@ -10,3 +10,9 @@ type Reaction struct { UserName string `yaml:"user_name" json:"user_name"` Content string `json:"content"` } + +// GetExternalName ExternalUserMigrated interface +func (r *Reaction) GetExternalName() string { return r.UserName } + +// GetExternalID ExternalUserMigrated interface +func (r *Reaction) GetExternalID() int64 { return r.UserID } diff --git a/modules/migration/release.go b/modules/migration/release.go index a83f5502cb951..cbdf01a3ed1d6 100644 --- a/modules/migration/release.go +++ b/modules/migration/release.go @@ -38,3 +38,9 @@ type Release struct { Created time.Time Published time.Time } + +// GetExternalName ExternalUserMigrated interface +func (r *Release) GetExternalName() string { return r.PublisherName } + +// GetExternalID ExternalUserMigrated interface +func (r *Release) GetExternalID() int64 { return r.PublisherID } diff --git a/modules/migration/review.go b/modules/migration/review.go index d6d15002af36c..85795385e97e4 100644 --- a/modules/migration/review.go +++ b/modules/migration/review.go @@ -28,6 +28,12 @@ type Review struct { Comments []*ReviewComment } +// GetExternalName ExternalUserMigrated interface +func (r *Review) GetExternalName() string { return r.ReviewerName } + +// ExternalID ExternalUserMigrated interface +func (r *Review) GetExternalID() int64 { return r.ReviewerID } + // ReviewComment represents a review comment type ReviewComment struct { ID int64 diff --git a/services/migrations/gitea_uploader.go b/services/migrations/gitea_uploader.go index 6e823031edb4c..1606a82c5033e 100644 --- a/services/migrations/gitea_uploader.go +++ b/services/migrations/gitea_uploader.go @@ -254,25 +254,8 @@ func (g *GiteaLocalUploader) CreateReleases(releases ...*base.Release) error { CreatedUnix: timeutil.TimeStamp(release.Created.Unix()), } - userid, ok := g.userMap[release.PublisherID] - tp := g.gitServiceType.Name() - if !ok && tp != "" { - var err error - userid, err = user_model.GetUserIDByExternalUserID(tp, fmt.Sprintf("%v", release.PublisherID)) - if err != nil { - log.Error("GetUserIDByExternalUserID: %v", err) - } - if userid > 0 { - g.userMap[release.PublisherID] = userid - } - } - - if userid > 0 { - rel.PublisherID = userid - } else { - rel.PublisherID = g.doer.ID - rel.OriginalAuthor = release.PublisherName - rel.OriginalAuthorID = release.PublisherID + if err := g.remapExternalUser(release, &rel); err != nil { + return err } // calc NumCommits if no draft @@ -394,25 +377,8 @@ func (g *GiteaLocalUploader) CreateIssues(issues ...*base.Issue) error { UpdatedUnix: timeutil.TimeStamp(issue.Updated.Unix()), } - userid, ok := g.userMap[issue.PosterID] - tp := g.gitServiceType.Name() - if !ok && tp != "" { - var err error - userid, err = user_model.GetUserIDByExternalUserID(tp, fmt.Sprintf("%v", issue.PosterID)) - if err != nil { - log.Error("GetUserIDByExternalUserID: %v", err) - } - if userid > 0 { - g.userMap[issue.PosterID] = userid - } - } - - if userid > 0 { - is.PosterID = userid - } else { - is.PosterID = g.doer.ID - is.OriginalAuthor = issue.PosterName - is.OriginalAuthorID = issue.PosterID + if err := g.remapExternalUser(issue, &is); err != nil { + return err } if issue.Closed != nil { @@ -420,27 +386,12 @@ func (g *GiteaLocalUploader) CreateIssues(issues ...*base.Issue) error { } // add reactions for _, reaction := range issue.Reactions { - userid, ok := g.userMap[reaction.UserID] - if !ok && tp != "" { - var err error - userid, err = user_model.GetUserIDByExternalUserID(tp, fmt.Sprintf("%v", reaction.UserID)) - if err != nil { - log.Error("GetUserIDByExternalUserID: %v", err) - } - if userid > 0 { - g.userMap[reaction.UserID] = userid - } - } res := models.Reaction{ Type: reaction.Content, CreatedUnix: timeutil.TimeStampNow(), } - if userid > 0 { - res.UserID = userid - } else { - res.UserID = g.doer.ID - res.OriginalAuthorID = reaction.UserID - res.OriginalAuthor = reaction.UserName + if err := g.remapExternalUser(reaction, &res); err != nil { + return err } is.Reactions = append(is.Reactions, &res) } @@ -477,19 +428,6 @@ func (g *GiteaLocalUploader) CreateComments(comments ...*base.Comment) error { issue = issueInter.(*models.Issue) } - userid, ok := g.userMap[comment.PosterID] - tp := g.gitServiceType.Name() - if !ok && tp != "" { - var err error - userid, err = user_model.GetUserIDByExternalUserID(tp, fmt.Sprintf("%v", comment.PosterID)) - if err != nil { - log.Error("GetUserIDByExternalUserID: %v", err) - } - if userid > 0 { - g.userMap[comment.PosterID] = userid - } - } - if comment.Created.IsZero() { comment.Created = time.Unix(int64(issue.CreatedUnix), 0) } @@ -505,37 +443,18 @@ func (g *GiteaLocalUploader) CreateComments(comments ...*base.Comment) error { UpdatedUnix: timeutil.TimeStamp(comment.Updated.Unix()), } - if userid > 0 { - cm.PosterID = userid - } else { - cm.PosterID = g.doer.ID - cm.OriginalAuthor = comment.PosterName - cm.OriginalAuthorID = comment.PosterID + if err := g.remapExternalUser(comment, &cm); err != nil { + return err } // add reactions for _, reaction := range comment.Reactions { - userid, ok := g.userMap[reaction.UserID] - if !ok && tp != "" { - var err error - userid, err = user_model.GetUserIDByExternalUserID(tp, fmt.Sprintf("%v", reaction.UserID)) - if err != nil { - log.Error("GetUserIDByExternalUserID: %v", err) - } - if userid > 0 { - g.userMap[reaction.UserID] = userid - } - } res := models.Reaction{ Type: reaction.Content, CreatedUnix: timeutil.TimeStampNow(), } - if userid > 0 { - res.UserID = userid - } else { - res.UserID = g.doer.ID - res.OriginalAuthorID = reaction.UserID - res.OriginalAuthor = reaction.UserName + if err := g.remapExternalUser(reaction, &res); err != nil { + return err } cm.Reactions = append(cm.Reactions, &res) } @@ -843,19 +762,6 @@ func (g *GiteaLocalUploader) CreateReviews(reviews ...*base.Review) error { issue = issueInter.(*models.Issue) } - userid, ok := g.userMap[review.ReviewerID] - tp := g.gitServiceType.Name() - if !ok && tp != "" { - var err error - userid, err = user_model.GetUserIDByExternalUserID(tp, fmt.Sprintf("%v", review.ReviewerID)) - if err != nil { - log.Error("GetUserIDByExternalUserID: %v", err) - } - if userid > 0 { - g.userMap[review.ReviewerID] = userid - } - } - if review.CreatedAt.IsZero() { review.CreatedAt = time.Unix(int64(issue.CreatedUnix), 0) } @@ -869,12 +775,8 @@ func (g *GiteaLocalUploader) CreateReviews(reviews ...*base.Review) error { UpdatedUnix: timeutil.TimeStamp(review.CreatedAt.Unix()), } - if userid > 0 { - cm.ReviewerID = userid - } else { - cm.ReviewerID = g.doer.ID - cm.OriginalAuthor = review.ReviewerName - cm.OriginalAuthorID = review.ReviewerID + if err := g.remapExternalUser(review, &cm); err != nil { + return err } // get pr @@ -926,7 +828,6 @@ func (g *GiteaLocalUploader) CreateReviews(reviews ...*base.Review) error { c := models.Comment{ Type: models.CommentTypeCode, - PosterID: comment.PosterID, IssueID: issue.ID, Content: comment.Content, Line: int64(line + comment.Position - 1), @@ -937,12 +838,8 @@ func (g *GiteaLocalUploader) CreateReviews(reviews ...*base.Review) error { UpdatedUnix: timeutil.TimeStamp(comment.UpdatedAt.Unix()), } - if userid > 0 { - c.PosterID = userid - } else { - c.PosterID = g.doer.ID - c.OriginalAuthor = review.ReviewerName - c.OriginalAuthorID = review.ReviewerID + if err := g.remapExternalUser(review, &c); err != nil { + return err } cm.Comments = append(cm.Comments, &c) @@ -983,3 +880,25 @@ func (g *GiteaLocalUploader) Finish() error { g.repo.Status = repo_model.RepositoryReady return repo_model.UpdateRepositoryCols(g.repo, "status") } + +func (g *GiteaLocalUploader) remapExternalUser(source user_model.ExternalUserMigrated, target user_model.ExternalUserRemappable) (err error) { + userid, ok := g.userMap[source.GetExternalID()] + tp := g.gitServiceType.Name() + if !ok && tp != "" { + var err error + userid, err = user_model.GetUserIDByExternalUserID(tp, fmt.Sprintf("%v", source.GetExternalID())) + if err != nil { + log.Error("GetUserIDByExternalUserID: %v", err) + } + if userid > 0 { + g.userMap[source.GetExternalID()] = userid + } + } + + if userid > 0 { + err = target.RemapExternalUser("", 0, userid) + } else { + err = target.RemapExternalUser(source.GetExternalName(), source.GetExternalID(), g.doer.ID) + } + return +} diff --git a/services/migrations/gitea_uploader_test.go b/services/migrations/gitea_uploader_test.go index 7d4f77eac8734..91adb105d98bf 100644 --- a/services/migrations/gitea_uploader_test.go +++ b/services/migrations/gitea_uploader_test.go @@ -7,6 +7,7 @@ package migrations import ( "context" + "strconv" "testing" "time" @@ -115,3 +116,52 @@ func TestGiteaUploadRepo(t *testing.T) { assert.NoError(t, pulls[0].Issue.LoadDiscussComments()) assert.Len(t, pulls[0].Issue.Comments, 2) } + +func TestGiteaUploadRemapExternalUser(t *testing.T) { + var err error + + unittest.PrepareTestEnv(t) + doer := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1}).(*user_model.User) + + repoName := "migrated" + uploader := NewGiteaLocalUploader(context.Background(), doer, doer.Name, repoName) + uploader.gitServiceType = structs.GiteaService + + externalID := int64(1234567) + externalName := "url.or.something" + source := base.Release{ + PublisherID: externalID, + PublisherName: externalName, + } + + // + // When there is no user linked to the external ID, the migrated data is authored + // by the doer + // + target := models.Release{} + err = uploader.remapExternalUser(&source, &target) + assert.NoError(t, err) + assert.EqualValues(t, doer.ID, target.GetUserID()) + + // + // Link the external ID to an existing user + // + linkedUser := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}).(*user_model.User) + externalLoginUser := &user_model.ExternalLoginUser{ + ExternalID: strconv.FormatInt(externalID, 10), + UserID: linkedUser.ID, + LoginSourceID: 0, + Provider: structs.GiteaService.Name(), + } + err = user_model.LinkExternalToUser(linkedUser, externalLoginUser) + assert.NoError(t, err) + + // + // When a user is linked to the external ID, it becomes the author of + // the migrated data + // + target = models.Release{} + err = uploader.remapExternalUser(&source, &target) + assert.NoError(t, err) + assert.EqualValues(t, target.GetUserID(), linkedUser.ID) +} From 180df9791aa50b4572c21b2196f42f09558fe77d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Dachary?= Date: Mon, 31 Jan 2022 23:50:13 +0100 Subject: [PATCH 2/3] fix variable scope and int64 formatting --- services/migrations/gitea_uploader.go | 3 +-- services/migrations/gitea_uploader_test.go | 4 +--- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/services/migrations/gitea_uploader.go b/services/migrations/gitea_uploader.go index 1606a82c5033e..fa2fb9f916e77 100644 --- a/services/migrations/gitea_uploader.go +++ b/services/migrations/gitea_uploader.go @@ -481,7 +481,7 @@ func (g *GiteaLocalUploader) CreatePullRequests(prs ...*base.PullRequest) error tp := g.gitServiceType.Name() if !ok && tp != "" { var err error - userid, err = user_model.GetUserIDByExternalUserID(tp, fmt.Sprintf("%v", pr.PosterID)) + userid, err = user_model.GetUserIDByExternalUserID(tp, fmt.Sprintf("%d", pr.PosterID)) if err != nil { log.Error("GetUserIDByExternalUserID: %v", err) } @@ -885,7 +885,6 @@ func (g *GiteaLocalUploader) remapExternalUser(source user_model.ExternalUserMig userid, ok := g.userMap[source.GetExternalID()] tp := g.gitServiceType.Name() if !ok && tp != "" { - var err error userid, err = user_model.GetUserIDByExternalUserID(tp, fmt.Sprintf("%v", source.GetExternalID())) if err != nil { log.Error("GetUserIDByExternalUserID: %v", err) diff --git a/services/migrations/gitea_uploader_test.go b/services/migrations/gitea_uploader_test.go index 91adb105d98bf..7552245d74463 100644 --- a/services/migrations/gitea_uploader_test.go +++ b/services/migrations/gitea_uploader_test.go @@ -118,8 +118,6 @@ func TestGiteaUploadRepo(t *testing.T) { } func TestGiteaUploadRemapExternalUser(t *testing.T) { - var err error - unittest.PrepareTestEnv(t) doer := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1}).(*user_model.User) @@ -139,7 +137,7 @@ func TestGiteaUploadRemapExternalUser(t *testing.T) { // by the doer // target := models.Release{} - err = uploader.remapExternalUser(&source, &target) + err := uploader.remapExternalUser(&source, &target) assert.NoError(t, err) assert.EqualValues(t, doer.ID, target.GetUserID()) From 45c367d5ce9a19f1723e19854dc426e345804f7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Dachary?= Date: Tue, 1 Feb 2022 00:17:25 +0100 Subject: [PATCH 3/3] add missing calls to remapExternalUser and fix misplaced %d --- modules/migration/pullrequest.go | 6 +++ services/migrations/gitea_uploader.go | 64 +++------------------------ 2 files changed, 13 insertions(+), 57 deletions(-) diff --git a/modules/migration/pullrequest.go b/modules/migration/pullrequest.go index bbf1fe76530c4..7a681940a7eeb 100644 --- a/modules/migration/pullrequest.go +++ b/modules/migration/pullrequest.go @@ -61,3 +61,9 @@ type PullRequestBranch struct { func (p PullRequestBranch) RepoPath() string { return fmt.Sprintf("%s/%s", p.OwnerName, p.RepoName) } + +// GetExternalName ExternalUserMigrated interface +func (p *PullRequest) GetExternalName() string { return p.PosterName } + +// ExternalID ExternalUserMigrated interface +func (p *PullRequest) GetExternalID() int64 { return p.PosterID } diff --git a/services/migrations/gitea_uploader.go b/services/migrations/gitea_uploader.go index fa2fb9f916e77..9edb6258d82d6 100644 --- a/services/migrations/gitea_uploader.go +++ b/services/migrations/gitea_uploader.go @@ -477,25 +477,8 @@ func (g *GiteaLocalUploader) CreatePullRequests(prs ...*base.PullRequest) error return err } - userid, ok := g.userMap[pr.PosterID] - tp := g.gitServiceType.Name() - if !ok && tp != "" { - var err error - userid, err = user_model.GetUserIDByExternalUserID(tp, fmt.Sprintf("%d", pr.PosterID)) - if err != nil { - log.Error("GetUserIDByExternalUserID: %v", err) - } - if userid > 0 { - g.userMap[pr.PosterID] = userid - } - } - - if userid > 0 { - gpr.Issue.PosterID = userid - } else { - gpr.Issue.PosterID = g.doer.ID - gpr.Issue.OriginalAuthor = pr.PosterName - gpr.Issue.OriginalAuthorID = pr.PosterID + if err := g.remapExternalUser(pr, gpr.Issue); err != nil { + return err } gprs = append(gprs, gpr) @@ -655,51 +638,18 @@ func (g *GiteaLocalUploader) newPullRequest(pr *base.PullRequest) (*models.PullR UpdatedUnix: timeutil.TimeStamp(pr.Updated.Unix()), } - tp := g.gitServiceType.Name() - - userid, ok := g.userMap[pr.PosterID] - if !ok && tp != "" { - var err error - userid, err = user_model.GetUserIDByExternalUserID(tp, fmt.Sprintf("%v", pr.PosterID)) - if err != nil { - log.Error("GetUserIDByExternalUserID: %v", err) - } - if userid > 0 { - g.userMap[pr.PosterID] = userid - } - } - - if userid > 0 { - issue.PosterID = userid - } else { - issue.PosterID = g.doer.ID - issue.OriginalAuthor = pr.PosterName - issue.OriginalAuthorID = pr.PosterID + if err := g.remapExternalUser(pr, &issue); err != nil { + return nil, err } // add reactions for _, reaction := range pr.Reactions { - userid, ok := g.userMap[reaction.UserID] - if !ok && tp != "" { - var err error - userid, err = user_model.GetUserIDByExternalUserID(tp, fmt.Sprintf("%v", reaction.UserID)) - if err != nil { - log.Error("GetUserIDByExternalUserID: %v", err) - } - if userid > 0 { - g.userMap[reaction.UserID] = userid - } - } res := models.Reaction{ Type: reaction.Content, CreatedUnix: timeutil.TimeStampNow(), } - if userid > 0 { - res.UserID = userid - } else { - res.UserID = g.doer.ID - res.OriginalAuthorID = reaction.UserID - res.OriginalAuthor = reaction.UserName + if err := g.remapExternalUser(reaction, &res); err != nil { + return nil, err } issue.Reactions = append(issue.Reactions, &res) } @@ -885,7 +835,7 @@ func (g *GiteaLocalUploader) remapExternalUser(source user_model.ExternalUserMig userid, ok := g.userMap[source.GetExternalID()] tp := g.gitServiceType.Name() if !ok && tp != "" { - userid, err = user_model.GetUserIDByExternalUserID(tp, fmt.Sprintf("%v", source.GetExternalID())) + userid, err = user_model.GetUserIDByExternalUserID(tp, fmt.Sprintf("%d", source.GetExternalID())) if err != nil { log.Error("GetUserIDByExternalUserID: %v", err) }