forked from forgejo/forgejo
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
This commit is contained in:
parent
bfdfa9a8b3
commit
f6067a8465
18 changed files with 567 additions and 32 deletions
|
@ -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
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue