summaryrefslogtreecommitdiffstats
path: root/modules/migrations
diff options
context:
space:
mode:
authorLunny Xiao <xiaolunwen@gmail.com>2020-01-15 19:14:07 +0800
committerAntoine GIRARD <sapk@users.noreply.github.com>2020-01-15 12:14:07 +0100
commit2b3e931cde3e3d70b69202164f35fc6f2c609ade (patch)
treedd519a5741642dde99433028d0e7d0e9da7b67c9 /modules/migrations
parent4e566df1c694e47908e59a6db163e5a98d144b83 (diff)
downloadgitea-2b3e931cde3e3d70b69202164f35fc6f2c609ade.tar.gz
gitea-2b3e931cde3e3d70b69202164f35fc6f2c609ade.zip
Migrate reactions when migrating repository from github (#9599)
* Migrate reactions when migrating repository from github * fix missed sleep * fix tests * update reactions when external user binding * Fix test * fix tests * change the copy head * fix test * fix migrator add/delete reaction
Diffstat (limited to 'modules/migrations')
-rw-r--r--modules/migrations/base/comment.go2
-rw-r--r--modules/migrations/base/issue.go2
-rw-r--r--modules/migrations/base/pullrequest.go1
-rw-r--r--modules/migrations/base/reaction.go17
-rw-r--r--modules/migrations/gitea.go90
-rw-r--r--modules/migrations/github.go96
-rw-r--r--modules/migrations/github_test.go89
7 files changed, 223 insertions, 74 deletions
diff --git a/modules/migrations/base/comment.go b/modules/migrations/base/comment.go
index 94cbabaae6..4a653e474b 100644
--- a/modules/migrations/base/comment.go
+++ b/modules/migrations/base/comment.go
@@ -16,5 +16,5 @@ type Comment struct {
Created time.Time
Updated time.Time
Content string
- Reactions *Reactions
+ Reactions []*Reaction
}
diff --git a/modules/migrations/base/issue.go b/modules/migrations/base/issue.go
index b87a7fec62..4e2bf25f17 100644
--- a/modules/migrations/base/issue.go
+++ b/modules/migrations/base/issue.go
@@ -22,5 +22,5 @@ type Issue struct {
Updated time.Time
Closed *time.Time
Labels []*Label
- Reactions *Reactions
+ Reactions []*Reaction
}
diff --git a/modules/migrations/base/pullrequest.go b/modules/migrations/base/pullrequest.go
index b1602b8218..3a1e0f25bd 100644
--- a/modules/migrations/base/pullrequest.go
+++ b/modules/migrations/base/pullrequest.go
@@ -33,6 +33,7 @@ type PullRequest struct {
Assignee string
Assignees []string
IsLocked bool
+ Reactions []*Reaction
}
// IsForkPullRequest returns true if the pull request from a forked repository but not the same repository
diff --git a/modules/migrations/base/reaction.go b/modules/migrations/base/reaction.go
index fd7a9543d3..b79223d4cd 100644
--- a/modules/migrations/base/reaction.go
+++ b/modules/migrations/base/reaction.go
@@ -1,17 +1,12 @@
-// Copyright 2019 The Gitea Authors. All rights reserved.
-// Copyright 2018 Jonas Franz. All rights reserved.
+// Copyright 2020 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package base
-// Reactions represents a summary of reactions.
-type Reactions struct {
- TotalCount int
- PlusOne int
- MinusOne int
- Laugh int
- Confused int
- Heart int
- Hooray int
+// Reaction represents a reaction to an issue/pr/comment.
+type Reaction struct {
+ UserID int64
+ UserName string
+ Content string
}
diff --git a/modules/migrations/gitea.go b/modules/migrations/gitea.go
index 0cffd60e84..82664d0d1a 100644
--- a/modules/migrations/gitea.go
+++ b/modules/migrations/gitea.go
@@ -361,7 +361,32 @@ func (g *GiteaLocalUploader) CreateIssues(issues ...*base.Issue) error {
if issue.Closed != nil {
is.ClosedUnix = timeutil.TimeStamp(issue.Closed.Unix())
}
- // TODO: add reactions
+ // add reactions
+ for _, reaction := range issue.Reactions {
+ userid, ok := g.userMap[reaction.UserID]
+ if !ok && tp != "" {
+ var err error
+ userid, err = models.GetUserIDByExternalUserID(tp, fmt.Sprintf("%v", reaction.UserID))
+ if err != nil {
+ log.Error("GetUserIDByExternalUserID: %v", err)
+ }
+ if userid > 0 {
+ g.userMap[reaction.UserID] = userid
+ }
+ }
+ var 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
+ }
+ is.Reactions = append(is.Reactions, &res)
+ }
iss = append(iss, &is)
}
@@ -420,9 +445,34 @@ func (g *GiteaLocalUploader) CreateComments(comments ...*base.Comment) error {
cm.OriginalAuthorID = comment.PosterID
}
- cms = append(cms, &cm)
+ // add reactions
+ for _, reaction := range comment.Reactions {
+ userid, ok := g.userMap[reaction.UserID]
+ if !ok && tp != "" {
+ var err error
+ userid, err = models.GetUserIDByExternalUserID(tp, fmt.Sprintf("%v", reaction.UserID))
+ if err != nil {
+ log.Error("GetUserIDByExternalUserID: %v", err)
+ }
+ if userid > 0 {
+ g.userMap[reaction.UserID] = userid
+ }
+ }
+ var 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
+ }
+ cm.Reactions = append(cm.Reactions, &res)
+ }
- // TODO: Reactions
+ cms = append(cms, &cm)
}
return models.InsertIssueComments(cms)
@@ -581,10 +631,12 @@ 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 {
+ if !ok && tp != "" {
var err error
- userid, err = models.GetUserIDByExternalUserID("github", fmt.Sprintf("%v", pr.PosterID))
+ userid, err = models.GetUserIDByExternalUserID(tp, fmt.Sprintf("%v", pr.PosterID))
if err != nil {
log.Error("GetUserIDByExternalUserID: %v", err)
}
@@ -601,6 +653,33 @@ func (g *GiteaLocalUploader) newPullRequest(pr *base.PullRequest) (*models.PullR
issue.OriginalAuthorID = pr.PosterID
}
+ // add reactions
+ for _, reaction := range pr.Reactions {
+ userid, ok := g.userMap[reaction.UserID]
+ if !ok && tp != "" {
+ var err error
+ userid, err = models.GetUserIDByExternalUserID(tp, fmt.Sprintf("%v", reaction.UserID))
+ if err != nil {
+ log.Error("GetUserIDByExternalUserID: %v", err)
+ }
+ if userid > 0 {
+ g.userMap[reaction.UserID] = userid
+ }
+ }
+ var 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
+ }
+ issue.Reactions = append(issue.Reactions, &res)
+ }
+
var pullRequest = models.PullRequest{
HeadRepoID: g.repo.ID,
HeadBranch: head,
@@ -622,7 +701,6 @@ func (g *GiteaLocalUploader) newPullRequest(pr *base.PullRequest) (*models.PullR
pullRequest.MergerID = g.doer.ID
}
- // TODO: reactions
// TODO: assignees
return &pullRequest, nil
diff --git a/modules/migrations/github.go b/modules/migrations/github.go
index 9183c9318f..17e49d90d1 100644
--- a/modules/migrations/github.go
+++ b/modules/migrations/github.go
@@ -319,18 +319,6 @@ func (g *GithubDownloaderV3) GetReleases() ([]*base.Release, error) {
return releases, nil
}
-func convertGithubReactions(reactions *github.Reactions) *base.Reactions {
- return &base.Reactions{
- TotalCount: *reactions.TotalCount,
- PlusOne: *reactions.PlusOne,
- MinusOne: *reactions.MinusOne,
- Laugh: *reactions.Laugh,
- Confused: *reactions.Confused,
- Heart: *reactions.Heart,
- Hooray: *reactions.Hooray,
- }
-}
-
// GetIssues returns issues according start and limit
func (g *GithubDownloaderV3) GetIssues(page, perPage int) ([]*base.Issue, bool, error) {
opt := &github.IssueListByRepoOptions{
@@ -366,15 +354,36 @@ func (g *GithubDownloaderV3) GetIssues(page, perPage int) ([]*base.Issue, bool,
for _, l := range issue.Labels {
labels = append(labels, convertGithubLabel(&l))
}
- var reactions *base.Reactions
- if issue.Reactions != nil {
- reactions = convertGithubReactions(issue.Reactions)
- }
var email string
if issue.User.Email != nil {
email = *issue.User.Email
}
+
+ // get reactions
+ var reactions []*base.Reaction
+ for i := 1; ; i++ {
+ g.sleep()
+ res, resp, err := g.client.Reactions.ListIssueReactions(g.ctx, g.repoOwner, g.repoName, issue.GetNumber(), &github.ListOptions{
+ Page: i,
+ PerPage: perPage,
+ })
+ if err != nil {
+ return nil, false, err
+ }
+ g.rate = &resp.Rate
+ if len(res) == 0 {
+ break
+ }
+ for _, reaction := range res {
+ reactions = append(reactions, &base.Reaction{
+ UserID: reaction.User.GetID(),
+ UserName: reaction.User.GetLogin(),
+ Content: reaction.GetContent(),
+ })
+ }
+ }
+
allIssues = append(allIssues, &base.Issue{
Title: *issue.Title,
Number: int64(*issue.Number),
@@ -418,9 +427,29 @@ func (g *GithubDownloaderV3) GetComments(issueNumber int64) ([]*base.Comment, er
if comment.User.Email != nil {
email = *comment.User.Email
}
- var reactions *base.Reactions
- if comment.Reactions != nil {
- reactions = convertGithubReactions(comment.Reactions)
+
+ // get reactions
+ var reactions []*base.Reaction
+ for i := 1; ; i++ {
+ g.sleep()
+ res, resp, err := g.client.Reactions.ListIssueCommentReactions(g.ctx, g.repoOwner, g.repoName, comment.GetID(), &github.ListOptions{
+ Page: i,
+ PerPage: 100,
+ })
+ if err != nil {
+ return nil, err
+ }
+ g.rate = &resp.Rate
+ if len(res) == 0 {
+ break
+ }
+ for _, reaction := range res {
+ reactions = append(reactions, &base.Reaction{
+ UserID: reaction.User.GetID(),
+ UserName: reaction.User.GetLogin(),
+ Content: reaction.GetContent(),
+ })
+ }
}
allComments = append(allComments, &base.Comment{
IssueIndex: issueNumber,
@@ -473,8 +502,6 @@ func (g *GithubDownloaderV3) GetPullRequests(page, perPage int) ([]*base.PullReq
labels = append(labels, convertGithubLabel(l))
}
- // FIXME: This API missing reactions, we may need another extra request to get reactions
-
var email string
if pr.User.Email != nil {
email = *pr.User.Email
@@ -515,6 +542,30 @@ func (g *GithubDownloaderV3) GetPullRequests(page, perPage int) ([]*base.PullReq
headUserName = *pr.Head.User.Login
}
+ // get reactions
+ var reactions []*base.Reaction
+ for i := 1; ; i++ {
+ g.sleep()
+ res, resp, err := g.client.Reactions.ListIssueReactions(g.ctx, g.repoOwner, g.repoName, pr.GetNumber(), &github.ListOptions{
+ Page: i,
+ PerPage: perPage,
+ })
+ if err != nil {
+ return nil, err
+ }
+ g.rate = &resp.Rate
+ if len(res) == 0 {
+ break
+ }
+ for _, reaction := range res {
+ reactions = append(reactions, &base.Reaction{
+ UserID: reaction.User.GetID(),
+ UserName: reaction.User.GetLogin(),
+ Content: reaction.GetContent(),
+ })
+ }
+ }
+
allPRs = append(allPRs, &base.PullRequest{
Title: *pr.Title,
Number: int64(*pr.Number),
@@ -545,7 +596,8 @@ func (g *GithubDownloaderV3) GetPullRequests(page, perPage int) ([]*base.PullReq
RepoName: *pr.Base.Repo.Name,
OwnerName: *pr.Base.User.Login,
},
- PatchURL: *pr.PatchURL,
+ PatchURL: *pr.PatchURL,
+ Reactions: reactions,
})
}
diff --git a/modules/migrations/github_test.go b/modules/migrations/github_test.go
index bf71ab4756..10943e4019 100644
--- a/modules/migrations/github_test.go
+++ b/modules/migrations/github_test.go
@@ -170,14 +170,12 @@ func TestGitHubDownloadRepo(t *testing.T) {
Description: "Good for newcomers",
},
},
- Reactions: &base.Reactions{
- TotalCount: 1,
- PlusOne: 1,
- MinusOne: 0,
- Laugh: 0,
- Confused: 0,
- Heart: 0,
- Hooray: 0,
+ Reactions: []*base.Reaction{
+ {
+ UserID: 1669571,
+ UserName: "mrsdizzie",
+ Content: "+1",
+ },
},
Closed: &closed1,
},
@@ -198,14 +196,37 @@ func TestGitHubDownloadRepo(t *testing.T) {
Description: "This issue or pull request already exists",
},
},
- Reactions: &base.Reactions{
- TotalCount: 6,
- PlusOne: 1,
- MinusOne: 1,
- Laugh: 1,
- Confused: 1,
- Heart: 1,
- Hooray: 1,
+ Reactions: []*base.Reaction{
+ {
+ UserID: 1669571,
+ UserName: "mrsdizzie",
+ Content: "heart",
+ },
+ {
+ UserID: 1669571,
+ UserName: "mrsdizzie",
+ Content: "laugh",
+ },
+ {
+ UserID: 1669571,
+ UserName: "mrsdizzie",
+ Content: "-1",
+ },
+ {
+ UserID: 1669571,
+ UserName: "mrsdizzie",
+ Content: "confused",
+ },
+ {
+ UserID: 1669571,
+ UserName: "mrsdizzie",
+ Content: "hooray",
+ },
+ {
+ UserID: 1669571,
+ UserName: "mrsdizzie",
+ Content: "+1",
+ },
},
Closed: &closed2,
},
@@ -223,14 +244,12 @@ func TestGitHubDownloadRepo(t *testing.T) {
Created: time.Date(2019, 11, 12, 21, 0, 13, 0, time.UTC),
Updated: time.Date(2019, 11, 12, 21, 0, 13, 0, time.UTC),
Content: "This is a comment",
- Reactions: &base.Reactions{
- TotalCount: 1,
- PlusOne: 1,
- MinusOne: 0,
- Laugh: 0,
- Confused: 0,
- Heart: 0,
- Hooray: 0,
+ Reactions: []*base.Reaction{
+ {
+ UserID: 1669571,
+ UserName: "mrsdizzie",
+ Content: "+1",
+ },
},
},
{
@@ -240,15 +259,7 @@ func TestGitHubDownloadRepo(t *testing.T) {
Created: time.Date(2019, 11, 12, 22, 7, 14, 0, time.UTC),
Updated: time.Date(2019, 11, 12, 22, 7, 14, 0, time.UTC),
Content: "A second comment",
- Reactions: &base.Reactions{
- TotalCount: 0,
- PlusOne: 0,
- MinusOne: 0,
- Laugh: 0,
- Confused: 0,
- Heart: 0,
- Hooray: 0,
- },
+ Reactions: nil,
},
}, comments[:2])
@@ -331,6 +342,18 @@ func TestGitHubDownloadRepo(t *testing.T) {
},
Merged: false,
MergeCommitSHA: "565d1208f5fffdc1c5ae1a2436491eb9a5e4ebae",
+ Reactions: []*base.Reaction{
+ {
+ UserID: 81045,
+ UserName: "lunny",
+ Content: "heart",
+ },
+ {
+ UserID: 81045,
+ UserName: "lunny",
+ Content: "+1",
+ },
+ },
},
}, prs)
}