Add support for migrating GitLab comment reactions

This commit is contained in:
Sebastian Brückner 2023-11-06 21:20:54 -08:00
parent 17f170ee37
commit da75245a46
2 changed files with 46 additions and 36 deletions

View File

@ -434,21 +434,11 @@ func (g *GitlabDownloader) GetIssues(page, perPage int) ([]*base.Issue, bool, er
milestone = issue.Milestone.Title
}
var reactions []*gitlab.AwardEmoji
awardPage := 1
for {
awards, _, err := g.client.AwardEmoji.ListIssueAwardEmoji(g.repoID, issue.IID, &gitlab.ListAwardEmojiOptions{Page: awardPage, PerPage: perPage}, gitlab.WithContext(g.ctx))
if err != nil {
return nil, false, fmt.Errorf("error while listing issue awards: %w", err)
}
reactions = append(reactions, awards...)
if len(awards) < perPage {
break
}
awardPage++
reactions, err := g.loadAwards(func(awardPage int) ([]*gitlab.AwardEmoji, *gitlab.Response, error) {
return g.client.AwardEmoji.ListIssueAwardEmoji(g.repoID, issue.IID, &gitlab.ListAwardEmojiOptions{Page: awardPage, PerPage: perPage}, gitlab.WithContext(g.ctx))
})
if err != nil {
return nil, false, err
}
allIssues = append(allIssues, &base.Issue{
@ -461,7 +451,7 @@ func (g *GitlabDownloader) GetIssues(page, perPage int) ([]*base.Issue, bool, er
State: issue.State,
Created: *issue.CreatedAt,
Labels: labels,
Reactions: g.awardsToReactions(reactions),
Reactions: reactions,
Closed: issue.ClosedAt,
IsLocked: issue.DiscussionLocked,
Updated: *issue.UpdatedAt,
@ -477,7 +467,6 @@ func (g *GitlabDownloader) GetIssues(page, perPage int) ([]*base.Issue, bool, er
}
// GetComments returns comments according issueNumber
// TODO: figure out how to transfer comment reactions
func (g *GitlabDownloader) GetComments(commentable base.Commentable) ([]*base.Comment, bool, error) {
context, ok := commentable.GetContext().(gitlabIssueContext)
if !ok {
@ -509,7 +498,18 @@ func (g *GitlabDownloader) GetComments(commentable base.Commentable) ([]*base.Co
}
for _, comment := range comments {
for _, note := range comment.Notes {
allComments = append(allComments, g.convertNoteToComment(commentable.GetLocalIndex(), note))
reactions, err := g.loadAwards(func(awardPage int) ([]*gitlab.AwardEmoji, *gitlab.Response, error) {
if context.IsMergeRequest {
return g.client.AwardEmoji.ListMergeRequestAwardEmojiOnNote(g.repoID, note.NoteableIID, note.ID, &gitlab.ListAwardEmojiOptions{Page: awardPage, PerPage: g.maxPerPage}, gitlab.WithContext(g.ctx))
} else {
return g.client.AwardEmoji.ListIssuesAwardEmojiOnNote(g.repoID, note.NoteableIID, note.ID, &gitlab.ListAwardEmojiOptions{Page: awardPage, PerPage: g.maxPerPage}, gitlab.WithContext(g.ctx))
}
})
if err != nil {
return nil, false, err
}
allComments = append(allComments, g.convertNoteToComment(commentable.GetLocalIndex(), note, reactions))
}
}
if resp.NextPage == 0 {
@ -576,7 +576,7 @@ func (g *GitlabDownloader) GetComments(commentable base.Commentable) ([]*base.Co
var targetBranchChangeRegexp = regexp.MustCompile("^changed target branch from `(.*?)` to `(.*?)`$")
func (g *GitlabDownloader) convertNoteToComment(localIndex int64, note *gitlab.Note) *base.Comment {
func (g *GitlabDownloader) convertNoteToComment(localIndex int64, note *gitlab.Note, reactions []*base.Reaction) *base.Comment {
comment := &base.Comment{
IssueIndex: localIndex,
Index: int64(note.ID),
@ -586,6 +586,7 @@ func (g *GitlabDownloader) convertNoteToComment(localIndex int64, note *gitlab.N
Content: note.Body,
Created: *note.CreatedAt,
Meta: map[string]any{},
Reactions: reactions,
}
// Try to find the underlying event of system notes.
@ -671,21 +672,11 @@ func (g *GitlabDownloader) GetPullRequests(page, perPage int) ([]*base.PullReque
milestone = pr.Milestone.Title
}
var reactions []*gitlab.AwardEmoji
awardPage := 1
for {
awards, _, err := g.client.AwardEmoji.ListMergeRequestAwardEmoji(g.repoID, pr.IID, &gitlab.ListAwardEmojiOptions{Page: awardPage, PerPage: perPage}, gitlab.WithContext(g.ctx))
if err != nil {
return nil, false, fmt.Errorf("error while listing merge requests awards: %w", err)
}
reactions = append(reactions, awards...)
if len(awards) < perPage {
break
}
awardPage++
reactions, err := g.loadAwards(func(awardPage int) ([]*gitlab.AwardEmoji, *gitlab.Response, error) {
return g.client.AwardEmoji.ListMergeRequestAwardEmoji(g.repoID, pr.IID, &gitlab.ListAwardEmojiOptions{Page: awardPage, PerPage: perPage}, gitlab.WithContext(g.ctx))
})
if err != nil {
return nil, false, err
}
// Generate new PR Numbers by the known Issue Numbers, because they share the same number space in Gitea, but they are independent in Gitlab
@ -706,7 +697,7 @@ func (g *GitlabDownloader) GetPullRequests(page, perPage int) ([]*base.PullReque
MergeCommitSHA: mergeCommitSHA,
MergedTime: mergeTime,
IsLocked: locked,
Reactions: g.awardsToReactions(reactions),
Reactions: reactions,
Head: base.PullRequestBranch{
Ref: pr.SourceBranch,
SHA: pr.SHA,
@ -767,6 +758,25 @@ func (g *GitlabDownloader) GetReviews(reviewable base.Reviewable) ([]*base.Revie
return reviews, nil
}
func (g *GitlabDownloader) loadAwards(load func(page int) ([]*gitlab.AwardEmoji, *gitlab.Response, error)) ([]*base.Reaction, error) {
var allAwards []*gitlab.AwardEmoji
page := 1
for {
awards, resp, err := load(page)
if err != nil {
return nil, fmt.Errorf("error while listing awards: %w", err)
}
allAwards = append(allAwards, awards...)
if resp.NextPage == 0 {
break
}
page = resp.NextPage
}
return g.awardsToReactions(allAwards), nil
}
func (g *GitlabDownloader) awardsToReactions(awards []*gitlab.AwardEmoji) []*base.Reaction {
result := make([]*base.Reaction, 0, len(awards))
uniqCheck := make(container.Set[string])

View File

@ -594,7 +594,7 @@ func TestNoteToComment(t *testing.T) {
}}
for i, note := range notes {
actualComment := *downloader.convertNoteToComment(17, &note)
actualComment := *downloader.convertNoteToComment(17, &note, nil)
assert.EqualValues(t, actualComment, comments[i])
}
}