diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2020-01-24 01:28:15 +0800 |
---|---|---|
committer | Lauris BH <lauris@nix.lv> | 2020-01-23 19:28:15 +0200 |
commit | f6067a8465e7762aea1561106cfee291409a0fd6 (patch) | |
tree | 35c97d4117acfe52dce32c9242e4c9a656c4bc6c /modules/migrations/github.go | |
parent | bfdfa9a8b32ae331f98137169693ba1d71c25b09 (diff) | |
download | gitea-f6067a8465e7762aea1561106cfee291409a0fd6.tar.gz gitea-f6067a8465e7762aea1561106cfee291409a0fd6.zip |
Migrate reviews when migrating repository from github (#9463)
* fix typo
* Migrate reviews when migrating repository from github
* fix lint
* Added test and migration when external user login
* fix test
* fix commented state
* Some improvements
* fix bug when get pull request and ref original author on code comments
* Fix migrated line; Added comment for review
* Don't load all pull requests attributes
* Fix typo
* wrong change copy head
* fix tests
* fix reactions
* Fix test
* fix fmt
* fix review comment reactions
Diffstat (limited to 'modules/migrations/github.go')
-rw-r--r-- | modules/migrations/github.go | 114 |
1 files changed, 111 insertions, 3 deletions
diff --git a/modules/migrations/github.go b/modules/migrations/github.go index a99b05e11c..488cd82f5b 100644 --- a/modules/migrations/github.go +++ b/modules/migrations/github.go @@ -418,10 +418,14 @@ func (g *GithubDownloaderV3) GetIssues(page, perPage int) ([]*base.Issue, bool, // GetComments returns comments according issueNumber func (g *GithubDownloaderV3) GetComments(issueNumber int64) ([]*base.Comment, error) { - var allComments = make([]*base.Comment, 0, 100) + var ( + allComments = make([]*base.Comment, 0, 100) + created = "created" + asc = "asc" + ) opt := &github.IssueListCommentsOptions{ - Sort: "created", - Direction: "asc", + Sort: created, + Direction: asc, ListOptions: github.ListOptions{ PerPage: 100, }, @@ -614,3 +618,107 @@ func (g *GithubDownloaderV3) GetPullRequests(page, perPage int) ([]*base.PullReq return allPRs, nil } + +func convertGithubReview(r *github.PullRequestReview) *base.Review { + return &base.Review{ + ID: r.GetID(), + ReviewerID: r.GetUser().GetID(), + ReviewerName: r.GetUser().GetLogin(), + CommitID: r.GetCommitID(), + Content: r.GetBody(), + CreatedAt: r.GetSubmittedAt(), + State: r.GetState(), + } +} + +func (g *GithubDownloaderV3) convertGithubReviewComments(cs []*github.PullRequestComment) ([]*base.ReviewComment, error) { + var rcs = make([]*base.ReviewComment, 0, len(cs)) + for _, c := range cs { + // get reactions + var reactions []*base.Reaction + for i := 1; ; i++ { + g.sleep() + res, resp, err := g.client.Reactions.ListPullRequestCommentReactions(g.ctx, g.repoOwner, g.repoName, c.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(), + }) + } + } + + rcs = append(rcs, &base.ReviewComment{ + ID: c.GetID(), + InReplyTo: c.GetInReplyTo(), + Content: c.GetBody(), + TreePath: c.GetPath(), + DiffHunk: c.GetDiffHunk(), + Position: c.GetPosition(), + CommitID: c.GetCommitID(), + PosterID: c.GetUser().GetID(), + Reactions: reactions, + CreatedAt: c.GetCreatedAt(), + UpdatedAt: c.GetUpdatedAt(), + }) + } + return rcs, nil +} + +// GetReviews returns pull requests review +func (g *GithubDownloaderV3) GetReviews(pullRequestNumber int64) ([]*base.Review, error) { + var allReviews = make([]*base.Review, 0, 100) + opt := &github.ListOptions{ + PerPage: 100, + } + for { + g.sleep() + reviews, resp, err := g.client.PullRequests.ListReviews(g.ctx, g.repoOwner, g.repoName, int(pullRequestNumber), opt) + if err != nil { + return nil, fmt.Errorf("error while listing repos: %v", err) + } + g.rate = &resp.Rate + for _, review := range reviews { + r := convertGithubReview(review) + r.IssueIndex = pullRequestNumber + // retrieve all review comments + opt2 := &github.ListOptions{ + PerPage: 100, + } + for { + g.sleep() + reviewComments, resp, err := g.client.PullRequests.ListReviewComments(g.ctx, g.repoOwner, g.repoName, int(pullRequestNumber), review.GetID(), opt2) + if err != nil { + return nil, fmt.Errorf("error while listing repos: %v", err) + } + g.rate = &resp.Rate + + cs, err := g.convertGithubReviewComments(reviewComments) + if err != nil { + return nil, err + } + r.Comments = append(r.Comments, cs...) + if resp.NextPage == 0 { + break + } + opt2.Page = resp.NextPage + } + allReviews = append(allReviews, r) + } + if resp.NextPage == 0 { + break + } + opt.Page = resp.NextPage + } + return allReviews, nil +} |